diff --git a/test/test-bashfmt.sh b/test/test-bashfmt.sh index b591ab68..1ff3a977 100755 --- a/test/test-bashfmt.sh +++ b/test/test-bashfmt.sh @@ -1,6 +1,9 @@ #!/bin/bash # check for any bash files that aren't properly formatted # TODO: this is hardly exhaustive + +. test/util.sh + echo running test-bashfmt.sh set -o errexit set -o nounset @@ -24,8 +27,5 @@ bad_files=$( ) if [[ -n "${bad_files}" ]]; then - echo 'FAIL' - echo 'The following bash files are not properly formatted:' - echo "${bad_files}" - exit 1 + fail_test "The following bash files are not properly formatted: ${bad_files}" fi diff --git a/test/test-gofmt.sh b/test/test-gofmt.sh index 920fd413..085fee87 100755 --- a/test/test-gofmt.sh +++ b/test/test-gofmt.sh @@ -1,5 +1,8 @@ #!/bin/bash # original version of this script from kubernetes project, under ALv2 license + +. test/util.sh + echo running test-gofmt.sh set -o errexit set -o nounset @@ -10,8 +13,7 @@ ROOT=$(dirname "${BASH_SOURCE}")/.. GO_VERSION=($(go version)) if [[ -z $(echo "${GO_VERSION[2]}" | grep -E 'go1.2|go1.3|go1.4|go1.5|go1.6|go1.7|go1.8|devel') ]]; then - echo "Unknown go version '${GO_VERSION[2]}', failing gofmt." - exit 1 + fail_test "Unknown go version '${GO_VERSION[2]}', failing gofmt." fi cd "${ROOT}" @@ -23,8 +25,5 @@ find_files() { GOFMT="gofmt" # we prefer to not use the -s flag, which is pretty annoying... bad_files=$(find_files | xargs $GOFMT -l) if [[ -n "${bad_files}" ]]; then - echo 'FAIL' - echo 'The following golang files are not properly formatted:' - echo "${bad_files}" - exit 1 + fail_test "The following golang files are not properly formatted: ${bad_files}" fi diff --git a/test/test-golint.sh b/test/test-golint.sh index 19ecb759..5d61de00 100755 --- a/test/test-golint.sh +++ b/test/test-golint.sh @@ -1,5 +1,8 @@ #!/bin/bash # check that go lint passes or doesn't get worse by some threshold + +. test/util.sh + echo running test-golint.sh # TODO: output a diff of what has changed in the golint output # FIXME: test a range of commits, since only the last patch is checked here @@ -26,10 +29,10 @@ COUNT=`echo -e "$LINT" | wc -l` # number of golint problems in current branch echo "$LINT" # display the issues T=`mktemp --tmpdir -d tmp.XXX` -[ "$T" = "" ] && exit 1 -cd $T || exit 1 +[ "$T" = "" ] && fail_test "Could not create tmpdir" +cd $T || fail_test "Could not change into tmpdir $T" git clone --recursive "${ROOT}" 2>/dev/null # make a copy -cd "`basename ${ROOT}`" >/dev/null || exit 1 +cd "`basename ${ROOT}`" >/dev/null || fail_test "Could not determine basename for the repo root '$ROOT'" if [ "$HACK" != "" ]; then # ensure master branch really exists when cloning from a branched repo! git checkout master &>/dev/null && git checkout - &>/dev/null @@ -62,7 +65,6 @@ echo "Curr. # of issues: $COUNT" echo "Issue count delta is: $DELTA %" if [ "$DELTA" -gt "$THRESHOLD" ]; then echo "Maximum threshold is: $THRESHOLD %" - echo '`golint` FAIL' - exit 1 + fail_test "`golint` - FAILED" fi echo PASS diff --git a/test/test-govet.sh b/test/test-govet.sh index 4595648f..debd24b7 100755 --- a/test/test-govet.sh +++ b/test/test-govet.sh @@ -1,12 +1,15 @@ #!/bin/bash # check that go vet passes + +. test/util.sh + echo running test-govet.sh ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" # dir! cd "${ROOT}" for file in `find . -maxdepth 3 -type f -name '*.go' -not -path './old/*' -not -path './tmp/*'`; do - go vet "$file" && echo PASS || exit 1 # since it doesn't output an ok message on pass - grep 'log.' "$file" | grep '\\n"' && echo 'no \n needed in log.Printf()' && exit 1 || echo PASS # no \n needed in log.Printf() - grep 'case _ = <-' "$file" && echo 'case _ = <- can be simplified to: case <-' && exit 1 || echo PASS # this can be simplified - grep -Ei "[\/]+[\/]+[ ]*+(FIXME[^:]|TODO[^:]|XXX[^:])" "$file" && echo 'Token is missing a colon' && exit 1 || echo PASS # tokens must end with a colon + go vet "$file" && echo PASS || fail_test "go vet did not pass" # since it doesn't output an ok message on pass + grep 'log.' "$file" | grep '\\n"' && fail_test 'no \n needed in log.Printf()' || echo PASS # no \n needed in log.Printf() + grep 'case _ = <-' "$file" && fail_test 'case _ = <- can be simplified to: case <-' || echo PASS # this can be simplified + grep -Ei "[\/]+[\/]+[ ]*+(FIXME[^:]|TODO[^:]|XXX[^:])" "$file" && fail_test 'Token is missing a colon' || echo PASS # tokens must end with a colon done diff --git a/test/test-headerfmt.sh b/test/test-headerfmt.sh index 7f5fcbcd..6b26c963 100755 --- a/test/test-headerfmt.sh +++ b/test/test-headerfmt.sh @@ -1,5 +1,8 @@ #!/bin/bash # check that headers are properly formatted + +. test/util.sh + echo running test-headerfmt.sh ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" # dir! FILE="${ROOT}/main.go" # file headers should match main.go @@ -23,8 +26,5 @@ bad_files=$( ) if [[ -n "${bad_files}" ]]; then - echo 'FAIL' - echo 'The following file headers are not properly formatted:' - echo "${bad_files}" - exit 1 + fail_test "The following file headers are not properly formatted: ${bad_files}" fi diff --git a/test/test-shell.sh b/test/test-shell.sh index e9d039a3..2ade9cb6 100755 --- a/test/test-shell.sh +++ b/test/test-shell.sh @@ -1,6 +1,9 @@ #!/bin/bash # simple test harness for testing mgmt # NOTE: this will rm -rf /tmp/mgmt/ + +. test/util.sh + if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then echo -e "usage: ./"`basename $0`" [[--help] | ]" echo -e "where: is empty to run all tests, or .sh from shell/ dir" @@ -65,9 +68,7 @@ rm -f "$MGMT" make clean if [ "$count" = '0' ]; then - echo 'FAIL' - echo 'No tests were run!' - exit 1 + fail_test 'No tests were run!' fi # display errors diff --git a/test/test-yamlfmt.sh b/test/test-yamlfmt.sh index 0d9bca5a..37549b7c 100755 --- a/test/test-yamlfmt.sh +++ b/test/test-yamlfmt.sh @@ -1,5 +1,8 @@ #!/bin/bash # check for any yaml files that aren't properly formatted + +. test/util.sh + echo running test-yamlfmt.sh set -o errexit set -o nounset @@ -14,14 +17,15 @@ ROOT=$(dirname "${BASH_SOURCE}")/.. RUBY=`which ruby 2>/dev/null` if [ -z $RUBY ]; then - echo "The 'ruby' utility can't be found." - exit 1 + fail_test "The 'ruby' utility can't be found." fi -$RUBY -e "require 'yaml'" 2>/dev/null || ( - echo "The ruby 'yaml' library can't be found." - exit 1 -) +$RUBY -e "require 'yaml'" 2>/dev/null || fail_test "The ruby 'yaml' library can't be found." + +if $RUBY -e "puts RUBY_VERSION" | grep -q ^1 ; then + echo "SKIPPING - cannot test YAML formatting with Ruby 1.x" + exit 0 +fi cd "${ROOT}" @@ -38,8 +42,5 @@ bad_files=$( ) if [[ -n "${bad_files}" ]]; then - echo 'FAIL' - echo 'The following yaml files are not properly formatted:' - echo "${bad_files}" - exit 1 + fail_test "The following yaml files are not properly formatted: ${bad_files}" fi diff --git a/test/util.sh b/test/util.sh new file mode 100644 index 00000000..da922a17 --- /dev/null +++ b/test/util.sh @@ -0,0 +1,7 @@ +# common settings and functions for test scripts + +fail_test() +{ + echo "FAIL: $@" + exit 1 +}