run-tests.sh41 lines · main
1#!/bin/bash
2set -e
3
4SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
5PACKAGE_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
6DB_DIR="$SCRIPT_DIR/db"
7
8# Unique project name per worktree so containers don't collide
9PROJECT_HASH=$(echo "$PACKAGE_DIR" | shasum | cut -c1-8)
10COMPOSE_PROJECT_NAME="pg-meta-${PROJECT_HASH}"
11export COMPOSE_PROJECT_NAME
12
13# Find an available port in the range 5432-5531
14PG_TEST_PORT=5432
15MAX_PORT=5531
16while nc -z localhost "$PG_TEST_PORT" 2>/dev/null; do
17 if [ "$PG_TEST_PORT" -ge "$MAX_PORT" ]; then
18 echo "error: no available port found in range 5432-${MAX_PORT}" >&2
19 exit 1
20 fi
21 PG_TEST_PORT=$((PG_TEST_PORT + 1))
22done
23export PG_TEST_PORT
24
25DATABASE_URL="postgresql://postgres:postgres@localhost:${PG_TEST_PORT}"
26export DATABASE_URL
27
28cleanup() {
29 cd "$DB_DIR"
30 docker compose down 2>/dev/null || true
31}
32trap cleanup EXIT
33
34cd "$DB_DIR"
35docker compose down 2>/dev/null || true
36docker compose up --detach --wait
37
38cd "$PACKAGE_DIR"
39"$@"
40rc=$?
41exit $rc