#!/usr/bin/env bash
# Run the unit tests under Cloverage inside the app image; needs only Docker. The report is
# copied out with `docker cp` (not a bind mount) so it works when this itself runs in a
# job container. Output: target/coverage/unit.
set -euo pipefail
cd "$(dirname "$0")/.."
source script/lib/docker.sh

build_unit_image

id=$(docker create --security-opt apparmor=unconfined tbd-unit \
  clojure -M:cov -m cloverage.coverage \
  --src-ns-path src --test-ns-path test \
  --lcov --html --output target/coverage/unit)
trap 'docker rm -f "$id" >/dev/null' EXIT

status=0
docker start -a "$id" || status=$?

rm -rf target/coverage/unit
mkdir -p target/coverage
docker cp "$id:/app/target/coverage/unit" target/coverage/ || echo "[test] WARNING: no unit coverage report was produced"
exit "$status"
