#!/bin/sh
# Compile upstream's tests with the installed compilers against the
# installed jars, and run them with debian/tests/runner.
set -eu
T=${AUTOPKGTEST_TMP:-$(mktemp -d)}
J=/usr/share/java
for v in 2.13 3; do
	if [ $v = 3 ]; then
		s=3 lib=scala3-library opts= L=$J/scala-library-3.jar
	else
		s=2 lib=scala-library-2.13 opts=-Yrangepos
		L=$J/scala-library-2.13.jar:$J/scala-reflect-2.13.jar
	fi
	CP=$J/utest-$v.jar:$J/sourcecode-$v.jar:$J/sbt-test-interface.jar:$J/scala-collection-compat_$v.jar
	mkdir -p "$T/$v/classes" "$T/$v/gen"
	printf 'package test.utest\nobject BuildInfo {\n  val scalaVersion = "%s"\n}\n' \
		"$(dpkg-query -W -f='${Version}' $lib | sed 's/[+~-].*//')" \
		> "$T/$v/gen/BuildInfo.scala"
	find utest/test/src utest/test/src-$s utest/test/src-jvm debian/tests/runner \
		"$T/$v/gen" -name '*.scala' | LC_ALL=C sort > "$T/$v.files"
	scalac-$v $opts -d "$T/$v/classes" -cp "$CP" @"$T/$v.files"
	java -cp "$L:$CP:$T/$v/classes" debian.utest.RunTests \
		test.utest.CustomFramework "$T/$v/classes"
	echo "Scala $v: upstream tests passed"
done
