#!/bin/bash

# Copyright 2022-2023 Julian Gilbey <jdg@debian.org>
# License: MIT License, as found in LICENSE.txt

# Determine whether every test passes in at least one pytest run.
# We run the command given on the command line at most MAX_PYTEST_RUNS,
# stopping if a pytest run is successful.

# See .github/scripts/run_tests.sh and .github/workflows/test-linux.yml
# for some of the ideas behind this.

MAX_PYTEST_RUNS=5

NO_TESTS_EXITCODE=$(python3 -c "from pytest import ExitCode; print(int(ExitCode.NO_TESTS_COLLECTED))")

# Remove any old log files
rm -f pytest_log.txt

run=1
while [ $run -le $MAX_PYTEST_RUNS ]
do
    echo "*** STARTING RUN $run at $(date +%H:%M:%S): $*"
    set -o pipefail
    "$@" | tee -a pytest_log.txt
    ret=$?
    set +o pipefail
    if [ $ret -eq 0 -o $ret -eq $NO_TESTS_EXITCODE ]
    then
	echo "*** END OF RUN $run: ALL TESTS RUN HAVE NOW PASSED/XFAILED ***"
	rm -f pytest_log.txt
	exit 0
    fi
    echo "*** END OF RUN $run: NOT ALL TESTS HAVE YET PASSED/XFAILED ***"
    run=$(($run + 1))
done

echo "*** SOME TESTS FAILED/ERRORED EVERY RUN, ABORTING ***"
rm -f pytest_log.txt
exit 1
