#! /bin/bash

set -e

PYS=$(py3versions -s)

cp -a $(cat debian/pybuild.testfiles) "$AUTOPKGTEST_TMP"
cd "$AUTOPKGTEST_TMP"

# Most tests work whether or not PYTHONPATH is set to the directory containing
# the tests.  But some tests require it to be set and others fail if it is set.
# So we run pytest twice, once with and once without PYTHONPATH.

NO_PATH_TESTS=(
    tests_python/test_debugger.py::test_module_entry_point
    tests_python/test_debugger.py::test_m_switch
    tests_python/test_debugger.py::test_stop_on_start_m_switch
    tests_python/test_debugger.py::test_stop_on_start_entry_point
)

NO_PATH_DESELECTS=()
for test in "${NO_PATH_TESTS[@]}"
do
    NO_PATH_DESELECTS+=("--deselect=$test")
done


# Some tests fail on mips64el, but I'm not sure why.  So we skip
# them; see README.Debian for some more comments on this.
# One test fails on the ppc64el autopkgtest, so we skip this too.
arch=$(dpkg --print-architecture)
if [ $$arch = mips64el ]
then
    EXCLUDE="\
        --deselect=tests_python/test_debugger.py::test_case_flask \
        --deselect=tests_python/test_debugger.py::test_case_django_a \
        --deselect=tests_python/test_debugger.py::test_case_django_template_inherits_no_exception \
        --deselect=tests_python/test_debugger.py::test_case_django_no_var_error \
        --deselect=tests_python/test_debugger.py::test_case_django_no_attribute_exception_breakpoint \
        --deselect=tests_python/test_debugger.py::test_case_django_no_attribute_exception_breakpoint_and_regular_exceptions \
        --deselect=tests_python/test_debugger.py::test_case_django_invalid_template_exception_breakpoint \
        --deselect=tests_python/test_debugger_json.py::test_case_django_no_attribute_exception_breakpoint \
        --deselect=tests_python/test_debugger_json.py::test_case_flask_line_validation"
elif [ $arch = ppc64el ]
then
    EXCLUDE="\
        --deselect=tests_python/test_debugger_json.py::test_code_reload"
else
    EXCLUDE=""
fi


for py in $PYS
do
    echo "Testing with $py:"
    export PYDEVD_USE_CYTHON=YES
    # Note: we don't use "-n auto" in the pytest call here, as that causes
    # weird breakages
    cmd="$py -m pytest \
        --ignore=tests/test_jyserver.py \
        --ignore=tests/test_pyserver.py \
        --ignore=tests_python/test_pydevcoverage.py \
        --deselect=tests_python/test_debugger_json.py::test_case_flask_exceptions \
        --deselect=tests_python/test_bytecode_manipulation.py::test_set_pydevd_break_01 \
        --deselect=tests_python/test_bytecode_manipulation.py::test_set_pydevd_break_01a \
        --deselect=tests_python/test_debugger.py::test_py_37_breakpoint_remote_no_import \
	$EXCLUDE \
	${NO_PATH_DESELECTS[*]}"
    export PYTHONPATH=$(pwd)
    xvfb-run -a -s "-screen 0 1024x1024x24" $cmd || \
        xvfb-run -a -s "-screen 0 1024x1024x24" $cmd
    unset PYTHONPATH

    cmd="$py -m pytest ${NO_PATH_TESTS[*]}"
    xvfb-run -a -s "-screen 0 1024x1024x24" $cmd || \
        xvfb-run -a -s "-screen 0 1024x1024x24" $cmd
done
