Skip to content

Instantly share code, notes, and snippets.

@icio
Last active March 20, 2026 15:53
Show Gist options
  • Select an option

  • Save icio/3deb766e8d18d5afd631a87b1418e08f to your computer and use it in GitHub Desktop.

Select an option

Save icio/3deb766e8d18d5afd631a87b1418e08f to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Program gotestloop individual executes each of the tests in the packages'
# arguments. It compiles each package's test binary then invokes it once for
# each test contained within.
#
# Each test failure is written to stdout in the format "FAIL pkg.test" followed
# by test output, indented.
#
# If the first argument is -v, then each test pass is written to stdout in
# the format "PASS pkg.test".
#
# TODO: Allow passing extra test flags.
set -eu
if [ $# == 0 ]; then
echo "Usage: gotestloop [-v] PKG [, ...]"
exit 1
fi
V=0
if [ $1 == -v ]; then
V=1
shift
fi
BASEDIR=$(mktemp -d)
trap 'rm -rf "$BASEDIR"' EXIT
C=0
ORIGDIR="$(pwd)"
for P in "$@"; do
# Move back to our base dir to ensure P resolves.
cd "$ORIGDIR"
# Resolve the package ref P.
PKG=$(${GO:-go} list "$P")
NAME=$(basename "$PKG")
# Create a temp dir for these logs.
DIR=$(mktemp -p $BASEDIR -d "$NAME.XXXXX")
# Compile the pkg test binary.
E=$DIR/$NAME.test
${GO:-go} test -c -o=$E $P
if [ ! -f $E ]; then continue; fi
# Move into the package directory so that local file reads resolve.
cd $(${GO:-go} list -f='{{.Dir}}' "$P")
for T in $($E -test.list=^Test); do
LOG=$DIR/$T.log
$E -test.run="^$T$" >$LOG 2>&1 && {
if [ "$V" = 1 ]; then
echo "PASS $PKG.$T"
fi
} || {
C=1
echo "FAIL $PKG.$T"
sed 's/^/ /' $LOG
}
done
done
exit $C
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment