github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/tests/suites/static_analysis/lint_shell.sh (about)

     1  run_shellcheck() {
     2  	OUT=$(shellcheck --shell=bash --severity=warning tests/main.sh tests/includes/*.sh tests/suites/**/*.sh 2>&1 || true)
     3  	if [ -n "${OUT}" ]; then
     4  		echo ""
     5  		echo "$(red 'Found some issues:')"
     6  		echo "${OUT}"
     7  		exit 1
     8  	fi
     9  }
    10  
    11  run_shfmt() {
    12  	# shellcheck disable=SC2038
    13  	OUT=$(find ./tests -type f -name "*.sh" | xargs -I% shfmt -l -s % | wc -l | grep "0" || echo "FAILED")
    14  
    15  	if [[ ${OUT} == "FAILED" ]]; then
    16  		echo ""
    17  		echo "$(red 'Found some issues:')"
    18  		# shellcheck disable=SC2038
    19  		echo "$(find ./tests -type f -name "*.sh" | xargs -I% shfmt -l -s %)"
    20  		exit 1
    21  	fi
    22  }
    23  
    24  run_trailing_whitespace() {
    25  	# Ensure we capture filename.sh and linenumber and nothing else.
    26  	# filename.sh:<linenumber>:filename.sh<error>
    27  	# shellcheck disable=SC2063
    28  	OUT=$(grep -n -r --include "*.sh" " $" tests/ | grep -v "tmp\.*" | grep -oP "^.*:\d+" || true)
    29  	if [ -n "${OUT}" ]; then
    30  		echo ""
    31  		echo "$(red 'Found some issues:')"
    32  		echo "trailing whitespace in script"
    33  		echo "${OUT}"
    34  		exit 1
    35  	fi
    36  }
    37  
    38  test_static_analysis_shell() {
    39  	if [ "$(skip 'test_static_analysis_shell')" ]; then
    40  		echo "==> TEST SKIPPED: static shell analysis"
    41  		return
    42  	fi
    43  
    44  	(
    45  		set_verbosity
    46  
    47  		cd .. || exit
    48  
    49  		# Shell static analysis
    50  		if which shellcheck >/dev/null 2>&1; then
    51  			run_linter "run_shellcheck"
    52  		else
    53  			echo "shellcheck not found, shell static analysis disabled"
    54  		fi
    55  
    56  		# shfmt static analysis
    57  		if which shfmt >/dev/null 2>&1; then
    58  			run_linter "run_shfmt"
    59  		else
    60  			echo "shfmt not found, shell static analysis disabled"
    61  		fi
    62  
    63  		## Trailing whitespace in scripts
    64  		run_linter "run_trailing_whitespace"
    65  	)
    66  }