#!/usr/bin/env bash
#MISE description="run all tests with coverage report"

echo "::group::Setup"
set -euxo pipefail
source <(cargo llvm-cov show-env --export-prefix)
cargo llvm-cov clean --workspace
if [[ -n "${MISE_GITHUB_BOT_TOKEN:-}" ]]; then
  export GITHUB_API_TOKEN="$MISE_GITHUB_BOT_TOKEN"
fi
export CARGO_TARGET_DIR="${CARGO_TARGET_DIR:-$PWD/target}"
export PATH="${CARGO_TARGET_DIR}/debug:$PATH"

echo "::group::Build w/ coverage"
cargo build --all-features
echo "::endgroup::"
./e2e/run_all_tests
if [[ "${TEST_TRANCHE:-}" == 0 ]]; then
  echo "::group::Implode"
  mise implode
elif [[ "${TEST_TRANCHE:-}" == 1 ]]; then
  echo "::group::Self update"
  # pick a version from the previous month because the current release might not have been published yet
  y=$(date +%Y)
  m=$(($(date +%m) - 1))
  if [[ $m -eq 0 ]]; then
    m=12
    y=$((y - 1))
  fi
  mise self-update -fy "$y.$m.0"
elif [[ "${TEST_TRANCHE:-}" == 3 ]]; then
  echo "::group::Unit tests"
  cargo test --all-features
fi
echo "::group::Render lcov report"
cargo llvm-cov report --lcov --output-path "coverage-${TEST_TRANCHE:-}.lcov"
