Skip to content

Commit

Permalink
Print usage instructions before checking for invalid env vars
Browse files Browse the repository at this point in the history
If the user runs 'lint-commit-msg -h' we should always print the usage
instructions... even in the case that the user has e.g. a misspelled
configuration variable (e.g. LCM_INTREACTIVE=never) in their .bashrc.

The same rationale applies with LCM_IGNORE_ALL=true, it should bypass
the linting even if there are errors in the configuration variables.
  • Loading branch information
laurivan committed Apr 17, 2024
1 parent d3338c2 commit 6af9f23
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 35 deletions.
72 changes: 37 additions & 35 deletions lint-commit-msg
Original file line number Diff line number Diff line change
Expand Up @@ -65,41 +65,6 @@ _LCM_MSG_BACKUP_FILE=lint-commit-msg.MSG
[ "${LCM_COLOR}" = "true" ] && LCM_COLOR="always"
[ "${LCM_COLOR}" = "false" ] && LCM_COLOR="never"

# Validate variables: misspelled variables with LCM_ prefix and variable values.
invalid_vars=false
for lcm_var in "${!LCM_@}" "${!lcm_@}"
do
case "${lcm_var}" in
LCM_IGNORE_2ND_LINE_NOT_BLANK|LCM_IGNORE_ALL|LCM_IGNORE_BODY_LINE_TOO_LONG|LCM_IGNORE_CONTAINS_TABS|LCM_IGNORE_INVALID_SUBJECT_LINE_PREFIX|LCM_IGNORE_LINE_COUNT_IS_2|LCM_IGNORE_MISSING_FINAL_EOL|LCM_IGNORE_SUBJECT_LINE_ENDS_IN_PERIOD|LCM_IGNORE_SUBJECT_NOT_CAPITALIZED|LCM_IGNORE_SUBJECT_LINE_TOO_LONG|LCM_IGNORE_SUBJECT_LINE_TOO_SHORT|LCM_IGNORE_SUBJECT_MOOD|LCM_IGNORE_SUBJECT_NOT_CAPITALIZED|LCM_IGNORE_TABS|LCM_IGNORE_TRAILING_WHITESPACE)
[[ "${!lcm_var}" =~ true|false ]] || {
echo "lint-commit-msg: ERROR: invalid value, ${lcm_var}='${!lcm_var}' (should be true/false)"
invalid_vars=true
}
;;
LCM_SUBJECT_LINE_PREFIX_HELP|LCM_SUBJECT_LINE_PREFIX_REGEX)
:
;;
LCM_COLOR|LCM_INTERACTIVE)
[[ "${!lcm_var}" =~ always|never|auto|true|false ]] || {
echo "lint-commit-msg: ERROR: invalid value, ${lcm_var}='${!lcm_var}' (should be always (aka true), never (aka false), or auto)"
invalid_vars=true
}
;;
LCM_SUBJECT_LINE_MAX_LENGTH|LCM_SUBJECT_LINE_MIN_LENGTH|LCM_BODY_LINE_MAX_LENGTH)
integer_regex='^[1-9][0-9]*$'
[[ "${!lcm_var}" =~ ${integer_regex} ]] || {
echo "lint-commit-msg: ERROR: invalid value, ${lcm_var}='${!lcm_var}' (should be a positive integer)"
invalid_vars=true
}
;;
*)
echo "lint-commit-msg: ERROR: unrecognized configuration variable: ${lcm_var}"
invalid_vars=true
;;
esac
done
${invalid_vars} && exit "${EXIT_CODE_USER_ERROR}"

color_red="\x1B[0;31m"
color_yellow="\x1B[0;33m"
color_white_bold="\x1B[1;37m"
Expand Down Expand Up @@ -213,6 +178,43 @@ then
exit 0
fi

