Skip to content

Commit de9370d

Browse files
authored
Add a build script with more code quality checks (#2)
* Add a build script with more code quality checks * Fix false go mod tidy check error * Download modules before checks
1 parent 75e0d81 commit de9370d

25 files changed

+133
-25
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ go:
1111
env:
1212
- CGO_ENABLED=0
1313

14-
script: travis_wait 20 go test ./... -v -parallel 1 -timeout 60m
14+
script: travis_wait 20 ./scripts/build.sh

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@ go 1.14
55
require (
66
github.com/blang/semver v3.5.1+incompatible
77
github.com/hashicorp/go-cleanhttp v0.5.1
8+
github.com/kr/pretty v0.1.0 // indirect
89
github.com/stretchr/testify v1.6.1
10+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
911
)

go.sum

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
github.com/blang/semver v1.1.0 h1:ol1rO7QQB5uy7umSNV7VAmLugfLRD+17sYJujRNYPhg=
21
github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdnnjpJbkM4JQ=
32
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
43
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
54
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
65
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
76
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
7+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
8+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
9+
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
10+
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
11+
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
812
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
913
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1014
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1115
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
1216
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1317
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
18+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
19+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1420
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1521
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

scripts/build.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Stop build script if there are any errors
5+
#
6+
set -x
7+
set -e
8+
9+
PROJECT_HOME=$(git rev-parse --show-toplevel)
10+
if [[ -z "$GOBIN" ]]; then
11+
GOBIN="$(go env GOPATH)/bin"
12+
fi
13+
14+
# Get build dependencies
15+
pushd /tmp
16+
if [[ ! -e $GOBIN/staticcheck ]]; then
17+
go get honnef.co/go/tools/cmd/staticcheck
18+
fi
19+
if [[ ! -e $GOBIN/goimports ]]; then
20+
go get golang.org/x/tools/cmd/goimports
21+
fi
22+
popd
23+
24+
# Go Mod Download
25+
go mod download
26+
27+
# Check format of Go files
28+
$PROJECT_HOME/scripts/check-gofmt.sh .
29+
30+
# Check format of Imports
31+
$PROJECT_HOME/scripts/check-goimports.sh .
32+
33+
# Check module tidiness
34+
#$PROJECT_HOME/scripts/check-gomodtidy.sh
35+
36+
# Check for common problems
37+
go vet ./...
38+
39+
# Lint
40+
staticcheck ./...
41+
42+
# Run tests
43+
go test ./... -v -parallel 1 -timeout 60m
44+

scripts/check-gofmt.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env bash
2+
3+
DIR="."
4+
if [[ ! -z "$1" ]]; then
5+
DIR="$1"
6+
fi
7+
8+
go_files=$(gofmt -l -s $(find . -type f -name \*.go))
9+
if [[ ! -z "$go_files" ]]; then
10+
echo "The following files need formatting:"
11+
echo $go_files
12+
exit 1
13+
fi
14+

scripts/check-goimports.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env bash
2+
3+
DIR="."
4+
if [[ ! -z "$1" ]]; then
5+
DIR="$1"
6+
fi
7+
8+
go_files=$(goimports -l $(find . -type f -name \*.go))
9+
if [[ ! -z "$go_files" ]]; then
10+
echo "The following files need imports fixed:"
11+
for f in $go_files; do
12+
echo $f
13+
done
14+
exit 1
15+
fi
16+

scripts/check-gomodtidy.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env bash
2+
3+
go_files=$(go mod tidy -v 2>&1)
4+
if [[ ! -z "$go_files" ]]; then
5+
echo "The following modules need tidied."
6+
echo $go_files
7+
echo "If run locally they will have been tidied for you."
8+
exit 1
9+
fi
10+

upcloud/account_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package upcloud
22

33
import (
44
"encoding/xml"
5-
"github.com/stretchr/testify/assert"
65
"testing"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
// TestUnmarshalAccount tests that Account objects unmarshal correctly

upcloud/firewall_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package upcloud
22

33
import (
44
"encoding/xml"
5-
"github.com/stretchr/testify/assert"
65
"testing"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
// TestUnmarshalFirewallRules tests the FirewallRules and FirewallRule are unmarshaled correctly

upcloud/ip_address_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package upcloud
22

33
import (
44
"encoding/xml"
5-
"github.com/stretchr/testify/assert"
65
"testing"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
// TestUnmarshalIPAddresses tests that IPAddresses and IPAddress structs are unmarshaled correctly

0 commit comments

Comments
 (0)