#!/bin/bash
set -e
set -o pipefail
shopt -s inherit_errexit # #514862, wtf

# Wrapper to run the tests in gitlab CI

x () { echo >&2 "+ $*"; "$@"; }

# Rebuild dgit-infrastructure because it can gain a versioned dependency on
# init-system-helpers that isn't satisfied by the stable release we may now be
# testing on.  This happens when d/rules exports DH_COMPAT=14 when running
# dh_installsystemduser.
x apt-get -y install --no-install-recommends --mark-auto \
  build-essential debhelper gettext po4a tcl
x rm debian/output/dgit-infrastructure_*.deb
x dpkg-buildpackage -uc -b -tc
x mv ../dgit-infrastructure_*.deb debian/output
x rm ../*.deb
x apt-get -y autoremove --purge

deps=" $(tests/enumerate-tests dep-packages) "
our_debs=$(find ./debian/output -name '*.deb')

our_pkgs=''; for p in $our_debs; do
    p=${p##*/}
    p=${p%%_*}
    our_pkgs+=" $p"
    deps="${deps// $p / }"
done

# This timeout is per test case, but becauswe of our gnupg serialisation,
# in principle, a test case might end up waiting for ages and ages for
# other test cases.  Empirically, locally, I have seen very alphabetically
# early test cases being the last ones to finish.  This is mad.
# Anyway, we must therefore set this long for the whole test suite.
export DGIT_TEST_RUN_PFX='timeout 2500'

x apt-get -y install $our_debs
x apt-get -y install --no-install-recommends $deps eatmydata moreutils
x apt-get -y build-dep .
x apt-get -y remove $our_pkgs

x eatmydata tests/using-intree tests/run-all "$@"
