#!/bin/sh
# cargo-auto-test uses the installed crate directory, which is not writable.
# Test a temporary copy so fixture creation never touches installed sources.
set -eu
version=$1
shift
work=$(mktemp -d "${AUTOPKGTEST_TMP:-${TMPDIR:-/tmp}}/blight-tests.XXXXXX")
trap 'rm -rf -- "$work"' EXIT HUP INT TERM
mkdir -p "$work/source" "$work/cargo-home"
cp -R "/usr/share/cargo/registry/blight-$version/." "$work/source/"
chmod -R u+w "$work/source"
cat > "$work/cargo-home/config.toml" <<'CONFIG'
[source.crates-io]
replace-with = "debian"
[source.debian]
directory = "/usr/share/cargo/registry"
CONFIG
export CARGO_HOME="$work/cargo-home" CARGO_TARGET_DIR="$work/target"
export CARGO_BUILD_JOBS=1 CARGO_INCREMENTAL=0
export CARGO_PROFILE_DEV_DEBUG=0 CARGO_PROFILE_TEST_DEBUG=0
/usr/bin/cargo test --offline --manifest-path "$work/source/Cargo.toml" \
    --all-targets "$@" -- --test-threads=1
