From 1da1898cc0ff66b18cce92290cc1c4d15344f8f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Boulle?= Date: Mon, 10 Aug 2020 19:37:08 +0200 Subject: [PATCH] chore(repo): enforce no trailing space policy --- README.md | 2 +- validator.bats | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ validator.sh | 20 +++++++++++++++++++- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ef2bdd7..7c9bbae 100644 --- a/README.md +++ b/README.md @@ -197,7 +197,7 @@ See the [open issues](https://github.com/lumapps/commit-msg-validator/issues) fo - [x] enforce the commit body length - [x] enforce the JIRA reference - [x] enforce the BROKEN part length -- [ ] avoid trailing space +- [x] avoid trailing space - [ ] allow automated revert commit - [ ] allow fixup! and squash! commit with an option diff --git a/validator.bats b/validator.bats index 6960289..9e0b0cc 100644 --- a/validator.bats +++ b/validator.bats @@ -396,6 +396,35 @@ LUM-2345' [[ "$status" -eq 0 ]] } +@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 "features and fixes commits need jira reference" { [[ `need_jira "feat"` -eq 1 ]] [[ `need_jira "fix"` -eq 1 ]] @@ -487,6 +516,15 @@ LUM-2345' [[ "$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 @@ -500,6 +538,19 @@ BROKEN: [[ "$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 diff --git a/validator.sh b/validator.sh index 84ea10b..62d4df1 100644 --- a/validator.sh +++ b/validator.sh @@ -6,6 +6,7 @@ readonly SCOPE_PATTERN="^([a-z][a-z0-9]*)(-[a-z0-9]+)*$" readonly SUBJECT_PATTERN="^([a-z0-9].*[^ ^\.])$" readonly JIRA_PATTERN="^([A-Z]{2,4}-[0-9]{1,6} ?)+$" readonly BROKE_PATTERN="^BROKEN:$" +readonly TRAILING_SPACE_PATTERN=" +$" readonly ERROR_STRUCTURE=1 readonly ERROR_HEADER=2 @@ -14,7 +15,8 @@ readonly ERROR_TYPE=4 readonly ERROR_SCOPE=5 readonly ERROR_SUBJECT=6 readonly ERROR_BODY_LENGTH=7 -readonly ERROR_JIRA=8 +readonly ERROR_TRAILING_SPACE=8 +readonly ERROR_JIRA=9 GLOBAL_HEADER="" GLOBAL_BODY="" @@ -177,6 +179,19 @@ validate_body_length() { done <<< "$BODY" } +validate_trailing_space() { + local BODY=$1 + local LINE="" + + while IFS= read -r LINE ; + do + if [[ $LINE =~ $TRAILING_SPACE_PATTERN ]]; then + echo -e "body message must not have trailing spaces" + exit $ERROR_TRAILING_SPACE + fi + done <<< "$BODY" +} + need_jira() { local TYPE=$1 @@ -226,5 +241,8 @@ validate() { validate_body_length "$BODY" validate_body_length "$FOOTER" + validate_trailing_space "$BODY" + validate_trailing_space "$FOOTER" + validate_jira "$TYPE" "$JIRA" }