Files
tutortool/scripts/test-env.sh
s0wlz (Matthias Puchstein) 681b43174b
Some checks failed
CI / test (pull_request) Has been cancelled
CI / test (push) Failing after 4m51s
fix: implement random-port discovery for CI E2E backend
When PORT=0, the backend now writes its actual bound port to
data/test/.port. test-env.sh reads that file when TT_TEST_PORT=0
so all targets (test-up, test-reset, test-down) resolve the real URL.
test-up waits for .port to appear before the health-check loop.
2026-05-05 02:24:29 +02:00

42 lines
1.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
TT_WORKTREE_ROOT="$(git rev-parse --show-toplevel)"
# Deterministic port: 3100 + (sha1 of path) % 100
_hash=$(echo -n "$TT_WORKTREE_ROOT" | sha1sum | cut -c1-8)
_dec=$(( 16#$_hash ))
TT_TEST_PORT=$(( 3100 + _dec % 100 ))
# TT_TEST_PORT_RANDOM=1 → bind :0; backend writes actual port to data/test/.port
if [ "${TT_TEST_PORT_RANDOM:-0}" = "1" ]; then
TT_TEST_PORT=0
fi
# If port is 0 and the backend already wrote its actual port, use it
_port_file="${TT_WORKTREE_ROOT}/data/test/.port"
if [ "$TT_TEST_PORT" = "0" ] && [ -f "$_port_file" ]; then
_real_port=$(cat "$_port_file")
if [ -n "$_real_port" ] && [ "$_real_port" != "0" ]; then
TT_TEST_PORT=$_real_port
fi
fi
TT_TEST_DB="${TT_WORKTREE_ROOT}/data/test/attendance.db"
TT_TEST_MODE=1
TT_BASE_URL="http://127.0.0.1:${TT_TEST_PORT}"
mkdir -p "${TT_WORKTREE_ROOT}/data/test"
export TT_WORKTREE_ROOT TT_TEST_PORT TT_TEST_DB TT_TEST_MODE TT_BASE_URL
cat > "${TT_WORKTREE_ROOT}/data/test/.env" <<EOF
TT_WORKTREE_ROOT=${TT_WORKTREE_ROOT}
TT_TEST_PORT=${TT_TEST_PORT}
TT_TEST_DB=${TT_TEST_DB}
TT_TEST_MODE=${TT_TEST_MODE}
TT_BASE_URL=${TT_BASE_URL}
EOF
echo "[test-env] TT_TEST_PORT=${TT_TEST_PORT} TT_TEST_DB=${TT_TEST_DB}"