#! /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=""

# 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 now fails on Python 3.11
EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py::test_tk_backend"

# This test fails on Python 3.12
# https://github.com/spyder-ide/spyder/issues/21688
EXCLUDE="${EXCLUDE} --deselect=spyder/app/tests/test_mainwindow.py::test_move_to_first_breakpoint"

# Two more tests fail on Python 3.12, but the failures seem to be local
EXCLUDE="${EXCLUDE} --deselect=spyder/app/tests/test_mainwindow.py::test_run_cython_code"
EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py::test_startup_run_lines_project_directory"

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
