forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.test.ts
505 lines (433 loc) Β· 17.9 KB
/
cli.test.ts
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
import path from 'path';
import {fix, git} from '@commitlint/test';
import execa from 'execa';
import merge from 'lodash/merge';
import fs from 'fs-extra';
const bin = require.resolve('../cli.js');
interface TestOptions {
cwd: string;
env?: Record<string, string>;
}
const cli = (args: string[], options: TestOptions) => {
return (input = '') => {
return execa(bin, args, {
cwd: options.cwd,
env: options.env,
input: input,
reject: false,
});
};
};
const gitBootstrap = (fixture: string) => git.bootstrap(fixture, __dirname);
const fixBootstrap = (fixture: string) => fix.bootstrap(fixture, __dirname);
test('should throw when called without [input]', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli([], {cwd})();
expect(actual.exitCode).toBe(1);
});
test('should reprint input from stdin', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli([], {cwd})('foo: bar');
expect(actual.stdout).toContain('foo: bar');
});
test('should produce success output with --verbose flag', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(['--verbose'], {cwd})('type: bar');
expect(actual.stdout).toContain('0 problems, 0 warnings');
expect(actual.stderr).toEqual('');
});
test('should produce no output with --quiet flag', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(['--quiet'], {cwd})('foo: bar');
expect(actual.stdout).toEqual('');
expect(actual.stderr).toEqual('');
});
test('should produce no output with -q flag', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(['-q'], {cwd})('foo: bar');
expect(actual.stdout).toEqual('');
expect(actual.stderr).toEqual('');
});
test('should produce help for empty config', async () => {
const cwd = await gitBootstrap('fixtures/empty');
const actual = await cli([], {cwd})('foo: bar');
expect(actual.stdout).toContain('Please add rules');
expect(actual.exitCode).toBe(1);
});
test('should produce help for problems', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli([], {cwd})('foo: bar');
expect(actual.stdout).toContain(
'Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint'
);
expect(actual.exitCode).toBe(1);
});
test('should produce help for problems with correct helpurl', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(
['-H https://github.com/conventional-changelog/commitlint/#testhelpurl'],
{cwd}
)('foo: bar');
expect(actual.stdout).toContain(
'Get help: https://github.com/conventional-changelog/commitlint/#testhelpurl'
);
expect(actual.exitCode).toBe(1);
});
test('should fail for input from stdin without rules', async () => {
const cwd = await gitBootstrap('fixtures/empty');
const actual = await cli([], {cwd})('foo: bar');
expect(actual.exitCode).toBe(1);
});
test('should succeed for input from stdin with rules', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli([], {cwd})('type: bar');
expect(actual.exitCode).toBe(0);
});
test('should fail for input from stdin with rule from rc', async () => {
const cwd = await gitBootstrap('fixtures/simple');
const actual = await cli([], {cwd})('foo: bar');
expect(actual.stdout).toContain('type must not be one of [foo]');
expect(actual.exitCode).toBe(1);
});
test('should work with --config option', async () => {
const file = 'config/commitlint.config.js';
const cwd = await gitBootstrap('fixtures/specify-config-file');
const actual = await cli(['--config', file], {cwd})('foo: bar');
expect(actual.stdout).toContain('type must not be one of [foo]');
expect(actual.exitCode).toBe(1);
});
test('should fail for input from stdin with rule from js', async () => {
const cwd = await gitBootstrap('fixtures/extends-root');
const actual = await cli(['--extends', './extended'], {cwd})('foo: bar');
expect(actual.stdout).toContain('type must not be one of [foo]');
expect(actual.exitCode).toBe(1);
});
test('should output help URL defined in config file', async () => {
const cwd = await gitBootstrap('fixtures/help-url');
const actual = await cli([], {cwd})('foo: bar');
expect(actual.stdout).toContain('Get help: https://www.example.com/foo');
expect(actual.exitCode).toBe(1);
});
test('should produce no error output with --quiet flag', async () => {
const cwd = await gitBootstrap('fixtures/simple');
const actual = await cli(['--quiet'], {cwd})('foo: bar');
expect(actual.stdout).toEqual('');
expect(actual.stderr).toEqual('');
expect(actual.exitCode).toBe(1);
});
test('should produce no error output with -q flag', async () => {
const cwd = await gitBootstrap('fixtures/simple');
const actual = await cli(['-q'], {cwd})('foo: bar');
expect(actual.stdout).toEqual('');
expect(actual.stderr).toEqual('');
expect(actual.exitCode).toBe(1);
});
test('should work with husky commitmsg hook and git commit', async () => {
const cwd = await gitBootstrap('fixtures/husky/integration');
await writePkg({husky: {hooks: {'commit-msg': `'${bin}' -e`}}}, {cwd});
// await execa('npm', ['install'], {cwd}); // npm install is failing on windows machines
await execa('git', ['add', 'package.json'], {cwd});
const commit = await execa(
'git',
['commit', '-m', '"test: this should work"'],
{cwd}
);
expect(commit).toBeTruthy();
});
test('should work with husky commitmsg hook in sub packages', async () => {
const upper = await gitBootstrap('fixtures/husky');
const cwd = path.join(upper, 'integration');
await writePkg({husky: {hooks: {'commit-msg': `'${bin}' -e`}}}, {cwd: upper});
// await execa('npm', ['install'], {cwd}); // npm install is failing on windows machines
await execa('git', ['add', 'package.json'], {cwd});
const commit = await execa(
'git',
['commit', '-m', '"test: this should work"'],
{cwd}
);
expect(commit).toBeTruthy();
});
test('should work with husky via commitlint -e $GIT_PARAMS', async () => {
const cwd = await gitBootstrap('fixtures/husky/integration');
await writePkg(
{husky: {hooks: {'commit-msg': `'${bin}' -e $GIT_PARAMS`}}},
{cwd}
);
// await execa('npm', ['install'], {cwd}); // npm install is failing on windows machines
await execa('git', ['add', 'package.json'], {cwd});
const commit = await execa(
'git',
['commit', '-m', '"test: this should work"'],
{cwd}
);
expect(commit).toBeTruthy();
});
test('should work with husky via commitlint -e %GIT_PARAMS%', async () => {
const cwd = await gitBootstrap('fixtures/husky/integration');
await writePkg(
{husky: {hooks: {'commit-msg': `'${bin}' -e %GIT_PARAMS%`}}},
{cwd}
);
// await execa('npm', ['install'], {cwd}); // npm install is failing on windows machines
await execa('git', ['add', 'package.json'], {cwd});
const commit = await execa(
'git',
['commit', '-m', '"test: this should work"'],
{cwd}
);
expect(commit).toBeTruthy();
});
test('should work with husky via commitlint -e $HUSKY_GIT_PARAMS', async () => {
const cwd = await gitBootstrap('fixtures/husky/integration');
await writePkg(
{husky: {hooks: {'commit-msg': `'${bin}' -e $HUSKY_GIT_PARAMS`}}},
{cwd}
);
// await execa('npm', ['install'], {cwd}); // npm install is failing on windows machines
await execa('git', ['add', 'package.json'], {cwd});
const commit = await execa(
'git',
['commit', '-m', '"test: this should work"'],
{cwd}
);
expect(commit).toBeTruthy();
});
test('should work with husky via commitlint -e %HUSKY_GIT_PARAMS%', async () => {
const cwd = await gitBootstrap('fixtures/husky/integration');
await writePkg(
{husky: {hooks: {'commit-msg': `'${bin}' -e %HUSKY_GIT_PARAMS%`}}},
{cwd}
);
// await execa('npm', ['install'], {cwd}); // npm install is failing on windows machines
await execa('git', ['add', 'package.json'], {cwd});
const commit = await execa(
'git',
['commit', '-m', '"test: this should work"'],
{cwd}
);
expect(commit).toBeTruthy();
});
test('should allow reading of environment variables for edit file, succeeding if valid', async () => {
const cwd = await gitBootstrap('fixtures/simple');
await fs.writeFile(path.join(cwd, 'commit-msg-file'), 'foo');
const actual = await cli(['--env', 'variable'], {
cwd,
env: {variable: 'commit-msg-file'},
})();
expect(actual.exitCode).toBe(0);
});
test('should allow reading of environment variables for edit file, failing if invalid', async () => {
const cwd = await gitBootstrap('fixtures/simple');
await fs.writeFile(
path.join(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'},
})();
expect(actual.exitCode).toBe(1);
});
test('should pick up parser preset and fail accordingly', async () => {
const cwd = await gitBootstrap('fixtures/parser-preset');
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(
'type(scope): subject'
);
expect(actual.exitCode).toBe(1);
expect(actual.stdout).toContain('may not be empty');
});
test('should pick up parser preset and succeed accordingly', async () => {
const cwd = await gitBootstrap('fixtures/parser-preset');
const actual = await cli(['--parser-preset', './parser-preset'], {cwd})(
'----type(scope): subject'
);
expect(actual.exitCode).toBe(0);
});
test('should pick up config from outside git repo and fail accordingly', async () => {
const outer = await fixBootstrap('fixtures/outer-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('inner: bar');
expect(actual.exitCode).toBe(1);
});
test('should pick up config from outside git repo and succeed accordingly', async () => {
const outer = await fixBootstrap('fixtures/outer-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('outer: bar');
expect(actual.exitCode).toBe(0);
});
test('should pick up config from inside git repo with precedence and succeed accordingly', async () => {
const outer = await fixBootstrap('fixtures/inner-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('inner: bar');
expect(actual.exitCode).toBe(0);
});
test('should pick up config from inside git repo with precedence and fail accordingly', async () => {
const outer = await fixBootstrap('fixtures/inner-scope');
const cwd = await git.init(path.join(outer, 'inner-scope'));
const actual = await cli([], {cwd})('outer: bar');
expect(actual.exitCode).toBe(1);
});
test('should handle --amend with signoff', async () => {
const cwd = await gitBootstrap('fixtures/signoff');
await writePkg({husky: {hooks: {'commit-msg': `'${bin}' -e`}}}, {cwd});
// await execa('npm', ['install'], {cwd}); // npm install is failing on windows machines
await execa('git', ['add', 'package.json'], {cwd});
await execa(
'git',
['commit', '-m', '"test: this should work"', '--signoff'],
{cwd}
);
const commit = await execa('git', ['commit', '--amend', '--no-edit'], {cwd});
expect(commit).toBeTruthy();
}, 10000);
test('should handle linting with issue prefixes', async () => {
const cwd = await gitBootstrap('fixtures/issue-prefixes');
const actual = await cli([], {cwd})('foobar REF-1');
expect(actual.exitCode).toBe(0);
}, 10000);
test('should print full commit message when input from stdin fails', async () => {
const cwd = await gitBootstrap('fixtures/simple');
const input = 'foo: bar\n\nFoo bar bizz buzz.\n\nCloses #123.';
// output text in plain text so we can compare it
const actual = await cli(['--color=false'], {cwd})(input);
expect(actual.stdout).toContain(input);
expect(actual.exitCode).toBe(1);
});
test('should not print commit message fully or partially when input succeeds', async () => {
const cwd = await gitBootstrap('fixtures/default');
const message = 'type: bar\n\nFoo bar bizz buzz.\n\nCloses #123.';
// output text in plain text so we can compare it
const actual = await cli(['--color=false'], {cwd})(message);
expect(actual.stdout).not.toContain(message);
expect(actual.stdout).not.toContain(message.split('\n')[0]);
expect(actual.exitCode).toBe(0);
});
test('should fail for invalid formatters from configuration', async () => {
const cwd = await gitBootstrap('fixtures/custom-formatter');
const actual = await cli([], {cwd})('foo: bar');
expect(actual.stderr).toContain(
'Using format custom-formatter, but cannot find the module'
);
expect(actual.stdout).toEqual('');
expect(actual.exitCode).toBe(1);
});
test('should skip linting if message matches ignores config', async () => {
const cwd = await gitBootstrap('fixtures/ignores');
const actual = await cli([], {cwd})('WIP');
expect(actual.exitCode).toBe(0);
});
test('should not skip linting if message does not match ignores config', async () => {
const cwd = await gitBootstrap('fixtures/ignores');
const actual = await cli([], {cwd})('foo');
expect(actual.exitCode).toBe(1);
});
test('should not skip linting if defaultIgnores is false', async () => {
const cwd = await gitBootstrap('fixtures/default-ignores-false');
const actual = await cli([], {cwd})('fixup! foo: bar');
expect(actual.exitCode).toBe(1);
});
test('should skip linting if defaultIgnores is true', async () => {
const cwd = await gitBootstrap('fixtures/default-ignores-true');
const actual = await cli([], {cwd})('fixup! foo: bar');
expect(actual.exitCode).toBe(0);
});
test('should skip linting if defaultIgnores is unset', async () => {
const cwd = await gitBootstrap('fixtures/default-ignores-unset');
const actual = await cli([], {cwd})('fixup! foo: bar');
expect(actual.exitCode).toBe(0);
});
test('should fail for invalid formatters from flags', async () => {
const cwd = await gitBootstrap('fixtures/custom-formatter');
const actual = await cli(['--format', 'through-flag'], {cwd})('foo: bar');
expect(actual.stderr).toContain(
'Using format through-flag, but cannot find the module'
);
expect(actual.stdout).toEqual('');
expect(actual.exitCode).toBe(1);
});
test('should work with absolute formatter path', async () => {
const formatterPath = path.resolve(
__dirname,
'../fixtures/custom-formatter/formatters/custom.js'
);
const cwd = await gitBootstrap('fixtures/custom-formatter');
const actual = await cli(['--format', formatterPath], {cwd})(
'test: this should work'
);
expect(actual.stdout).toContain('custom-formatter-ok');
expect(actual.exitCode).toBe(0);
});
test('should work with relative formatter path', async () => {
const cwd = path.resolve(
await gitBootstrap('fixtures/custom-formatter'),
'./formatters'
);
const actual = await cli(['--format', './custom.js'], {cwd})(
'test: this should work'
);
expect(actual.stdout).toContain('custom-formatter-ok');
expect(actual.exitCode).toBe(0);
});
test('should print help', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(['--help'], {cwd})();
expect(actual.stdout).toMatchInlineSnapshot(`
"@commitlint/[email protected] - Lint your commit messages
[input] reads from stdin if --edit, --env, --from and --to are omitted
Options:
-c, --color toggle colored output [boolean] [default: true]
-g, --config path to the config file [string]
--print-config print resolved config [boolean] [default: false]
-d, --cwd directory to execute in
[string] [default: (Working Directory)]
-e, --edit read last commit message from the specified file or
fallbacks to ./.git/COMMIT_EDITMSG [string]
-E, --env check message in the file at path given by environment
variable value [string]
-x, --extends array of shareable configurations to extend [array]
-H, --help-url help url in error message [string]
-f, --from lower end of the commit range to lint; applies if
edit=false [string]
-o, --format output format of the results [string]
-p, --parser-preset configuration preset to use for
conventional-commits-parser [string]
-q, --quiet toggle console output [boolean] [default: false]
-t, --to upper end of the commit range to lint; applies if
edit=false [string]
-V, --verbose enable verbose output for reports without problems
[boolean]
-v, --version display version information [boolean]
-h, --help Show help [boolean]"
`);
});
test('should print version', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(['--version'], {cwd})();
expect(actual.stdout).toMatch('@commitlint/cli@');
});
test('should print config', async () => {
const cwd = await gitBootstrap('fixtures/default');
const actual = await cli(['--print-config', '--no-color'], {cwd})();
const stdout = actual.stdout
.replace(/^{[^\n]/g, '{\n ')
.replace(/[^\n]}$/g, '\n}')
.replace(/(helpUrl:)\n[ ]+/, '$1 ');
expect(stdout).toMatchInlineSnapshot(`
"{
extends: [],
formatter: '@commitlint/format',
parserPreset: undefined,
ignores: undefined,
defaultIgnores: undefined,
plugins: {},
rules: { 'type-enum': [ 2, 'never', [ 'foo' ] ] },
helpUrl: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint'
}"
`);
});
async function writePkg(payload: unknown, options: TestOptions) {
const pkgPath = path.join(options.cwd, 'package.json');
const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8'));
const result = merge(pkg, payload);
await fs.writeFile(pkgPath, JSON.stringify(result, null, ' '));
}