Skip to content

Commit

Permalink
chore(repo): enforce no trailing space policy
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-boulle committed Aug 20, 2020
1 parent 3f61be0 commit 1da1898
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 51 additions & 0 deletions validator.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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 ]]
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 19 additions & 1 deletion validator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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=""
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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"
}

0 comments on commit 1da1898

Please sign in to comment.