superlint/test/testUtils.sh
Marco Ferrari 6fdc091361
feat: local fix mode (#5978)
Certain linters and formatters support fixing linting and formatting
issues (fix mode). Before this change, Super-linter runs linters and
formatters in a mode that doesn't modify the source code in any way
(check only mode).

With this change, Super-linter supports running linters and formatters
in fix mode if explicitly requested by the configuration. If the
configuration includes a variable named FIX_<language_name>,
Super-linters modifies the command to run the linter or formatter for
<language_name> to enable fix mode.

The modifications to the linter or formatter command that Super-linter
applies depend on what is the default for a particular linter: it either
removes or adds options to the command to run the linter or formatter.
2024-08-07 15:36:16 +02:00

44 lines
1.4 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"
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
}