#!/bin/bash
set -e

DEST=$1
: ${PARALLEL_JOBS:=$(nproc)}

RED=$'\033[31m'
GREEN=$'\033[32m'
TEXTRESET=$'\033[0m' # reset the foreground colour

# Run Docker's test suite, including sub-packages, and store their output as a bundle
# If $TESTFLAGS is set in the environment, it is passed as extra arguments to 'go test'.
# You can use this to select certain tests to run, eg.
#
#   TESTFLAGS='-run ^TestBuild$' ./hack/make.sh test-unit
#
bundle_test_unit() {
	{
		date

		# Run all the tests if no TESTDIRS were specified.
		if [ -z "$TESTDIRS" ]; then
			TESTDIRS=$(find_dirs '*_test.go')
		fi
		(
			export LDFLAGS="$LDFLAGS $LDFLAGS_STATIC_DOCKER"
			export TESTFLAGS
			export HAVE_GO_TEST_COVER
			export DEST
			if command -v parallel &> /dev/null; then
				# accomodate parallel to be able to access variables
				export SHELL="$BASH"
				export HOME="$(mktemp -d)"
				mkdir -p "$HOME/.parallel"
				touch "$HOME/.parallel/ignored_vars"

				# some hack to export array variables
				export BUILDFLAGS_FILE="$HOME/buildflags_file"
				( IFS=$'\n'; echo "${BUILDFLAGS[*]}" ) > "$BUILDFLAGS_FILE"

				echo "$TESTDIRS" | parallel --jobs "$PARALLEL_JOBS" --halt 2 --env _ "$(dirname "$BASH_SOURCE")/.go-compile-test-dir"
				rm -rf "$HOME"
			else
				# aww, no "parallel" available - fall back to boring
				for test_dir in $TESTDIRS; do
					"$(dirname "$BASH_SOURCE")/.go-compile-test-dir" "$test_dir"
				done
			fi
		)
		echo "$TESTDIRS" | go_run_test_dir
	}
}

go_run_test_dir() {
	TESTS_FAILED=()
	while read dir; do
		echo
		echo '+ go test' $TESTFLAGS "${DOCKER_PKG}${dir#.}"
		precompiled="$DEST/precompiled/$dir.test"
		if ! ( cd "$dir" && "$precompiled" $TESTFLAGS ); then
			TESTS_FAILED+=("$dir")
			echo
			echo "${RED}Tests failed: $dir${TEXTRESET}"
			sleep 1 # give it a second, so observers watching can take note
		fi
	done

	echo
	echo
	echo

	# if some tests fail, we want the bundlescript to fail, but we want to
	# try running ALL the tests first, hence TESTS_FAILED
	if [ "${#TESTS_FAILED[@]}" -gt 0 ]; then
		echo "${RED}Test failures in: ${TESTS_FAILED[@]}${TEXTRESET}"
		echo
		false
	else
		echo "${GREEN}Test success${TEXTRESET}"
		echo
		true
	fi
}

exec > >(tee -a $DEST/test.log) 2>&1
bundle_test_unit
