2024-01-30 14:24:55 -05:00
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
SUPER_LINTER_TEST_CONTAINER_URL = " ${ 1 } "
TEST_FUNCTION_NAME = " ${ 2 } "
2024-07-11 04:01:11 -04:00
SUPER_LINTER_CONTAINER_IMAGE_TYPE = " ${ 3 } "
echo " Super-linter container image type: ${ SUPER_LINTER_CONTAINER_IMAGE_TYPE } "
2024-01-30 14:24:55 -05:00
2024-04-18 02:48:55 -04:00
DEFAULT_BRANCH = "main"
2024-04-20 05:18:14 -04:00
COMMAND_TO_RUN = ( docker run -t -e DEFAULT_BRANCH = " ${ DEFAULT_BRANCH } " -e ENABLE_GITHUB_ACTIONS_GROUP_TITLE = true )
2024-04-18 02:48:55 -04:00
2024-04-30 11:58:11 -04:00
ignore_test_cases( ) {
COMMAND_TO_RUN += ( -e FILTER_REGEX_EXCLUDE = ".*(/test/linters/|CHANGELOG.md).*" )
}
2024-04-18 02:48:55 -04:00
configure_linters_for_test_cases( ) {
COMMAND_TO_RUN += ( -e TEST_CASE_RUN = true -e JSCPD_CONFIG_FILE = ".jscpd-test-linters.json" -e RENOVATE_SHAREABLE_CONFIG_PRESET_FILE_NAMES = "default.json,hoge.json" -e TYPESCRIPT_STANDARD_TSCONFIG_FILE = ".github/linters/tsconfig.json" )
}
2024-01-30 14:24:55 -05:00
run_test_cases_expect_failure( ) {
2024-04-18 02:48:55 -04:00
configure_linters_for_test_cases
2024-01-30 14:24:55 -05:00
COMMAND_TO_RUN += ( -e ANSIBLE_DIRECTORY = "/test/linters/ansible/bad" -e CHECKOV_FILE_NAME = ".checkov-test-linters-failure.yaml" -e FILTER_REGEX_INCLUDE = ".*bad.*" )
EXPECTED_EXIT_CODE = 1
2024-07-11 04:01:11 -04:00
EXPECTED_SUPER_LINTER_STEP_SUMMARY_FILE_PATH = " test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-failure- ${ SUPER_LINTER_CONTAINER_IMAGE_TYPE } .md "
2024-01-30 14:24:55 -05:00
}
run_test_cases_expect_success( ) {
2024-04-18 02:48:55 -04:00
configure_linters_for_test_cases
2024-01-30 14:24:55 -05:00
COMMAND_TO_RUN += ( -e ANSIBLE_DIRECTORY = "/test/linters/ansible/good" -e CHECKOV_FILE_NAME = ".checkov-test-linters-success.yaml" -e FILTER_REGEX_INCLUDE = ".*good.*" )
2024-07-11 04:01:11 -04:00
EXPECTED_SUPER_LINTER_STEP_SUMMARY_FILE_PATH = " test/data/github-actions-step-summary/expected-step-summary-test-linters-expect-success- ${ SUPER_LINTER_CONTAINER_IMAGE_TYPE } .md "
2024-01-30 14:24:55 -05:00
}
2024-02-09 13:43:58 -05:00
run_test_cases_log_level( ) {
run_test_cases_expect_success
2024-04-20 05:18:14 -04:00
CREATE_LOG_FILE = "true"
2024-02-09 13:43:58 -05:00
LOG_LEVEL = "NOTICE"
}
2024-02-10 04:16:31 -05:00
run_test_cases_expect_failure_notice_log( ) {
run_test_cases_expect_failure
LOG_LEVEL = "NOTICE"
}
2024-02-09 17:57:01 -05:00
run_test_cases_non_default_home( ) {
run_test_cases_expect_success
COMMAND_TO_RUN += ( -e HOME = /tmp)
}
2024-02-27 13:17:22 -05:00
run_test_case_bash_exec_library_expect_failure( ) {
run_test_cases_expect_failure
COMMAND_TO_RUN += ( -e BASH_EXEC_IGNORE_LIBRARIES = "true" )
}
run_test_case_bash_exec_library_expect_success( ) {
run_test_cases_expect_success
COMMAND_TO_RUN += ( -e BASH_EXEC_IGNORE_LIBRARIES = "true" )
}
2024-04-18 02:48:55 -04:00
run_test_case_git_initial_commit( ) {
local GIT_REPOSITORY_PATH
GIT_REPOSITORY_PATH = " $( mktemp -d) "
# shellcheck disable=SC2064 # Once the path is set, we don't expect it to change
trap " rm -fr ' ${ GIT_REPOSITORY_PATH } ' " EXIT
git -C " ${ GIT_REPOSITORY_PATH } " init --initial-branch= " ${ DEFAULT_BRANCH } "
git -C " ${ GIT_REPOSITORY_PATH } " config user.name "Super-linter Test"
git -C " ${ GIT_REPOSITORY_PATH } " config user.email "super-linter-test@example.com"
cp -v test/data/github-event/github-event-push.json " ${ GIT_REPOSITORY_PATH } / "
git -C " ${ GIT_REPOSITORY_PATH } " add .
git -C " ${ GIT_REPOSITORY_PATH } " commit -m "feat: initial commit"
local TEST_GITHUB_SHA
TEST_GITHUB_SHA = " $( git -C " ${ GIT_REPOSITORY_PATH } " rev-parse HEAD) "
RUN_LOCAL = false
SUPER_LINTER_WORKSPACE = " ${ GIT_REPOSITORY_PATH } "
COMMAND_TO_RUN += ( -e GITHUB_WORKSPACE = "/tmp/lint" )
COMMAND_TO_RUN += ( -e GITHUB_EVENT_NAME = "push" )
COMMAND_TO_RUN += ( -e GITHUB_EVENT_PATH = "/tmp/lint/github-event-push.json" )
COMMAND_TO_RUN += ( -e GITHUB_SHA = " ${ TEST_GITHUB_SHA } " )
COMMAND_TO_RUN += ( -e MULTI_STATUS = false )
COMMAND_TO_RUN += ( -e VALIDATE_ALL_CODEBASE = false )
COMMAND_TO_RUN += ( -e VALIDATE_JSON = true )
}
2024-04-30 11:58:11 -04:00
run_test_case_use_find_and_ignore_gitignored_files( ) {
ignore_test_cases
COMMAND_TO_RUN += ( -e IGNORE_GITIGNORED_FILES = true )
COMMAND_TO_RUN += ( -e USE_FIND_ALGORITHM = true )
}
2024-07-01 08:50:52 -04:00
run_test_cases_save_super_linter_output( ) {
run_test_cases_expect_success
SAVE_SUPER_LINTER_OUTPUT = "true"
}
run_test_cases_save_super_linter_output_custom_path( ) {
run_test_cases_save_super_linter_output
SUPER_LINTER_OUTPUT_DIRECTORY_NAME = "custom-super-linter-output-directory-name"
}
2024-01-30 14:24:55 -05:00
# Run the test setup function
${ TEST_FUNCTION_NAME }
2024-04-20 05:18:14 -04:00
CREATE_LOG_FILE = " ${ CREATE_LOG_FILE :- false } "
2024-07-01 08:50:52 -04:00
SAVE_SUPER_LINTER_OUTPUT = " ${ SAVE_SUPER_LINTER_OUTPUT :- false } "
if [ -n " ${ SUPER_LINTER_OUTPUT_DIRECTORY_NAME :- } " ] ; then
COMMAND_TO_RUN += ( -e SUPER_LINTER_OUTPUT_DIRECTORY_NAME = " ${ SUPER_LINTER_OUTPUT_DIRECTORY_NAME } " )
fi
2024-04-20 05:18:14 -04:00
COMMAND_TO_RUN += ( -e CREATE_LOG_FILE = " ${ CREATE_LOG_FILE } " )
2024-02-09 13:43:58 -05:00
COMMAND_TO_RUN += ( -e LOG_LEVEL = " ${ LOG_LEVEL :- "DEBUG" } " )
2024-04-18 02:48:55 -04:00
COMMAND_TO_RUN += ( -e RUN_LOCAL = " ${ RUN_LOCAL :- true } " )
2024-07-01 08:50:52 -04:00
COMMAND_TO_RUN += ( -e SAVE_SUPER_LINTER_OUTPUT = " ${ SAVE_SUPER_LINTER_OUTPUT } " )
2024-04-18 02:48:55 -04:00
COMMAND_TO_RUN += ( -v " ${ SUPER_LINTER_WORKSPACE :- $( pwd ) } :/tmp/lint " )
2024-07-11 04:01:11 -04:00
if [ -n " ${ EXPECTED_SUPER_LINTER_STEP_SUMMARY_FILE_PATH :- } " ] ; then
echo " Expected Super-linter step summary file path: ${ EXPECTED_SUPER_LINTER_STEP_SUMMARY_FILE_PATH } "
SUPER_LINTER_STEP_SUMMARY_FILE = " $( pwd ) /super-linter-github-actions-step-summary-output.md "
echo " Create Super-linter step summary file: ${ SUPER_LINTER_STEP_SUMMARY_FILE } "
# Remove eventual leftovers from previous tests
rm --force " ${ SUPER_LINTER_STEP_SUMMARY_FILE } "
touch " ${ SUPER_LINTER_STEP_SUMMARY_FILE } "
SUPER_LINTER_STEP_SUMMARY_FILE_INSIDE_CONTAINER = " /tmp/lint/ $( basename " ${ SUPER_LINTER_STEP_SUMMARY_FILE } " ) "
COMMAND_TO_RUN += ( -e GITHUB_STEP_SUMMARY = " ${ SUPER_LINTER_STEP_SUMMARY_FILE_INSIDE_CONTAINER } " )
ENABLE_GITHUB_ACTIONS_STEP_SUMMARY = "true"
fi
COMMAND_TO_RUN += ( -e ENABLE_GITHUB_ACTIONS_STEP_SUMMARY = " ${ ENABLE_GITHUB_ACTIONS_STEP_SUMMARY :- "false" } " )
2024-01-30 14:24:55 -05:00
COMMAND_TO_RUN += ( " ${ SUPER_LINTER_TEST_CONTAINER_URL } " )
declare -i EXPECTED_EXIT_CODE
EXPECTED_EXIT_CODE = ${ EXPECTED_EXIT_CODE :- 0 }
if [ ${ EXPECTED_EXIT_CODE } -ne 0 ] ; then
echo " Disable failures on error because the expected exit code is ${ EXPECTED_EXIT_CODE } "
set +o errexit
fi
echo " Command to run: ${ COMMAND_TO_RUN [*] } "
" ${ COMMAND_TO_RUN [@] } "
SUPER_LINTER_EXIT_CODE = $?
# Enable the errexit option in case we disabled it
set -o errexit
echo " Super-linter exit code: ${ SUPER_LINTER_EXIT_CODE } "
2024-04-20 05:18:14 -04:00
if [ [ " ${ CREATE_LOG_FILE } " = = true ] ] ; then
LOG_FILE_PATH = " $( pwd ) /super-linter.log "
if [ ! -e " ${ LOG_FILE_PATH } " ] ; then
2024-07-01 08:50:52 -04:00
echo " Log file was requested but it's not available at ${ LOG_FILE_PATH } "
2024-04-20 05:18:14 -04:00
exit 1
else
sudo chown -R " $( id -u) " :" $( id -g) " " ${ LOG_FILE_PATH } "
echo "Log file contents:"
cat " ${ LOG_FILE_PATH } "
fi
else
echo " Log file was not requested. CREATE_LOG_FILE: ${ CREATE_LOG_FILE } "
fi
2024-07-01 08:50:52 -04:00
if [ [ " ${ SAVE_SUPER_LINTER_OUTPUT } " = = true ] ] ; then
SUPER_LINTER_OUTPUT_DIRECTORY_NAME = " ${ SUPER_LINTER_OUTPUT_DIRECTORY_NAME :- "super-linter-output" } "
SUPER_LINTER_OUTPUT_PATH = " $( pwd ) / ${ SUPER_LINTER_OUTPUT_DIRECTORY_NAME } "
if [ ! -d " ${ SUPER_LINTER_OUTPUT_PATH } " ] ; then
echo " Super-linter output was requested but it's not available at ${ SUPER_LINTER_OUTPUT_PATH } "
exit 1
else
sudo chown -R " $( id -u) " :" $( id -g) " " ${ SUPER_LINTER_OUTPUT_PATH } "
echo " Super-linter output path ( ${ SUPER_LINTER_OUTPUT_PATH } ) contents: "
ls -alhR " ${ SUPER_LINTER_OUTPUT_PATH } "
fi
else
echo " Super-linter output was not requested. SAVE_SUPER_LINTER_OUTPUT: ${ SAVE_SUPER_LINTER_OUTPUT } "
fi
2024-07-11 04:01:11 -04:00
if [ -n " ${ EXPECTED_SUPER_LINTER_STEP_SUMMARY_FILE_PATH :- } " ] ; then
# Remove eventual HTML comments from the expected file because we use them to disable certain linter rules
if ! diff " ${ SUPER_LINTER_STEP_SUMMARY_FILE } " <( grep -vE '^\s*<!--' " ${ EXPECTED_SUPER_LINTER_STEP_SUMMARY_FILE_PATH } " ) ; then
echo " Super-linter step summary ( ${ SUPER_LINTER_STEP_SUMMARY_FILE } ) contents don't match with the expected contents ( ${ EXPECTED_SUPER_LINTER_STEP_SUMMARY_FILE_PATH } ) "
exit 1
else
echo " Super-linter step summary ( ${ SUPER_LINTER_STEP_SUMMARY_FILE } ) contents match with the expected contents ( ${ EXPECTED_SUPER_LINTER_STEP_SUMMARY_FILE_PATH } ) "
fi
else
echo "Super-linter step summary output was not requested."
fi
2024-01-30 14:24:55 -05:00
if [ ${ SUPER_LINTER_EXIT_CODE } -ne ${ EXPECTED_EXIT_CODE } ] ; then
echo " Super-linter exited with an unexpected code: ${ SUPER_LINTER_EXIT_CODE } "
exit 1
else
echo " Super-linter exited with the expected code: ${ SUPER_LINTER_EXIT_CODE } "
fi
2024-04-22 05:40:23 -04:00
# Check if super-linter leaves leftovers behind
declare -a TEMP_ITEMS_TO_CLEAN
TEMP_ITEMS_TO_CLEAN = ( )
TEMP_ITEMS_TO_CLEAN += ( " $( pwd ) /.lintr " )
TEMP_ITEMS_TO_CLEAN += ( " $( pwd ) /.mypy_cache " )
TEMP_ITEMS_TO_CLEAN += ( " $( pwd ) /.ruff_cache " )
TEMP_ITEMS_TO_CLEAN += ( " $( pwd ) /logback.log " )
for item in " ${ TEMP_ITEMS_TO_CLEAN [@] } " ; do
echo " Check if ${ item } exists "
if [ [ -e " ${ item } " ] ] ; then
echo " Error: ${ item } exists and it should have been deleted "
exit 1
else
echo " ${ item } does not exist as expected "
fi
done