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