From 091eaa71e334a000b4b411531b06bd0bd24f12eb Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Sat, 10 Feb 2024 10:16:31 +0100 Subject: [PATCH] feat: show error output when info is disabled (#5251) In case of linting errors, print stdout and stderr (if present) at the ERROR level if users set LOG_LEVEL to NOTICE to avoid failures without any explanation. --- Makefile | 8 +++++++- lib/functions/detectFiles.sh | 2 +- lib/functions/worker.sh | 21 +++++++++++++++++++-- lib/linter.sh | 24 +++++++++++++++++++++--- test/run-super-linter-tests.sh | 5 +++++ 5 files changed, 53 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 75a8f20d..d345e010 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ all: info docker test ## Run all targets. .PHONY: test -test: info validate-container-image-labels test-lib inspec lint-codebase test-default-config-files test-find lint-subset-files test-custom-ssl-cert test-non-default-workdir test-git-flags test-non-default-home-directory test-log-level test-linters ## Run the test suite +test: info validate-container-image-labels test-lib inspec lint-codebase test-default-config-files test-find lint-subset-files test-custom-ssl-cert test-non-default-workdir test-git-flags test-non-default-home-directory test-log-level test-linters-expect-failure-log-level-notice test-linters ## Run the test suite # if this session isn't interactive, then we don't want to allocate a # TTY, which would fail, but if it is interactive, we do want to attach @@ -316,6 +316,12 @@ test-log-level: ## Run a test to check if there are conflicts with the LOG_LEVEL $(SUPER_LINTER_TEST_CONTAINER_URL) \ "run_test_cases_log_level" +.phony: test-linters-expect-failure-log-level-notice +test-linters-expect-failure-log-level-notice: ## Run the linters test suite expecting failures with a LOG_LEVEL set to NOTICE + $(CURDIR)/test/run-super-linter-tests.sh \ + $(SUPER_LINTER_TEST_CONTAINER_URL) \ + "run_test_cases_expect_failure_notice_log" + .phony: build-dev-container-image build-dev-container-image: ## Build commit linter container image DOCKER_BUILDKIT=1 docker buildx build --load \ diff --git a/lib/functions/detectFiles.sh b/lib/functions/detectFiles.sh index 19483a52..7c28b674 100755 --- a/lib/functions/detectFiles.sh +++ b/lib/functions/detectFiles.sh @@ -318,7 +318,7 @@ function RunAdditionalInstalls() { local MYPY_CACHE_DIRECTORY_PATH MYPY_CACHE_DIRECTORY_PATH="${GITHUB_WORKSPACE}/.mypy_cache" debug "Create MyPy cache directory: ${MYPY_CACHE_DIRECTORY_PATH}" - mkdir -v "${MYPY_CACHE_DIRECTORY_PATH}" + mkdir -p "${MYPY_CACHE_DIRECTORY_PATH}" fi ############################### diff --git a/lib/functions/worker.sh b/lib/functions/worker.sh index 9527155d..76156884 100755 --- a/lib/functions/worker.sh +++ b/lib/functions/worker.sh @@ -193,7 +193,16 @@ function LintCodebase() { fi if [ -n "${STDOUT_LINTER}" ]; then - info "Command output for ${FILE_TYPE}:\n------\n${STDOUT_LINTER}\n------" + local STDOUT_LINTER_LOG_MESSAGE + STDOUT_LINTER_LOG_MESSAGE="Command output for ${FILE_TYPE}:\n------\n${STDOUT_LINTER}\n------" + info "${STDOUT_LINTER_LOG_MESSAGE}" + + if [ ${PARALLEL_COMMAND_RETURN_CODE} -ne 0 ]; then + local STDOUT_LINTER_FILE_PATH + STDOUT_LINTER_FILE_PATH="/tmp/super-linter-parallel-stdout-${FILE_TYPE}" + debug "Saving stdout for ${FILE_TYPE} to ${STDOUT_LINTER_FILE_PATH} in case we need it later" + printf '%s\n' "${STDOUT_LINTER_LOG_MESSAGE}" >"${STDOUT_LINTER_FILE_PATH}" + fi else debug "Stdout for ${FILE_TYPE} is empty" fi @@ -204,7 +213,15 @@ function LintCodebase() { fi if [ -n "${STDERR_LINTER}" ]; then - info "Stderr contents for ${FILE_TYPE}:\n------\n${STDERR_LINTER}\n------" + local STDERR_LINTER_LOG_MESSAGE + STDERR_LINTER_LOG_MESSAGE="Stderr contents for ${FILE_TYPE}:\n------\n${STDERR_LINTER}\n------" + info "${STDERR_LINTER_LOG_MESSAGE}" + if [ ${PARALLEL_COMMAND_RETURN_CODE} -ne 0 ]; then + local STDERR_LINTER_FILE_PATH + STDERR_LINTER_FILE_PATH="/tmp/super-linter-parallel-stderr-${FILE_TYPE}" + debug "Saving stderr for ${FILE_TYPE} to ${STDERR_LINTER_FILE_PATH} in case we need it later" + printf '%s\n' "${STDERR_LINTER_LOG_MESSAGE}" >"${STDERR_LINTER_FILE_PATH}" + fi else debug "Stderr for ${FILE_TYPE} is empty" fi diff --git a/lib/linter.sh b/lib/linter.sh index 2b5d1f43..df0aab0f 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -626,6 +626,25 @@ Footer() { if [[ ${ERROR_COUNTER} -ne 0 ]]; then error "Errors found in ${LANGUAGE}" + # Print output as error in case users disabled the INFO level so they + # get feedback + if [[ "${LOG_VERBOSE}" != "true" ]]; then + local STDOUT_LINTER_FILE_PATH + STDOUT_LINTER_FILE_PATH="/tmp/super-linter-parallel-stdout-${LANGUAGE}" + if [[ -e "${STDOUT_LINTER_FILE_PATH}" ]]; then + error "$(cat "${STDOUT_LINTER_FILE_PATH}")" + else + debug "Stdout output file path for ${LANGUAGE} (${STDOUT_LINTER_FILE_PATH}) doesn't exist" + fi + + local STDERR_LINTER_FILE_PATH + STDERR_LINTER_FILE_PATH="/tmp/super-linter-parallel-stderr-${LANGUAGE}" + if [[ -e "${STDERR_LINTER_FILE_PATH}" ]]; then + error "$(cat "${STDERR_LINTER_FILE_PATH}")" + else + debug "Stderr output file path for ${LANGUAGE} (${STDERR_LINTER_FILE_PATH}) doesn't exist" + fi + fi CallStatusAPI "${LANGUAGE}" "error" SUPER_LINTER_EXIT_CODE=1 debug "Setting super-linter exit code to ${SUPER_LINTER_EXIT_CODE} because there were errors for ${LANGUAGE}" @@ -687,13 +706,13 @@ cleanup() { local -ri EXIT_CODE=$? debug "Removing temporary files and directories" - rm -rfv \ + rm -rf \ "${GITHUB_WORKSPACE}/.mypy_cache" \ "${GITHUB_WORKSPACE}/logback.log" if [ "${SUPER_LINTER_COPIED_R_LINTER_RULES_FILE}" == "true" ]; then debug "Deleting ${R_RULES_FILE_PATH_IN_ROOT} because super-linter created it." - rm -rfv "${R_RULES_FILE_PATH_IN_ROOT}" + rm -rf "${R_RULES_FILE_PATH_IN_ROOT}" fi # Define this variable here so we can rely on it as soon as possible @@ -703,7 +722,6 @@ cleanup() { debug "Moving log file from ${LOG_TEMP} to ${LOG_FILE_PATH}" mv \ --force \ - --verbose \ "${LOG_TEMP}" "${LOG_FILE_PATH}" else debug "Skipping the moving of the log file from ${LOG_TEMP} to ${LOG_FILE_PATH}" diff --git a/test/run-super-linter-tests.sh b/test/run-super-linter-tests.sh index 4d0fd5e4..929dba84 100755 --- a/test/run-super-linter-tests.sh +++ b/test/run-super-linter-tests.sh @@ -23,6 +23,11 @@ run_test_cases_log_level() { LOG_LEVEL="NOTICE" } +run_test_cases_expect_failure_notice_log() { + run_test_cases_expect_failure + LOG_LEVEL="NOTICE" +} + run_test_cases_non_default_home() { run_test_cases_expect_success COMMAND_TO_RUN+=(-e HOME=/tmp)