#!/bin/bash
set -e

pkg=stopt-examples

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  # Double quote below to expand the temporary directory variable now versus
  # later is on purpose.
  # shellcheck disable=SC2064
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/python/unit "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

# Running all Python unit tests that we shipped, looking for the string "OK"
# that indicates the test fulfilled.
# Excluding testGridKernelRegression, which passes on 64-bit arches but fails
# on 32-bit ones as it requires too many simulations.
for F in $(find . -name "test*.py" -a ! -name "testGridKernelRegression.py"); do
    python3 $F 2>&1 | grep -q "OK"
    if [ $? -ne 0 ]; then
        echo "Test $F FAILED"
        exit 1
    fi
    echo "Test $F PASSED"
done
