Files
DungeonCrawlerWorld/tests/scripts/test_godot_import_runners.sh
T

109 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
tmp_dir="$(mktemp -d)"
trap 'rm -rf "$tmp_dir"' EXIT
project_dir="$tmp_dir/project"
cp -a "$root_dir" "$project_dir"
rm -rf "$project_dir/.git" "$project_dir/.godot" "$project_dir/artifacts"
fake_godot="$tmp_dir/godot4"
cat > "$fake_godot" <<'FAKE_GODOT'
#!/usr/bin/env bash
set -euo pipefail
printf '%s\n' "$*" >> "$FAKE_GODOT_LOG"
if [[ " $* " == *" --import "* ]]; then
printf '%s\n' "fake import output" >&2
exit "${FAKE_IMPORT_STATUS:-0}"
fi
if [[ " $* " == *" --capture "* || " $* " == *" --capture-exit "* || " $* " == *" --visual-check-compact "* ]]; then
if [[ " $* " == *" --capture-exit "* ]]; then
capture_name="visual-check-exit.png"
elif [[ " $* " == *" --visual-check-compact "* ]]; then
capture_name="visual-check-compact.png"
else
capture_name="visual-check.png"
fi
python3 - "$capture_name" <<'PY'
import os
import struct
import sys
import zlib
os.makedirs("artifacts", exist_ok=True)
compact = sys.argv[1] == "visual-check-compact.png"
width, height = (1280, 720) if compact else (1920, 1080)
unknown = (1, 2, 5)
visible = (176, 176, 176)
# Match the blue-black explored tile palette accepted by the real capture
# validator; do not model it as a single renderer-specific RGB tuple.
explored = (9, 14, 23)
def pixel_for_x(x):
if x < width // 3:
return unknown
if x < 2 * width // 3 and sys.argv[1].endswith("-exit.png"):
return explored
return visible
row = b"".join(bytes(pixel_for_x(x)) for x in range(width))
scanlines = b"".join(b"\x00" + row for _ in range(height))
def chunk(kind, data):
return struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data) & 0xFFFFFFFF)
png = b"\x89PNG\r\n\x1a\n"
png += chunk(b"IHDR", struct.pack(">IIBBBBB", width, height, 8, 2, 0, 0, 0))
png += chunk(b"IDAT", zlib.compress(scanlines))
png += chunk(b"IEND", b"")
with open(f"artifacts/{sys.argv[1]}", "wb") as capture:
capture.write(png)
PY
fi
exit 0
FAKE_GODOT
chmod +x "$fake_godot"
assert_import_precedes_run() {
local runner="$1"
local log="$tmp_dir/${runner}.log"
FAKE_GODOT_LOG="$log" GODOT_BIN="$fake_godot" DISPLAY=:99 "$project_dir/scripts/$runner"
local first_line
first_line="$(head -n 1 "$log")"
[[ "$first_line" == *"--headless"* && "$first_line" == *"--import"* ]] || {
echo "$runner did not invoke headless import before its runner command" >&2
return 1
}
[[ "$(wc -l < "$log")" -ge 2 ]] || {
echo "$runner did not invoke its runner command after importing" >&2
return 1
}
}
visual_log="$tmp_dir/run-visual-check.sh.log"
FAKE_GODOT_LOG="$visual_log" GODOT_BIN="$fake_godot" DISPLAY=:99 "$project_dir/scripts/run-visual-check.sh"
compact_run="$(grep -- '--visual-check-compact' "$visual_log")"
[[ "$compact_run" == *"--windowed"* && "$compact_run" == *"--resolution 1280x720"* ]] || {
echo "run-visual-check.sh did not request a real 1280x720 compact window" >&2
exit 1
}
[[ -s "$project_dir/artifacts/visual-check-compact.png" ]] || {
echo "run-visual-check.sh did not produce a compact capture" >&2
exit 1
}
assert_import_precedes_run test-unit.sh
assert_import_precedes_run test-e2e.sh
assert_import_precedes_run run-visual-check.sh
failure_log="$tmp_dir/import-failure.log"
set +e
failure_output="$(FAKE_GODOT_LOG="$failure_log" FAKE_IMPORT_STATUS=42 GODOT_BIN="$fake_godot" "$project_dir/scripts/test-unit.sh" 2>&1)"
failure_status=$?
set -e
[[ "$failure_status" -eq 42 ]] || {
echo "test-unit.sh did not return the import failure status" >&2
exit 1
}
[[ "$failure_output" == *"fake import output"* ]] || {
echo "test-unit.sh did not preserve import error output" >&2
exit 1
}