#! /bin/sh

set -e

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

# If the environment variable TIMEOUT is set, the test is only allowed
# to last that long before it is killed.  This is needed because
# test_mainwindow.py hangs for some reason that I cannot fathom, even
# though every individual test succeeds.
# https://github.com/spyder-ide/spyder/issues/21189

# Some tests have to be excluded for various reasons

arch=$(dpkg --print-architecture)
EXCLUDE=""

# Various tests fail or cause pytest to crash.
# Reported to: https://github.com/spyder-ide/spyder/issues/17651
# and excluded here.

EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/editor/widgets/tests/test_hints_and_calltips.py::test_get_hints"
EXCLUDE="${EXCLUDE} --deselect=spyder/app/tests/test_mainwindow.py::test_run_cython_code"

# This test fails on some architectures on ci.debian.net for an undetermined
# reason.
# Reported to: https://github.com/spyder-ide/spyder/issues/19602

EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/findinfiles/widgets/tests/test_widgets.py::test_no_empty_file_items"

# These tests tend to fail on armhf for some reason
if [ $arch = armhf ]
then
    EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py"
    EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/pylint/tests/test_pylint.py::test_pylint_widget_pylintrc"
    EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/editor/widgets/tests/test_completions_hide.py::test_automatic_completions_hide_complete"
    EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/editor/widgets/tests/test_hints_and_calltips.py::test_get_calltips"
    EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/editor/widgets/tests/test_introspection.py::test_automatic_completions"
fi

# This test has started failing on ci.debian.net but works on my local
# lxc i386 container, which is very strange.  Excluding for now.
if [ $arch = i386 ]
then
    EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/findinfiles/widgets/tests/test_widgets.py::test_no_empty_file_items"
fi

# This test now fails on Python 3.11
EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py::test_tk_backend"


PYS=$(py3versions -s)
if [ -n "$TIMEOUT" ]; then timeout_cmd="timeout -k 5 $TIMEOUT"; fi

for py in $PYS; do
    echo "Testing with $py:"

    # 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 --break-system-packages --no-deps -q -e . )

    export CI=true
    export QTCONSOLE_TESTING=true

    for slow in "" "--run-slow"
    do
        debian/tests/run_pytest \
          xvfb-run -a -s "-screen 0 1024x768x24 +extension GLX" \
          $timeout_cmd $py runtests.py $EXCLUDE $slow "$@"
    done
done
