#!/bin/sh
# Check that fluster finds and loads every test suite it ships under
# /usr/share/fluster/test_suites without being told where they are.
# debian/rules runs this against the staged package by setting DESTDIR.
set -e

fluster=fluster
if [ -n "$DESTDIR" ]; then
	fluster="python3 $DESTDIR/usr/bin/fluster"
	export PYTHONPATH="$DESTDIR/usr/lib/python3/dist-packages"
	export XDG_DATA_DIRS="$DESTDIR/usr/share"
	# Don't leave __pycache__ directories in the staged package
	export PYTHONDONTWRITEBYTECODE=1
fi
suites_dir="$DESTDIR/usr/share/fluster/test_suites"

expected=$(find "$suites_dir" -name '*.json' | wc -l)
if [ "$expected" -eq 0 ]; then
	echo "no test suites found in $suites_dir" >&2
	exit 1
fi

output=$($fluster list)
echo "$output"

# fluster only warns when a test suite fails to load
if echo "$output" | grep -q '^Error loading'; then
	echo "fluster failed to load some test suites" >&2
	exit 1
fi

listed=$(echo "$output" | grep -c '^    Codec:')
if [ "$listed" -ne "$expected" ]; then
	echo "fluster listed $listed test suites, expected $expected" >&2
	exit 1
fi

echo "$output" | grep -qx 'JVT-AVC_V1'
