From f7c40d3bb45c13377fa446bcc3821a2ca04be776 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Fri, 11 Jan 2019 17:38:10 -0800 Subject: [PATCH] golint/gofmt only local .go files not in vendor/ Also, do not fail on the first failed check. --- scripts/travis_checker.sh | 85 +++++++++++++++++++++++++++++++++------ 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/scripts/travis_checker.sh b/scripts/travis_checker.sh index 1e01e9f5c..717198f5f 100755 --- a/scripts/travis_checker.sh +++ b/scripts/travis_checker.sh @@ -1,19 +1,78 @@ #!/bin/bash -go test ./... -if [ $? -ne 0 ]; then - echo "Some tests failed" - exit 1 +# Print a file surrounded by BEGIN/END preamble, e.g.: +# +# --- BEGIN a.out output --- +# Hello world +# --- END a.out output --- +# +print_file() { + echo "--- BEGIN ${2} ---" + cat "${1}" + echo "--- END ${2} ---" +} + +unset -v ok tmpdir go_files go_dirs gofmt_output golint_output +ok=true +tmpdir= +trap 'case "${tmpdir}" in ?*) rm -rf "${tmpdir}";; esac' EXIT +tmpdir=$(mktemp -d) + +go_files="${tmpdir}/go_files.txt" +git ls-files '*.go' | grep -v '^vendor/' > "${go_files}" + +# Print dirname of each relative pathname from stdin (one per line). +dirnames() { + # pathname dirname + # ---------------------- + # a/b/c.go a/b + # c.go . + sed \ + -e 's:^:./:' \ + -e 's:/[^/]*$::' \ + -e 's:^\./::' +} + +go_dirs="${tmpdir}/go_dirs.txt" +dirnames < "${go_files}" | sort -u -t/ > "${go_dirs}" + +echo "Running go test..." +if go test ./... +then + echo "go test succeeded." +else + echo "go test FAILED!" + ok=false fi -if [ $(golint ./... | wc | awk '{print $1}') -gt 2 ]; then - echo "Go code is not formatted:" - gofmt -d . - exit 1 + +echo "Running golint..." +golint_output="${tmpdir}/golint_output.txt" +if xargs golint -set_exit_status < "${go_dirs}" > "${golint_output}" 2>&1 +then + echo "golint passed." +else + echo "golint FAILED!" + print_file "${golint_output}" "golint" + ok=false fi -if [ -n "$(gofmt -l .)" ]; then - echo "Go code is not formatted:" - gofmt -d . - exit 1 + +echo "Running gofmt..." +gofmt_output="${tmpdir}/gofmt_output.txt" +xargs gofmt -d < "${go_files}" > "${gofmt_output}" 2>&1 +if [ -s "${gofmt_output}" ] +then + echo "gofmt FAILED!" + print_file "${gofmt_output}" "gofmt" + ok=false else - echo "Go code is well formatted ;)" + echo "gofmt passed." fi + +if ! ${ok} +then + echo "Some checks failed; see output above." + exit 1 +fi + +echo "All checks passed. :-)" +exit 0