/
test.lint.js
163 lines (148 loc) · 4.58 KB
/
test.lint.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
import { it, describe } from 'mocha';
import { assert } from 'chai';
import * as sinon from 'sinon';
import defaultLintCommand from '../../../src/cmd/lint.js';
describe('lint', () => {
function setUp({ createLinter, createFileFilter } = {}) {
const lintResult = '<lint.run() result placeholder>';
const runLinter = sinon.spy(() => Promise.resolve(lintResult));
if (!createLinter) {
createLinter = sinon.spy(() => {
return { run: runLinter };
});
}
return {
lintResult,
createLinter,
runLinter,
lint: (params = {}, options = {}) => {
const mergedArgs = { sourceDir: '/fake/source/dir', ...params };
const mergedOpts = {
createLinter,
createFileFilter,
...options,
};
return defaultLintCommand(mergedArgs, mergedOpts);
},
};
}
it('creates and runs a linter', () => {
const { lint, createLinter, runLinter, lintResult } = setUp();
return lint().then((actualLintResult) => {
assert.equal(actualLintResult, lintResult);
sinon.assert.called(createLinter);
sinon.assert.called(runLinter);
});
});
it('fails when the linter fails', async () => {
const createLinter = () => {
return {
run: () => Promise.reject(new Error('some error from the linter')),
};
};
const { lint } = setUp({ createLinter });
await assert.isRejected(lint(), /error from the linter/);
});
it('runs as a binary', () => {
const { lint, createLinter } = setUp();
return lint().then(() => {
sinon.assert.calledWithMatch(createLinter, { runAsBinary: true });
});
});
it('sets runAsBinary according shouldExitProgram option', () => {
const { lint, createLinter } = setUp();
return lint({}, { shouldExitProgram: false }).then(() => {
sinon.assert.calledWithMatch(createLinter, { runAsBinary: false });
});
});
it('passes sourceDir to the linter', () => {
const { lint, createLinter } = setUp();
return lint({ sourceDir: '/some/path' }).then(() => {
const config = createLinter.firstCall.args[0].config;
assert.equal(config._[0], '/some/path');
});
});
it('passes warningsAsErrors to the linter', () => {
const { lint, createLinter } = setUp();
return lint({ warningsAsErrors: true }).then(() => {
sinon.assert.calledWithMatch(createLinter, {
config: {
warningsAsErrors: true,
},
});
});
});
it('passes warningsAsErrors undefined to the linter', () => {
const { lint, createLinter } = setUp();
return lint().then(() => {
sinon.assert.calledWithMatch(createLinter, {
config: {
warningsAsErrors: undefined,
},
});
});
});
it('configures the linter when verbose', () => {
const { lint, createLinter } = setUp();
return lint({ verbose: true }).then(() => {
sinon.assert.calledWithMatch(createLinter, {
config: {
logLevel: 'debug',
stack: true,
},
});
});
});
it('configures the linter when not verbose', () => {
const { lint, createLinter } = setUp();
return lint({ verbose: false }).then(() => {
sinon.assert.calledWithMatch(createLinter, {
config: {
logLevel: 'fatal',
stack: false,
},
});
});
});
it('passes through linter configuration', () => {
const { lint, createLinter } = setUp();
return lint({
pretty: true,
privileged: true,
metadata: true,
output: 'json',
boring: true,
selfHosted: true,
}).then(() => {
sinon.assert.calledWithMatch(createLinter, {
config: {
pretty: true,
privileged: true,
metadata: true,
output: 'json',
boring: true,
selfHosted: true,
minManifestVersion: 2,
maxManifestVersion: 3,
},
});
});
});
it('configures a lint command with the expected fileFilter', () => {
const fileFilter = { wantFile: sinon.spy(() => true) };
const createFileFilter = sinon.spy(() => fileFilter);
const { lint, createLinter } = setUp({ createFileFilter });
const params = {
sourceDir: '.',
artifactsDir: 'artifacts',
ignoreFiles: ['file1', '**/file2'],
};
return lint(params).then(() => {
sinon.assert.calledWith(createFileFilter, params);
assert.ok(createLinter.called);
const { shouldScanFile } = createLinter.firstCall.args[0].config;
shouldScanFile('path/to/file');
sinon.assert.calledWith(fileFilter.wantFile, 'path/to/file');
});
});
});