Skip to content

Commit 1da1898

Browse files
chore(repo): enforce no trailing space policy
1 parent 3f61be0 commit 1da1898

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ See the [open issues](https://github.com/lumapps/commit-msg-validator/issues) fo
197197
- [x] enforce the commit body length
198198
- [x] enforce the JIRA reference
199199
- [x] enforce the BROKEN part length
200-
- [ ] avoid trailing space
200+
- [x] avoid trailing space
201201
- [ ] allow automated revert commit
202202
- [ ] allow fixup! and squash! commit with an option
203203

validator.bats

+51
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,35 @@ LUM-2345'
396396
[[ "$status" -eq 0 ]]
397397
}
398398

399+
@test "body with trailing space on line should not be valid" {
400+
MESSAGE='pdzofjzf '
401+
402+
run validate_trailing_space "$MESSAGE"
403+
[[ "$status" -eq $ERROR_TRAILING_SPACE ]]
404+
}
405+
406+
@test "body with trailing space on new line should not be valid" {
407+
MESSAGE='
408+
rerer
409+
410+
411+
LUM-2345'
412+
413+
run validate_trailing_space "$MESSAGE"
414+
[[ "$status" -eq $ERROR_TRAILING_SPACE ]]
415+
}
416+
417+
@test "body without trailing space should be valid" {
418+
MESSAGE='
419+
rerer
420+
421+
422+
LUM-2345'
423+
424+
run validate_trailing_space "$MESSAGE"
425+
[[ "$status" -eq 0 ]]
426+
}
427+
399428
@test "features and fixes commits need jira reference" {
400429
[[ `need_jira "feat"` -eq 1 ]]
401430
[[ `need_jira "fix"` -eq 1 ]]
@@ -487,6 +516,15 @@ LUM-2345'
487516
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
488517
}
489518

519+
@test "overall validation invalid body trailing space" {
520+
MESSAGE='chore(scope1): subject
521+
522+
123456789012345678901234567890123456789012 '
523+
524+
run validate "$MESSAGE"
525+
[[ "$status" -eq $ERROR_TRAILING_SPACE ]]
526+
}
527+
490528
@test "overall validation invalid footer length" {
491529
MESSAGE='feat(scope1): subject
492530
@@ -500,6 +538,19 @@ BROKEN:
500538
[[ "$status" -eq $ERROR_BODY_LENGTH ]]
501539
}
502540

541+
@test "overall validation invalid footer trailing space" {
542+
MESSAGE='feat(scope1): subject
543+
544+
plop
545+
546+
LUM-2345
547+
BROKEN:
548+
- 123456 '
549+
550+
run validate "$MESSAGE"
551+
[[ "$status" -eq $ERROR_TRAILING_SPACE ]]
552+
}
553+
503554
@test "overall validation missing jira" {
504555
MESSAGE='feat(scope1): subject
505556

validator.sh

+19-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ readonly SCOPE_PATTERN="^([a-z][a-z0-9]*)(-[a-z0-9]+)*$"
66
readonly SUBJECT_PATTERN="^([a-z0-9].*[^ ^\.])$"
77
readonly JIRA_PATTERN="^([A-Z]{2,4}-[0-9]{1,6} ?)+$"
88
readonly BROKE_PATTERN="^BROKEN:$"
9+
readonly TRAILING_SPACE_PATTERN=" +$"
910

1011
readonly ERROR_STRUCTURE=1
1112
readonly ERROR_HEADER=2
@@ -14,7 +15,8 @@ readonly ERROR_TYPE=4
1415
readonly ERROR_SCOPE=5
1516
readonly ERROR_SUBJECT=6
1617
readonly ERROR_BODY_LENGTH=7
17-
readonly ERROR_JIRA=8
18+
readonly ERROR_TRAILING_SPACE=8
19+
readonly ERROR_JIRA=9
1820

1921
GLOBAL_HEADER=""
2022
GLOBAL_BODY=""
@@ -177,6 +179,19 @@ validate_body_length() {
177179
done <<< "$BODY"
178180
}
179181

182+
validate_trailing_space() {
183+
local BODY=$1
184+
local LINE=""
185+
186+
while IFS= read -r LINE ;
187+
do
188+
if [[ $LINE =~ $TRAILING_SPACE_PATTERN ]]; then
189+
echo -e "body message must not have trailing spaces"
190+
exit $ERROR_TRAILING_SPACE
191+
fi
192+
done <<< "$BODY"
193+
}
194+
180195
need_jira() {
181196
local TYPE=$1
182197

@@ -226,5 +241,8 @@ validate() {
226241
validate_body_length "$BODY"
227242
validate_body_length "$FOOTER"
228243

244+
validate_trailing_space "$BODY"
245+
validate_trailing_space "$FOOTER"
246+
229247
validate_jira "$TYPE" "$JIRA"
230248
}

0 commit comments

Comments
 (0)