#!/bin/sh

set -e

# Test that pkgos-debcrate double-guesses the salsa subgroup from the
# directory it is started from: started under .../openstack/vector/, the
# Vcs URLs must point to the vector subgroup, without prompting.

ME=$(basename $0)
WORK=$(mktemp -d)
trap 'rm -rf "${WORK}"' EXIT

TESTDIR=$(cd "$(dirname "$0")" && pwd)
. "${TESTDIR}/fixtures.sh"

RUNDIR=${WORK}/openstack/vector/run
mkdir -p ${RUNDIR}
cd "${RUNDIR}"

SERDE_REQ=1 NIX_REQ=0.30 fixture_cargo_toml ${WORK}/Cargo.toml
fixture_pack_crate ${RUNDIR} ${WORK}/Cargo.toml
mock_git ${RUNDIR} ${RUNDIR}/fixture/serde_test-0.1.0 v0.1.0

cat >${WORK}/policy.txt <<EOF
librust-serde-dev=1.0.200
librust-serde-json-dev=1.0.140
librust-tokio-dev=1.53.1
librust-tempfile-dev=3.20.0
librust-cc-dev=1.2.0
librust-nix-dev=0.30.1
EOF

mock_apt ${WORK} ${WORK}/policy.txt
debcrate_env ${WORK}
# The subgroup must be guessed from the directory, not from the env.
unset PKGOS_DEBCRATE_TEAM

"${SCRIPT}" serde_test >${WORK}/out.log 2>&1 || {
	cat ${WORK}/out.log
	echo "${ME}: pkgos-debcrate failed"
	exit 1
}

check () {
	if ! grep -q "$2" "$1" ; then
		echo "${ME}: $1 doesn't contain: $2"
		exit 1
	fi
}

check ${WORK}/out.log 'Guessing the salsa subgroup from the current directory: vector'
check ${RUNDIR}/rust-serde-test/debian/control 'Vcs-Browser: https://salsa.debian.org/openstack-team/vector/rust-serde-test'
check ${RUNDIR}/rust-serde-test/debian/control 'Vcs-Git: https://salsa.debian.org/openstack-team/vector/rust-serde-test.git'

exit 0
