lint/test/testUtils.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

125 lines
3 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"
# TODO: use TEST_CASE_FOLDER instead of redefining this after we extract the
# initialization of TEST_CASE_FOLDER from linter.sh
# shellcheck disable=SC2034
LINTERS_TEST_CASE_DIRECTORY="test/linters"
# shellcheck disable=SC2034
LANGUAGES_WITH_FIX_MODE=(
"ANSIBLE"
"CLANG_FORMAT"
"CSHARP"
"CSS"
"ENV"
"GO_MODULES"
"GO"
"GOOGLE_JAVA_FORMAT"
"GROOVY"
"JAVASCRIPT_ES"
"JAVASCRIPT_PRETTIER"
"JAVASCRIPT_STANDARD"
"JSON"
"JSONC"
"JSX"
"MARKDOWN"
"NATURAL_LANGUAGE"
"POWERSHELL"
"PROTOBUF"
"PYTHON_BLACK"
"PYTHON_ISORT"
"PYTHON_RUFF"
"RUBY"
"RUST_2015"
"RUST_2018"
"RUST_2021"
"RUST_CLIPPY"
"SCALAFMT"
"SHELL_SHFMT"
"SNAKEMAKE_SNAKEFMT"
"SQLFLUFF"
"TERRAFORM_FMT"
"TSX"
"TYPESCRIPT_ES"
"TYPESCRIPT_PRETTIER"
"TYPESCRIPT_STANDARD"
)
# TODO: extract this list from linter.sh (see REMOVE_ARRAY) instead of
# redefining it here
# shellcheck disable=SC2034
LANGUAGES_NOT_IN_SLIM_IMAGE=(
"ARM"
"CSHARP"
"POWERSHELL"
"RUST_2015"
"RUST_2018"
"RUST_2021"
"RUST_CLIPPY"
)
function AssertArraysElementsContentMatch() {
local ARRAY_1_VARIABLE_NAME="${1}"
local ARRAY_2_VARIABLE_NAME="${2}"
local -n ARRAY_1="${ARRAY_1_VARIABLE_NAME}"
local -n ARRAY_2="${ARRAY_2_VARIABLE_NAME}"
if [[ "${ARRAY_1[*]}" == "${ARRAY_2[*]}" ]]; then
debug "${ARRAY_1_VARIABLE_NAME} (${ARRAY_1[*]}) matches the expected value: ${ARRAY_2[*]}"
RETURN_CODE=0
else
error "${ARRAY_1_VARIABLE_NAME} (${ARRAY_1[*]}) doesn't match the expected value: ${ARRAY_2[*]}"
RETURN_CODE=1
fi
unset -n ARRAY_1
unset -n ARRAY_2
return ${RETURN_CODE}
}
function CheckUnexpectedGitChanges() {
local GIT_REPOSITORY_PATH="${1}"
# Check if there are unexpected changes in the working directory:
# - Unstaged changes
# - Changes that are staged but not committed
# - Untracked files and directories
if ! git -C "${GIT_REPOSITORY_PATH}" diff --exit-code --quiet ||
! git -C "${GIT_REPOSITORY_PATH}" diff --cached --exit-code --quiet ||
! git -C "${GIT_REPOSITORY_PATH}" ls-files --others --exclude-standard --directory; then
echo "There are unexpected changes in the working directory of the ${GIT_REPOSITORY_PATH} Git repository."
git -C "${GIT_REPOSITORY_PATH}" status
return 1
fi
}
AssertFileContentsMatch() {
local FILE_1_PATH="${1}"
local FILE_2_PATH="${2}"
if diff -r "${FILE_1_PATH}" "${FILE_2_PATH}"; then
echo "${FILE_1_PATH} contents match with ${FILE_2_PATH} contents"
return 0
else
echo "${FILE_1_PATH} contents don't match with ${FILE_2_PATH} contents"
return 1
fi
}
IsLanguageInSlimImage() {
local LANGUAGE="${1}"
if [[ " ${LANGUAGES_NOT_IN_SLIM_IMAGE[*]} " =~ [[:space:]]${LANGUAGE}[[:space:]] ]]; then
debug "${LANGUAGE} is not available in the Super-linter slim image"
return 1
else
debug "${LANGUAGE} is available in the Super-linter slim image"
return 0
fi
}