forked from purpleidea/mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-govet.sh
executable file
·154 lines (132 loc) · 4.02 KB
/
test-govet.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
#!/bin/bash
# check that go vet passes
echo running "$0"
#ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" # dir!
ROOT=$(dirname "${BASH_SOURCE}")/..
cd "${ROOT}"
. test/util.sh
make --quiet -C test # run make in test directory to prepare any needed tools
failures=''
function run-test()
{
$@ || failures=$( [ -n "$failures" ] && echo "$failures\\n$@" || echo "$@" )
}
GO_VERSION=($(go version))
function typos() {
if grep -i 'reversable' "$1"; then # the word is "reversible"
return 1
fi
return 0
}
function simplify-gocase() {
if grep 'case _ = <-' "$1"; then
return 1 # 'case _ = <- can be simplified to: case <-'
fi
return 0
}
function token-coloncheck() {
# add quotes to avoid matching three X's
if grep -Ei "[\/]+[\/]+[ ]*(T"'O''D'"O[^:]|F"'I''X''M'"E[^:]|X"'X'"X[^:])" "$1"; then
return 1 # tokens must end with a colon
fi
# tokens must be upper case
if grep -E "[t][Oo][Dd][Oo]|[Tt][o][Dd][Oo]|[Tt][Oo][d][Oo]|[Tt][Oo][Dd][o]|[f][Ii][Xx][Mm][Ee]|[Ff][i][Xx][Mm][Ee]|[Ff][Ii][x][Mm][Ee]|[Ff][Ii][Xx][m][Ee]|[Ff][Ii][Xx][Mm][e]|[x][Xx][Xx]|[Xx][x][Xx]|[Xx][Xx][x]" "$1"; then
return 1
fi
return 0
}
function naked-error() {
# the $ before the \t magically makes grep match the tab somehow...
if grep $'\terrors.New(' "$1"; then # missing a leading return
return 1
fi
if grep $'\tfmt.Errorf(' "$1"; then # missing a leading return
return 1
fi
if grep $'\terrwrap.Wrap' "$1"; then # missing a leading return
return 1
fi
return 0
}
# catch errors that start with a capital
function lowercase-errors() {
if [[ $1 == *"/bindata.go" ]]; then # ends with bindata.go ?
return 0 # skip those generated files
fi
if grep -E 'errors\.New\(\"[A-Z]' "$1"; then
return 1
fi
if grep -E 't\.Errorf\(\"[A-Z]' "$1"; then # t.Errorf or fmt.Errorf
return 1
fi
# TODO: add errwrap.Wrap* related matching
return 0
}
function consistent-imports() {
if [ "$1" = './util/errwrap/errwrap.go' ]; then
return 0
fi
# import as github.com/purpleidea/mgmt/util/errwrap
if grep $'\t"github.com/pkg/errors"' "$1"; then
return 1
fi
# import as github.com/purpleidea/mgmt/util/errwrap
if grep $'\t"github.com/hashicorp/go-multierror"' "$1"; then
return 1
fi
# import as langutil
if grep $'\t"github.com/purpleidea/mgmt/lang/util"' "$1"; then
return 1
fi
# import as engineutil
if grep $'\t"github.com/purpleidea/mgmt/engine/util"' "$1"; then
return 1
fi
if grep '"golang.org/x/net/context"' "$1"; then # use built-in context
return 1
fi
}
function reflowed-comments() {
if [[ $1 == *"/bindata.go" ]]; then # ends with bindata.go ?
return 0 # skip those generated files
fi
if [ "$1" = './lang/funcs/core/generated_funcs.go' ]; then
return 0
fi
if [ "$1" = './lang/lexer.nn.go' ]; then
return 0
fi
if [ "$1" = './lang/interpolate/parse.generated.go' ]; then
return 0
fi
./test/comment_parser "$1"
}
# run go vet on a per-package basis
base=$(go list .)
for pkg in `go list -e ./... | grep -v "^${base}/vendor/" | grep -v "^${base}/examples/" | grep -v "^${base}/test/" | grep -v "^${base}/old" | grep -v "^${base}/old/" | grep -v "^${base}/tmp" | grep -v "^${base}/tmp/"`; do
echo -e "\tgo vet: $pkg"
# workaround go vet issues by adding the new -source flag (go1.9+)
run-test go vet -source "$pkg" || fail_test "go vet -source did not pass pkg"
done
# loop through individual *.go files
for file in `find . -maxdepth 9 -type f -name '*.go' -not -path './old/*' -not -path './tmp/*' -not -path './vendor/*'`; do
#if [[ $file == "./vendor/"* ]]; then # skip files that start with...
# continue
#fi
run-test grep 'log.Print' "$file" | grep '\\n"' && fail_test 'no newline needed in log.Print*()' # no \n needed in log.Printf or log.Println
run-test typos "$file"
run-test simplify-gocase "$file"
run-test token-coloncheck "$file"
run-test naked-error "$file"
run-test lowercase-errors "$file"
run-test consistent-imports "$file"
run-test reflowed-comments "$file"
done
if [[ -n "$failures" ]]; then
echo 'FAIL'
echo 'The following tests have failed:'
echo -e "$failures"
echo
exit 1
fi
echo 'PASS'