Skip to content

Commit

Permalink
feat: add module swapper to lint script (#2088)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Peabody <[email protected]>
  • Loading branch information
bharathkkb and apeabody authored Jan 11, 2024
1 parent f03a524 commit d6ecb03
Showing 1 changed file with 18 additions and 59 deletions.
77 changes: 18 additions & 59 deletions infra/build/developer-tools/build/scripts/test_lint.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#! /bin/bash
# Copyright 2019 Google LLC
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -25,21 +25,8 @@ source /usr/local/bin/task_helper_functions.sh
set +e

# constants
MARKDOWN=0
MARKDOWN_STR=""
CONTRIBUTING_GUIDE=""
# shellcheck disable=SC2089,SC2016 # Quotes/backslashes will be treated literally, expressions don't expand
messages='{
"check_generate_modules": "The modules need to be regenerated. Please run `make_build`.",
"check_documentation": "The documentation needs to be regenerated. Please run `make generate_docs`.",
"check_whitespace": "Failed whitespace check. More details below.",
"check_shell": "Failed shell check. More info on running shellcheck locally [here](https://www.shellcheck.net).",
"check_headers": "All files need a license header. Please make sure all your files include the appropriate header. A helper tool available [here](https://github.com/google/addlicense).",
"check_python": "Failed flake8 Python lint check.",
"check_tflint": "Failed TFlint check. More details below.",
"check_terraform": "Failed Terraform check. More details below."
"check_metadata": "Failed metadata validation check. More details below."
}'
# Store working dir as module-swapper needs to be executed in this dir.
CWD="${PWD}"
rval=0
failed_tests=()
tests=(
Expand All @@ -54,55 +41,27 @@ tests=(
check_metadata
)

# parse args
for arg in "$@"
do
case $arg in
-m|--markdown)
MARKDOWN=1
shift
;;
-c=*|--contrib-guide=*)
CONTRIBUTING_GUIDE="${arg#*=}"
shift
;;
*) # end argument parsing
shift
;;
esac
done
if [[ -z "${DISABLE_MODULE_SWAPPER:-}" ]]; then
module-swapper
fi

function restore {
cd "${CWD}"
module-swapper -restore >/dev/null 2>&1
exit 1
}
trap restore INT

for test in "${tests[@]}"; do
# if not in markdown mode, pipe test output to stdout tty
# nested if condition is a workaround for test[[]] not echoing some outputs from check_* tests even with subshell
if [[ $MARKDOWN -eq 0 ]]; then
if ! "${test}"; then
failed_tests+=("${test}")
((rval++))
fi
# if control reaches here - in markdown mode, pipe test stderr to stdout for capture
elif ! output=$(${test} 2>&1); then
# add test name to list of failed_tests
if ! "${test}"; then
failed_tests+=("${test}")
((rval++))
# clean output color, sqash multiple empty blank lines
output=$(echo "$output" | sed -r "s/\x1b\[[0-9;]*m/\n/g" | tr -s '\n')
# try to get a helpful error message, otherwise unknown
error_help_message=$(echo "$messages" | jq --arg check_name "$test" -r '.[$check_name] // "🦖 An unknown error has occurred" ')
#construct markdown body
MARKDOWN_STR+="- ⚠️${test}\n ${error_help_message} \n \`\`\`bash \n${output}\n \`\`\` \n"
fi
done
module-swapper -restore >/dev/null 2>&1

# if any tests have failed
if [[ "${#failed_tests[@]}" -ne 0 ]]; then
# echo output in markdown
if [[ $MARKDOWN -eq 1 ]]; then
header="Thanks for the PR! 🚀\nUnfortunately it looks like some of our CI checks failed. See the [Contributing Guide](${CONTRIBUTING_GUIDE}) for details.\n"
echo -e "${header}${MARKDOWN_STR}"
else
# shellcheck disable=SC2145 # Output all elements of the array
echo "Error: The following tests have failed: ${failed_tests[@]}"
exit "${rval}"
fi
# shellcheck disable=SC2145 # Output all elements of the array
echo "Error: The following tests have failed: ${failed_tests[@]}"
exit "${rval}"
fi

0 comments on commit d6ecb03

Please sign in to comment.