#!/bin/bash
set -e

# Compile phase run by parallel in test-unit. No support for coverpkg

dir=$1
out_file="$DEST/precompiled/$dir.test"
testcover=()
if [ "$HAVE_GO_TEST_COVER" ]; then
	# if our current go install has -cover, we want to use it :)
	mkdir -p "$DEST/coverprofiles"
	coverprofile="docker${dir#.}"
	coverprofile="$DEST/coverprofiles/${coverprofile//\//-}"
	testcover=( -cover -coverprofile "$coverprofile" ) # missing $coverpkg
fi
if [ "$BUILDFLAGS_FILE" ]; then
	readarray -t BUILDFLAGS < "$BUILDFLAGS_FILE"
fi
(
	cd "$dir"
	go test "${testcover[@]}" -ldflags "$LDFLAGS" "${BUILDFLAGS[@]}" $TESTFLAGS -c
)
[ $? -ne 0 ] && return 1
mkdir -p "$(dirname "$out_file")"
mv "$dir/$(basename "$dir").test" "$out_file"
echo "Precompiled: ${DOCKER_PKG}${dir#.}"
