forked from decred/dcrlnd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gotest.sh
executable file
·172 lines (145 loc) · 5.32 KB
/
gotest.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
#!/bin/bash
# The script does automatic checking on a Go package and its sub-packages, including:
# 1. gofmt (http://golang.org/cmd/gofmt/)
# 2. golint (https://github.com/golang/lint)
# 3. go vet (http://golang.org/cmd/vet)
# 4. gosimple (https://github.com/dominikh/go-simple)
# 5. unconvert (https://github.com/mdempsky/unconvert)
# 6. race detector (http://blog.golang.org/race-detector)
# 7. test coverage (http://blog.golang.org/cover)
#
# gometalinter (github.com/alecthomas/gometalinter) is used to run each static
# checker.
declare GREEN='\033[0;32m'
declare NC='\033[0m'
print () {
echo -e "${GREEN}$1${NC}"
}
# check_test_ports checks that test lnd ports are not used.
check_test_ports() {
# Make sure that test lnd ports are not used.
o=$(lsof -i :19555,19556 | sed '1d' | awk '{ printf "%s\n", $2 }')
if [ "$o" != "" ]; then
printf "Can't run the lnd tests:\n"
printf "some program is using the test lnd ports (19555 | 19556)\n"
exit 1
fi
}
# test_with_profile run test coverage on each subdirectories and merge the
# coverage profile. Be aware that we are skipping the integration tests, as the
# tool won't gather any useful coverage information from them.
test_with_coverage_profile() {
print "* Running tests with creating coverage profile"
check_test_ports
echo "mode: count" > profile.cov
# Standard go tooling behavior is to ignore dirs with leading underscores.
for dir in $(find . -maxdepth 10 \
-not -path './.git*' \
-not -path '*/_*' \
-not -path '.' \
-not -path './cmd*' \
-not -path './release*' \
-not -path './vendor*' \
-type d)
do
if ls $dir/*.go &> /dev/null; then
go test -v -covermode=count -coverprofile=$dir/profile.tmp $dir
if [ -f $dir/profile.tmp ]; then
cat $dir/profile.tmp | tail -n +2 >> profile.cov
rm $dir/profile.tmp
fi
fi
done
if [ "$TRAVIS" == "true" ] ; then
print "* Send test coverage to travis server"
# Make sure goveralls is installed and $GOPATH/bin is in your path.
if [ ! -x "$(type -p goveralls)" ]; then
print "** Install goveralls:"
go get github.com/mattn/goveralls
fi
print "** Submit the test coverage result to coveralls.io"
goveralls -coverprofile=profile.cov -service=travis-ci
fi
}
# test_race_conditions run standard go test without creating coverage
# profile but with race condition checks.
test_race_conditions() {
print "* Running tests with the race condition detector"
check_test_ports
test_targets=$(go list ./... | grep -v '/vendor/')
env GORACE="history_size=7 halt_on_error=1" go test -v -p 1 -race $test_targets
}
# lint_check runs static checks.
lint_check() {
print "* Run static checks"
# Make sure gometalinter is installed and $GOPATH/bin is in your path.
if [ ! -x "$(type -p gometalinter.v1)" ]; then
print "** Install gometalinter"
go get -u gopkg.in/alecthomas/gometalinter.v1
gometalinter.v1 --install
fi
# Update metalinter if needed.
gometalinter.v1 --install 1>/dev/null
# Automatic checks
linter_targets=$(glide novendor | grep -v lnrpc)
test -z "$(gometalinter.v1 --disable-all \
--enable=gofmt \
--enable=vet \
--enable=golint \
--line-length=72 \
--deadline=4m $linter_targets 2>&1 | grep -v 'ALL_CAPS\|OP_' 2>&1 | tee /dev/stderr)"
}
set -e
print "====+ Start +===="
# Read input flags and initialize variables
NEED_LINT="false"
NEED_COVERAGE="false"
NEED_RACE="false"
NEED_INSTALL="false"
while getopts "lrci" flag; do
case "${flag}" in
l) NEED_LINT="true" ;;
r) NEED_RACE="true" ;;
c) NEED_COVERAGE="true" ;;
i) NEED_INSTALL="true" ;;
*)
printf '\nUsage: %s [-l] [-r] [-c] [-i], where:\n' $0
printf ' -l: include code lint check\n'
printf ' -r: run tests with race condition check\n'
printf ' -c: run tests with test coverage\n'
printf ' -i: reinstall project dependencies\n'
exit 1 ;;
esac
done
# remove the options from the positional parameters
shift $(( OPTIND - 1 ))
# Make sure glide is installed and $GOPATH/bin is in your path.
if [ ! -x "$(type -p glide)" ]; then
print "* Install glide"
go get -u github.com/Masterminds/glide
fi
# Install the dependency if vendor directory not exist or if flag have been
# specified.
if [ "$NEED_INSTALL" == "true" ] || [ ! -d "./vendor" ]; then
print "* Install dependencies"
glide install
fi
# Required for the integration tests
print "* Build source"
go install -v . ./cmd/...
# Lint check is first because we shouldn't run tests on garbage code.
if [ "$NEED_LINT" == "true" ] || [ "$RACE" == "false" ]; then
lint_check
fi
# Race condition second because we shouldn't check coverage on buggy code.
if [ "$NEED_RACE" == "true" ] || [ "$RACE" == "true" ]; then
test_race_conditions
fi
# Test coverage is third because in this case code should work properly and
# we may calmly send coverage profile (if script is run on travis)
if [ "$NEED_COVERAGE" == "true" ] || [ "$RACE" == "false" ]; then
print "* Running integration tests"
go test -v -tags rpctest
test_with_coverage_profile
fi
print "=====+ End +====="