-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-full.sh
More file actions
executable file
·96 lines (77 loc) · 2.93 KB
/
Copy pathtest-full.sh
File metadata and controls
executable file
·96 lines (77 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
set -e
PORT=3001
export DB_PORT=5433
CLEANED_UP=0
COMPOSE_FILE="docker-compose.test.yaml"
# Point Prisma to the ephemeral test database
export DATABASE_URL="postgresql://postgres:postgres@localhost:$DB_PORT/openblog_test"
get_pid_on_port() {
ss -lptn "sport = :$PORT" 2>/dev/null | grep -oP 'pid=\K\d+' | head -n 1
}
is_openblog_nextjs() {
local pid=$1
if [ -z "$pid" ]; then return 1; fi
local cmdline=$(tr '\0' ' ' < "/proc/$pid/cmdline" 2>/dev/null || echo "")
local cwd=$(readlink "/proc/$pid/cwd" 2>/dev/null || echo "")
[[ ("$cmdline" == *"node"* || "$cmdline" == *"next"*) && ("$cwd" == *"OpenBlog"* || "$cmdline" == *"OpenBlog"*) ]]
}
kill_port_3001_surgical() {
local pid=$(get_pid_on_port)
if [ -z "$pid" ]; then return; fi
if is_openblog_nextjs "$pid"; then
kill -9 "$pid" 2>/dev/null || true
for i in {1..5}; do
[ -z "$(get_pid_on_port)" ] && return
sleep 1
done
else
echo "CRITICAL: Port $PORT occupied by unrecognized process (PID: $pid)." >&2
exit 1
fi
}
cleanup() {
if [ "$CLEANED_UP" -eq 1 ]; then return; fi
CLEANED_UP=1
# 1. Stop Next.js server
if [ -n "$SERVER_PID" ] && kill -0 "$SERVER_PID" 2>/dev/null; then
kill -TERM "$SERVER_PID" 2>/dev/null || true
fi
kill_port_3001_surgical
# 2. Spin down the test container and remove volumes
docker compose -f "$COMPOSE_FILE" down -v > /dev/null 2>&1
}
trap cleanup EXIT INT TERM
# Initial cleanup to ensure a clean state
kill_port_3001_surgical
# --- Database Orchestration ---
docker compose --progress quiet -f "$COMPOSE_FILE" up -d
until docker exec openblog-db-test pg_isready -h localhost -U postgres > /dev/null 2>&1; do
sleep 1
done
docker exec openblog-db-test psql -U postgres -c 'DROP DATABASE IF EXISTS openblog_test;' > /dev/null
docker exec openblog-db-test psql -U postgres -c 'CREATE DATABASE openblog_test;' > /dev/null
pnpm --silent run prisma-test migrate deploy > /dev/null 2>&1
# --- Run Tests ---
pnpm --silent run test:unit --reporter=agent
# --- Run Integration Tests ---
export DATABASE_URL="postgresql://postgres:postgres@localhost:$DB_PORT/openblog_test"
export E2E_TESTING=true
pnpm --silent run test:integration --reporter=agent
export NEXT_PUBLIC_BASE_URL="http://localhost:3001"
DATABASE_URL="postgresql://postgres:postgres@localhost:$DB_PORT/openblog_test" pnpm --silent run build:test > /dev/null 2>&1
SIGN_UP_ENABLED=true DISABLE_RATE_LIMITING=true E2E_TESTING=true NEXT_PUBLIC_BASE_URL=http://localhost:3001 pnpm --silent exec next start -p 3001 > /dev/null 2>&1 &
SERVER_PID=$!
TIMEOUT=60
ELAPSED=0
while ! curl -s http://localhost:$PORT > /dev/null 2>&1; do
sleep 1
ELAPSED=$((ELAPSED + 1))
if [ $ELAPSED -ge $TIMEOUT ]; then echo "ERROR: Server timeout"; exit 1; fi
if ! kill -0 "$SERVER_PID" 2>/dev/null; then echo "ERROR: Server died"; exit 1; fi
done
set +e
NEXT_PUBLIC_BASE_URL=http://localhost:3001 pnpm run test:e2e --reporter=list
E2E_EXIT_CODE=$?
set -e
exit $E2E_EXIT_CODE