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

     1  run_api_imports() {
     2  	allowed=$(cat .github/api-client-allowed-list.txt)
     3  	for dir in ./api/client/* ./api/base/*; do
     4  		if [[ ! -d $dir ]]; then
     5  			continue
     6  		fi
     7  		if [[ $dir =~ "api/base/testing" ]]; then
     8  			continue
     9  		fi
    10  
    11  		got=$(go run ./scripts/import-inspector "$dir" 2>/dev/null | jq -r ".[]")
    12  		python3 tests/suites/static_analysis/lint_go.py -a "${allowed}" -g "${got}" || (echo "Error: API Client import failure in $dir" && exit 1)
    13  	done
    14  }
    15  
    16  run_go() {
    17  	VER=$(golangci-lint --version | tr -s ' ' | cut -d ' ' -f 4 | cut -d '.' -f 1,2)
    18  	if [[ ${VER} != "1.54" ]] && [[ ${VER} != "v1.54" ]]; then
    19  		(echo >&2 -e '\nError: golangci-lint version does not match 1.54. Please upgrade/downgrade to the right version.')
    20  		exit 1
    21  	fi
    22  	OUT=$(golangci-lint run -c .github/golangci-lint.config.yaml 2>&1)
    23  	if [[ -n ${OUT} ]]; then
    24  		(echo >&2 "\\nError: linter has issues:\\n\\n${OUT}")
    25  		exit 1
    26  	fi
    27  	OUT=$(golangci-lint run -c .github/golangci-lint.config.experimental.yaml 2>&1)
    28  	if [[ -n ${OUT} ]]; then
    29  		(echo >&2 "\\nError: experimental linter has issues:\\n\\n${OUT}")
    30  		exit 1
    31  	fi
    32  }
    33  
    34  run_go_tidy() {
    35  	CUR_SHA=$(git show HEAD:go.sum | shasum -a 1 | awk '{ print $1 }')
    36  	go mod tidy 2>&1
    37  	NEW_SHA=$(cat go.sum | shasum -a 1 | awk '{ print $1 }')
    38  	if [[ ${CUR_SHA} != "${NEW_SHA}" ]]; then
    39  		git diff >&2
    40  		(echo >&2 -e "\\nError: go mod sum is out of sync. Run 'go mod tidy' and commit source.")
    41  		exit 1
    42  	fi
    43  }
    44  
    45  test_static_analysis_go() {
    46  	if [ "$(skip 'test_static_analysis_go')" ]; then
    47  		echo "==> TEST SKIPPED: static go analysis"
    48  		return
    49  	fi
    50  
    51  	(
    52  		set_verbosity
    53  
    54  		cd .. || exit
    55  
    56  		run "run_api_imports"
    57  		run_linter "run_go"
    58  		run_linter "run_go_tidy"
    59  	)
    60  }