@@ -2,11 +2,11 @@ import {describe, test, expect} from 'vitest';
2
2
import { createRequire } from 'module' ;
3
3
import path from 'path' ;
4
4
import { fileURLToPath } from 'url' ;
5
-
6
5
import { fix , git } from '@commitlint/test' ;
7
6
import fs from 'fs-extra' ;
8
7
import merge from 'lodash.merge' ;
9
8
import { x } from 'tinyexec' ;
9
+ import { ExitCode } from './cli-error.js' ;
10
10
11
11
const require = createRequire ( import . meta. url ) ;
12
12
@@ -42,7 +42,7 @@ test('should throw when called without [input]', async () => {
42
42
const cwd = await gitBootstrap ( 'fixtures/default' ) ;
43
43
const result = cli ( [ ] , { cwd} ) ( ) ;
44
44
await result ;
45
- expect ( result . exitCode ) . toBe ( 1 ) ;
45
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
46
46
} ) ;
47
47
48
48
test ( 'should reprint input from stdin' , async ( ) => {
@@ -107,7 +107,7 @@ test('should produce help for empty config', async () => {
107
107
const result = cli ( [ ] , { cwd} ) ( 'foo: bar' ) ;
108
108
const output = await result ;
109
109
expect ( output . stdout . trim ( ) ) . toContain ( 'Please add rules' ) ;
110
- expect ( result . exitCode ) . toBe ( 1 ) ;
110
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintInvalidArgument ) ;
111
111
} ) ;
112
112
113
113
test ( 'should produce help for problems' , async ( ) => {
@@ -117,7 +117,7 @@ test('should produce help for problems', async () => {
117
117
expect ( output . stdout . trim ( ) ) . toContain (
118
118
'Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint'
119
119
) ;
120
- expect ( result . exitCode ) . toBe ( 1 ) ;
120
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
121
121
} ) ;
122
122
123
123
test ( 'should produce help for problems with correct helpurl' , async ( ) => {
@@ -130,29 +130,29 @@ test('should produce help for problems with correct helpurl', async () => {
130
130
expect ( output . stdout . trim ( ) ) . toContain (
131
131
'Get help: https://github.com/conventional-changelog/commitlint/#testhelpurl'
132
132
) ;
133
- expect ( result . exitCode ) . toBe ( 1 ) ;
133
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
134
134
} ) ;
135
135
136
136
test ( 'should fail for input from stdin without rules' , async ( ) => {
137
137
const cwd = await gitBootstrap ( 'fixtures/empty' ) ;
138
138
const result = cli ( [ ] , { cwd} ) ( 'foo: bar' ) ;
139
139
await result ;
140
- expect ( result . exitCode ) . toBe ( 1 ) ;
140
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintInvalidArgument ) ;
141
141
} ) ;
142
142
143
143
test ( 'should succeed for input from stdin with rules' , async ( ) => {
144
144
const cwd = await gitBootstrap ( 'fixtures/default' ) ;
145
145
const result = cli ( [ ] , { cwd} ) ( 'type: bar' ) ;
146
146
await result ;
147
- expect ( result . exitCode ) . toBe ( 0 ) ;
147
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
148
148
} ) ;
149
149
150
150
test ( 'should fail for input from stdin with rule from rc' , async ( ) => {
151
151
const cwd = await gitBootstrap ( 'fixtures/simple' ) ;
152
152
const result = cli ( [ ] , { cwd} ) ( 'foo: bar' ) ;
153
153
const output = await result ;
154
154
expect ( output . stdout . trim ( ) ) . toContain ( 'type must not be one of [foo]' ) ;
155
- expect ( result . exitCode ) . toBe ( 1 ) ;
155
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
156
156
} ) ;
157
157
158
158
test ( 'should work with --config option' , async ( ) => {
@@ -161,15 +161,15 @@ test('should work with --config option', async () => {
161
161
const result = cli ( [ '--config' , file ] , { cwd} ) ( 'foo: bar' ) ;
162
162
const output = await result ;
163
163
expect ( output . stdout . trim ( ) ) . toContain ( 'type must not be one of [foo]' ) ;
164
- expect ( result . exitCode ) . toBe ( 1 ) ;
164
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
165
165
} ) ;
166
166
167
167
test ( 'should fail for input from stdin with rule from js' , async ( ) => {
168
168
const cwd = await gitBootstrap ( 'fixtures/extends-root' ) ;
169
169
const result = cli ( [ '--extends' , './extended' ] , { cwd} ) ( 'foo: bar' ) ;
170
170
const output = await result ;
171
171
expect ( output . stdout . trim ( ) ) . toContain ( 'type must not be one of [foo]' ) ;
172
- expect ( result . exitCode ) . toBe ( 1 ) ;
172
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
173
173
} ) ;
174
174
175
175
test ( 'should output help URL defined in config file' , async ( ) => {
@@ -179,7 +179,7 @@ test('should output help URL defined in config file', async () => {
179
179
expect ( output . stdout . trim ( ) ) . toContain (
180
180
'Get help: https://www.example.com/foo'
181
181
) ;
182
- expect ( result . exitCode ) . toBe ( 1 ) ;
182
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
183
183
} ) ;
184
184
185
185
test ( 'should produce no error output with --quiet flag' , async ( ) => {
@@ -188,7 +188,7 @@ test('should produce no error output with --quiet flag', async () => {
188
188
const output = await result ;
189
189
expect ( output . stdout . trim ( ) ) . toEqual ( '' ) ;
190
190
expect ( output . stderr ) . toEqual ( '' ) ;
191
- expect ( result . exitCode ) . toBe ( 1 ) ;
191
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
192
192
} ) ;
193
193
194
194
test ( 'should produce no error output with -q flag' , async ( ) => {
@@ -197,7 +197,7 @@ test('should produce no error output with -q flag', async () => {
197
197
const output = await result ;
198
198
expect ( output . stdout . trim ( ) ) . toEqual ( '' ) ;
199
199
expect ( output . stderr ) . toEqual ( '' ) ;
200
- expect ( result . exitCode ) . toBe ( 1 ) ;
200
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
201
201
} ) ;
202
202
203
203
test ( 'should work with husky commitmsg hook and git commit' , async ( ) => {
@@ -294,7 +294,7 @@ test('should allow reading of environment variables for edit file, succeeding if
294
294
env : { variable : 'commit-msg-file' } ,
295
295
} ) ( ) ;
296
296
await result ;
297
- expect ( result . exitCode ) . toBe ( 0 ) ;
297
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
298
298
} ) ;
299
299
300
300
test ( 'should allow reading of environment variables for edit file, failing if invalid' , async ( ) => {
@@ -308,7 +308,7 @@ test('should allow reading of environment variables for edit file, failing if in
308
308
env : { variable : 'commit-msg-file' } ,
309
309
} ) ( ) ;
310
310
await result ;
311
- expect ( result . exitCode ) . toBe ( 1 ) ;
311
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
312
312
} ) ;
313
313
314
314
test ( 'should pick up parser preset and fail accordingly' , async ( ) => {
@@ -318,7 +318,7 @@ test('should pick up parser preset and fail accordingly', async () => {
318
318
) ;
319
319
const output = await result ;
320
320
expect ( output . stdout . trim ( ) ) . toContain ( 'may not be empty' ) ;
321
- expect ( result . exitCode ) . toBe ( 1 ) ;
321
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
322
322
} ) ;
323
323
324
324
test ( 'should pick up parser preset and succeed accordingly' , async ( ) => {
@@ -327,7 +327,7 @@ test('should pick up parser preset and succeed accordingly', async () => {
327
327
'----type(scope): subject'
328
328
) ;
329
329
await result ;
330
- expect ( result . exitCode ) . toBe ( 0 ) ;
330
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
331
331
} ) ;
332
332
333
333
test ( 'should pick up config from outside git repo and fail accordingly' , async ( ) => {
@@ -336,7 +336,7 @@ test('should pick up config from outside git repo and fail accordingly', async (
336
336
337
337
const result = cli ( [ ] , { cwd} ) ( 'inner: bar' ) ;
338
338
await result ;
339
- expect ( result . exitCode ) . toBe ( 1 ) ;
339
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
340
340
} ) ;
341
341
342
342
test ( 'should pick up config from outside git repo and succeed accordingly' , async ( ) => {
@@ -345,7 +345,7 @@ test('should pick up config from outside git repo and succeed accordingly', asyn
345
345
346
346
const result = cli ( [ ] , { cwd} ) ( 'outer: bar' ) ;
347
347
await result ;
348
- expect ( result . exitCode ) . toBe ( 0 ) ;
348
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
349
349
} ) ;
350
350
351
351
test ( 'should pick up config from inside git repo with precedence and succeed accordingly' , async ( ) => {
@@ -354,7 +354,7 @@ test('should pick up config from inside git repo with precedence and succeed acc
354
354
355
355
const result = cli ( [ ] , { cwd} ) ( 'inner: bar' ) ;
356
356
await result ;
357
- expect ( result . exitCode ) . toBe ( 0 ) ;
357
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
358
358
} ) ;
359
359
360
360
test ( 'should pick up config from inside git repo with precedence and fail accordingly' , async ( ) => {
@@ -363,7 +363,7 @@ test('should pick up config from inside git repo with precedence and fail accord
363
363
364
364
const result = cli ( [ ] , { cwd} ) ( 'outer: bar' ) ;
365
365
await result ;
366
- expect ( result . exitCode ) . toBe ( 1 ) ;
366
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
367
367
} ) ;
368
368
369
369
test ( 'should handle --amend with signoff' , async ( ) => {
@@ -389,7 +389,7 @@ test('it uses parserOpts.commentChar when not using edit mode', async () => {
389
389
const result = cli ( [ ] , { cwd} ) ( input ) ;
390
390
const output = await result ;
391
391
expect ( output . stdout . trim ( ) ) . toContain ( '[body-empty]' ) ;
392
- expect ( result . exitCode ) . toBe ( 1 ) ;
392
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
393
393
} ) ;
394
394
395
395
test ( "it doesn't use parserOpts.commentChar when using edit mode" , async ( ) => {
@@ -402,7 +402,7 @@ test("it doesn't use parserOpts.commentChar when using edit mode", async () => {
402
402
const result = cli ( [ '--edit' , '.git/COMMIT_EDITMSG' ] , { cwd} ) ( ) ;
403
403
const output = await result ;
404
404
expect ( output . stdout . trim ( ) ) . not . toContain ( '[body-empty]' ) ;
405
- expect ( result . exitCode ) . toBe ( 0 ) ;
405
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
406
406
} ) ;
407
407
408
408
test ( 'it uses core.commentChar git config when using edit mode' , async ( ) => {
@@ -418,7 +418,7 @@ test('it uses core.commentChar git config when using edit mode', async () => {
418
418
const result = cli ( [ '--edit' , '.git/COMMIT_EDITMSG' ] , { cwd} ) ( ) ;
419
419
const output = await result ;
420
420
expect ( output . stdout . trim ( ) ) . toContain ( '[body-empty]' ) ;
421
- expect ( result . exitCode ) . toBe ( 1 ) ;
421
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
422
422
} ) ;
423
423
424
424
test ( 'it falls back to # for core.commentChar when using edit mode' , async ( ) => {
@@ -432,14 +432,14 @@ test('it falls back to # for core.commentChar when using edit mode', async () =>
432
432
const output = await result ;
433
433
expect ( output . stdout . trim ( ) ) . toContain ( '[body-empty]' ) ;
434
434
expect ( output . stderr ) . toEqual ( '' ) ;
435
- expect ( result . exitCode ) . toBe ( 1 ) ;
435
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
436
436
} ) ;
437
437
438
438
test ( 'should handle linting with issue prefixes' , async ( ) => {
439
439
const cwd = await gitBootstrap ( 'fixtures/issue-prefixes' ) ;
440
440
const result = cli ( [ ] , { cwd} ) ( 'foobar REF-1' ) ;
441
441
await result ;
442
- expect ( result . exitCode ) . toBe ( 0 ) ;
442
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
443
443
} , 10000 ) ;
444
444
445
445
test ( 'should print full commit message when input from stdin fails' , async ( ) => {
@@ -449,7 +449,7 @@ test('should print full commit message when input from stdin fails', async () =>
449
449
const result = cli ( [ '--color=false' ] , { cwd} ) ( input ) ;
450
450
const output = await result ;
451
451
expect ( output . stdout . trim ( ) ) . toContain ( input ) ;
452
- expect ( result . exitCode ) . toBe ( 1 ) ;
452
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
453
453
} ) ;
454
454
455
455
test ( 'should not print commit message fully or partially when input succeeds' , async ( ) => {
@@ -460,7 +460,7 @@ test('should not print commit message fully or partially when input succeeds', a
460
460
const output = await result ;
461
461
expect ( output . stdout . trim ( ) ) . not . toContain ( message ) ;
462
462
expect ( output . stdout . trim ( ) ) . not . toContain ( message . split ( '\n' ) [ 0 ] ) ;
463
- expect ( result . exitCode ) . toBe ( 0 ) ;
463
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
464
464
} ) ;
465
465
466
466
test ( 'should fail for invalid formatters from configuration' , async ( ) => {
@@ -471,42 +471,42 @@ test('should fail for invalid formatters from configuration', async () => {
471
471
'Using format custom-formatter, but cannot find the module'
472
472
) ;
473
473
expect ( output . stdout . trim ( ) ) . toEqual ( '' ) ;
474
- expect ( result . exitCode ) . toBe ( 1 ) ;
474
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
475
475
} ) ;
476
476
477
477
test ( 'should skip linting if message matches ignores config' , async ( ) => {
478
478
const cwd = await gitBootstrap ( 'fixtures/ignores' ) ;
479
479
const result = cli ( [ ] , { cwd} ) ( 'WIP' ) ;
480
480
await result ;
481
- expect ( result . exitCode ) . toBe ( 0 ) ;
481
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
482
482
} ) ;
483
483
484
484
test ( 'should not skip linting if message does not match ignores config' , async ( ) => {
485
485
const cwd = await gitBootstrap ( 'fixtures/ignores' ) ;
486
486
const result = cli ( [ ] , { cwd} ) ( 'foo' ) ;
487
487
await result ;
488
- expect ( result . exitCode ) . toBe ( 1 ) ;
488
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
489
489
} ) ;
490
490
491
491
test ( 'should not skip linting if defaultIgnores is false' , async ( ) => {
492
492
const cwd = await gitBootstrap ( 'fixtures/default-ignores-false' ) ;
493
493
const result = cli ( [ ] , { cwd} ) ( 'fixup! foo: bar' ) ;
494
494
await result ;
495
- expect ( result . exitCode ) . toBe ( 1 ) ;
495
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
496
496
} ) ;
497
497
498
498
test ( 'should skip linting if defaultIgnores is true' , async ( ) => {
499
499
const cwd = await gitBootstrap ( 'fixtures/default-ignores-true' ) ;
500
500
const result = cli ( [ ] , { cwd} ) ( 'fixup! foo: bar' ) ;
501
501
await result ;
502
- expect ( result . exitCode ) . toBe ( 0 ) ;
502
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
503
503
} ) ;
504
504
505
505
test ( 'should skip linting if defaultIgnores is unset' , async ( ) => {
506
506
const cwd = await gitBootstrap ( 'fixtures/default-ignores-unset' ) ;
507
507
const result = cli ( [ ] , { cwd} ) ( 'fixup! foo: bar' ) ;
508
508
await result ;
509
- expect ( result . exitCode ) . toBe ( 0 ) ;
509
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
510
510
} ) ;
511
511
512
512
test ( 'should fail for invalid formatters from flags' , async ( ) => {
@@ -517,7 +517,7 @@ test('should fail for invalid formatters from flags', async () => {
517
517
'Using format through-flag, but cannot find the module'
518
518
) ;
519
519
expect ( output . stdout . trim ( ) ) . toEqual ( '' ) ;
520
- expect ( result . exitCode ) . toBe ( 1 ) ;
520
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintErrorDefault ) ;
521
521
} ) ;
522
522
523
523
test ( 'should work with absolute formatter path' , async ( ) => {
@@ -531,7 +531,7 @@ test('should work with absolute formatter path', async () => {
531
531
) ;
532
532
const output = await result ;
533
533
expect ( output . stdout . trim ( ) ) . toContain ( 'custom-formatter-ok' ) ;
534
- expect ( result . exitCode ) . toBe ( 0 ) ;
534
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
535
535
} ) ;
536
536
537
537
test ( 'should work with relative formatter path' , async ( ) => {
@@ -544,28 +544,28 @@ test('should work with relative formatter path', async () => {
544
544
) ;
545
545
const output = await result ;
546
546
expect ( output . stdout . trim ( ) ) . toContain ( 'custom-formatter-ok' ) ;
547
- expect ( result . exitCode ) . toBe ( 0 ) ;
547
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
548
548
} ) ;
549
549
550
550
test ( 'strict: should exit with 3 on error' , async ( ) => {
551
551
const cwd = await gitBootstrap ( 'fixtures/warning' ) ;
552
552
const result = cli ( [ '--strict' ] , { cwd} ) ( 'foo: abcdef' ) ;
553
553
await result ;
554
- expect ( result . exitCode ) . toBe ( 3 ) ;
554
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitLintError ) ;
555
555
} ) ;
556
556
557
557
test ( 'strict: should exit with 2 on warning' , async ( ) => {
558
558
const cwd = await gitBootstrap ( 'fixtures/warning' ) ;
559
559
const result = cli ( [ '--strict' ] , { cwd} ) ( 'feat: abcdef' ) ;
560
560
await result ;
561
- expect ( result . exitCode ) . toBe ( 2 ) ;
561
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitLintWarning ) ;
562
562
} ) ;
563
563
564
564
test ( 'strict: should exit with 0 on success' , async ( ) => {
565
565
const cwd = await gitBootstrap ( 'fixtures/warning' ) ;
566
566
const result = cli ( [ '--strict' ] , { cwd} ) ( 'feat: abc' ) ;
567
567
await result ;
568
- expect ( result . exitCode ) . toBe ( 0 ) ;
568
+ expect ( result . exitCode ) . toBe ( ExitCode . CommitlintDefault ) ;
569
569
} ) ;
570
570
571
571
test ( 'should print help' , async ( ) => {
0 commit comments