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

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"

if [ $arch = amd64 ]
then
    EXCLUDE="${EXCLUDE} --deselect=spyder/app/tests/test_mainwindow.py::test_run_cython_code"
    EXCLUDE="${EXCLUDE} --deselect=spyder/app/tests/test_mainwindow.py::test_ordering_lsp_requests_at_startup"
else
    # On i386 and possibly other archs, these tests usually crash
    EXCLUDE="${EXCLUDE} --deselect=spyder/app/tests/test_mainwindow.py"
fi

if [ $arch = i386 ]
then
    # On i386 and armhf, these tests usually cause pytest to crash
    EXCLUDE="${EXCLUDE} --deselect=spyder/plugins/ipythonconsole/tests/test_ipythonconsole.py"
elif [ $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

PYS=$(py3versions -s)
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 --no-deps -q -e . )

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