Skip to content

Commit

Permalink
Use gotestsum to publish test results in JUnit format
Browse files Browse the repository at this point in the history
Signed-off-by: Marcelo E. Magallon <marcelo@sylabs.io>
  • Loading branch information
Marcelo E. Magallon authored and mem committed Oct 10, 2019
1 parent aba6994 commit 78a9a21
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
3 changes: 2 additions & 1 deletion mlocal/frags/Makefile.stub
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ dist:
$(V)(cd $(SOURCEDIR) && $(SOURCEDIR)/scripts/make-dist.sh)

.PHONY: unit-test
unit-test: EXTRA_FLAGS := $(if $(filter yes,$(strip $(JUNIT_OUTPUT))),-junit $(BUILDDIR_ABSPATH)/unit-test.xml)
unit-test:
@echo " TEST sudo go test [unit]"
$(V)cd $(SOURCEDIR) && \
sudo -E \
scripts/go-test -v \
scripts/go-test -v $(EXTRA_FLAGS) \
./...
@echo " PASS"

Expand Down
80 changes: 79 additions & 1 deletion scripts/go-test.in
Original file line number Diff line number Diff line change
@@ -1,10 +1,54 @@
#!/bin/sh

set -e

info() {
printf 'I: %s\n' "$*"
}

error() {
printf 'E: %s\n' "$*"
}

gotestsum_runner() {
gotestsum \
--jsonfile "${junitOutput}.json" \
--format "${gotestrunner_format}" \
--raw-command \
-- \
"${GO}" test -json "$@"
}

gotestsum_postprocess() {
gotestsum \
--junitfile "${junitOutput}" \
--raw-command \
-- \
cat "${junitOutput}.json"
}

gotest_runner() {
"${GO}" test "$@"
}

gotest_postprocess() {
true
}

export GOFLAGS='@GOFLAGS@'
export GO111MODULE='@GO111MODULE@'

GO='@GO@'
GO_TAGS='@GO_TAGS@'

verbose=false
use_gotestsum=false
junitOutput=
gotestrunner_format=standard-quiet

test_runner=gotest_runner
test_postprocess=gotest_postprocess

skip=false

for arg in "$@" ; do
Expand All @@ -21,17 +65,51 @@ for arg in "$@" ; do
skip=true
;;

-junit)
if ! command -v gotestsum > /dev/null 2>&1 ; then
error 'JUnit output requested but gotestsum not found in PATH. Abort.'
info ''
info 'Looked in the following directories, in order:'
info ''
IFS=:
for dir in ${PATH} ; do
info " ${dir}"
done
exit 1
fi

use_gotestsum=true
test_runner=gotestsum_runner
test_postprocess=gotestsum_postprocess
junitOutput="${1}"

skip=true
;;

-v|-verbose)
verbose=true
set -- "$@" -v
;;

*)
set -- "$@" "${arg}"
;;
esac
done

exec '@GO@' test \
if ${use_gotestsum} ; then
if ${verbose} ; then
gotestrunner_format=standard-verbose
fi
fi

"${test_runner}" \
-count=1 \
-timeout=30m \
-tags "${GO_TAGS}" \
-failfast \
-cover \
-race \
"$@"

"${test_postprocess}"

0 comments on commit 78a9a21

Please sign in to comment.