#! /bin/sh

set -e

# If arguments are supplied to this function, they will be passed to
# the runtests.py invocation.

# Some tests have to be excluded for various reasons

EXCLUDE=""

# This test causes pytest to segfault.
# Reported to: https://github.com/spyder-ide/spyder/issues/17651
EXCLUDE="${EXCLUDE} --deselect=spyder/app/tests/test_mainwindow.py"

# This test raises an error.
# Reported to: https://github.com/spyder-ide/spyder/issues/17651
EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/editor/widgets/tests/test_hints_and_calltips.py::test_get_hints"

# We need to set up a home directory for some of the pytest tests, but
# we cannot do so within the script as it gets deleted too early, before
# the pytest finalisers have run.  So we set it up it here (and
# autopkgtest will delete it afterwards).
PYS=$(py3versions -s)
for py in $PYS; do
    echo "Testing with $py:"

    tempdir=$(mktemp -d -p "$AUTOPKGTEST_TMP")
    export HOME="$tempdir"

    # We need to install the test plugin.
    # See https://github.com/spyder-ide/spyder/issues/17104
    ( cd spyder/app/tests/spyder-boilerplate && \
        $py -m pip install --no-deps -q -e . )

    export CI=true
    for slow in "" "--run-slow"; do
        xvfbcmd="xvfb-run -a -s"
	xvfbopt="-screen 0 1024x768x24 +extension GLX"
        cmd="$py runtests.py --homedir=$tempdir $EXCLUDE $slow"
        $xvfbcmd "$xvfbopt" $cmd "$@" || \
          $xvfbcmd "$xvfbopt" $cmd "$@" || \
          $xvfbcmd "$xvfbopt" $cmd "$@" || \
          $xvfbcmd "$xvfbopt" $cmd "$@" || \
          $xvfbcmd "$xvfbopt" $cmd "$@"
    done
done
