-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidator.bats
745 lines (556 loc) · 16.7 KB
/
validator.bats
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
#!/usr/bin/env bats
source $BATS_TEST_DIRNAME/validator.sh
@test "structure: one trailing line" {
COMMIT="plop plop
"
run validate_overall_structure "$COMMIT"
[ "$status" -eq $ERROR_STRUCTURE ]
}
@test "structure: missing empty line after the header" {
COMMIT="plop plop
plop
plop
"
run validate_overall_structure "$COMMIT"
[ "$status" -eq $ERROR_STRUCTURE ]
}
@test "structure: missing empty line after the header for jira" {
COMMIT="plop plop
ABC-1234
"
run validate_overall_structure "$COMMIT"
[ "$status" -eq $ERROR_STRUCTURE ]
}
@test "structure: missing empty line after the header for broken" {
COMMIT="plop plop
BROKEN:
"
run validate_overall_structure "$COMMIT"
[ "$status" -eq $ERROR_STRUCTURE ]
}
@test "structure: missing empty line after the body with jira ref" {
COMMIT="plop plop
plop
plop
plop
plop
LUM-1234
"
run validate_overall_structure "$COMMIT"
[ "$status" -eq $ERROR_STRUCTURE ]
}
@test "structure: missing empty line after the body with broken stuff" {
COMMIT="plop plop
plop
plop
plop
plop
BROKEN:
"
run validate_overall_structure "$COMMIT"
[ "$status" -eq $ERROR_STRUCTURE ]
}
@test "structure: empty line after the body" {
COMMIT="plop plop
plop
plop
plop
plop
"
run validate_overall_structure "$COMMIT"
[ "$status" -eq $ERROR_STRUCTURE ]
}
@test "structure: valid commit message with header only" {
COMMIT="plop plop"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "" ]]
[[ $GLOBAL_JIRA == "" ]]
[[ $GLOBAL_FOOTER == "" ]]
}
@test "structure: valid commit message with header and JIRA" {
COMMIT="plop plop
ABC-1234"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "" ]]
[[ $GLOBAL_JIRA == "ABC-1234" ]]
[[ $GLOBAL_FOOTER == "" ]]
}
@test "structure: valid commit message with JIRA in header" {
COMMIT="feat(abc): ABC-1234
plop"
GLOBAL_JIRA_IN_HEADER="allow" validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "feat(abc): ABC-1234" ]]
[[ $GLOBAL_JIRA == "ABC-1234" ]]
[[ $GLOBAL_BODY == "plop"$'\n' ]]
[[ $GLOBAL_FOOTER == "" ]]
}
@test "structure: valid commit message with header and multiple JIRA" {
COMMIT="plop plop
ABC-1234 DE-1234"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "" ]]
[[ $GLOBAL_JIRA == "ABC-1234 DE-1234" ]]
[[ $GLOBAL_FOOTER == "" ]]
}
@test "structure: valid commit message with header and broken" {
COMMIT="plop plop
BROKEN:
- plop
- plop"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "" ]]
[[ $GLOBAL_JIRA == "" ]]
[[ $GLOBAL_FOOTER == "- plop"$'\n'"- plop"$'\n' ]]
}
@test "structure: valid commit message with header, jira and broken" {
COMMIT="plop plop
ABC-1234
BROKEN:
- plop
- plop"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "" ]]
[[ $GLOBAL_JIRA == "ABC-1234" ]]
[[ $GLOBAL_FOOTER == "- plop"$'\n'"- plop"$'\n' ]]
}
@test "structure: valid commit message with header and body" {
COMMIT="plop plop
hello"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "hello"$'\n' ]]
[[ $GLOBAL_JIRA == "" ]]
[[ $GLOBAL_FOOTER == "" ]]
}
@test "structure: valid commit message with header and multiline body" {
COMMIT="plop plop
hello
plopplop
plopplop
toto"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "hello"$'\n'"plopplop"$'\n'"plopplop"$'\n'"toto"$'\n' ]]
[[ $GLOBAL_JIRA == "" ]]
[[ $GLOBAL_FOOTER == "" ]]
}
@test "structure: valid commit message with header, multiline body and jira" {
COMMIT="plop plop
hello
plopplop
plopplop
toto
ABC-1234"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "hello"$'\n'"plopplop"$'\n'"plopplop"$'\n'"toto"$'\n' ]]
[[ $GLOBAL_JIRA == "ABC-1234" ]]
[[ $GLOBAL_FOOTER == "" ]]
}
@test "structure: valid commit message with header, multiline body and broken" {
COMMIT="plop plop
hello
plopplop
plopplop
toto
BROKEN:
- plop
- plop"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "hello"$'\n'"plopplop"$'\n'"plopplop"$'\n'"toto"$'\n' ]]
[[ $GLOBAL_JIRA == "" ]]
[[ $GLOBAL_FOOTER == "- plop"$'\n'"- plop"$'\n' ]]
}
@test "structure: valid commit message with header, multiline body, jira and broken" {
COMMIT="plop plop
hello
plopplop
plopplop
toto
ABC-1234
BROKEN:
- plop
- plop"
validate_overall_structure "$COMMIT"
[[ $GLOBAL_HEADER == "plop plop" ]]
[[ $GLOBAL_BODY == "hello"$'\n'"plopplop"$'\n'"plopplop"$'\n'"toto"$'\n' ]]
[[ $GLOBAL_JIRA == "ABC-1234" ]]
[[ $GLOBAL_FOOTER == "- plop"$'\n'"- plop"$'\n' ]]
}
@test "header overall should not allow 'type'" {
run validate_header "type"
[ "$status" -eq $ERROR_HEADER ]
}
@test "header overall should not allow 'type(scope)'" {
run validate_header "type(scope)"
[ "$status" -eq $ERROR_HEADER ]
}
@test "header overall should not allow 'type(scope) message'" {
run validate_header "type(scope) message"
[ "$status" -eq $ERROR_HEADER ]
}
@test "header overall should not allow 'type(scope) : message'" {
run validate_header "type(scope) : message"
[ "$status" -eq $ERROR_HEADER ]
}
@test "header overall should not allow 'type(scope: message'" {
run validate_header "type(scope: message"
[ "$status" -eq $ERROR_HEADER ]
}
@test "header overall should not allow 'type scope: message'" {
run validate_header "type scope: message"
[ "$status" -eq $ERROR_HEADER ]
}
@test "header overall should not allow 'type(scope):message'" {
run validate_header "type(scope):message"
[ "$status" -eq $ERROR_HEADER ]
}
@test "header overall should allow fixup if env not empty" {
COMMIT_VALIDATOR_ALLOW_TEMP=1 validate_header "fixup! plopezrr"
[[ $GLOBAL_TYPE == "temp" ]]
}
@test "header overall should allow squash if env not empty" {
COMMIT_VALIDATOR_ALLOW_TEMP=1 validate_header "squash! plopezrr"
[[ $GLOBAL_TYPE == "temp" ]]
}
@test "header overall should reject fixup if env empty" {
COMMIT_VALIDATOR_ALLOW_TEMP= run validate_header "fixup! plopezrr"
[[ "$status" -eq $ERROR_HEADER ]]
}
@test "header overall should reject squash if env empty" {
COMMIT_VALIDATOR_ALLOW_TEMP= run validate_header "squash! plopezrr"
[[ "$status" -eq $ERROR_HEADER ]]
}
@test "header overall should reject fixup if env not set" {
run validate_header "fixup! plopezrr"
[[ "$status" -eq $ERROR_HEADER ]]
}
@test "header overall should reject squash if env not set" {
run validate_header "squash! plopezrr"
[[ "$status" -eq $ERROR_HEADER ]]
}
@test "header overall should allow 'revert: type(scope): message'" {
validate_header "revert: type(scope): message"
[[ $GLOBAL_TYPE == "revert" ]]
}
@test "header overall should allow 'Revert \#type(scope): message\"'" {
validate_header "Revert \"type(scope): message\""
[[ $GLOBAL_TYPE == "revert" ]]
}
@test "header overall should allow 'type(scope): message'" {
validate_header "type(scope): message"
[[ $GLOBAL_TYPE == "type" ]]
[[ $GLOBAL_SCOPE == "scope" ]]
[[ $GLOBAL_SUBJECT == "message" ]]
}
@test "header overall should allow 'type 1(scope 2): message 3'" {
validate_header "type 1(scope 2): message 3"
[[ $GLOBAL_TYPE == "type 1" ]]
[[ $GLOBAL_SCOPE == "scope 2" ]]
[[ $GLOBAL_SUBJECT == "message 3" ]]
}
@test "header length cannot be more than 100" {
run validate_header_length "01234567890123456789012345678901234567890123456789012345678901234567891234567890123456789012345678901"
[ "$status" -eq $ERROR_HEADER_LENGTH ]
}
@test "header length cannot be more than 100 with spaces" {
run validate_header_length "012345678 012345678 012345678 012345678 012345678 012345678 012345678 123456789 0123456789 0123456789"
[ "$status" -eq $ERROR_HEADER_LENGTH ]
}
@test "header length cannot be more than 150 with spaces. overriden" {
GLOBAL_MAX_LENGTH=150 validate_header_length "012345678 012345678 012345678 012345678 012345678 012345678 012345678 1"
}
@test "header length can be 100" {
run validate_header_length "0123456789012345678901234567890123456789012345678901234567890123456789123456789012345678901234567890"
[ "$status" -eq 0 ]
}
@test "header length can be 100 with spaces" {
run validate_header_length "012345678 012345678 012345678 012345678 012345678 012345678 012345678 123456789 0123456789 01234567 "
[ "$status" -eq 0 ]
}
@test "unknown commit type 'plop' should be rejected" {
run validate_type "plop"
[ "$status" -eq $ERROR_TYPE ]
}
@test "known commit type with trailing space should be rejected" {
run validate_type "feat "
[ "$status" -eq $ERROR_TYPE ]
}
@test "known commit type with capitalized letter should be rejected" {
run validate_type "Feat"
[ "$status" -eq $ERROR_TYPE ]
}
@test "commit type 'feat' should be ok" {
run validate_type "feat"
[ "$status" -eq 0 ]
}
@test "scope cannot be empty" {
run validate_scope ""
[ "$status" -eq $ERROR_SCOPE ]
}
@test "scope cannot be capitalized" {
run validate_scope "plopPlop"
[ "$status" -eq $ERROR_SCOPE ]
}
@test "scope cannot have space" {
run validate_scope "plop plop"
[ "$status" -eq $ERROR_SCOPE ]
}
@test "scope cannot have trailing space" {
run validate_scope "plop "
[ "$status" -eq $ERROR_SCOPE ]
}
@test "scope should allow simple kebab-case" {
run validate_scope "p2"
[ "$status" -eq 0 ]
}
@test "scope should allow kebab-case" {
run validate_scope "pl2op-plop1-plop-0001"
[ "$status" -eq 0 ]
}
@test "subject cannot be empty" {
run validate_subject ""
[ "$status" -eq $ERROR_SUBJECT ]
}
@test "subject cannot have trailing space" {
run validate_subject "plop "
[ "$status" -eq $ERROR_SUBJECT ]
}
@test "subject cannot end with a point" {
run validate_subject "plop."
[ "$status" -eq $ERROR_SUBJECT ]
}
@test "subject should allow sentences" {
run validate_subject "0002 dedezf ef zefzef zfeze fzef zf zef"
[ "$status" -eq 0 ]
}
@test "body with 101 line length should be rejected" {
MESSAGE='
12345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 01
LUM-2345'
run validate_body_length "$MESSAGE"
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
}
@test "body with 100 line length should be valid" {
MESSAGE='
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
12345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 012345678 0
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
LUM-2345'
run validate_body_length "$MESSAGE"
[[ "$status" -eq 0 ]]
}
@test "body with 100+ line length should be valid if no space" {
MESSAGE='
0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_
LUM-2345'
run validate_body_length "$MESSAGE"
[[ "$status" -eq 0 ]]
}
@test "body length cannot be more than 150 with spaces. overridden" {
GLOBAL_BODY_MAX_LENGTH=150 validate_body_length "012345678 012345678 012345678 012345678 012345678 012345678 012345678 1"
}
@test "body with trailing space on line should not be valid" {
MESSAGE='pdzofjzf '
run validate_trailing_space "$MESSAGE"
[[ "$status" -eq $ERROR_TRAILING_SPACE ]]
}
@test "body with trailing space on new line should not be valid" {
MESSAGE='
rerer
LUM-2345'
run validate_trailing_space "$MESSAGE"
[[ "$status" -eq $ERROR_TRAILING_SPACE ]]
}
@test "body without trailing space should be valid" {
MESSAGE='
rerer
LUM-2345'
run validate_trailing_space "$MESSAGE"
[[ "$status" -eq 0 ]]
}
@test "revert body without commit sha1 should be refused" {
MESSAGE='rerer
LUM-2345'
run validate_revert "$MESSAGE"
[[ "$status" -eq $ERROR_REVERT ]]
COMMIT_VALIDATOR_NO_REVERT_SHA1= run validate_revert "$MESSAGE"
[[ "$status" -eq $ERROR_REVERT ]]
}
@test "revert body with commit sha1 should be valid" {
MESSAGE='rerer
This reverts commit 1234567890.
LUM-2345'
run validate_revert "$MESSAGE"
[[ "$status" -eq 0 ]]
COMMIT_VALIDATOR_NO_REVERT_SHA1= run validate_revert "$MESSAGE"
[[ "$status" -eq 0 ]]
}
@test "revert body without sha1 should be valid with the flag" {
MESSAGE='rerer
LUM-2345'
COMMIT_VALIDATOR_NO_REVERT_SHA1=1 run validate_revert "$MESSAGE"
[[ "$status" -eq 0 ]]
}
@test "features and fixes commits need jira reference" {
need_jira "feat"
need_jira "fix"
}
@test "features and fixes commits need jira reference if env empty" {
COMMIT_VALIDATOR_NO_JIRA= need_jira "feat"
COMMIT_VALIDATOR_NO_JIRA= need_jira "fix"
}
@test "features and fixes commits don't need jira reference if env non empty" {
! COMMIT_VALIDATOR_NO_JIRA=1 need_jira "feat"
! COMMIT_VALIDATOR_NO_JIRA=1 need_jira "fix"
}
@test "other commits don't need jira reference" {
! need_jira "docs"
! need_jira "test"
}
@test "feat without jira ref should be rejected" {
run validate_jira "feat" ""
[[ "$status" -eq $ERROR_JIRA ]]
}
@test "lint without jira ref should be validated" {
run validate_jira "lint" ""
[[ "$status" -eq 0 ]]
}
@test "feat with jira ref should be validated" {
run validate_jira "feat" "ABC-123"
[[ "$status" -eq 0 ]]
}
@test "feat with short jira ref should be validated" {
run validate_jira "feat" "AB-123"
[[ "$status" -eq 0 ]]
}
@test "overall validation invalid structure" {
MESSAGE='plop
plop'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_STRUCTURE ]]
}
@test "overall validation invalid header" {
MESSAGE='plop'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_HEADER ]]
}
@test "overall validation invalid header length" {
MESSAGE='feat(plop): 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_HEADER_LENGTH ]]
}
@test "overall validation invalid type" {
MESSAGE='Feat(scope1): subject
Commit about stuff\"plop \"
LUM-2345'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_TYPE ]]
}
@test "overall validation invalid scope" {
MESSAGE='feat(scope 1): subject
Commit about stuff\"plop \"
LUM-2345'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_SCOPE ]]
}
@test "overall validation invalid subject" {
MESSAGE='feat(scope1): Subject
Commit about stuff\"plop \"
LUM-2345'
run validate "$MESSAGE"
[[ "$status" -ne $ERROR_SUBJECT ]]
}
@test "overall validation invalid body length" {
MESSAGE='feat(scope1): subject
1 2 3 4 5678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901
LUM-2345'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
}
@test "overall validation invalid body trailing space" {
MESSAGE='chore(scope1): subject
123456789012345678901234567890123456789012 '
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_TRAILING_SPACE ]]
}
@test "overall validation invalid footer length" {
MESSAGE='feat(scope1): subject
plop
LUM-2345
BROKEN:
- 12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
}
@test "overall validation invalid footer trailing space" {
MESSAGE='feat(scope1): subject
plop
LUM-2345
BROKEN:
- 123456 '
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_TRAILING_SPACE ]]
}
@test "overall validation missing jira" {
MESSAGE='feat(scope1): subject
Commit about stuff\"plop \"
2345'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_JIRA ]]
}
@test "overall validation" {
MESSAGE='feat(scope1): subject
Commit about stuff\"plop \" dezd
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
LUM-2345
BROKEN:
- plop
- plop'
run validate "$MESSAGE"
[[ "$status" -eq 0 ]]
}
@test "overall revert validation" {
MESSAGE='Revert "feat(scope1): subject"
This reverts commit 12345678900.
Commit about stuff\"plop \" dezd
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
LUM-2345
BROKEN:
- plop
- plop'
run validate "$MESSAGE"
[[ "$status" -eq 0 ]]
}
@test "overall fixup! validation" {
MESSAGE='fixup! plepozkfopezr
Commit about stuff\"plop \" dezd
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
LUM-2345
BROKEN:
- plop
- plop'
COMMIT_VALIDATOR_ALLOW_TEMP=1 run validate "$MESSAGE"
[[ "$status" -eq 0 ]]
}
@test "overall fixup! rejection" {
MESSAGE='fixup! plepozkfopezr
Commit about stuff\"plop \" dezd
12345678901234567890123456789012345678901234567890
12345678901234567890123456789012345678901234567890
LUM-2345
BROKEN:
- plop
- plop'
run validate "$MESSAGE"
[[ "$status" -eq $ERROR_HEADER ]]
}