#!/bin/sh

# Run the upstream source tests against the installed jar.  Presumably
# this isn't something ever intended upstream, but perhaps a bit
# surprisingly, it does work, so rely on it for now.  We might well
# need to replace it with something else if it becomes untenable, but
# even with the exclusions below, it's a fairly extensive autopkgtest.

set -ex

src="$(pwd)"
test="$AUTOPKGTEST_TMP/test"

# Copy just what's needed to test to ensure we're not testing the
# source tree.  This may need adjustment if upstream changes.
mkdir "$test"
cp -a --parents readme.txt src/script test "$test"

cd "$test"

cp="/usr/share/java/clojure.jar"
cp="$cp:/usr/share/java/test.check.jar"
cp="$cp:/usr/share/java/test.generative.jar"
cp="$cp:/usr/share/java/tools.namespace.jar"
cp="$cp:test/java"
cp="$cp:test"

for java in $(find test -name "*.java"); do
    javac -cp "$cp" "$java"
done

# See build.xml compile-tests target
java -cp "$cp" \
     -Dclojure.compile.path=test \
     clojure.lang.Compile \
     clojure.test-clojure.protocols.examples \
     clojure.test-clojure.proxy.examples \
     clojure.test-clojure.genclass.examples \
     clojure.test-clojure.compilation.load-ns \
     clojure.test-clojure.annotations

# See build.xml test-example target for the normal upstream
# exclusions.  We add namespaces that throw this coercion exception
# (set expanded when moving from 1.12.5-2 to 1.12.7-1):
#
# expected: nil
#   actual: java.lang.ClassCastException: class clojure.pprint.proxy$java.io.Writer$IDeref$PrettyFlush$4923d848 cannot be cast to class clojure.pprint.PrettyFlush (clojure.pprint.proxy$java.io.Writer$I
# Deref$PrettyFlush$4923d848 is in unnamed module of loader 'app'; clojure.pprint.PrettyFlush is in unnamed module of loader clojure.lang.DynamicClassLoader @486b9a36)
#  at clojure.pprint$pretty_writer$fn__25783.invoke (pretty_writer.clj:392)
#     clojure.pprint.proxy$java.io.Writer$IDeref$PrettyFlush$4923d848.flush (:-1)
#     clojure.core$flush.invokeStatic (core.clj:3741)
#     clojure.core$flush.invoke (core.clj:3735)
#     ...
#
ex='#{clojure.test-clojure.compilation
      clojure.test-clojure.java-interop
      clojure.test-clojure.ns-libs-load-later
      clojure.test-clojure.printer
      clojure.test-clojure.pprint
      clojure.test-clojure.rt}'

java -cp "$cp" \
     -Dclojure.test-clojure.exclude-namespaces="$ex" \
     clojure.main src/script/run_test.clj
