/
cli.js
245 lines (203 loc) · 7.31 KB
/
cli.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* global describe, it, before, after */
import 'should';
import path from 'path';
import cli from '../src/cli';
import testCases from './spec';
import { resolveShortPath } from './utils';
function makeArgv(module, options) {
const testPath = path.resolve('test/fake_modules', module);
const argv = [testPath];
if (options.json) {
argv.push('--json');
}
if (typeof options.ignoreBinPackage !== 'undefined') {
argv.push(`--ignore-bin-package=${options.ignoreBinPackage}`);
}
if (options.ignoreMatches) {
argv.push(`--ignores=${options.ignoreMatches.join(',')}`);
}
if (options.ignoreDirs) {
argv.push(`--ignore-dirs=${options.ignoreDirs.join(',')}`);
}
if (options.detectors) {
argv.push(`--detectors=${options.detectors.map(f => f.name).join(',')}`);
}
if (options.argv && options.argv.length) {
argv.push(...options.argv);
}
if (options.skipMissing !== undefined) {
argv.push(`--skip-missing=${options.skipMissing}`);
}
return argv;
}
function testCli(argv) {
let log = '';
let error = '';
return new Promise(resolve =>
cli(
argv,
(data) => { log = data; },
(data) => { error = data; },
exitCode => resolve({
log,
error,
exitCode,
logs: log.split('\n').filter(line => line),
errors: error.split('\n').filter(line => line),
}),
));
}
describe('depcheck command line', () => {
testCases.forEach((testCase) => {
const run = testCase.only === 'cli' ? it.only : it;
const options = Object.assign({ json: true }, testCase.options);
run(`should ${testCase.name}`, () =>
testCli(makeArgv(testCase.module, options))
.then(({ log, error, exitCode }) => {
const actual = JSON.parse(log);
const { expected } = testCase;
actual.dependencies.should.eql(expected.dependencies);
actual.devDependencies.should.eql(expected.devDependencies);
actual.missing.should.eql(resolveShortPath(expected.missing, testCase.module));
actual.using.should.eql(resolveShortPath(expected.using, testCase.module));
error.should.be.empty();
exitCode.should.equal(0); // JSON output always return 0
}));
});
it('should output error when folder is not a package', () =>
testCli([__dirname])
.then(({ log, error, exitCode }) => {
error.should.containEql(__dirname)
.and.containEql('not contain')
.and.containEql('package.json');
log.should.be.empty();
exitCode.should.equal(-1);
}));
it('should output error when folder not exists', () =>
testCli(['./not/exist/folder'])
.then(({ log, error, exitCode }) => {
error.should.containEql('/not/exist/folder').and.containEql('not exist');
log.should.be.empty();
exitCode.should.equal(-1);
}));
it('should output call stack for invalid files in JSON view', () =>
testCli(makeArgv('bad_js', { json: true }))
.then(({ log, error, exitCode }) => {
const json = JSON.parse(log);
json.should.have.properties([
'dependencies',
'devDependencies',
'invalidFiles',
'invalidDirs',
]);
const badJsPath = path.resolve(__dirname, './fake_modules/bad_js/index.js');
json.invalidFiles.should.have.property(badJsPath)
.startWith('SyntaxError: Unexpected token')
.and.containEql('\n at '); // call stack information
error.should.be.empty();
exitCode.should.equal(0);
}));
it('should output no depcheck issue when happen', () =>
testCli([path.resolve(__dirname, './fake_modules/good')])
.then(({ log, error, exitCode }) => {
log.should.equal('No depcheck issue');
error.should.be.empty();
exitCode.should.equal(0);
}));
it('should output unused dependencies', () =>
testCli(makeArgv('bad', {}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
logs[0].should.equal('Unused dependencies');
logs[1].should.containEql('optimist');
error.should.be.empty();
exitCode.should.equal(-1);
}));
it('should output unused devDependencies', () =>
testCli(makeArgv('dev', {}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
logs[0].should.equal('Unused devDependencies');
logs[1].should.containEql('unused-dev-dep');
error.should.be.empty();
exitCode.should.equal(-1);
}));
it('should output missing dependencies', () =>
testCli(makeArgv('missing', {}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
logs[0].should.equal('Missing dependencies');
logs[1].should.containEql('missing-dep');
error.should.be.empty();
exitCode.should.equal(-1);
}));
it('should recognize JSX file even only pass jsx parser and require detector', () =>
testCli(makeArgv('jsx', {
argv: ['--parsers="*.jsx:jsx"', '--dectors=requireCallExpression'],
}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(1);
logs[0].should.equal('No depcheck issue');
error.should.be.empty();
exitCode.should.equal(0);
}));
it('should not recognize JSX file when not pass jsx parser', () =>
testCli(makeArgv('jsx', {
argv: ['--parsers="*.jsx:es6"'],
}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
logs[0].should.equal('Unused dependencies');
logs[1].should.containEql('react');
error.should.be.empty();
exitCode.should.equal(-1);
}));
it('should not recognize JSX file when not enable require detector', () =>
testCli(makeArgv('jsx', {
argv: ['--detectors=importDeclaration'],
}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
logs[0].should.equal('Unused dependencies');
logs[1].should.containEql('react');
error.should.be.empty();
exitCode.should.equal(-1);
}));
it('should find dependencies with special parser', () =>
testCli(makeArgv('eslint_config', {
argv: ['--specials=eslint'],
}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(2);
logs[0].should.equal('Unused devDependencies');
logs[1].should.containEql('eslint-config-unused');
error.should.be.empty();
exitCode.should.equal(-1);
}));
it('should find dependencies with special parser', () =>
testCli(makeArgv('gatsby', {
argv: ['--specials=gatsby'],
}))
.then(({ logs, error, exitCode }) => {
logs.should.have.length(3);
logs[0].should.equal('Unused dependencies');
logs[1].should.containEql('gatsby-plugin-react-helmet');
error.should.be.empty();
exitCode.should.equal(-1);
}));
describe('without specified directory', () => {
let originalCwd;
before(() => {
originalCwd = process.cwd;
process.cwd = () => '/not/exist';
});
it('should default to the current directory', () =>
testCli([])
.then(({ log, error, exitCode }) => {
error.should.containEql('not exist');
log.should.be.empty();
exitCode.should.equal(-1);
}));
after(() => { process.cwd = originalCwd; });
});
});