#!/bin/sh

set -e

cp -a pyproject.toml tests "$AUTOPKGTEST_TMP"
cd "$AUTOPKGTEST_TMP"

# In the lxc autopkgtest environment, /tmp/pytest-of-test and /home
# are on different mounts, causing send2trash to raise an OSError in
# some of the tests.  $AUTOPKGTEST_TMP is on yet a different mount!
# So we set HOME to be in /tmp.  (This is a shortcoming of send2trash;
# see https://github.com/arsenetar/send2trash/issues/67.)
mkdir /tmp/home
export HOME=/tmp/home

DEFAULT_PY=$(py3versions -d)

# riscv64 fails on the restarting kernel test
arch=$(dpkg --print-architecture)
excludes=
if [ $arch = riscv64 ]
then
    excludes="--deselect tests/services/sessions/test_api.py::test_restart_kernel"
fi

for py in $(py3versions -s)
do
    echo "Testing with $py:"
    if [ $py = $DEFAULT_PY ]
    then
	echo "Running all non-integration tests with $py"
	$py -m pytest -v $excludes tests
	echo "Running all integration tests with $py"
	$py -m pytest -v --integration_tests=true $timeout tests/unix_sockets/test_serverapp_integration.py
    else
	# The deselected test breaks because it executes a /usr/bin/python3
	# script in the same pytest process, causing conflicting libraries
	# to be loaded.  We cannot run the integration tests in this case,
	# either.
	$py -m pytest -v $timeout --deselect=tests/extension/test_entrypoint.py::test_server_extension_list tests
    fi
done
