#!/bin/bash

if [ -z "$1" ]; then
	echo "No packages need to be tested"
	exit 0
fi

echo WHAT: $@
echo SKIP: $SKIP
echo GINKGO_ARGS: $GINKGO_ARGS

# We want to run all the tests even if one suite fails. We'll increment this each
# time a suite fails.
RC=0

mkdir -p report

# Go through each package we've been told to test. If there are actually test files present,
# then run those tests. If the package is included in SKIP, we'll skip them.
for PKG in "$@"; do
	# Skip any tests we've been told to skip.
	if [[ "$SKIP" == *"$PKG"* ]]; then
	  echo "Skipping tests for package: ${PKG}"
	  continue
	fi

	HAS_TESTS=$(find ${PKG} -name "ut.test")
	if [ ! -z "${HAS_TESTS}" ]; then
		echo "Running tests for package: ${PKG}";
		REPORT_NAME=$(echo "${PKG}" | tr '/./' '_' | sed 's/^_//')

		# Only pass Ginkgo flags to Ginkgo-based test binaries. Vanilla go test
		# binaries don't recognize --ginkgo.* flags and will fail immediately.
		if ${PKG}/ut.test --help 2>&1 | grep -q ginkgo; then
			${PKG}/ut.test --ginkgo.junit-report=report/${REPORT_NAME}.xml ${GINKGO_ARGS} || ((RC++))
		else
			${PKG}/ut.test -test.v || ((RC++))
		fi
	else
		echo "WARNING: No tests to run in ${PKG}, skipping"
	fi
done

exit ${RC}
