#!/bin/sh
# sbt bridge smoke test: the bridge jar is found through its service file
# (as sbt's Zinc does), its classes load and link against the installed
# compiler and REPL, and an instance of the bridge can be created.
set -e

WORK="${AUTOPKGTEST_TMP:-$(mktemp -d)}"
cd "$WORK"
J=/usr/share/java
CP=$J/scala3-sbt-bridge-3.jar:$J/scala3-repl-3.jar:$J/scala3-directives-parser-3.jar
CP=$CP:$J/scala3-compiler-3.jar:$J/scala3-interfaces-3.jar:$J/tasty-core-3.jar:$J/scala-library-3.jar
CP=$CP:/usr/share/maven-repo/org/scala-lang/modules/scala-asm/debian/scala-asm-debian.jar
CP=$CP:$J/sbt-compiler-interface.jar:$J/sbt-util-interface.jar

cat > Bridge.java <<'X'
import java.util.ServiceLoader;
import xsbti.compile.CompilerInterface2;

public class Bridge {
    public static void main(String[] args) throws Exception {
        int n = 0;
        for (CompilerInterface2 c : ServiceLoader.load(CompilerInterface2.class)) {
            System.out.println("service: " + c.getClass().getName());
            n++;
        }
        if (n != 1) throw new AssertionError("expected one CompilerInterface2, got " + n);
        for (String name : new String[] {
                "dotty.tools.xsbt.CompilerBridgeDriver", "dotty.tools.xsbt.DelegatingReporter",
                "xsbt.ConsoleInterface", "xsbt.CompilerInterface", "xsbt.CachedCompilerImpl" }) {
            Class<?> k = Class.forName(name, true, Bridge.class.getClassLoader());
            k.getDeclaredMethods(); // resolves the types of all methods
            System.out.println("loaded: " + name);
        }
    }
}
X
javac -cp "$CP" -d . Bridge.java
java -cp ".:$CP" Bridge | tee out.txt
grep -qx 'service: dotty.tools.xsbt.CompilerBridge' out.txt
echo "bridge test: PASS"
