-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathcli.test.js
298 lines (252 loc) Β· 9.86 KB
/
cli.test.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import path from 'path';
import {fix, git} from '@commitlint/test';
import test from 'ava';
import execa from 'execa';
import merge from 'lodash.merge';
import * as sander from 'sander';
import stream from 'string-to-stream';
const bin = path.join(__dirname, './cli.js');
const cli = (args, options) => {
return (input = '') => {
const c = execa(bin, args, {
capture: ['stdout'],
cwd: options.cwd,
env: options.env
});
stream(input).pipe(c.stdin);
return c.catch(err => err);
};
};
test('should throw when called without [input]', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli([], {cwd})();
t.is(actual.code, 1);
});
test('should reprint input from stdin', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli([], {cwd})('foo: bar');
t.true(actual.stdout.includes('foo: bar'));
});
test('should produce no success output with --quiet flag', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli(['--quiet'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
});
test('should produce no success output with -q flag', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli(['-q'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
});
test('should succeed for input from stdin without rules', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const actual = await cli([], {cwd})('foo: bar');
t.is(actual.code, 0);
});
test('should fail for input from stdin with rule from rc', async t => {
const cwd = await git.bootstrap('fixtures/simple');
const actual = await cli([], {cwd})('foo: bar');
t.true(actual.stdout.includes('type must not be one of [foo]'));
t.is(actual.code, 1);
});
test('should work with --config option', async t => {
const file = 'config/commitlint.config.js';
const cwd = await git.bootstrap('fixtures/specify-config-file');
const actual = await cli(['--config', file], {cwd})('foo: bar');
t.true(actual.stdout.includes('type must not be one of [foo]'));
t.is(actual.code, 1);
});
test('should fail for input from stdin with rule from js', async t => {
const cwd = await git.bootstrap('fixtures/extends-root');
const actual = await cli(['--extends', './extended'], {cwd})('foo: bar');
t.true(actual.stdout.includes('type must not be one of [foo]'));
t.is(actual.code, 1);
});
test('should produce no error output with --quiet flag', async t => {
const cwd = await git.bootstrap('fixtures/simple');
const actual = await cli(['--quiet'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
t.is(actual.code, 1);
});
test('should produce no error output with -q flag', async t => {
const cwd = await git.bootstrap('fixtures/simple');
const actual = await cli(['-q'], {cwd})('foo: bar');
t.is(actual.stdout, '');
t.is(actual.stderr, '');
t.is(actual.code, 1);
});
test('should work with husky commitmsg hook and git commit', async () => {
const cwd = await git.bootstrap('fixtures/husky/integration');
await writePkg({scripts: {commitmsg: `'${bin}' -e`}}, {cwd});
await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
});
test('should work with husky commitmsg hook in sub packages', async () => {
const upper = await git.bootstrap('fixtures/husky');
const cwd = path.join(upper, 'integration');
await writePkg({scripts: {commitmsg: `'${bin}' -e`}}, {cwd: upper});
await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
});
test('should work with husky via commitlint -e $GIT_PARAMS', async () => {
const cwd = await git.bootstrap('fixtures/husky/integration');
await writePkg({scripts: {commitmsg: `'${bin}' -e $GIT_PARAMS`}}, {cwd});
await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
});
test('should work with husky via commitlint -e %GIT_PARAMS%', async () => {
const cwd = await git.bootstrap('fixtures/husky/integration');
await writePkg({scripts: {commitmsg: `'${bin}' -e %GIT_PARAMS%`}}, {cwd});
await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa('git', ['commit', '-m', '"test: this should work"'], {cwd});
});
test('should allow reading of environment variables for edit file, succeeding if valid', async t => {
const cwd = await git.bootstrap();
await sander.writeFile(cwd, 'commit-msg-file', 'foo');
const actual = await cli(['--env', 'variable'], {
cwd,
env: {variable: 'commit-msg-file'}
})();
t.is(actual.code, 0);
});
test('should allow reading of environment variables for edit file, failing if invalid', async t => {
const cwd = await git.bootstrap('fixtures/simple');
await sander.writeFile(
cwd,
'commit-msg-file',
'foo: bar\n\nFoo bar bizz buzz.\n\nCloses #123.'
);
const actual = await cli(['--env', 'variable'], {
cwd,
env: {variable: 'commit-msg-file'}
})();
t.is(actual.code, 1);
});
test('should pick up parser preset and fail accordingly', async t => {
const cwd = await git.bootstrap('fixtures/parser-preset');
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(
'type(scope): subject'
);
t.is(actual.code, 1);
t.true(actual.stdout.includes('may not be empty'));
});
test('should pick up parser preset and succeed accordingly', async t => {
const cwd = await git.bootstrap('fixtures/parser-preset');
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(
'----type(scope): subject'
);
t.is(actual.code, 0);
});
test('should pick up config from outside git repo and fail accordingly', async t => {
const outer = await fix.bootstrap('fixtures/outer-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('inner: bar');
t.is(actual.code, 1);
});
test('should pick up config from outside git repo and succeed accordingly', async t => {
const outer = await fix.bootstrap('fixtures/outer-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('outer: bar');
t.is(actual.code, 0);
});
test('should pick up config from inside git repo with precedence and succeed accordingly', async t => {
const outer = await fix.bootstrap('fixtures/inner-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('inner: bar');
t.is(actual.code, 0);
});
test('should pick up config from inside git repo with precedence and fail accordingly', async t => {
const outer = await fix.bootstrap('fixtures/inner-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('outer: bar');
t.is(actual.code, 1);
});
test('should handle --amend with signoff', async () => {
const cwd = await git.bootstrap('fixtures/signoff');
await writePkg({scripts: {commitmsg: `'${bin}' -e`}}, {cwd});
await execa('npm', ['install'], {cwd});
await execa('git', ['add', 'package.json'], {cwd});
await execa(
'git',
['commit', '-m', '"test: this should work"', '--signoff'],
{cwd}
);
await execa('git', ['commit', '--amend', '--no-edit'], {cwd});
});
test('should handle linting with issue prefixes', async t => {
const cwd = await git.bootstrap('fixtures/issue-prefixes');
const actual = await cli([], {cwd})('foobar REF-1');
t.is(actual.code, 0);
});
test('should print full commit message when input from stdin fails', async t => {
const cwd = await git.bootstrap('fixtures/simple');
const input = 'foo: bar\n\nFoo bar bizz buzz.\n\nCloses #123.';
const actual = await cli([], {cwd})(input);
t.true(actual.stdout.includes(input));
t.is(actual.code, 1);
});
test('should not print full commit message when input succeeds', async t => {
const cwd = await git.bootstrap('fixtures/empty');
const message = 'foo: bar\n\nFoo bar bizz buzz.\n\nCloses #123.';
const actual = await cli([], {cwd})(message);
t.false(actual.stdout.includes(message));
t.true(actual.stdout.includes(message.split('\n')[0]));
t.is(actual.code, 0);
});
test('should fail for invalid formatters from configuration', async t => {
const cwd = await git.bootstrap('fixtures/custom-formatter');
const actual = await cli([], {cwd})('foo: bar');
t.true(
actual.stderr.includes(
`Using format custom-formatter, but cannot find the module`
)
);
t.is(actual.stdout, '');
t.is(actual.code, 1);
});
test('should fail for invalid formatters from flags', async t => {
const cwd = await git.bootstrap('fixtures/custom-formatter');
const actual = await cli(['--format', 'through-flag'], {cwd})('foo: bar');
t.true(
actual.stderr.includes(
`Using format through-flag, but cannot find the module`
)
);
t.is(actual.stdout, '');
t.is(actual.code, 1);
});
test('should work with absolute formatter path', async t => {
const formatterPath = path.resolve(
__dirname,
'../fixtures/custom-formatter/formatters/custom.js'
);
const cwd = await git.bootstrap('fixtures/custom-formatter');
const actual = await cli(['--format', formatterPath], {cwd})(
'test: this should work'
);
t.true(actual.stdout.includes('custom-formatter-ok'));
t.is(actual.code, 0);
});
test('should work with relative formatter path', async t => {
const cwd = path.resolve(
await git.bootstrap('fixtures/custom-formatter'),
'./formatters'
);
const actual = await cli(['--format', './custom.js'], {cwd})(
'test: this should work'
);
t.true(actual.stdout.includes('custom-formatter-ok'));
t.is(actual.code, 0);
});
async function writePkg(payload, options) {
const pkgPath = path.join(options.cwd, 'package.json');
const pkg = JSON.parse(await sander.readFile(pkgPath));
const result = merge(pkg, payload);
await sander.writeFile(pkgPath, JSON.stringify(result, null, ' '));
}