diff --git a/bin/consider-early-travis-exit.sh b/bin/consider-early-travis-exit.sh new file mode 100644 index 000000000..eea852bcf --- /dev/null +++ b/bin/consider-early-travis-exit.sh @@ -0,0 +1,37 @@ +# Exits with status 0 if it can be determined that the +# current PR should not trigger all travis checks. +# +# This could be done with a "git ...|grep -vqE" oneliner +# but as travis triggering is refined it's useful to check +# travis logs to see how branch files were considered. +function consider-early-travis-exit { + if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then + echo "Unknown pull request." + return + fi + # Might use this to improve checks on multi-commit PRs. + echo "TRAVIS_COMMIT_RANGE=$TRAVIS_COMMIT_RANGE" + echo "Branch Files ('T'==trigger tests, ' '=ignore):" + echo "---" + local triggers=0 + local invisibles=0 + for fn in $(git diff --name-only HEAD origin/master); do + if [[ "$fn" =~ (\.md$)|(^docs/) ]]; then + echo " $fn" + let invisibles+=1 + else + echo " T $fn" + let triggers+=1 + fi + done + echo "---" + printf >&2 "%6d files invisible to travis.\n" $invisibles + printf >&2 "%6d files trigger travis.\n" $triggers + if [ $triggers -eq 0 ]; then + echo "No files triggered travis test, exiting early." + # see https://github.com/travis-ci/travis-build/blob/master/lib/travis/build/templates/header.sh + travis_terminate 0 + fi +} +consider-early-travis-exit +unset -f consider-early-travis-exit diff --git a/bin/pre-commit.sh b/bin/pre-commit.sh new file mode 100755 index 000000000..044720b4a --- /dev/null +++ b/bin/pre-commit.sh @@ -0,0 +1,56 @@ +#!/bin/bash + +# Make sure, we run in the root of the repo and +# therefore run the tests on all packages +base_dir="$( cd "$(dirname "$0")/.." && pwd )" +cd "$base_dir" || { + echo "Cannot cd to '$base_dir'. Aborting." >&2 + exit 1 +} + +rc=0 + +function go_dirs { + go list -f '{{.Dir}}' ./... | tr '\n' '\0' +} + +function runTest { + local name=$1 + local result="SUCCESS" + printf "============== begin %s\n" "$name" + $name + local code=$? + rc=$((rc || $code)) + if [ $code -ne 0 ]; then + result="FAILURE" + fi + printf "============== end %s : %s code=%d\n\n\n" "$name" "$result" $code +} + +function testGoFmt { + diff <(echo -n) <(go_dirs | xargs -0 gofmt -s -d -l) +} + +function testGoImports { + diff -u <(echo -n) <(go_dirs | xargs -0 goimports -l) +} + +function testGoVet { + go vet -all ./... +} + +function testGoTest { + go test -v ./... +} + +function testTutorial { + mdrip --mode test --label test ./cmd/kustomize +} + +runTest testGoFmt +runTest testGoImports +runTest testGoVet +runTest testGoTest +runTest testTutorial + +exit $rc