test: forward runtest arguments

This commit is contained in:
Guillaume ARM 2026-06-08 20:21:39 +02:00
parent 9fc474ce25
commit f533d49c93

118
Justfile
View File

@ -101,52 +101,80 @@ ci *args: check-craftos check
@just test-timeout @just test-timeout
# Run CraftOS-PC headless integration tests. Pass `--pretty` for grouped output. # Run CraftOS-PC headless integration tests. Pass `--pretty` for grouped output.
[positional-arguments]
test *args: test *args:
@if [ -f .env.test ]; then set -a; . ./.env.test; set +a; fi; \ #!/usr/bin/env bash
pretty=0; \ set -uo pipefail
verbose=0; \
timeout_seconds="${TRAP_CCLIBS_TEST_TIMEOUT_SECONDS:-3}"; \ if [ -f .env.test ]; then set -a; . ./.env.test; set +a; fi
case "$timeout_seconds" in ''|*[!0-9]*) printf '%s\n' 'TRAP_CCLIBS_TEST_TIMEOUT_SECONDS must be a positive integer' >&2; exit 1 ;; esac; \
if [ "$timeout_seconds" -lt 1 ]; then printf '%s\n' 'TRAP_CCLIBS_TEST_TIMEOUT_SECONDS must be >= 1' >&2; exit 1; fi; \ pretty=0
for a in {{args}}; do case "$a" in --pretty) pretty=1 ;; --verbose|-v) pretty=1; verbose=1 ;; esac; done; \ has_output=0
rom_arg=""; \ timeout_seconds="${TRAP_CCLIBS_TEST_TIMEOUT_SECONDS:-3}"
if [ "$(uname -s)" = "Darwin" ]; then \ case "$timeout_seconds" in ''|*[!0-9]*) printf '%s\n' 'TRAP_CCLIBS_TEST_TIMEOUT_SECONDS must be a positive integer' >&2; exit 1 ;; esac
rom_arg="--rom /Applications/CraftOS-PC.app/Contents/Resources"; \ if [ "$timeout_seconds" -lt 1 ]; then printf '%s\n' 'TRAP_CCLIBS_TEST_TIMEOUT_SECONDS must be >= 1' >&2; exit 1; fi
fi; \
repo='{{justfile_directory()}}'; \ runtest_args=("$@")
mount_arg="--mount-ro /apis=$repo/apis --mount-ro /programs=$repo/programs --mount-ro /startup=$repo/startup --mount-ro /tests=$repo/tests"; \ for a in "$@"; do
tmp="$(mktemp)"; \ case "$a" in
data_dir="$(mktemp -d)"; \ --pretty|--verbose|-v) pretty=1 ;;
output_path="$data_dir/computer/0/trapos-test-output"; \ --output) has_output=1 ;;
exec_code="shell.run('/programs/runtest.lua', '--shutdown')"; \ esac
if [ "$pretty" -eq 1 ]; then exec_code="shell.run('/programs/runtest.lua', '--pretty', '--output', '/trapos-test-output', '--shutdown')"; fi; \ done
if [ "$verbose" -eq 1 ]; then exec_code="shell.run('/programs/runtest.lua', '--verbose', '--output', '/trapos-test-output', '--shutdown')"; fi; \ if [ "$pretty" -eq 1 ] && [ "$has_output" -eq 0 ]; then
craftos --directory "$data_dir" --headless $rom_arg $mount_arg --exec "$exec_code" >"$tmp" 2>&1 & \ runtest_args+=(--output /trapos-test-output)
pid="$!"; \ fi
( sleep "$timeout_seconds"; kill -TERM "$pid" >/dev/null 2>&1 ) & \ runtest_args+=(--shutdown)
watchdog="$!"; \
wait "$pid" >/dev/null 2>&1; \ lua_quote() {
status="$?"; \ local value="$1"
kill "$watchdog" >/dev/null 2>&1 || true; \ value="${value//\\/\\\\}"
wait "$watchdog" >/dev/null 2>&1 || true; \ value="${value//\'/\\\'}"
if grep -q __TRAPOS_TEST_OK__ "$tmp"; then \ printf "'%s'" "$value"
if [ "$pretty" -eq 1 ] && [ -f "$output_path" ]; then cat "$output_path"; fi; \ }
rm -f "$tmp"; \
rm -rf "$data_dir"; \ exec_code="shell.run('/programs/runtest.lua'"
else \ for arg in "${runtest_args[@]}"; do
red=$(printf '\033[31m'); reset=$(printf '\033[0m'); \ exec_code+=", $(lua_quote "$arg")"
if [ "$status" -eq 143 ]; then \ done
printf '%s\n' "${red}FAIL${reset} CraftOS integration tests timed out after ${timeout_seconds}s" >&2; \ exec_code+=")"
else \
printf '%s\n' "${red}FAIL${reset} CraftOS integration tests did not print __TRAPOS_TEST_OK__" >&2; \ rom_arg=()
fi; \ if [ "$(uname -s)" = "Darwin" ]; then
if [ -f "$output_path" ]; then cat "$output_path" >&2; fi; \ rom_arg=(--rom /Applications/CraftOS-PC.app/Contents/Resources)
cat "$tmp" >&2; \ fi
rm -f "$tmp"; \ repo='{{justfile_directory()}}'
rm -rf "$data_dir"; \ mount_arg=(--mount-ro "/apis=$repo/apis" --mount-ro "/programs=$repo/programs" --mount-ro "/startup=$repo/startup" --mount-ro "/tests=$repo/tests")
exit 1; \ tmp="$(mktemp)"
fi; \ data_dir="$(mktemp -d)"
printf '%s\n' 'OK: CraftOS integration tests passed' output_path="$data_dir/computer/0/trapos-test-output"
craftos --directory "$data_dir" --headless "${rom_arg[@]}" "${mount_arg[@]}" --exec "$exec_code" >"$tmp" 2>&1 &
pid="$!"
( sleep "$timeout_seconds"; kill -TERM "$pid" >/dev/null 2>&1 ) &
watchdog="$!"
wait "$pid" >/dev/null 2>&1
status="$?"
kill "$watchdog" >/dev/null 2>&1 || true
wait "$watchdog" >/dev/null 2>&1 || true
if grep -q __TRAPOS_TEST_OK__ "$tmp"; then
if [ "$pretty" -eq 1 ] && [ -f "$output_path" ]; then cat "$output_path"; fi
rm -f "$tmp"
rm -rf "$data_dir"
else
red=$(printf '\033[31m'); reset=$(printf '\033[0m')
if [ "$status" -eq 143 ]; then
printf '%s\n' "${red}FAIL${reset} CraftOS integration tests timed out after ${timeout_seconds}s" >&2
else
printf '%s\n' "${red}FAIL${reset} CraftOS integration tests did not print __TRAPOS_TEST_OK__" >&2
fi
if [ -f "$output_path" ]; then cat "$output_path" >&2; fi
cat "$tmp" >&2
rm -f "$tmp"
rm -rf "$data_dir"
exit 1
fi
printf '%s\n' 'OK: CraftOS integration tests passed'
# Harness self-test: run a tests/harness fixture and assert which timeout layer # Harness self-test: run a tests/harness fixture and assert which timeout layer
# caught it. `expect` is "lua" (libtest cancels the case) or "shell" (the shell # caught it. `expect` is "lua" (libtest cancels the case) or "shell" (the shell