Skip to content

Commit

Permalink
./test: Add PASSES="mod_tidy" check
Browse files Browse the repository at this point in the history
Added check that ensures that go.mod & go.sum files are up-to-date.
The check verifies whether 'go mod tidy' does not generate any mutations
in these files.
The check can be run on its own:
  PASSES="mod_tidy" ./test
Or as part of "fmt" pass:
  PASSES="fmt" ./test

Examplar outputs:

```
% PASSES="fmt" ./test

Running with TEST_CPUS: 1,2,4
Starting 'fmt' pass at Fri 11 Sep 2020 11:07:54 PM CEST
'shellcheck' started at Fri 11 Sep 2020 11:07:54 PM CEST
'shellcheck' completed at Fri 11 Sep 2020 11:07:54 PM CEST
'markdown_you' started at Fri 11 Sep 2020 11:07:54 PM CEST
'markdown_you' completed at Fri 11 Sep 2020 11:07:54 PM CEST
'goword' started at Fri 11 Sep 2020 11:07:54 PM CEST
'goword' completed at Fri 11 Sep 2020 11:07:54 PM CEST
'gofmt' started at Fri 11 Sep 2020 11:07:54 PM CEST
'gofmt' completed at Fri 11 Sep 2020 11:07:55 PM CEST
'govet' started at Fri 11 Sep 2020 11:07:55 PM CEST
'govet' completed at Fri 11 Sep 2020 11:07:57 PM CEST
'revive' started at Fri 11 Sep 2020 11:07:57 PM CEST
Skipping revive...
'revive' completed at Fri 11 Sep 2020 11:07:57 PM CEST
'license_header' started at Fri 11 Sep 2020 11:07:57 PM CEST
'license_header' completed at Fri 11 Sep 2020 11:07:58 PM CEST
'receiver_name' started at Fri 11 Sep 2020 11:07:58 PM CEST
'receiver_name' completed at Fri 11 Sep 2020 11:07:58 PM CEST
'commit_title' started at Fri 11 Sep 2020 11:07:58 PM CEST
'commit_title' completed at Fri 11 Sep 2020 11:07:58 PM CEST
'mod_tidy' started at Fri 11 Sep 2020 11:07:58 PM CEST
*** /tmp/fileiALKRA_go.mod	2020-09-11 23:07:58.838010716 +0200
--- ./go.mod	2020-09-11 23:07:58.974010922 +0200
***************
*** 29,39 ****
  	github.com/mattn/go-runewidth v0.0.2 // indirect
  	github.com/modern-go/reflect2 v1.0.1
  	github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5
  	github.com/prometheus/client_golang v1.0.0
  	github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4
- 	github.com/prometheus/common v0.4.1
  	github.com/sirupsen/logrus v1.4.2 // indirect
  	github.com/soheilhy/cmux v0.1.4
  	github.com/spf13/cobra v0.0.3
  	github.com/spf13/pflag v1.0.1
  	github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8
--- 29,38 ----
./go.mod is not in sync with 'go mod tidy'
```

```
% PASSES="mod_tidy" ./test

Running with TEST_CPUS: 1,2,4
Starting 'mod_tidy' pass at Fri 11 Sep 2020 11:09:21 PM CEST
*** /tmp/file9gy4so_go.mod	2020-09-11 23:09:21.166133290 +0200
--- ./go.mod	2020-09-11 23:09:21.286133466 +0200
***************
*** 29,39 ****
  	github.com/mattn/go-runewidth v0.0.2 // indirect
  	github.com/modern-go/reflect2 v1.0.1
  	github.com/olekukonko/tablewriter v0.0.0-20170122224234-a0225b3f23b5
  	github.com/prometheus/client_golang v1.0.0
  	github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4
- 	github.com/prometheus/common v0.4.1
  	github.com/sirupsen/logrus v1.4.2 // indirect
  	github.com/soheilhy/cmux v0.1.4
  	github.com/spf13/cobra v0.0.3
  	github.com/spf13/pflag v1.0.1
  	github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8
--- 29,38 ----
./go.mod is not in sync with 'go mod tidy'
```
  • Loading branch information
ptabor committed Sep 11, 2020
1 parent d60b7af commit 80fe9d0
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions test
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ function fmt_pass {
license_header \
receiver_name \
commit_title \
mod_tidy \
; do
echo "'$p' started at $(date)"
"${p}"_pass "$@"
Expand Down Expand Up @@ -684,6 +685,35 @@ function build_pass {
GO_BUILD_FLAGS="-v" tools_build
}

function mod_tidy_pass {
# Watch for upstream solution: https://github.com/golang/go/issues/27005
local tmpFileGoMod=$(tempfile -s "_go.mod")
local tmpFileGoSum=$(tempfile -s "_go.sum")
cp "./go.mod" "${tmpFileGoMod}"
cp "./go.sum" "${tmpFileGoSum}"
go mod tidy

set +e
diff -C 5 "${tmpFileGoMod}" "./go.mod"
local tmpFileGoModInSync="$?"
diff -C 5 "${tmpFileGoSum}" "./go.sum"
local tmpFileGoSumInSync="$?"
set -e

# Bring back initial state
mv "${tmpFileGoMod}" "./go.mod"
mv "${tmpFileGoSum}" "./go.sum"

if [ "${tmpFileGoModInSync}" -ne 0 ]; then
echo "./go.mod is not in sync with 'go mod tidy'"
exit 255
fi
if [ "${tmpFileGoSumInSync}" -ne 0 ]; then
echo "./go.sum is not in sync with 'go mod tidy'"
exit 255
fi
}

for pass in $PASSES; do
echo "Starting '$pass' pass at $(date)"
"${pass}"_pass "$@"
Expand Down

0 comments on commit 80fe9d0

Please sign in to comment.