From 16dbf17306476db3158ea632d25d427f8ea49983 Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Mon, 5 Aug 2024 09:55:58 +0200 Subject: [PATCH] chore: add linter commands tests (#5957) --- Makefile | 10 ++++- test/lib/linterCommandsTest.sh | 74 ++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100755 test/lib/linterCommandsTest.sh diff --git a/Makefile b/Makefile index c9d836ff..7d688660 100644 --- a/Makefile +++ b/Makefile @@ -281,7 +281,7 @@ lint-subset-files-enable-expensive-io-checks: ## Lint a small subset of files in $(SUPER_LINTER_TEST_CONTAINER_URL) .PHONY: test-lib -test-lib: test-globals-languages test-linter-rules test-build-file-list test-detect-files test-github-event test-setup-ssh test-validation test-output ## Test super-linter libs and globals +test-lib: test-globals-languages test-linter-rules test-build-file-list test-detect-files test-github-event test-setup-ssh test-validation test-output test-linter-commands ## Test super-linter libs and globals .PHONY: test-globals-languages test-globals-languages: ## Test globals/languages.sh @@ -348,6 +348,14 @@ test-output: ## Test output --entrypoint /tmp/lint/test/lib/outputTest.sh \ $(SUPER_LINTER_TEST_CONTAINER_URL) +.PHONY: test-linter-commands +test-linter-commands: ## Test linterCommands + docker run \ + -v "$(CURDIR):/tmp/lint" \ + -w /tmp/lint \ + --entrypoint /tmp/lint/test/lib/linterCommandsTest.sh \ + $(SUPER_LINTER_TEST_CONTAINER_URL) + # Run this test against a small directory because we're only interested in # loading default configuration files. The directory that we run super-linter # against should not be .github because that includes default linter rules. diff --git a/test/lib/linterCommandsTest.sh b/test/lib/linterCommandsTest.sh new file mode 100755 index 00000000..08d8ce45 --- /dev/null +++ b/test/lib/linterCommandsTest.sh @@ -0,0 +1,74 @@ +#!/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" + +# linterCommands.sh needs these + +# shellcheck source=/dev/null +source "lib/globals/languages.sh" +# shellcheck source=/dev/null +source "lib/globals/linterRules.sh" +# shellcheck source=/dev/null +source "lib/functions/linterRules.sh" +# shellcheck source=/dev/null +source "lib/functions/validation.sh" + +# Initialize the environment +# shellcheck disable=SC2034 +BASH_EXEC_IGNORE_LIBRARIES="false" +# shellcheck disable=SC2034 +GITHUB_WORKSPACE="$(pwd)" +# shellcheck disable=SC2034 +IGNORE_GITIGNORED_FILES="false" +LINTER_RULES_PATH="TEMPLATES" +# shellcheck disable=SC2034 +TYPESCRIPT_STANDARD_TSCONFIG_FILE=".github/linters/tsconfig.json" +# shellcheck disable=SC2034 +YAML_ERROR_ON_WARNING="false" +for LANGUAGE in "${LANGUAGE_ARRAY_FOR_LINTER_RULES[@]}"; do + GetLinterRules "${LANGUAGE}" "${LINTER_RULES_PATH}" +done +ValidateValidationVariables + +# The slim image might not have this variable defined +if [[ ! -v ARM_TTK_PSD1 ]]; then + ARM_TTK_PSD1="/usr/lib/microsoft/arm-ttk/arm-ttk.psd1" +fi + +# Source the file so we can load commands to compare them without redefining +# each command. We're not interested in the actual values of those commands, but +# only in how we eventually modify them. + +# shellcheck source=/dev/null +source "lib/functions/linterCommands.sh" + +function LinterCommandPresenceTest() { + local FUNCTION_NAME + FUNCTION_NAME="${FUNCNAME[0]}" + info "${FUNCTION_NAME} start" + + for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do + local LINTER_COMMAND_ARRAY_VARIABLE_NAME="LINTER_COMMANDS_ARRAY_${LANGUAGE}" + debug "Check if ${LINTER_COMMAND_ARRAY_VARIABLE_NAME} has at least one element" + local -n LINTER_COMMAND_ARRAY="${LINTER_COMMAND_ARRAY_VARIABLE_NAME}" + if [ ${#LINTER_COMMAND_ARRAY[@]} -eq 0 ]; then + fatal "LINTER_COMMAND_ARRAY for ${LANGUAGE} is empty." + else + debug "LINTER_COMMAND_ARRAY for ${LANGUAGE} has ${#LINTER_COMMAND_ARRAY[@]} elements: ${LINTER_COMMAND_ARRAY[*]}" + fi + unset -n LINTER_COMMAND_ARRAY + done + + notice "${FUNCTION_NAME} PASS" +} + +LinterCommandPresenceTest