#!/bin/bash

set -e

outdir=${ADT_ARTIFACTS:-/tmp}/ocaml-dune-tests
echo "* Using $outdir"
mkdir -p ${outdir}
cp -a . ${outdir}
cd ${outdir}

# Tests that need melc
rm -rf test/blackbox-tests/test-cases/melange
rm -rf test/blackbox-tests/test-cases/merlin

# Tests that need files in /usr/doc
rm -rf test/blackbox-tests/test-cases/cinaps
rm -rf test/blackbox-tests/test-cases/inline_tests
rm -rf test/blackbox-tests/test-cases/ppx
rm -rf test/blackbox-tests/test-cases/utop
rm -rf test/blackbox-tests/test-cases/jsoo
rm -rf test/blackbox-tests/test-cases/cinaps
rm -rf test/blackbox-tests/test-cases/mdx-stanza
rm -rf test/blackbox-tests/test-cases/ctypes

# Tests with other unsatisfied assumptions
rm -rf test/blackbox-tests/test-cases/custom-cross-compilation
rm -rf test/blackbox-tests/test-cases/subst
rm -rf test/blackbox-tests/test-cases/pkg
rm -rf test/blackbox-tests/test-cases/os-variables.t
rm -rf test/blackbox-tests/test-cases/cram
rm -rf test/blackbox-tests/test-cases/actions
rm -rf test/blackbox-tests/test-cases/foreign-stubs/github7146.t
rm -rf test/expect-tests/vcs

# Other tests assume a native architecture
if ! command -v ocamlopt > /dev/null; then
    rm -rf otherlibs/dune-site/test
    rm -rf test/blackbox-tests/test-cases
fi

# Many expect-tests fail, but common and test_scheduler are needed for the rest
mkdir test-libs
mv test/expect-tests/{common,test_scheduler} test-libs
rm -rf test/expect-tests

# Flaky tests (to be investigated)
rm -rf otherlibs/dune-site/test # failed on amd64
rm -rf otherlibs/dune-rpc-lwt/examples/rpc_client/test # failed on riscv64
rm -rf otherlibs/dune-rpc-lwt/test # failed on s390x
rm -rf otherlibs/stdune/test # failed on i386

echo "* Run tests"
dune runtest 2>&1
