#!/bin/sh
# Compile the upstream JUnit 5 test suite against the installed jars
# from libdiffutils-java and run it with the JUnit console launcher.
set -eu

SRC=$(pwd)
WORK=${AUTOPKGTEST_TMP:-$(mktemp -d)}
J=/usr/share/java
JUNIT=$J/junit-platform-console-standalone.jar
DEPS=$JUNIT:$J/assertj-core.jar:$J/opentest4j.jar:$J/apiguardian-api.jar

run_module() {
	module=$1
	classpath=$2
	out=$WORK/$module/target/test-classes

	echo "### Running $module tests"
	mkdir -p "$out"
	# The tests read fixtures via relative target/test-classes/... paths
	# and via the classpath, so mirror Maven's layout.
	cp -r "$SRC/$module/src/test/resources/." "$out/"
	find "$SRC/$module/src/test/java" -name '*.java' >"$WORK/$module/sources"
	javac -encoding UTF-8 -nowarn -d "$out" -cp "$classpath:$DEPS" \
		@"$WORK/$module/sources"

	(cd "$WORK/$module" && java -jar "$JUNIT" execute \
		--disable-banner --disable-ansi-colors --fail-if-no-tests \
		--class-path "target/test-classes:$classpath:$DEPS" \
		--scan-class-path target/test-classes)
}

run_module java-diff-utils "$J/java-diff-utils.jar"
run_module java-diff-utils-jgit \
	"$J/java-diff-utils.jar:$J/java-diff-utils-jgit.jar:$J/org.eclipse.jgit.jar"
