Add a threshold based golint test case

This lets some golint errors in, but fails if you're over a certain
threshold. The current threshold of 15% (of LOC) is arbitrary and
subject to change. The algorithm should be extended to check a range of
commits, although it's unclear how to detect what range of commits make
up a patch set.
This commit is contained in:
James Shubin
2016-03-10 03:09:13 -05:00
parent 23647445d7
commit 6b6dc75152
4 changed files with 55 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ go:
- 1.6 - 1.6
- tip - tip
sudo: false sudo: false
before_install: 'git fetch --unshallow'
install: 'make deps' install: 'make deps'
script: 'make test' script: 'make test'
matrix: matrix:

View File

@@ -41,3 +41,4 @@ go get ./... # get all the go dependencies
[ -e "$GOBIN/mgmt" ] && rm -f "$GOBIN/mgmt" # the `go get` version has no -X [ -e "$GOBIN/mgmt" ] && rm -f "$GOBIN/mgmt" # the `go get` version has no -X
go get golang.org/x/tools/cmd/vet # add in `go vet` for travis go get golang.org/x/tools/cmd/vet # add in `go vet` for travis
go get golang.org/x/tools/cmd/stringer # for automatic stringer-ing go get golang.org/x/tools/cmd/stringer # for automatic stringer-ing
go get github.com/golang/lint/golint # for `golint`-ing

View File

@@ -31,3 +31,4 @@ fi
if env | grep -q -e '^JENKINS_URL=' -e '^BUILD_TAG=jenkins'; then if env | grep -q -e '^JENKINS_URL=' -e '^BUILD_TAG=jenkins'; then
./test/test-omv.sh ./test/test-omv.sh
fi fi
./test/test-golint.sh # test last, because this test is somewhat arbitrary

52
test/test-golint.sh Executable file
View File

@@ -0,0 +1,52 @@
#!/bin/bash
# check that go lint passes or doesn't get worse by some threshold
echo running test-golint.sh
# FIXME: test a range of commits, since only the last patch is checked here
PREVIOUS='HEAD^'
CURRENT='HEAD'
THRESHOLD=15 # percent problems per new LOC
XPWD=`pwd`
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" # dir!
cd "${ROOT}" >/dev/null
LINT=`golint` # current golint output
COUNT=`echo -e "$LINT" | wc -l` # number of golint problems in current branch
T=`mktemp --tmpdir -d tmp.XXX`
[ "$T" = "" ] && exit 1
cd $T || exit 1
git clone --recursive "${ROOT}" 2>/dev/null # make a copy
cd "`basename ${ROOT}`" >/dev/null || exit 1
DIFF1=0
NUMSTAT1=`git diff "$PREVIOUS" "$CURRENT" --numstat` # numstat diff since previous commit
while read -r line; do
add=`echo "$line" | cut -f1`
# TODO: should we only count added lines, or count the difference?
sum="$add"
#del=`echo "$line" | cut -f2`
#sum=`expr $add - $del`
DIFF1=`expr $DIFF1 + $sum`
done <<< "$NUMSTAT1" # three < is the secret to putting a variable into read
git checkout "$PREVIOUS" 2>/dev/null # previous commit
LINT1=`golint`
COUNT1=`echo -e "$LINT1" | wc -l` # number of golint problems in older branch
# clean up
cd "$XPWD" >/dev/null
rm -rf "$T"
[ "$LINT1" = "" ] && echo PASS && exit # everything is "perfect"
DELTA=$(printf "%.0f\n" `echo "(($COUNT1 - $COUNT) / $DIFF1) * 100" | bc -l`)
echo "Lines of code: $DIFF1"
echo "Prev. # of issues: $COUNT"
echo "Curr. # of issues: $COUNT1"
echo "Issue count delta is: $DELTA %"
if [ "$DELTA" -gt "$THRESHOLD" ]; then
echo "Maximum threshold is: $THRESHOLD %"
echo '`golint` FAIL'
exit 1
fi
echo PASS