#!/bin/bash -ex

# Use binaries and helpers installed on the system
export PODMAN=/usr/bin/podman
export QUADLET=/usr/libexec/podman/quadlet
export PODMAN_TESTING=/usr/libexec/podman/podman-testing

# System tests are implemented using the bats framework.
# Default invocation runs all non-ignored system tests in test/system.
#
# Environment variables for filtering / debugging individual tests:
#
# - BATS_FILTER: Filter test names matching a regex (passed safely to --filter without quoting issues).
#   Examples:
#     BATS_FILTER="podman ps --external"
#     BATS_FILTER="podman ps --external|zstd chunked does not modify image content"
#
# - BATS_TESTS: Specific test files or directories to execute (default: test/system).
#   Examples:
#     BATS_TESTS="test/system/040-ps.bats"
#     BATS_TESTS="test/system/040-ps.bats test/system/155-partial-pull.bats"
#
# - BATS_EXTRA_FLAGS: Additional options passed to bats CLI (e.g. --abort, -x).
#
# Usage with autopkgtest:
#   autopkgtest --env=BATS_FILTER="podman ps --external|zstd chunked does not modify image content" \
#               --env=BATS_TESTS="test/system/040-ps.bats test/system/155-partial-pull.bats" \
#               ... --test-name=system-tests

BATS_FLAGS=(--timing --tap --verbose-run --filter-tags !debian_ignore)

if [ -n "${BATS_FILTER:-}" ]; then
    BATS_FLAGS+=(--filter "${BATS_FILTER}")
fi

if [ -n "${BATS_EXTRA_FLAGS:-}" ]; then
    eval "EXTRA_FLAGS=(${BATS_EXTRA_FLAGS})"
    BATS_FLAGS+=("${EXTRA_FLAGS[@]}")
fi

read -r -a TESTS <<< "${BATS_TESTS:-test/system}"

exec bats "${BATS_FLAGS[@]}" "${TESTS[@]}"
