#!/bin/sh

set -e -u

# Copy the tests and symlink the 'tests' directory into its expected location
mkdir -vp "$AUTOPKGTEST_TMP"/kivy/tests
cp -va kivy/tests/* "$AUTOPKGTEST_TMP"/kivy/tests/
sudo ln -vs "$AUTOPKGTEST_TMP"/kivy/tests /usr/lib/python3/dist-packages/kivy/

# Copy and symlink various 'tools' needed by the testsuite (but not part of the binary pkg)
mkdir -vp "$AUTOPKGTEST_TMP"/kivy/tools
for tool in \
	changelog_parser.py \
	gallery.py \
	stub-gl-debug.py \
	highlight \
	image-testsuite \
	packaging \
	pep8checker \
	precommit_hooks
do
	cp -va kivy/tools/"$tool" "$AUTOPKGTEST_TMP"/kivy/tools/"$tool"
	sudo ln -vs "$AUTOPKGTEST_TMP"/kivy/tools/"$tool" /usr/lib/python3/dist-packages/kivy/tools/
done

cd "$AUTOPKGTEST_TMP"

for py in $(py3versions -s); do
	# Clean up cruft
	find "$AUTOPKGTEST_TMP" -name '*.py[ocx]' -type f -delete
	rm -rf "$AUTOPKGTEST_TMP"/home
	mkdir -vp "$AUTOPKGTEST_TMP"/home

	# Run the testsuite
	export NONETWORK=1
	export KIVY_UNITTEST=1
	export HOME="$AUTOPKGTEST_TMP"/home
	xvfb-run -a $py -m pytest kivy/tests --benchmark-skip --import-mode=importlib
done
