From ef57e132e136ab9991a0cc1517236e827d27baf9 Mon Sep 17 00:00:00 2001 From: Marco Ferrari Date: Tue, 30 Jul 2024 08:35:14 +0200 Subject: [PATCH] fix: avoid duplicated content in summary (#5939) - Remove the previous summary file is present to avoid showing stale contents from old summaries. - Extract the logic to write summary heading and lines to dedicated functions in output.sh to make it easier adding new formats in the future. --- Makefile | 11 +- README.md | 2 + lib/functions/output.sh | 40 ++++++ lib/linter.sh | 26 ++-- .../table/expected-summary-footer-failure.md | 3 + .../table/expected-summary-footer-success.md | 3 + .../table/expected-summary-heading.md | 4 + .../table/expected-summary-line-failure.md | 2 + .../table/expected-summary-line-success.md | 2 + ...mmary-test-linters-expect-failure-slim.md} | 0 ...y-test-linters-expect-failure-standard.md} | 0 ...mmary-test-linters-expect-success-slim.md} | 0 ...y-test-linters-expect-success-standard.md} | 0 .../super-linter/controls/super_linter.rb | 1 + test/lib/outputTest.sh | 114 ++++++++++++++++++ test/run-super-linter-tests.sh | 4 +- 16 files changed, 193 insertions(+), 19 deletions(-) create mode 100755 lib/functions/output.sh create mode 100644 test/data/super-linter-summary/markdown/table/expected-summary-footer-failure.md create mode 100644 test/data/super-linter-summary/markdown/table/expected-summary-footer-success.md create mode 100644 test/data/super-linter-summary/markdown/table/expected-summary-heading.md create mode 100644 test/data/super-linter-summary/markdown/table/expected-summary-line-failure.md create mode 100644 test/data/super-linter-summary/markdown/table/expected-summary-line-success.md rename test/data/{github-actions-step-summary/expected-step-summary-test-linters-expect-failure-slim.md => super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-slim.md} (100%) rename test/data/{github-actions-step-summary/expected-step-summary-test-linters-expect-failure-standard.md => super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-standard.md} (100%) rename test/data/{github-actions-step-summary/expected-step-summary-test-linters-expect-success-slim.md => super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-slim.md} (100%) rename test/data/{github-actions-step-summary/expected-step-summary-test-linters-expect-success-standard.md => super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-standard.md} (100%) create mode 100755 test/lib/outputTest.sh diff --git a/Makefile b/Makefile index cefdedfb..31b7c57a 100644 --- a/Makefile +++ b/Makefile @@ -238,6 +238,7 @@ lint-codebase: ## Lint the entire codebase -e FILTER_REGEX_EXCLUDE=".*(/test/linters/|CHANGELOG.md).*" \ -e GITLEAKS_CONFIG_FILE=".gitleaks-ignore-tests.toml" \ -e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES="default.json,hoge.json" \ + -e SAVE_SUPER_LINTER_SUMMARY=true \ -e VALIDATE_ALL_CODEBASE=true \ -v "$(CURDIR):/tmp/lint" \ $(SUPER_LINTER_TEST_CONTAINER_URL) @@ -280,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-build-file-list test-detect-files test-github-event test-setup-ssh test-validation ## Test super-linter +test-lib: test-build-file-list test-detect-files test-github-event test-setup-ssh test-validation test-output ## Test super-linter .PHONY: test-build-file-list test-build-file-list: ## Test buildFileList @@ -323,6 +324,14 @@ test-validation: ## Test validation --entrypoint /tmp/lint/test/lib/validationTest.sh \ $(SUPER_LINTER_TEST_CONTAINER_URL) +.PHONY: test-output +test-output: ## Test output + docker run \ + -v "$(CURDIR):/tmp/lint" \ + -w /tmp/lint \ + --entrypoint /tmp/lint/test/lib/outputTest.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/README.md b/README.md index dbf3226e..c2b41336 100644 --- a/README.md +++ b/README.md @@ -476,6 +476,8 @@ The summary is in Markdown format. Super-linter supports the following formats: - Table (default) +The summary output of previous Super-linter runs is not preserved. + ### Super-linter outputs If you set `SAVE_SUPER_LINTER_OUTPUT` to `true`, Super-linter saves its output diff --git a/lib/functions/output.sh b/lib/functions/output.sh new file mode 100755 index 00000000..ae9939dd --- /dev/null +++ b/lib/functions/output.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +WriteSummaryHeader() { + local SUPER_LINTER_SUMMARY_OUTPUT_PATH="${1}" + + { + echo "# Super-linter summary" + echo "" + echo "| Language | Validation result |" + echo "| -----------------------|-------------------|" + } >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" +} + +WriteSummaryLineSuccess() { + local SUPER_LINTER_SUMMARY_OUTPUT_PATH="${1}" + local LANGUAGE_NAME="${2}" + echo "| ${LANGUAGE_NAME} | Pass ✅ |" >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" +} + +WriteSummaryLineFailure() { + local SUPER_LINTER_SUMMARY_OUTPUT_PATH="${1}" + local LANGUAGE_NAME="${2}" + echo "| ${LANGUAGE_NAME} | Fail ❌ |" >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" +} + +WriteSummaryFooterSuccess() { + local SUPER_LINTER_SUMMARY_OUTPUT_PATH="${1}" + { + echo "" + echo "All files and directories linted successfully" + } >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" +} + +WriteSummaryFooterFailure() { + local SUPER_LINTER_SUMMARY_OUTPUT_PATH="${1}" + { + echo "" + echo "Super-linter detected linting errors" + } >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" +} diff --git a/lib/linter.sh b/lib/linter.sh index a9d67000..eb6d23c0 100755 --- a/lib/linter.sh +++ b/lib/linter.sh @@ -31,6 +31,8 @@ source /action/lib/functions/setupSSH.sh # Source the function script(s) source /action/lib/functions/githubEvent.sh # shellcheck source=/dev/null source /action/lib/functions/githubDomain.sh +# shellcheck source=/dev/null +source /action/lib/functions/output.sh if ! ValidateGitHubUrls; then fatal "GitHub URLs failed validation" @@ -565,12 +567,7 @@ Footer() { if [[ "${SAVE_SUPER_LINTER_SUMMARY}" == "true" ]]; then debug "Saving Super-linter summary to ${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" - { - echo "# Super-linter summary" - echo "" - echo "| Language | Validation result |" - echo "| -----------------------|-------------------|" - } >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" + WriteSummaryHeader "${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" fi for LANGUAGE in "${LANGUAGE_ARRAY[@]}"; do @@ -591,7 +588,7 @@ Footer() { error "Errors found in ${LANGUAGE}" if [[ "${SAVE_SUPER_LINTER_SUMMARY}" == "true" ]]; then - echo "| ${LANGUAGE} | Fail ❌ |" >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" + WriteSummaryLineFailure "${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" "${LANGUAGE}" fi # Print output as error in case users disabled the INFO level so they @@ -619,7 +616,7 @@ Footer() { elif [[ ${ERROR_COUNTER} -eq 0 ]]; then notice "Successfully linted ${LANGUAGE}" if [[ "${SAVE_SUPER_LINTER_SUMMARY}" == "true" ]]; then - echo "| ${LANGUAGE} | Pass ✅ |" >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" + WriteSummaryLineSuccess "${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" "${LANGUAGE}" fi CallStatusAPI "${LANGUAGE}" "success" ANY_LINTER_SUCCESS="true" @@ -641,18 +638,12 @@ Footer() { if [[ ${SUPER_LINTER_EXIT_CODE} -eq 0 ]]; then notice "All files and directories linted successfully" if [[ "${SAVE_SUPER_LINTER_SUMMARY}" == "true" ]]; then - { - echo "" - echo "All files and directories linted successfully" - } >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" + WriteSummaryFooterSuccess "${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" fi else error "Super-linter detected linting errors" if [[ "${SAVE_SUPER_LINTER_SUMMARY}" == "true" ]]; then - { - echo "" - echo "Super-linter detected linting errors" - } >>"${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" + WriteSummaryFooterFailure "${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" fi fi @@ -820,6 +811,9 @@ if [[ "${SAVE_SUPER_LINTER_OUTPUT}" = "true" ]] || fi if [[ "${SAVE_SUPER_LINTER_SUMMARY}" == "true" ]]; then + debug "Remove eventual ${SUPER_LINTER_SUMMARY_OUTPUT_PATH} leftover" + rm -f "${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" + debug "Ensuring that ${SUPER_LINTER_SUMMARY_OUTPUT_PATH} exists." if ! touch "${SUPER_LINTER_SUMMARY_OUTPUT_PATH}"; then fatal "Cannot create Super-linter summary file: ${SUPER_LINTER_SUMMARY_OUTPUT_PATH}" diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-footer-failure.md b/test/data/super-linter-summary/markdown/table/expected-summary-footer-failure.md new file mode 100644 index 00000000..361fd336 --- /dev/null +++ b/test/data/super-linter-summary/markdown/table/expected-summary-footer-failure.md @@ -0,0 +1,3 @@ + + +Super-linter detected linting errors diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-footer-success.md b/test/data/super-linter-summary/markdown/table/expected-summary-footer-success.md new file mode 100644 index 00000000..13bfcf83 --- /dev/null +++ b/test/data/super-linter-summary/markdown/table/expected-summary-footer-success.md @@ -0,0 +1,3 @@ + + +All files and directories linted successfully diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-heading.md b/test/data/super-linter-summary/markdown/table/expected-summary-heading.md new file mode 100644 index 00000000..0cf1217f --- /dev/null +++ b/test/data/super-linter-summary/markdown/table/expected-summary-heading.md @@ -0,0 +1,4 @@ +# Super-linter summary + +| Language | Validation result | +| -----------------------|-------------------| diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-line-failure.md b/test/data/super-linter-summary/markdown/table/expected-summary-line-failure.md new file mode 100644 index 00000000..cd921854 --- /dev/null +++ b/test/data/super-linter-summary/markdown/table/expected-summary-line-failure.md @@ -0,0 +1,2 @@ + +| Test Language | Fail ❌ | diff --git a/test/data/super-linter-summary/markdown/table/expected-summary-line-success.md b/test/data/super-linter-summary/markdown/table/expected-summary-line-success.md new file mode 100644 index 00000000..fc19a3c7 --- /dev/null +++ b/test/data/super-linter-summary/markdown/table/expected-summary-line-success.md @@ -0,0 +1,2 @@ + +| Test Language | Pass ✅ | diff --git a/test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-failure-slim.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-slim.md similarity index 100% rename from test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-failure-slim.md rename to test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-slim.md diff --git a/test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-failure-standard.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-standard.md similarity index 100% rename from test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-failure-standard.md rename to test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-failure-standard.md diff --git a/test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-success-slim.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-slim.md similarity index 100% rename from test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-success-slim.md rename to test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-slim.md diff --git a/test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-success-standard.md b/test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-standard.md similarity index 100% rename from test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-success-standard.md rename to test/data/super-linter-summary/markdown/table/expected-summary-test-linters-expect-success-standard.md diff --git a/test/inspec/super-linter/controls/super_linter.rb b/test/inspec/super-linter/controls/super_linter.rb index 7a670fd2..a04a5f3b 100644 --- a/test/inspec/super-linter/controls/super_linter.rb +++ b/test/inspec/super-linter/controls/super_linter.rb @@ -466,6 +466,7 @@ control "super-linter-validate-files" do "/action/lib/functions/linterRules.sh", version_file_path, "/action/lib/functions/log.sh", + "/action/lib/functions/output.sh", "/action/lib/functions/possum.sh", "/action/lib/functions/updateSSL.sh", "/action/lib/functions/validation.sh", diff --git a/test/lib/outputTest.sh b/test/lib/outputTest.sh new file mode 100755 index 00000000..39cab465 --- /dev/null +++ b/test/lib/outputTest.sh @@ -0,0 +1,114 @@ +#!/usr/bin/env bash +set -o errexit +set -o nounset +set -o pipefail + +# shellcheck disable=SC2034 +CREATE_LOG_FILE=false +# 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/output.sh" + +TEMP_WORKSPACE="$(pwd)/super-linter-output" + +function InitWorkspace() { + CleanupWorkspace + mkdir -p "${TEMP_WORKSPACE}" +} + +function CleanupWorkspace() { + rm -rf "${TEMP_WORKSPACE}" +} + +function CheckIfFileDiff() { + local INPUT_FILE="${1}" + local EXPECTED_FILE="${2}" + # Remove eventual HTML comments from the expected file because we use them to disable certain linter rules + if ! diff "${INPUT_FILE}" <(grep -vE '^\s*