-
-
Notifications
You must be signed in to change notification settings - Fork 222
/
bin.js
192 lines (183 loc) · 5.41 KB
/
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
181
182
183
184
185
186
187
188
189
190
191
192
import 'should';
import parser from '../../src/special/bin';
import { getTestParserWithContentPromise } from '../utils';
const testParser = getTestParserWithContentPromise(parser);
const testCases = [
{
name: 'detect packages used in scripts in local directory',
script: 'binary-entry --argument',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect packages used in scripts in parent directory',
script: 'binupper --upperarg',
dependencies: ['upperbin'],
expected: ['upperbin'],
},
{
name: 'detect packages used in scripts (anywhere)',
script: 'binary-entry --argument || binupper --upperarg',
dependencies: ['binary-package', 'upperbin'],
expected: ['binary-package', 'upperbin'],
},
{
name: 'detect packages used as `.bin` path',
script: './node_modules/.bin/binary-entry',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect packages used as package path',
script: './node_modules/binary-package/bin/binary-exe',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect packages combined with `npm bin` command',
script: '$(npm bin)/binary-entry',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect package bin without prefix dot',
script: 'node_modules/.bin/binary-entry',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect package path without prefix dot',
script: 'node_modules/binary-package/bin/binary-exe',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect binary call with variable set',
script: 'NODE_ENV=production binary-entry',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'not report it when it is not used',
script: 'other-binary-entry',
dependencies: ['binary-package', 'upperbin'],
expected: [],
},
{
name: 'ignore detection when no scripts section',
script: false,
dependencies: ['binary-package', 'upperbin'],
expected: [],
},
{
name: 'ignore dependency without bin entry',
script: 'binary-entry',
dependencies: ['binary-no-bin'],
expected: [],
},
{
name: 'handle dependency without package.json',
script: 'binary-entry',
dependencies: ['binary-no-package'],
expected: [],
},
{
name: 'detect packages used with --require and /register',
script: 'module --require binary-entry/register',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect packages used with --require',
script: 'module --require binary-entry',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect packages used with -r and /register',
script: 'module -r binary-entry/register',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect packages used with -r',
script: 'module -r binary-entry',
dependencies: ['binary-package'],
expected: ['binary-package'],
},
{
name: 'detect packages with single binary with default name',
script: 'single-binary-package --argument',
dependencies: ['single-binary-package'],
expected: ['single-binary-package'],
},
{
name: 'detect scoped packages with single binary with default name',
script: 'scoped-single-binary-package --argument',
dependencies: ['@scoped/scoped-single-binary-package'],
expected: ['@scoped/scoped-single-binary-package'],
},
{
name: 'detect scoped packages with named binary',
script: 'scoped-binary-entry --argument',
dependencies: ['@scoped/scoped-binary-package'],
expected: ['@scoped/scoped-binary-package'],
},
{
name: 'esm package.json in subpath',
script: 'esm-packagejson-subpath',
dependencies: ['esm-packagejson-subpath'],
expected: ['esm-packagejson-subpath'],
},
{
name: 'esm',
script: 'esm',
dependencies: ['esm'],
expected: ['esm'],
},
];
async function testBin(testCase, content, filename) {
const result = await testParser(
content,
filename,
testCase.dependencies,
__dirname,
);
result.should.deepEqual(testCase.expected);
}
describe('bin special parser', () => {
it('should ignore when filename is not supported', async () => {
const result = await parser('not-supported.txt', [], '/root/dir');
result.should.deepEqual([]);
});
describe('on `package.json`', () =>
testCases.forEach((testCase) =>
it(`should ${testCase.name}`, () => {
const content = testCase.script
? JSON.stringify({ scripts: { t: testCase.script } })
: '{}';
return testBin(
testCase,
content,
`/path/to/${testCase.name}/package.json`,
);
}),
));
describe('on `.travis.yml`', () =>
testCases.forEach((testCase) =>
it(`should ${testCase.name}`, () => {
const content = testCase.script
? `script:\n - ${testCase.script}`
: '';
return testBin(
testCase,
content,
`/path/to/${testCase.name}/.travis.yml`,
);
}),
));
it('should check lifecycle commands in `.travis.yml` file', () => {
const content = `before_deploy:\n - ${testCases[0].script}`;
return testBin(testCases[0], content, '/path/to/.travis.yml');
});
});