Skip to content

Commit f6e1bf2

Browse files
committed
Rework Go mod tidy/vendor checks
This change reworks the Go mod tidy/vendor checks to run for all tracked Go modules by the project and fail for any uncommitted changes. Signed-off-by: Austin Vazquez <[email protected]>
1 parent 58fdbd1 commit f6e1bf2

2 files changed

Lines changed: 44 additions & 40 deletions

File tree

hack/validate/vendor

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,30 @@ set -e
55
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
66
source "${SCRIPTDIR}/.validate"
77

8-
tidy_files=('go.mod' 'go.sum')
8+
modules_files=('api/go.mod' 'client/go.mod' 'man/go.mod' 'go.mod')
9+
tidy_files=("${modules_files[@]}" 'api/go.sum' 'client/go.sum' 'man/go.sum' 'go.sum')
910
vendor_files=("${tidy_files[@]}" 'vendor/')
1011

11-
validate_vendor_files_exist() {
12-
local file
13-
for file in "${vendor_files[@]}"; do
14-
if [ ! -e "$file" ]; then
15-
echo >&2 "ERROR: required file '$file' does not exist"
12+
validate_tidy_modules() {
13+
# check that all go.mod files exist in HEAD; go.sum files are generated by 'go mod tidy'
14+
# so we don't need to check for their existence beforehand
15+
for f in "${modules_files[@]}"; do
16+
if [ ! -f "$f" ]; then
17+
echo >&2 "ERROR: missing $f"
1618
return 1
1719
fi
1820
done
19-
return 0
20-
}
21-
22-
validate_vendor_tidy() {
2321
# run mod tidy
2422
./hack/vendor.sh tidy
2523
# check if any files have changed
26-
git diff --quiet HEAD -- "${tidy_files[@]}"
24+
git diff --quiet HEAD -- "${tidy_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ]
2725
}
2826

2927
validate_vendor_diff() {
30-
mapfile -t changed_files < <(validate_diff --diff-filter=ACMR --name-only -- "${vendor_files[@]}")
31-
32-
if [ -n "${TEST_FORCE_VALIDATE:-}" ] || [ "${#changed_files[@]}" -gt 0 ]; then
33-
# recreate vendor/
34-
./hack/vendor.sh vendor
35-
# check if any files have changed
36-
git diff --quiet HEAD -- "${vendor_files[@]}"
37-
else
38-
echo >&2 'INFO: no vendor changes in diff; skipping vendor check.'
39-
fi
28+
# recreate vendor/
29+
./hack/vendor.sh vendor
30+
# check if any files have changed
31+
git diff --quiet HEAD -- "${vendor_files[@]}" && [ -z "$(git ls-files --others --exclude-standard)" ]
4032
}
4133

4234
validate_vendor_license() {
@@ -48,26 +40,22 @@ validate_vendor_license() {
4840
done < <(awk '/^# /{ print $2 }' vendor/modules.txt)
4941
}
5042

51-
# First check the required files exist as git diff will not report missing files.
52-
if ! validate_vendor_files_exist; then
53-
{
54-
echo 'FAIL: Vendoring was not performed correctly!'
55-
echo
56-
echo 'Please revendor with hack/vendor.sh'
57-
} >&2
58-
exit 1
59-
fi
60-
61-
if validate_vendor_tidy && validate_vendor_diff && validate_vendor_license; then
43+
if validate_tidy_modules && validate_vendor_diff && validate_vendor_license; then
6244
echo >&2 'PASS: Vendoring has been performed correctly!'
6345
else
6446
{
6547
echo 'FAIL: Vendoring was not performed correctly!'
6648
echo
67-
echo 'The following files changed during re-vendor:'
68-
echo
69-
git diff --name-status HEAD -- "${vendor_files[@]}"
70-
echo
49+
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
50+
echo 'The following files are missing:'
51+
git ls-files --others --exclude-standard
52+
echo
53+
fi
54+
if [ -n "$(git diff --name-status HEAD -- "${vendor_files[@]}")" ]; then
55+
echo 'The following files changed during re-vendor:'
56+
git diff --name-status HEAD -- "${vendor_files[@]}"
57+
echo
58+
fi
7159
echo 'Please revendor with hack/vendor.sh'
7260
echo
7361
git diff --diff-filter=M -- "${vendor_files[@]}"

hack/vendor.sh

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,31 @@
77
set -e
88

99
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10+
PROJECT_DIR="$(cd "$SCRIPTDIR/.." && pwd)"
11+
12+
# All module paths are relative to PROJECT_DIR
13+
# Currently only a subset of modules are vendored.
14+
vendor_modules_paths=(. man)
15+
modules_paths=(api client "${vendor_modules_paths[@]}")
1016

1117
tidy() (
12-
set -x
13-
go mod tidy
18+
for module_path in "${modules_paths[@]}"; do
19+
(
20+
set -x
21+
cd "$PROJECT_DIR/$module_path"
22+
go mod tidy
23+
)
24+
done
1425
)
1526

1627
vendor() (
17-
set -x
18-
go mod vendor
28+
for module_path in "${vendor_modules_paths[@]}"; do
29+
(
30+
set -x
31+
cd "$PROJECT_DIR/$module_path"
32+
go mod vendor
33+
)
34+
done
1935
)
2036

2137
replace() (

0 commit comments

Comments
 (0)