#!/bin/sh

set -e -u

# Keep the test dir rather than copy to the root of $AUTOPKGTEST_TMP.
# Upstream scripts add the parent dir to the python path; this way
# one can be sure it will only find an (otherwise) empty dir there.
cp -va test "$AUTOPKGTEST_TMP"/
cd "$AUTOPKGTEST_TMP"/test

for pyv in $(py3versions -s -v); do
	echo "Running testsuite with python$pyv:"
	for t in testlex.py testyacc.py testcpp.py; do
		echo "$t:"
		if [ "$t" = "testcpp.py" ]; then
			# Only run testcpp with Python <=3.13, something in 3.14's multiprocessing causes breakage (see #1123161)
			if dpkg --compare-versions $pyv lt 3.14; then
				# Ensure multiprocessing.SemLock is supported (needed for testcpp.py only)
				if python$pyv -c 'from _multiprocessing import SemLock; sl = SemLock(1, 2, 3, "4", 5); sl.release()' > /dev/null 2>&1; then
					python$pyv $t
				else
					echo "Support for multiprocessing.SemLock missing, skipping test"
				fi
			else
				echo "Breakage in multiprocessing, skipping test"
			fi
		else
			python$pyv $t
		fi
	done
	# cleanup
	/bin/sh cleanup.sh
done