# Validate variables: misspelled variables with LCM_ prefix and variable values.
# Do this only after (possibly) printing the usage instructions and after checking
# whether LCM_IGNORE_ALL is used to bypass lint-commit-msg altogether.
invalid_vars=false
for lcm_var in "${!LCM_@}" "${!lcm_@}"
do
case "${lcm_var}" in
LCM_IGNORE_2ND_LINE_NOT_BLANK|LCM_IGNORE_ALL|LCM_IGNORE_BODY_LINE_TOO_LONG|LCM_IGNORE_CONTAINS_TABS|LCM_IGNORE_INVALID_SUBJECT_LINE_PREFIX|LCM_IGNORE_LINE_COUNT_IS_2|LCM_IGNORE_MISSING_FINAL_EOL|LCM_IGNORE_SUBJECT_LINE_ENDS_IN_PERIOD|LCM_IGNORE_SUBJECT_NOT_CAPITALIZED|LCM_IGNORE_SUBJECT_LINE_TOO_LONG|LCM_IGNORE_SUBJECT_LINE_TOO_SHORT|LCM_IGNORE_SUBJECT_MOOD|LCM_IGNORE_SUBJECT_NOT_CAPITALIZED|LCM_IGNORE_TABS|LCM_IGNORE_TRAILING_WHITESPACE)
[[ "${!lcm_var}" =~ true|false ]] || {
echo "lint-commit-msg: ERROR: invalid value, ${lcm_var}='${!lcm_var}' (should be true/false)"
invalid_vars=true
}
;;
LCM_SUBJECT_LINE_PREFIX_HELP|LCM_SUBJECT_LINE_PREFIX_REGEX)
:
;;
LCM_COLOR|LCM_INTERACTIVE)
[[ "${!lcm_var}" =~ always|never|auto|true|false ]] || {
echo "lint-commit-msg: ERROR: invalid value, ${lcm_var}='${!lcm_var}' (should be always (aka true), never (aka false), or auto)"
invalid_vars=true
}
;;
LCM_SUBJECT_LINE_MAX_LENGTH|LCM_SUBJECT_LINE_MIN_LENGTH|LCM_BODY_LINE_MAX_LENGTH)
integer_regex='^[1-9][0-9]*$'
[[ "${!lcm_var}" =~ ${integer_regex} ]] || {
echo "lint-commit-msg: ERROR: invalid value, ${lcm_var}='${!lcm_var}' (should be a positive integer)"
invalid_vars=true
}
;;
*)
echo "lint-commit-msg: ERROR: unrecognized configuration variable: ${lcm_var}"
invalid_vars=true
;;
esac
done
${invalid_vars} && exit "${EXIT_CODE_USER_ERROR}"

# If no input file is given, use '-' which (when passed to 'cat') will make
# this script work also when the commit message is passed in via stdin.
input_file="${1--}"
Expand Down
11 changes: 11 additions & 0 deletions test/cases/param-validation.test
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,14 @@ test_multiple_invalid_values() {
[ "${error_count}" -eq "${expected_error_count}" ] ||
fail "Expected ${expected_error_count} errors in stdout, got ${error_count}"
}

test_invalid_var_with_ignore_all() {
# Invalid environment variable value
export LCM_IGNORE_BODY_LINE_TOO_LONG=treu
# LCM_IGNORE_ALL should still be able to bypass the linting.
export LCM_IGNORE_ALL=true
call lint-commit-msg ../msg.txt
exit_code 0
stdout_not_contains "ERROR"
stderr_not_contains "ERROR"
}
10 changes: 10 additions & 0 deletions test/cases/usage-output.test
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,13 @@ test_usage_with_color() {
diff "${TEST_FILE_DIR}/usage-with-color.snapshot" "${STDOUT}" ||
fail 'Usage output does not match "snapshot"'
}

test_usage_with_invalid_env_var() {
export LCM_COLOR=never

# Misspelled variable shouldn't prevent the usage output from being printed.
export LCM_INTREACTIVE=never
call lint-commit-msg -h
stdout_contains "lint-commit-msg (LCM) checks that a Git commit message"
stderr_empty
}

0 comments on commit 6af9f23

Please sign in to comment.