#!/bin/sh
set -e

trap 'rm -f main' EXIT

# Compile the upstream CLI runner against the installed shared library.
# Named 'main' since tests/922_py_compile.py hardcodes './main' as the
# interpreter name when spawning sub-processes via os.system().
# PK_ENABLE_OS=1 matches the default CMake build configuration.
gcc -std=c11 -o main src2/main.c \
    -DPK_ENABLE_OS=1 -lpocketpy -lm -ldl

# Run the upstream test scripts using the compiled runner.
failed=0
for f in $(ls tests/*.py | sort); do
    echo "> $f"
    if ! ./main "$f"; then
        echo "FAILED: $f"
        failed=1
    fi
done

if [ "$failed" -ne 0 ]; then
    echo "One or more test scripts failed"
    exit 1
fi

echo "All test scripts passed"
