Make pre-commit more portable and less tricky.

This commit is contained in:
jregan
2019-09-24 20:29:03 -07:00
parent 7a0946a922
commit 9d7b65446f
4 changed files with 121 additions and 58 deletions

View File

@@ -1,15 +1,38 @@
#!/bin/bash
set -e
# 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
# Tracks success or failure of various operations.
# 0==success, any other value is a failure.
rcAccumulator=0
# Not used, and not cross platform,
# but kept because I don't want to have to
# look it up again.
function installHelm {
wget https://storage.googleapis.com/kubernetes-helm/helm-v2.13.1-linux-amd64.tar.gz
tar -xvzf helm-v2.13.1-linux-amd64.tar.gz
sudo mv linux-amd64/helm /usr/local/bin/helm
}
rc=0
function doInstall {
local tool=$1
shift
if hash $tool 2>/dev/null; then
echo "$tool available."
else
echo "Installing ${tool}..."
eval $@
fi
}
function installTools {
doInstall mdrip \
'go get -u github.com/monopole/mdrip'
doInstall golangci-lint \
'curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.17.1'
doInstall pluginator \
'go install sigs.k8s.io/kustomize/pluginator'
}
function runFunc {
local name=$1
@@ -17,7 +40,7 @@ function runFunc {
printf "============== begin %s\n" "$name"
$name
local code=$?
rc=$((rc || $code))
rcAccumulator=$((rcAccumulator || $code))
if [ $code -ne 0 ]; then
result="FAILURE"
fi
@@ -35,7 +58,7 @@ function testGoTests {
echo " "
echo Not on travis, so running the notravis Go tests
echo " "
# Requires helm.
# At the moment not asking travis to install it.
go test -v sigs.k8s.io/kustomize/v3/pkg/target \
@@ -53,92 +76,116 @@ function testGoTests {
}
function testExamplesAgainstLatestRelease {
/bin/rm -f $HOME/go/bin/kustomize
/bin/rm -f $(go env GOPATH)/bin/kustomize
# Install latest release.
go get sigs.k8s.io/kustomize/v3/cmd/kustomize
PATH=$HOME/go/bin:$PATH \
mdrip --mode test --label testAgainstLatestRelease ./examples
mdrip --mode test --label testAgainstLatestRelease ./examples
if [ -z ${TRAVIS+x} ]; then
echo " "
echo Not on travis, so running the notravis example tests
echo " "
# Requires helm. At the moment not asking travis to install it.
PATH=$HOME/go/bin:$PATH \
mdrip --mode test --label helmtest README.md ./examples/chart.md
# The following requires helm.
# At the moment not asking travis to install it.
mdrip --mode test --label helmtest README.md ./examples/chart.md
fi
}
function testExamplesAgainstHead {
/bin/rm -f $HOME/go/bin/kustomize
/bin/rm -f $(go env GOPATH)/bin/kustomize
# Install from head.
go install sigs.k8s.io/kustomize/v3/cmd/kustomize
# To test examples of unreleased features, add
# examples with code blocks annotated with some
# label _other than_ @testAgainstLatestRelease.
PATH=$HOME/go/bin:$PATH \
mdrip --mode test --label testAgainstLatestRelease ./examples
mdrip --mode test --label testAgainstLatestRelease ./examples
}
function generateCode {
./plugin/generateBuiltins.sh $oldGoPath
./plugin/generateBuiltins.sh $preferredGoPath
}
# Use of GOPATH is optional if go modules are
# used. This script tries to work for people who
# don't have GOPATH set, and work for travis.
#
# Upon entry, travis has GOPATH set, and used it
# to install mdrip and the like.
# This script tries to work for both travis
# and contributors who have or do not have
# GOPATH set.
#
# Use GOPATH to define XDG_CONFIG_HOME, then unset
# GOPATH so that go.mod is unambiguously honored.
echo "GOPATH=$GOPATH"
if [ -z ${GOPATH+x} ]; then
echo GOPATH is unset
tmp=$HOME/gopath
if [ -d "$tmp" ]; then
oldGoPath=$tmp
else
tmp=$HOME/go
function setPreferredGoPathAndUnsetGoPath {
preferredGoPath=$GOPATH
if [ -z ${GOPATH+x} ]; then
# GOPATH is unset
local tmp=$HOME/gopath
if [ -d "$tmp" ]; then
oldGoPath=$tmp
preferredGoPath=$tmp
else
# this works even if GOPATH undefined.
preferredGoPath=$(go env GOPATH)
fi
else
unset GOPATH
fi
else
oldGoPath=$GOPATH
unset GOPATH
fi
echo "oldGoPath=$oldGoPath"
export XDG_CONFIG_HOME=$oldGoPath/src/sigs.k8s.io
echo "XDG_CONFIG_HOME=$XDG_CONFIG_HOME"
if [ ! -d "$XDG_CONFIG_HOME" ]; then
echo "$XDG_CONFIG_HOME is not a directory."
exit 1
fi
if [ -z ${GOPATH+x} ]; then
echo GOPATH is unset
else
echo "GOPATH=$GOPATH, but should be unset at this point."
exit 1
fi
echo "preferredGoPath=$preferredGoPath"
}
# Until go v1.13, set this explicitly.
export GO111MODULE=on
# We don't want GOPATH to be defined, as it
# has too much baggage.
setPreferredGoPathAndUnsetGoPath
# This is needed for plugins.
export XDG_CONFIG_HOME=$preferredGoPath/src/sigs.k8s.io
echo "XDG_CONFIG_HOME=$XDG_CONFIG_HOME"
if [ ! -d "$XDG_CONFIG_HOME" ]; then
echo "$XDG_CONFIG_HOME is not a directory."
echo "Unable to compile or otherwise work with kustomize plugins."
exit 1
fi
# With GOPATH now undefined, this most
# likely this puts $HOME/go/bin on the path.
# Regardless, subsequent go tool installs will
# be placing binaries in this location.
PATH=$(go env GOPATH)/bin:$PATH
# 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
}
echo "HOME=$HOME"
echo "GOPATH=$GOPATH"
echo "GO111MODULE=$GO111MODULE"
echo "PATH=$PATH"
echo pwd=`pwd`
echo " "
echo "Working..."
runFunc installTools
runFunc generateCode
runFunc testGoLangCILint
runFunc testGoTests
runFunc testExamplesAgainstLatestRelease
runFunc testExamplesAgainstHead
if [ $rc -eq 0 ]; then
if [ $rcAccumulator -eq 0 ]; then
echo "SUCCESS!"
else
echo "FAILURE; exit code $rc"
echo "FAILURE; exit code $rcAccumulator"
fi
exit $rc
exit $rcAccumulator