2017-02-23 21:58:56 +03:00
|
|
|
#!/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}"
|
|
|
|
}
|
|
|
|
|
2017-03-02 16:57:41 +03:00
|
|
|
# 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
|
|
|
|
}
|
|
|
|
|
2017-02-23 21:58:56 +03:00
|
|
|
# test_with_profile run test coverage on each subdirectories and merge the
|
2017-03-09 07:39:09 +03:00
|
|
|
# coverage profile. Be aware that we are skipping the integration tests, as the
|
|
|
|
# tool won't gather any useful coverage information from them.
|
2017-02-23 21:58:56 +03:00
|
|
|
test_with_coverage_profile() {
|
2017-03-14 17:43:42 +03:00
|
|
|
print "* Run tests with creating coverage profile"
|
2017-03-02 16:57:41 +03:00
|
|
|
check_test_ports
|
2017-02-23 21:58:56 +03:00
|
|
|
|
|
|
|
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 '*/_*' \
|
2017-03-09 07:39:09 +03:00
|
|
|
-not -path '.' \
|
2017-02-23 21:58:56 +03:00
|
|
|
-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
|
2017-03-14 17:43:42 +03:00
|
|
|
print "* Send test coverage to travis server"
|
2017-02-23 21:58:56 +03:00
|
|
|
# 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() {
|
2017-03-14 17:43:42 +03:00
|
|
|
print "* Run tests with race conditions checks"
|
2017-03-02 16:57:41 +03:00
|
|
|
check_test_ports
|
2017-02-23 21:58:56 +03:00
|
|
|
|
|
|
|
test_targets=$(go list ./... | grep -v '/vendor/')
|
|
|
|
env GORACE="halt_on_error=1" go test -p 1 -v -race $test_targets
|
|
|
|
}
|
|
|
|
|
|
|
|
# lint_check runs static checks.
|
|
|
|
lint_check() {
|
2017-03-14 17:43:42 +03:00
|
|
|
print "* Run static checks"
|
2017-02-23 21:58:56 +03:00
|
|
|
|
|
|
|
# Make sure gometalinter is installed and $GOPATH/bin is in your path.
|
2017-03-16 15:13:40 +03:00
|
|
|
if [ ! -x "$(type -p gometalinter.v1)" ]; then
|
2017-03-14 17:43:42 +03:00
|
|
|
print "** Install gometalinter"
|
2017-03-16 15:13:40 +03:00
|
|
|
go get -u gopkg.in/alecthomas/gometalinter.v1
|
|
|
|
gometalinter.v1 --install
|
2017-02-23 21:58:56 +03:00
|
|
|
fi
|
|
|
|
|
2017-03-16 15:13:40 +03:00
|
|
|
# Update metalinter if needed.
|
|
|
|
gometalinter.v1 --install 1>/dev/null
|
|
|
|
|
2017-02-23 21:58:56 +03:00
|
|
|
# Automatic checks
|
|
|
|
linter_targets=$(glide novendor | grep -v lnrpc)
|
2017-03-16 15:13:40 +03:00
|
|
|
test -z "$(gometalinter.v1 --disable-all \
|
2017-02-23 21:58:56 +03:00
|
|
|
--enable=gofmt \
|
|
|
|
--enable=vet \
|
|
|
|
--enable=golint \
|
|
|
|
--enable=gosimple \
|
|
|
|
--enable=unconvert \
|
|
|
|
--line-length=72 \
|
|
|
|
--deadline=4m $linter_targets 2>&1 | grep -v 'ALL_CAPS\|OP_' 2>&1 | tee /dev/stderr)"
|
|
|
|
}
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
2017-03-14 17:43:42 +03:00
|
|
|
print "====+ Start +===="
|
|
|
|
|
2017-02-23 21:58:56 +03:00
|
|
|
# Read input flags and initialize variables
|
|
|
|
NEED_LINT="false"
|
|
|
|
NEED_COVERAGE="false"
|
|
|
|
NEED_RACE="false"
|
2017-03-14 17:43:42 +03:00
|
|
|
NEED_INSTALL="false"
|
2017-02-23 21:58:56 +03:00
|
|
|
|
2017-03-14 17:43:42 +03:00
|
|
|
while getopts "lrci" flag; do
|
2017-02-23 21:58:56 +03:00
|
|
|
case "${flag}" in
|
|
|
|
l) NEED_LINT="true" ;;
|
|
|
|
r) NEED_RACE="true" ;;
|
|
|
|
c) NEED_COVERAGE="true" ;;
|
2017-03-14 17:43:42 +03:00
|
|
|
i) NEED_INSTALL="true" ;;
|
2017-02-23 21:58:56 +03:00
|
|
|
*)
|
2017-03-14 17:43:42 +03:00
|
|
|
printf '\nUsage: %s [-l] [-r] [-c] [-i], where:\n' $0
|
2017-02-23 21:58:56 +03:00
|
|
|
printf ' -l: include code lint check\n'
|
|
|
|
printf ' -r: run tests with race condition check\n'
|
|
|
|
printf ' -c: run tests with test coverage\n'
|
2017-03-14 17:43:42 +03:00
|
|
|
printf ' -i: reinstall project dependencies\n'
|
2017-02-23 21:58:56 +03:00
|
|
|
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
|
2017-03-14 17:43:42 +03:00
|
|
|
print "* Install glide"
|
2017-02-23 21:58:56 +03:00
|
|
|
go get -u github.com/Masterminds/glide
|
2017-03-14 17:43:42 +03:00
|
|
|
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"
|
2017-02-23 21:58:56 +03:00
|
|
|
glide install
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Required for the integration tests
|
2017-03-14 17:43:42 +03:00
|
|
|
print "* Build source"
|
2017-02-23 21:58:56 +03:00
|
|
|
go install -v . ./cmd/...
|
|
|
|
|
|
|
|
# Lint check is first because we shouldn't run tests on garbage code.
|
|
|
|
if [ "$NEED_LINT" == "true" ]; then
|
|
|
|
lint_check
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Race condition second because we shouldn't check coverage on buggy code.
|
|
|
|
if [ "$NEED_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" ]; then
|
|
|
|
test_with_coverage_profile
|
2017-03-14 17:43:42 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
print "=====+ End +====="
|