-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest-bin.js
180 lines (146 loc) · 4.57 KB
/
test-bin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
require('mocha');
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const del = require('delete');
const createRepo = require('./support/git');
const Normalizer = require('..');
let config;
const origCwd = process.cwd();
const project = path.resolve(__dirname, 'fixtures/project-bin');
const remote = `https://github.com/jonschlinkert/${path.basename(project)}.git`;
const gitPath = path.resolve(project, '.git');
describe('normalize (bin)', () => {
beforeEach(() => {
config = new Normalizer({ verbose: false });
});
before(cb => {
process.chdir(project);
createRepo(project, remote, cb);
});
after(cb => {
process.chdir(origCwd);
del(gitPath, cb);
});
describe('main', () => {
it('should remove the property if the file does not exist', () => {
const pkg = { main: 'foo.js' };
const res = config.normalize(pkg);
assert(!res.hasOwnProperty('main'));
});
it('should not remove the property if the file exists', () => {
const pkg = { main: 'main.js' };
const res = config.normalize(pkg);
assert(res.hasOwnProperty('main'));
});
it('should add the main file to the `files` array', () => {
const pkg = { main: 'main.js' };
const res = config.normalize(pkg);
assert.equal(res.files.indexOf('bin'), 0);
assert.equal(res.files.indexOf('cli.js'), 1);
assert.equal(res.files.indexOf('main.js'), 2);
});
it('should not add `main` file to files array when file does not exist', () => {
const pkg = {
files: [],
main: 'index.js'
};
const res = config.normalize(pkg);
assert(res.hasOwnProperty('files'));
assert(res.files.indexOf('index.js') === -1);
});
it('should create files array with main', () => {
const pkg = {
main: 'main.js'
};
const res = config.normalize(pkg);
assert(res.files.length);
assert(res.files.indexOf('main.js') !== -1);
});
it('should not double add the file to files', () => {
const pkg = {
files: ['main.js'],
main: 'main.js'
};
const res = config.normalize(pkg);
assert.equal(res.files.length, 3);
assert(res.files.indexOf('main.js') !== -1);
});
it('should remove main if the file does not exist', () => {
const pkg = { main: 'foo.js' };
const res = config.normalize(pkg);
assert(!res.main);
});
it('should do nothing if not defined', () => {
const pkg = {};
const res = config.normalize(pkg);
assert.equal(typeof res.main, 'undefined');
});
});
describe('preferGlobal', () => {
beforeEach(() => {
config = new Normalizer({ verbose: false });
});
it('should not warn when preferGlobal is defined and `bin` is defined', cb => {
const pkg = { preferGlobal: true, bin: 'main.js' };
let count = 0;
config.on('warning', function(method, key, err) {
if (key === 'preferGlobal') {
count++;
}
});
const res = config.normalize(pkg);
assert(res.preferGlobal);
assert.equal(count, 0);
cb();
});
it('should return bin as-is when it is a string', () => {
const pkg = { bin: 'main.js' };
const res = config.normalize(pkg);
assert(res.bin);
assert.equal(res.bin, 'main.js');
});
});
describe('bin', () => {
beforeEach(() => {
config = new Normalizer({ verbose: false });
});
it('should add `bin/foo.js` to bin', () => {
const res = config.normalize({});
assert.equal(res.bin[res.name], 'bin/foo.js');
});
it('should add `cli.js` to bin', cb => {
del('bin/foo.js', function(err) {
if (err) return cb(err);
const res = config.normalize({});
assert.equal(res.bin[res.name], 'cli.js');
fs.writeFile('bin/foo.js', '#!/usr/bin/env node\n\n', cb);
});
});
it('should not emit a warning when bin file string exists', cb => {
const pkg = { bin: 'main.js' };
let count = 0;
config.on('warning', function(method, key, err) {
if (key === 'bin') {
count++;
}
});
config.normalize(pkg);
assert.equal(count, 0);
cb();
});
it('should not emit a warning when bin file object exists', cb => {
const pkg = { bin: { foo: 'main.js' } };
let count = 0;
config.on('warning', function(method, key, err) {
if (key === 'bin') {
count++;
}
});
config.normalize(pkg);
assert.equal(count, 0);
cb();
});
});
});