#!/bin/sh

set -e -u

SOURCE_DIR="$(pwd)"

# Ensure chromium is found where the upstream tests expect it
if [ -x "/usr/bin/chromium" ] && [ ! -x "/usr/bin/chromium-browser" ]; then
	ln -vs /usr/bin/chromium /usr/bin/chromium-browser
fi

for py in $(py3versions -s); do
	cd "$AUTOPKGTEST_TMP"
	rm -rf *
	cp -va "$SOURCE_DIR"/tests "$SOURCE_DIR"/scripts "$AUTOPKGTEST_TMP"/

	echo "Running testsuite with $py:"
	# Deselect or ignore some tests:
	# test_validate_host: intermittent failure (network/dns config?)
	# test_publicipv4: requires a configured interface and network access
	# test_extract_pot: only relevant for internal upstream use
	# test_functional_downloads: needs network access and usenet server credentials
	# test_happyeyeballs, test_internetspeed.py: needs network access
	# test_rss_basic_flow: needs pytest httpserver
	# test_queue_repair: too flaky
	# tests/test_consistency.py: intended for internal upstream use

	CI=TRUE PYTHONPATH=/usr/share/sabnzbdplus $py -m pytest -v \
		--deselect=tests/test_getipaddress.py::TestGetIpAddress::test_publicipv4 \
		--deselect=tests/test_functional_misc.py::TestExtractPot::test_extract_pot \
		--deselect=tests/test_filesystem.py::TestCreateAllDirs::test_permissions_450 \
		--deselect=tests/test_urlgrabber.py::TestBuildRequest::test_http_params_etc \
		--deselect=tests/test_functional_config.py::TestConfigRSS::test_rss_basic_flow \
		--deselect=tests/test_functional_misc.py::TestQueueRepair::test_queue_repair \
		--deselect=tests/test_newsunpack.py::TestPar2Repair::test_basic \
		--deselect=tests/test_cfg.py::TestValidators::test_validate_host \
		--ignore=tests/test_consistency.py \
		--ignore=tests/test_functional_downloads.py \
		--ignore=tests/test_utils/test_happyeyeballs.py \
		--ignore=tests/test_utils/test_internetspeed.py
done
