superlint/test/lib/linterRulesTest.sh
Marco Ferrari 91dc6d7234
fix: add missing fix mode options and test cases (#5987)
- Add missing fix mode options for: CLANG_FORMAT, ENV,
  GOOGLE_JAVA_FORMAT, NATURAL_LANGUAGE, PYTHON_ISORT, RUST_CLIPPY.
- Refactor linter tests to make them shorter because there's no need to
  have big test files.
- Refactor 'bad' linter tests for linters that support fix mode so they
  contain only automatically fixable issues. This is needed to avoid
  adding another set of 'bad' linter tests for fix mode.
- Provide configuration files for linters that support fix mode and for
  which the default configuration is not suitable to enable fix mode:
  ansible-lint, ESLint, golangci-lint.
- Add a test case for linter commands options for linters that support
  fix mode, to ensure that fix mode and check-only mode options have
  been defined.
- Refactor the fix mode test to check if linters actually applied
  modifications to files.
- Update documentation about adding test cases for linters that support
  fix mode.
- Don't exit with a fatal error if VALIDATE_xxx is false when testing
  fix mode because not all linters support fix mode. To enable this, set
  the new FIX_MODE_TEST_CASE_RUN variable to true.
2024-08-12 12:31:38 +02:00

148 lines
4.5 KiB
Bash
Executable file

#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# Default log level
# shellcheck disable=SC2034
LOG_LEVEL="DEBUG"
# shellcheck source=/dev/null
source "lib/functions/log.sh"
# shellcheck source=/dev/null
source "lib/functions/detectFiles.sh"
# shellcheck source=/dev/null
source "lib/globals/linterRules.sh"
TEST_LANGUAGE_NAME="TEST_LANGUAGE"
TEST_LANGUAGE_NAME_WITHOUT_RULES="TEST_LANGUAGE_WITHOUT_RULES"
LANGUAGE_ARRAY=("${TEST_LANGUAGE_NAME}" "${TEST_LANGUAGE_NAME_WITHOUT_RULES}")
# shellcheck source=/dev/null
source "lib/functions/linterRules.sh"
# shellcheck disable=SC2034
GITHUB_WORKSPACE="$(pwd)"
function GetLinterRulesTest() {
local FUNCTION_NAME
FUNCTION_NAME="${FUNCNAME[0]}"
info "${FUNCTION_NAME} start"
LinterRulesLocation
DEFAULT_RULES_LOCATION="${DEFAULT_RULES_LOCATION:-"TEMPLATES"}"
# Use an existing configuration file. Can be anything inside
# DEFAULT_RULES_LOCATION
TEST_LANGUAGE_FILE_NAME="${TEST_LANGUAGE_FILE_NAME:-".eslintrc.yml"}"
for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do
debug "Loading rules for ${LANGUAGE}..."
GetLinterRules "${LANGUAGE}" "${DEFAULT_RULES_LOCATION}"
done
local EXPECTED_TEST_LANGUAGE_LINTER_RULES="${GITHUB_WORKSPACE}"
if [[ -n "${LINTER_RULES_PATH:-}" ]]; then
EXPECTED_TEST_LANGUAGE_LINTER_RULES="${EXPECTED_TEST_LANGUAGE_LINTER_RULES}/${LINTER_RULES_PATH}"
fi
EXPECTED_TEST_LANGUAGE_LINTER_RULES="${EXPECTED_TEST_LANGUAGE_LINTER_RULES}/${TEST_LANGUAGE_FILE_NAME}"
if [[ "${TEST_LANGUAGE_LINTER_RULES}" == "${EXPECTED_TEST_LANGUAGE_LINTER_RULES}" ]]; then
debug "TEST_LANGUAGE_LINTER_RULES (${TEST_LANGUAGE_LINTER_RULES}) matches the expected value (${EXPECTED_TEST_LANGUAGE_LINTER_RULES})"
else
fatal "TEST_LANGUAGE_LINTER_RULES (${TEST_LANGUAGE_LINTER_RULES}) doesn't match the expected value (${EXPECTED_TEST_LANGUAGE_LINTER_RULES})"
fi
if [[ -z "${TEST_LANGUAGE_WITHOUT_RULES_LINTER_RULES:-}" ]]; then
debug "TEST_LANGUAGE_WITHOUT_RULES_LINTER_RULES is not set as expected"
else
fatal "TEST_LANGUAGE_WITHOUT_RULES_LINTER_RULES shouldn't be set"
fi
unset TEST_LANGUAGE_LINTER_RULES
unset TEST_LANGUAGE_WITHOUT_RULES_LINTER_RULES
unset EXPECTED_TEST_LANGUAGE_LINTER_RULES
notice "${FUNCTION_NAME} PASS"
}
function GetLinterRulesEmptyDotRulesPathTest() {
local FUNCTION_NAME
FUNCTION_NAME="${FUNCNAME[0]}"
info "${FUNCTION_NAME} start"
# shellcheck disable=SC2034
LINTER_RULES_PATH="."
TEST_LANGUAGE_FILE_NAME="README.md"
DEFAULT_RULES_LOCATION="$(pwd)"
GetLinterRulesTest
unset LINTER_RULES_PATH
unset TEST_LANGUAGE_FILE_NAME
notice "${FUNCTION_NAME} PASS"
}
function GetLinterRulesEmptyRootRulesPathTest() {
local FUNCTION_NAME
FUNCTION_NAME="${FUNCNAME[0]}"
info "${FUNCTION_NAME} start"
# shellcheck disable=SC2034
LINTER_RULES_PATH="."
TEST_LANGUAGE_FILE_NAME="README.md"
DEFAULT_RULES_LOCATION="$(pwd)"
GetLinterRulesTest
unset LINTER_RULES_PATH
unset TEST_LANGUAGE_FILE_NAME
notice "${FUNCTION_NAME} PASS"
}
function LinterRulesVariablesExportTest() {
local FUNCTION_NAME
FUNCTION_NAME="${FUNCNAME[0]}"
info "${FUNCTION_NAME} start"
DEFAULT_RULES_LOCATION="TEMPLATES"
TEST_LANGUAGE_FILE_NAME=".eslintrc.yml"
# shellcheck source=/dev/null
source "lib/globals/linterRules.sh"
for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do
debug "Loading rules for ${LANGUAGE}..."
GetLinterRules "${LANGUAGE}" "${DEFAULT_RULES_LOCATION}"
done
for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do
debug "Verify that ${LANGUAGE} configuration file variable is exported"
if [[ "${LANGUAGE}" == "${TEST_LANGUAGE_NAME_WITHOUT_RULES}" ]]; then
debug "${LANGUAGE} doesn't have linter configuration file variable. Skipping export test."
continue
fi
local LANGUAGE_LINTER_RULES_VARIABLE_NAME="${LANGUAGE}_LINTER_RULES"
if [[ ! -v "${LANGUAGE_LINTER_RULES_VARIABLE_NAME}" ]]; then
fatal "${LANGUAGE_LINTER_RULES_VARIABLE_NAME} is not defined"
fi
local -n LANGUAGE_LINTER_RULES
LANGUAGE_LINTER_RULES="${LANGUAGE_LINTER_RULES_VARIABLE_NAME}"
debug "LANGUAGE_LINTER_RULES (${LANGUAGE}) variable attributes: ${LANGUAGE_LINTER_RULES@a}"
if [[ "${LANGUAGE_LINTER_RULES@a}" == *x* ]]; then
info "LANGUAGE_LINTER_RULES for ${LANGUAGE} is exported as expected"
else
fatal "LANGUAGE_LINTER_RULES for ${LANGUAGE} should have been exported"
fi
unset -n LANGUAGE_LINTER_RULES
done
notice "${FUNCTION_NAME} PASS"
}
GetLinterRulesTest
GetLinterRulesEmptyDotRulesPathTest
GetLinterRulesEmptyRootRulesPathTest
LinterRulesVariablesExportTest