Debian Codegraph @

internal/testing/runchecks.sh

raw
1 #!/usr/bin/env bash
2 # Copyright 2018 The Go Cloud Development Kit Authors
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # https://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # This script runs all checks for Go CDK, including go test suites,
17 # compatibility checks, consistency checks, Wire, etc.
18
19 # https://coderwall.com/p/fkfaqq/safer-bash-scripts-with-set-euxo-pipefail
20 # Change to -euxo if debugging.
21 set -euo pipefail
22
23 if [[ $# -gt 0 ]]; then
24 echo "usage: runchecks.sh" 1>&2
25 exit 64
26 fi
27
28 # start_local_deps.sh requires that Docker is installed,
29 # which is only supported on Linux.
30 # Tests that depend on them should check the RUNNER_OS environment before running.
31 # Don't do this when running locally, as it's slow; user should do it.
32 if [[ "${RUNNER_OS:-}" == "Linux" ]]; then
33 echo
34 echo "Starting local dependencies..."
35 ./internal/testing/start_local_deps.sh
36 else
37 echo "Not starting local dependencies for ${RUNNER_OS:-}."
38 fi
39
40 result=0
41 rootdir="$(pwd)"
42
43 # Update the regexp below when upgrading to a
44 # new Go version. Some checks below we only run
45 # for the latest Go version.
46 latest_go_version=0
47 go_version=$(go version)
48 if [[ ${go_version} == *go1\.26* ]]; then
49 latest_go_version=1
50 fi
51 echo "Go Version: ${go_version}; latest=${latest_go_version}"
52
53 # Build the test-summary app, which is used inside the loop to summarize results
54 # from Go tests.
55 (cd internal/testing/test-summary && go build)
56 while read -r path || [[ -n "$path" ]]; do
57 echo
58 echo "******************************"
59 echo "* Running Go tests for module: $path"
60 echo "******************************"
61 echo
62
63 # TODO(rvangent): Special case modules to skip for Windows. Perhaps
64 # this should be data-driven by allmodules?
65 # (https://github.com/google/go-cloud/issues/2111).
66 if [[ "${RUNNER_OS:-}" == "Windows" ]] && [[ "$path" == "internal/website" ]]; then
67 echo " Skipping on Windows"
68 continue
69 fi
70
71 gotestflags=("-json" "-race")
72 testsummaryflags=("-progress")
73
74 if [[ $latest_go_version -eq 1 ]]; then
75 # Only do coverage for the latest Linux build because it is slow, and
76 # codecov will only save the last one anyway.
77 if [[ "${RUNNER_OS:-}" == "Linux" ]]; then
78 gotestflags+=("-coverpkg=./..." "-coverprofile=$rootdir/modcoverage.out")
79 fi
80 # Previous versions of the "go" command may have
81 # different opinions about what go.mod should look
82 # like.
83 gotestflags+=("-mod=readonly")
84 fi
85
86 # Run the tests.
87 (cd "$path" && go test "${gotestflags[@]}" ./...) | ./internal/testing/test-summary/test-summary "${testsummaryflags[@]}" || result=1
88 if [ -f modcoverage.out ] && [ $result -eq 0 ]; then
89 cat modcoverage.out >> coverage.out
90 rm modcoverage.out
91 fi
92 done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
93 # The above filters out comments and empty lines from allmodules and only takes
94 # the first (whitespace-separated) field from each line.
95
96 # Upload cumulative coverage data if we generated it.
97 if [ -f coverage.out ] && [ $result -eq 0 ]; then
98 # Filter out test packages.
99 grep -v test coverage.out > coverage2.out
100 mv coverage2.out coverage.out
101 bash <(curl -s https://codecov.io/bash)
102 rm coverage.out
103 fi
104
105 # The rest of these checks are not OS-specific, so we only run them for the
106 # Linux build, or when running locally.
107 if [[ "${RUNNER_OS:-linux}" != "Linux" ]]; then
108 exit $result
109 fi
110
111 if [[ ${latest_go_version} -eq 1 ]]; then
112 echo
113 echo "************************"
114 echo "* Checking go mod tidy"
115 echo "************************"
116 echo
117 while read -r path || [[ -n "$path" ]]; do
118 echo "Module: $path"
119 ( cd "$path" && "$rootdir"/internal/testing/check_mod_tidy.sh && echo " OK" ) || { echo "FAIL: please run ./internal/testing/gomodcleanup.sh" && result=1; }
120 done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
121 # The above filters out comments and empty lines from allmodules and only takes
122 # the first (whitespace-separated) field from each line.
123 fi
124
125
126 if [[ ${latest_go_version} -eq 1 ]]; then
127 echo
128 echo "**********************"
129 echo "* Checking wire diff"
130 echo "**********************"
131 echo
132 echo "Installing Wire..."
133 go install github.com/google/wire/cmd/wire@latest
134 echo
135 while read -r path || [[ -n "$path" ]]; do
136 echo "Module: $path"
137 ( cd "$path" && wire diff ./... && echo " OK" ) || { echo "FAIL: wire diff found diffs!" && result=1; }
138 done < <( sed -e '/^#/d' -e '/^$/d' allmodules | awk '{print $1}' )
139 # The above filters out comments and empty lines from allmodules and only takes
140 # the first (whitespace-separated) field from each line.
141 fi;
142
143 if [[ ${latest_go_version} -eq 1 ]]; then
144 echo
145 echo "******************************"
146 echo "* Doing non-module checks"
147 echo "******************************"
148 echo
149 echo "Ensuring .go files are formatted with gofmt -s..."
150 DIFF="$(gofmt -s -d .)"
151 if [ -n "$DIFF" ]; then
152 echo "FAIL: please run 'gofmt -s -w .' and commit the result"
153 echo "$DIFF";
154 exit 1;
155 else
156 echo " OK"
157 fi;
158 fi;
159
160 if [[ ${latest_go_version} -eq 1 ]]; then
161 echo
162 echo "Ensuring that there are no dependencies not listed in ./internal/testing/alldeps..."
163 ( ./internal/testing/listdeps.sh | diff -u ./internal/testing/alldeps - && echo " OK" ) || {
164 echo "FAIL: dependencies changed; run: internal/testing/listdeps.sh > internal/testing/alldeps" && result=1
165 # Module behavior may differ across versions.
166 echo "using the most recent go version."
167 }
168 fi
169
170
171 if [[ ${latest_go_version} -eq 1 ]]; then
172 echo
173 echo "Ensuring that any new packages have the corresponding entries in Hugo..."
174 missing_packages="$(internal/website/listnewpkgs.sh)"
175 if ! [[ -z "$missing_packages" ]]; then
176 echo "FAIL: missing package meta tags for:" 1>&2
177 echo "$missing_packages" 1>&2
178 result=1
179 else
180 echo " OK"
181 fi
182
183 echo
184 echo "Ensuring that all examples used in Hugo match what's in source..."
185 (internal/website/gatherexamples/run.sh | diff -u internal/website/data/examples.json - > /dev/null && echo " OK") || {
186 echo "FAIL: examples changed; run: internal/website/gatherexamples/run.sh > internal/website/data/examples.json"
187 result=1
188 }
189 fi;
190
191
192 echo
193 if [[ ${result} -eq 0 ]]; then
194 echo "SUCCESS!"
195 else
196 echo "FAILED; see above for more info."
197 fi
198
199 exit $result