Skip to content

Commit

Permalink
test: fix test script to not miss errors
Browse files Browse the repository at this point in the history
  • Loading branch information
docwhat committed Mar 15, 2022
1 parent 3f2206d commit 8c66228
Showing 1 changed file with 42 additions and 17 deletions.
59 changes: 42 additions & 17 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -34,43 +34,68 @@ function inc_color {
}

function colorpipe {
local line
global_color=$((global_color + 1))
if ((global_color > 6)); then
global_color=1
fi
local c=$1 line

while read -r line; do
tput setaf "$global_color"
printf "%s" "$line"
tput setaf "$c"
printf "\t%s" "$line"
tput sgr0
echo
done
}

failures=0

function runc {
tput setaf 8
tput bold
printf "Running:"
printf " %q" "$@"
printf "\n"
tput sgr0

global_color=$((global_color + 1))
if ((global_color > 7)); then
global_color=2
fi

"$@" | colorpipe "$global_color" || failures=$((failures + 1))
}

header "Running tests..."
colorpipe < <(go test ./... -v)
runc go test ./... -v

header "Running examples..."
for example in examples/*; do
if [ ! -d "$example" ]; then
continue
fi

header "Running example $example"

# compgen -G is a badly documented trick to resolve wildcards into a
# single file or an empty string.
template=$(compgen -G "${example}"/*.template || :)
json=$(compgen -G "${example}/*.json" || :)
if [ -r "${json}" ]; then
colorpipe < <(
env RUNNING_WITH=json TODAY_IS="$(date)" go run ./*.go --json-data "$json" "$template"
)
yaml=$(compgen -G "${example}/*.yaml" || :)
TODAY_IS=$(date '+%x')

if [ -r "${json}" ] || [ -r "${yaml}" ]; then
if [ -r "${json}" ]; then
runc env "TODAY_IS=$TODAY_IS" RUNNING_WITH=json-data go run ./... --data "$json" "$template"
fi
if [ -r "${yaml}" ]; then
runc env "TODAY_IS=$TODAY_IS" RUNNING_WITH=yaml-data go run ./... --data "$yaml" "$template"
fi
else
colorpipe < <(
env RUNNING_WITH=env TODAY_IS="$(date)" go run ./*.go "$template"
)
runc env "TODAY_IS=$TODAY_IS" RUNNING_WITH=env go run ./... "$template"
fi
done

if ((failures > 0)); then
tput setaf 1 # red
tput bold
printf "FAILURES: %d\n" "$failures"
tput sgr0
exit 1
fi

# vim: set ft=sh :

0 comments on commit 8c66228

Please sign in to comment.