Skip to content

Instantly share code, notes, and snippets.

@mewmew
Forked from h12w/goclean.sh
Last active April 22, 2021 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mewmew/379014c9a2e6885e238d to your computer and use it in GitHub Desktop.
Save mewmew/379014c9a2e6885e238d to your computer and use it in GitHub Desktop.
#!/bin/bash
# The script does automatic checking on a Go package and its sub-packages, including:
# 1. gofmt (http://golang.org/cmd/gofmt/)
# 2. goimports (http://godoc.org/golang.org/x/tools/cmd/goimports)
# 3. golint (https://github.com/golang/lint)
# 4. go vet (http://golang.org/cmd/vet)
# 5. race detector (http://blog.golang.org/race-detector)
# 6. test coverage (http://blog.golang.org/cover)
# Automatic checks
echo "### gofmt"
test -z "$(find . -type f -name '*.go' -print0 | xargs -0 gofmt -l -s | tee /dev/stderr)"
GOFMT_FAIL=$?
echo
echo "### goimports"
test -z "$(find . -type f -name '*.go' -print0 | xargs -0 goimports -l | tee /dev/stderr)"
GOIMPORTS_FAIL=$?
echo
echo "### golint"
if [ "${GOLINT_IGNORE}" ]; then
test -z "$(echo ${GOLINT_IGNORE} | xargs find . -type f -name '*.go' | xargs -I '{}' golint "{}" | tee /dev/stderr)"
GOLINT_FAIL=$?
else
test -z "$(golint ./... | tee /dev/stderr)"
GOLINT_FAIL=$?
fi
echo
echo "### go vet"
go vet ./...
GOVET_FAIL=$?
echo
echo "### go test -race"
go test -race ./...
GOTEST_FAIL=$?
echo
if [ "${COVERALLS_TOKEN}" ]; then
echo "### go tool cover"
# Run test coverage on each subdirectories and merge the coverage profile.
echo "mode: count" > coverage.out
# Standard go tooling behaviour is to ignore directories with leading
# underscores.
for dir in $(find . -maxdepth 10 -not -path './.git*' -not -path '*/_*' -type d);
do
if ls ${dir}/*.go &> /dev/null; then
go test -covermode=count -coverprofile=${dir}/coverage.tmp ${dir}
if [ -f ${dir}/coverage.tmp ]; then
cat ${dir}/coverage.tmp | tail -n +2 >> coverage.out
rm ${dir}/coverage.tmp
fi
fi
done
go tool cover -func coverage.out
# To submit the test coverage result to coveralls.io use goveralls
# (https://github.com/mattn/goveralls)
goveralls -coverprofile coverage.out -service travis-ci -repotoken ${COVERALLS_TOKEN}
fi
RET=0
if [ ${GOFMT_FAIL} -eq 1 ] ; then
echo "gofmt: FAIL"
RET=1
else
echo "gofmt: PASS"
fi
if [ ${GOIMPORTS_FAIL} -eq 1 ] ; then
echo "goimports: FAIL"
RET=1
else
echo "goimports: PASS"
fi
if [ ${GOLINT_FAIL} -eq 1 ] ; then
echo "golint: FAIL"
RET=1
else
echo "golint: PASS"
fi
if [ ${GOVET_FAIL} -eq 1 ] ; then
echo "go vet: FAIL"
RET=1
else
echo "go vet: PASS"
fi
if [ ${GOTEST_FAIL} -eq 1 ] ; then
echo "go test: FAIL"
RET=1
else
echo "go test: PASS"
fi
exit ${RET}
@karlek
Copy link

karlek commented Nov 21, 2015

There's a conflict with goimports and godep.
godep creates a folder in your repository called Godeps. The problem being that some example code, not used by the application, could contain an arbitrary import that you don't have.

For example: https://raw.githubusercontent.com/Sirupsen/logrus/master/examples/hook/hook.go

@karlek
Copy link

karlek commented Nov 21, 2015

line 16:

$ test -z "$(goimports -l -w . | tee /dev/stderr)"

could instead be:

$ test -z "$(goimports -l -w . | grep --invert-match "Godeps/*" | tee /dev/stderr)"

@mewmew
Copy link
Author

mewmew commented Feb 4, 2016

As of Go 1.6, vendor directories have been enabled by default, Godep paths have thus been superseded by vendor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment