diff --git a/README.md b/README.md index 32ddb57b1e..e570ef7177 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,15 @@ Included in this repo are tools shared by weave.git and scope.git. They include - ```cover```: a tool which merges overlapping coverage reports generated by go test +- ```files-with-type```: a tool to search directories for files of a given + MIME type - ```lint```: a script to lint Go project; runs various tools like golint, go vet, errcheck etc - ```rebuild-image```: a script to rebuild docker images when their input files change; useful when you using docker images to build your software, but you don't want to build the image every time. +- ```shell-lint```: a script to lint multiple shell files with + [shellcheck](http://www.shellcheck.net/) - ```socks```: a simple, dockerised SOCKS proxy for getting your laptop onto the Weave network - ```test```: a script to run all go unit tests in subdirectories, gather the diff --git a/cover/gather_coverage.sh b/cover/gather_coverage.sh index 9026745a1f..271ac7d407 100755 --- a/cover/gather_coverage.sh +++ b/cover/gather_coverage.sh @@ -3,19 +3,18 @@ # merges them and produces a complete report. set -ex -DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" DESTINATION=$1 FROMDIR=$2 -mkdir -p $DESTINATION +mkdir -p "$DESTINATION" if [ -n "$CIRCLECI" ]; then - for i in $(seq 1 $(($CIRCLE_NODE_TOTAL - 1))); do - scp node$i:$FROMDIR/* $DESTINATION || true + for i in $(seq 1 $((CIRCLE_NODE_TOTAL - 1))); do + scp "node$i:$FROMDIR"/* "$DESTINATION" || true done fi go get github.com/weaveworks/build-tools/cover -cover $DESTINATION/* >profile.cov +cover "$DESTINATION"/* >profile.cov go tool cover -html=profile.cov -o coverage.html go tool cover -func=profile.cov -o coverage.txt -tar czf coverage.tar.gz $DESTINATION +tar czf coverage.tar.gz "$DESTINATION" diff --git a/files-with-type b/files-with-type new file mode 100755 index 0000000000..d969f44052 --- /dev/null +++ b/files-with-type @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# +# Find all files with a given MIME type. +# +# e.g. +# $ files-with-type text/x-shellscript k8s infra +# +# Assumes `find`, `xargs`, and `file` are all installed. + +mime_type=$1 +shift + +find "$@" -print0 -type f |xargs -0 file --mime-type | grep "${mime_type}" | sed -e 's/:.*$//' diff --git a/integration/assert.sh b/integration/assert.sh index 1a2f1f7788..da689b3eb9 100644 --- a/integration/assert.sh +++ b/integration/assert.sh @@ -24,14 +24,14 @@ export INVARIANT=${INVARIANT:-} export CONTINUE=${CONTINUE:-} args="$(getopt -n "$0" -l \ - verbose,help,stop,discover,invariant,continue vhxdic $*)" \ + verbose,help,stop,discover,invariant,continue vhxdic "$@")" \ || exit -1 for arg in $args; do case "$arg" in -h) echo "$0 [-vxidc]" \ "[--verbose] [--stop] [--invariant] [--discover] [--continue]" - echo "`sed 's/./ /g' <<< "$0"` [-h] [--help]" + echo "$(sed 's/./ /g' <<< "$0") [-h] [--help]" exit 0;; --help) cat < /dev/null 2>&1 || status=$? + (eval "$1" <<< "${3:-}") > /dev/null 2>&1 || status=$? expected=${2:-0} if [[ "$status" -eq "$expected" ]]; then [[ -z "$DEBUG" ]] || echo -n . @@ -143,7 +143,7 @@ _assert_fail() { skip_if() { # skip_if - (eval $@) > /dev/null 2>&1 && status=0 || status=$? + (eval "$@") > /dev/null 2>&1 && status=0 || status=$? [[ "$status" -eq 0 ]] || return skip } diff --git a/integration/gce.sh b/integration/gce.sh index bb6f95efaa..1f1019fcee 100755 --- a/integration/gce.sh +++ b/integration/gce.sh @@ -7,15 +7,15 @@ set -e -: ${KEY_FILE:=/tmp/gce_private_key.json} -: ${SSH_KEY_FILE:=$HOME/.ssh/gce_ssh_key} -: ${IMAGE:=ubuntu-14-04} -: ${ZONE:=us-central1-a} -: ${PROJECT:=} -: ${TEMPLATE_NAME:=} -: ${NUM_HOSTS:=} - -if [ -z "${PROJECT}" -o -z "${NUM_HOSTS}" -o -z "${TEMPLATE_NAME}" ]; then +: "${KEY_FILE:=/tmp/gce_private_key.json}" +: "${SSH_KEY_FILE:=$HOME/.ssh/gce_ssh_key}" +: "${IMAGE:=ubuntu-14-04}" +: "${ZONE:=us-central1-a}" +: "${PROJECT:=}" +: "${TEMPLATE_NAME:=}" +: "${NUM_HOSTS:=}" + +if [ -z "${PROJECT}" ] || [ -z "${NUM_HOSTS}" ] || [ -z "${TEMPLATE_NAME}" ]; then echo "Must specify PROJECT, NUM_HOSTS and TEMPLATE_NAME" exit 1 fi @@ -26,21 +26,21 @@ if [ -n "$CIRCLECI" ]; then fi # Setup authentication -gcloud auth activate-service-account --key-file $KEY_FILE 1>/dev/null -gcloud config set project $PROJECT +gcloud auth activate-service-account --key-file "$KEY_FILE" 1>/dev/null +gcloud config set project "$PROJECT" function vm_names { local names= - for i in $(seq 1 $NUM_HOSTS); do - names="host$i$SUFFIX $names" + for i in $(seq 1 "$NUM_HOSTS"); do + names=( "host$i$SUFFIX" "${names[@]}" ) done - echo "$names" + echo "${names[@]}" } # Delete all vms in this account function destroy { names="$(vm_names)" - if [ $(gcloud compute instances list --zone $ZONE -q $names | wc -l) -le 1 ] ; then + if [ "$(gcloud compute instances list --zone "$ZONE" -q "$names" | wc -l)" -le 1 ] ; then return 0 fi for i in {0..10}; do @@ -60,23 +60,23 @@ function destroy { } function internal_ip { - jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].networkIP" $1 + jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].networkIP" "$1" } function external_ip { - jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].accessConfigs[0].natIP" $1 + jq -r ".[] | select(.name == \"$2\") | .networkInterfaces[0].accessConfigs[0].natIP" "$1" } function try_connect { for i in {0..10}; do - ssh -t $1 true && return + ssh -t "$1" true && return sleep 2 done } function install_docker_on { name=$1 - ssh -t $name sudo bash -x -s <>/etc/hosts\"" + ssh -t "$hostname" "sudo -- sh -c \"cat >>/etc/hosts\"" < "$hosts" } # Create new set of VMs function setup { destroy - names="$(vm_names)" - gcloud compute instances create $names --image $TEMPLATE_NAME --zone $ZONE - gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE + names=( $(vm_names) ) + gcloud compute instances create "${names[@]}" --image "$TEMPLATE_NAME" --zone "$ZONE" + gcloud compute config-ssh --ssh-key-file "$SSH_KEY_FILE" sed -i '/UserKnownHostsFile=\/dev\/null/d' ~/.ssh/config # build an /etc/hosts file for these vms hosts=$(mktemp hosts.XXXXXXXXXX) json=$(mktemp json.XXXXXXXXXX) - gcloud compute instances list --format=json >$json - for name in $names; do - echo "$(internal_ip $json $name) $name.$ZONE.$PROJECT" >>$hosts + gcloud compute instances list --format=json > "$json" + for name in "${names[@]}"; do + echo "$(internal_ip "$json" "$name") $name.$ZONE.$PROJECT" >> "$hosts" done - for name in $names; do + for name in "${names[@]}"; do hostname="$name.$ZONE.$PROJECT" # Add the remote ip to the local /etc/hosts sudo sed -i "/$hostname/d" /etc/hosts - sudo sh -c "echo \"$(external_ip $json $name) $hostname\" >>/etc/hosts" - try_connect $hostname + sudo sh -c "echo \"$(external_ip "$json" "$name") $hostname\" >>/etc/hosts" + try_connect "$hostname" - copy_hosts $hostname $hosts & + copy_hosts "$hostname" "$hosts" & done wait - rm $hosts $json + rm "$hosts" "$json" } function make_template { - gcloud compute instances create $TEMPLATE_NAME --image $IMAGE --zone $ZONE - gcloud compute config-ssh --ssh-key-file $SSH_KEY_FILE + gcloud compute instances create "$TEMPLATE_NAME" --image "$IMAGE" --zone "$ZONE" + gcloud compute config-ssh --ssh-key-file "$SSH_KEY_FILE" name="$TEMPLATE_NAME.$ZONE.$PROJECT" - try_connect $name - install_docker_on $name - gcloud -q compute instances delete $TEMPLATE_NAME --keep-disks boot --zone $ZONE - gcloud compute images create $TEMPLATE_NAME --source-disk $TEMPLATE_NAME --source-disk-zone $ZONE + try_connect "$name" + install_docker_on "$name" + gcloud -q compute instances delete "$TEMPLATE_NAME" --keep-disks boot --zone "$ZONE" + gcloud compute images create "$TEMPLATE_NAME" --source-disk "$TEMPLATE_NAME" --source-disk-zone "$ZONE" } function hosts { hosts= args= json=$(mktemp json.XXXXXXXXXX) - gcloud compute instances list --format=json >$json + gcloud compute instances list --format=json > "$json" for name in $(vm_names); do hostname="$name.$ZONE.$PROJECT" - hosts="$hostname $hosts" - args="--add-host=$hostname:$(internal_ip $json $name) $args" + hosts=( $hostname "${hosts[@]}" ) + args=( "--add-host=$hostname:$(internal_ip "$json" "$name")" "${args[@]}" ) done echo export SSH=\"ssh -l vagrant\" - echo export HOSTS=\"$hosts\" - echo export ADD_HOST_ARGS=\"$args\" - rm $json + echo "export HOSTS=\"${hosts[*]}\"" + echo "export ADD_HOST_ARGS=\"${args[*]}\"" + rm "$json" } case "$1" in @@ -170,7 +170,7 @@ destroy) make_template) # see if template exists - if ! gcloud compute images list | grep $PROJECT | grep $TEMPLATE_NAME; then + if ! gcloud compute images list | grep "$PROJECT" | grep "$TEMPLATE_NAME"; then make_template fi esac diff --git a/integration/run_all.sh b/integration/run_all.sh index 13cb82c412..cb6d93d2c2 100755 --- a/integration/run_all.sh +++ b/integration/run_all.sh @@ -1,6 +1,9 @@ #!/bin/bash +set -ex + DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +# shellcheck disable=SC1090 . "$DIR/config.sh" whitely echo Sanity checks @@ -10,18 +13,19 @@ if ! bash "$DIR/sanity_check.sh"; then fi whitely echo ...ok -TESTS="${@:-$(find . -name '*_test.sh')}" -RUNNER_ARGS="" +# shellcheck disable=SC2068 +TESTS=( ${@:-$(find . -name '*_test.sh')} ) +RUNNER_ARGS=( ) # If running on circle, use the scheduler to work out what tests to run -if [ -n "$CIRCLECI" -a -z "$NO_SCHEDULER" ]; then - RUNNER_ARGS="$RUNNER_ARGS -scheduler" +if [ -n "$CIRCLECI" ] && [ -z "$NO_SCHEDULER" ]; then + RUNNER_ARGS=( "${RUNNER_ARGS[@]}" -scheduler ) fi # If running on circle or PARALLEL is not empty, run tests in parallel -if [ -n "$CIRCLECI" -o -n "$PARALLEL" ]; then - RUNNER_ARGS="$RUNNER_ARGS -parallel" +if [ -n "$CIRCLECI" ] || [ -n "$PARALLEL" ]; then + RUNNER_ARGS=( "${RUNNER_ARGS[@]}" -parallel ) fi -make -C ${DIR}/../runner -HOSTS="$HOSTS" "${DIR}/../runner/runner" $RUNNER_ARGS $TESTS +make -C "${DIR}/../runner" +HOSTS="$HOSTS" "${DIR}/../runner/runner" "${RUNNER_ARGS[@]}" "${TESTS[@]}" diff --git a/integration/sanity_check.sh b/integration/sanity_check.sh index 592a6b77c1..c88337fe80 100755 --- a/integration/sanity_check.sh +++ b/integration/sanity_check.sh @@ -1,5 +1,5 @@ #! /bin/bash - +# shellcheck disable=SC1091 . ./config.sh set -e @@ -7,7 +7,7 @@ set -e whitely echo Ping each host from the other for host in $HOSTS; do for other in $HOSTS; do - [ $host = $other ] || run_on $host $PING $other + [ "$host" = "$other" ] || run_on "$host" "$PING" "$other" done done @@ -15,12 +15,12 @@ whitely echo Check we can reach docker for host in $HOSTS; do echo - echo Host Version Info: $host - echo ===================================== + echo "Host Version Info: $host" + echo "=====================================" echo "# docker version" - docker_on $host version + docker_on "$host" version echo "# docker info" - docker_on $host info + docker_on "$host" info echo "# weave version" - weave_on $host version + weave_on "$host" version done diff --git a/lint b/lint index 771ea4a60c..e807f88241 100755 --- a/lint +++ b/lint @@ -1,7 +1,7 @@ #!/bin/bash # This scipt lints go files for common errors. # -# Its runs gofmt and go vet, and optionally golint and +# It runs gofmt and go vet, and optionally golint and # gocyclo, if they are installed. # # With no arguments, it lints the current files staged @@ -41,7 +41,7 @@ function spell_check { local lint_result=0 # we don't want to spell check tar balls or binaries - if file $filename | grep executable >/dev/null 2>&1; then + if file "$filename" | grep executable >/dev/null 2>&1; then return $lint_result fi if [[ $filename == *".tar" || $filename == *".gz" ]]; then @@ -63,11 +63,11 @@ function spell_check { function test_mismatch { filename="$1" - package=$(grep '^package ' $filename | awk '{print $2}') + package=$(grep '^package ' "$filename" | awk '{print $2}') local lint_result=0 if [[ $package == "main" ]]; then - continue # in package main, all bets are off + return # in package main, all bets are off fi if [[ $filename == *"_internal_test.go" ]]; then @@ -115,7 +115,7 @@ function lint_go { # don't have it installed. Also never blocks a commit, # it just warns. if type gocyclo >/dev/null 2>&1; then - gocyclo -over 25 "${filename}" | while read line; do + gocyclo -over 25 "${filename}" | while read -r line; do echo "${filename}": higher than 25 cyclomatic complexity - "${line}" done fi @@ -158,7 +158,7 @@ function lint { function lint_files { local lint_result=0 - while read filename; do + while read -r filename; do lint "${filename}" || lint_result=1 done exit $lint_result diff --git a/publish-site b/publish-site index 4d0984b928..900eb7cb69 100755 --- a/publish-site +++ b/publish-site @@ -3,7 +3,7 @@ set -e set -o pipefail -: ${PRODUCT:=} +: "${PRODUCT:=}" fatal() { echo "$@" >&2 diff --git a/rebuild-image b/rebuild-image index 1e00cbbe08..a579a47de6 100755 --- a/rebuild-image +++ b/rebuild-image @@ -5,37 +5,39 @@ set -eux IMAGENAME=$1 -SAVEDNAME=$(echo $IMAGENAME | sed "s/[\/\-]/\./g") +# shellcheck disable=SC2001 +SAVEDNAME=$(echo "$IMAGENAME" | sed "s/[\/\-]/\./g") IMAGEDIR=$2 shift 2 -INPUTFILES=$@ +INPUTFILES=( "$@" ) CACHEDIR=$HOME/docker/ # Rebuild the image rebuild() { - mkdir -p $CACHEDIR - rm $CACHEDIR/$SAVEDNAME* || true - docker build -t $IMAGENAME $IMAGEDIR - docker save $IMAGENAME:latest | gzip - > $CACHEDIR/$SAVEDNAME-$CIRCLE_SHA1.gz + mkdir -p "$CACHEDIR" + rm "$CACHEDIR/$SAVEDNAME"* || true + docker build -t "$IMAGENAME" "$IMAGEDIR" + docker save "$IMAGENAME:latest" | gzip - > "$CACHEDIR/$SAVEDNAME-$CIRCLE_SHA1.gz" } # Get the revision the cached image was build at cached_image_rev() { - find $CACHEDIR -name "$SAVEDNAME-*" -type f | sed -n 's/^[^\-]*\-\([a-z0-9]*\).gz$/\1/p' + find "$CACHEDIR" -name "$SAVEDNAME-*" -type f | sed -n 's/^[^\-]*\-\([a-z0-9]*\).gz$/\1/p' } # Have there been any revision between $1 and $2 has_changes() { local rev1=$1 local rev2=$2 - local changes=$(git diff --oneline $rev1..$rev2 -- $INPUTFILES | wc -l) + local changes + changes=$(git diff --oneline "$rev1..$rev2" -- "${INPUTFILES[@]}" | wc -l) [ "$changes" -gt 0 ] } commit_timestamp() { local rev=$1 - git show -s --format=%ct $rev + git show -s --format=%ct "$rev" } cached_revision=$(cached_image_rev) @@ -46,14 +48,14 @@ if [ -z "$cached_revision" ]; then fi echo ">>> Found cached image rev $cached_revision" -if has_changes $cached_revision $CIRCLE_SHA1 ; then +if has_changes "$cached_revision" "$CIRCLE_SHA1" ; then echo ">>> Found changes, rebuilding" rebuild exit 0 fi IMAGE_TIMEOUT="$(( 3 * 24 * 60 * 60 ))" -if [ "$(commit_timestamp $cached_revision)" -lt "${IMAGE_TIMEOUT}" ]; then +if [ "$(commit_timestamp "$cached_revision")" -lt "${IMAGE_TIMEOUT}" ]; then echo ">>> Image is more the 24hrs old; rebuilding" rebuild exit 0 @@ -61,4 +63,4 @@ fi # we didn't rebuild; import cached version echo ">>> No changes found, importing cached image" -zcat $CACHEDIR/$SAVEDNAME-$cached_revision.gz | docker load +zcat "$CACHEDIR/$SAVEDNAME-$cached_revision.gz" | docker load diff --git a/runner/runner.go b/runner/runner.go index 54b45feb36..707369d81f 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -262,9 +262,10 @@ func main() { verbose = true } - tests, err := getTests(mflag.Args()) + testArgs := mflag.Args() + tests, err := getTests(testArgs) if err != nil { - fmt.Printf("Error parsing tests: %v\n", err) + fmt.Printf("Error parsing tests: %v (%v)\n", err, testArgs) os.Exit(1) } diff --git a/shell-lint b/shell-lint new file mode 100755 index 0000000000..5cc77cb2a7 --- /dev/null +++ b/shell-lint @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +# +# Lint all shell files in given directories with `shellcheck`. +# +# e.g. +# $ shell-lint infra k8s +# +# Depends on: +# - shellcheck +# - files-with-type +# - file >= 5.22 + +"$(dirname "${BASH_SOURCE[0]}")/files-with-type" text/x-shellscript "$@" | xargs shellcheck diff --git a/socks/connect.sh b/socks/connect.sh index 0d5ef84da9..25dcf82218 100755 --- a/socks/connect.sh +++ b/socks/connect.sh @@ -10,14 +10,17 @@ fi HOST=$1 echo "Starting proxy container..." -PROXY_CONTAINER=$(ssh $HOST weave run -d weaveworks/socksproxy) +PROXY_CONTAINER=$(ssh "$HOST" weave run -d weaveworks/socksproxy) function finish { echo "Removing proxy container.." - ssh $HOST docker rm -f $PROXY_CONTAINER + # shellcheck disable=SC2029 + ssh "$HOST" docker rm -f "$PROXY_CONTAINER" } trap finish EXIT -PROXY_IP=$(ssh $HOST -- "docker inspect --format='{{.NetworkSettings.IPAddress}}' $PROXY_CONTAINER") +# shellcheck disable=SC2029 +PROXY_IP=$(ssh "$HOST" -- "docker inspect --format='{{.NetworkSettings.IPAddress}}' $PROXY_CONTAINER") echo 'Please configure your browser for proxy http://localhost:8080/proxy.pac' -ssh -L8000:$PROXY_IP:8000 -L8080:$PROXY_IP:8080 $HOST docker attach $PROXY_CONTAINER +# shellcheck disable=SC2029 +ssh "-L8000:$PROXY_IP:8000" "-L8080:$PROXY_IP:8080" "$HOST" docker attach "$PROXY_CONTAINER" diff --git a/test b/test index 5b88d52702..61791772b6 100755 --- a/test +++ b/test @@ -1,9 +1,9 @@ #!/bin/bash -set -e +set -ex DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" -GO_TEST_ARGS="-tags netgo -cpu 4 -timeout 8m" +GO_TEST_ARGS=( -tags netgo -cpu 4 -timeout 8m ) SLOW= NO_GO_GET= @@ -28,66 +28,67 @@ while [ $# -gt 0 ]; do esac done -if [ -n "$SLOW" -o -n "$CIRCLECI" ]; then +if [ -n "$SLOW" ] || [ -n "$CIRCLECI" ]; then SLOW=true fi if [ -n "$SLOW" ]; then - GO_TEST_ARGS="$GO_TEST_ARGS -race -covermode=atomic" + GO_TEST_ARGS=( "${GO_TEST_ARGS[@]}" -race -covermode=atomic ) + # shellcheck disable=SC2153 if [ -n "$COVERDIR" ] ; then coverdir="$COVERDIR" else coverdir=$(mktemp -d coverage.XXXXXXXXXX) fi - mkdir -p $coverdir + mkdir -p "$coverdir" fi fail=0 -TESTDIRS=$(find . -type f -name '*_test.go' | xargs -n1 dirname | grep -vE '^\./(\.git|vendor|prog|experimental)/' | sort -u) +TESTDIRS=( $(find . -type f -name '*_test.go' -print0 | xargs -0 -n1 dirname | grep -vE '^\./(\.git|vendor|prog|experimental)/' | sort -u) ) # If running on circle, use the scheduler to work out what tests to run on what shard -if [ -n "$CIRCLECI" -a -z "$NO_SCHEDULER" -a -x "$DIR/sched" ]; then +if [ -n "$CIRCLECI" ] && [ -z "$NO_SCHEDULER" ] && [ -x "$DIR/sched" ]; then PREFIX=$(go list -e ./ | sed -e 's/\//-/g') - TESTDIRS=$(echo $TESTDIRS | "$DIR/sched" sched $PREFIX-$CIRCLE_BUILD_NUM $CIRCLE_NODE_TOTAL $CIRCLE_NODE_INDEX) - echo $TESTDIRS + TESTDIRS=( $(echo "${TESTDIRS[@]}" | "$DIR/sched" sched "$PREFIX-$CIRCLE_BUILD_NUM" "$CIRCLE_NODE_TOTAL" "$CIRCLE_NODE_INDEX") ) + echo "${TESTDIRS[@]}" fi PACKAGE_BASE=$(go list -e ./) -# Speed up the tests by compiling and installing their dependancies first. -go test -i $GO_TEST_ARGS $TESTDIRS +# Speed up the tests by compiling and installing their dependencies first. +go test -i "${GO_TEST_ARGS[@]}" "${TESTDIRS[@]}" -for dir in $TESTDIRS; do +for dir in "${TESTDIRS[@]}"; do if [ -z "$NO_GO_GET" ]; then - go get -t -tags netgo $dir + go get -t -tags netgo "$dir" fi - GO_TEST_ARGS_RUN="$GO_TEST_ARGS" + GO_TEST_ARGS_RUN=( "${GO_TEST_ARGS[@]}" ) if [ -n "$SLOW" ]; then - COVERPKGS=$( (go list $dir; go list -f '{{join .Deps "\n"}}' $dir | grep -v "vendor" | grep "^$PACKAGE_BASE/") | paste -s -d, -) - output=$(mktemp $coverdir/unit.XXXXXXXXXX) - GO_TEST_ARGS_RUN="$GO_TEST_ARGS -coverprofile=$output -coverpkg=$COVERPKGS" + COVERPKGS=$( (go list "$dir"; go list -f '{{join .Deps "\n"}}' "$dir" | grep -v "vendor" | grep "^$PACKAGE_BASE/") | paste -s -d, -) + output=$(mktemp "$coverdir/unit.XXXXXXXXXX") + GO_TEST_ARGS_RUN=( "${GO_TEST_ARGS[@]}" -coverprofile=$output -coverpkg=$COVERPKGS ) fi START=$(date +%s) - if ! go test $GO_TEST_ARGS_RUN $dir; then + if ! go test "${GO_TEST_ARGS_RUN[@]}" "$dir"; then fail=1 fi - RUNTIME=$(( $(date +%s) - $START )) + RUNTIME=$(( $(date +%s) - START )) # Report test runtime when running on circle, to help scheduler - if [ -n "$CIRCLECI" -a -z "$NO_SCHEDULER" -a -x "$DIR/sched" ]; then - "$DIR/sched" time $dir $RUNTIME + if [ -n "$CIRCLECI" ] && [ -z "$NO_SCHEDULER" ] && [ -x "$DIR/sched" ]; then + "$DIR/sched" time "$dir" $RUNTIME fi done -if [ -n "$SLOW" -a -z "$COVERDIR" ] ; then +if [ -n "$SLOW" ] && [ -z "$COVERDIR" ] ; then go get github.com/weaveworks/tools/cover - cover $coverdir/* >profile.cov - rm -rf $coverdir + cover "$coverdir"/* >profile.cov + rm -rf "$coverdir" go tool cover -html=profile.cov -o=coverage.html go tool cover -func=profile.cov | tail -n1 fi