/ scripts / test-e2e
test-e2e
 1  #!/bin/bash
 2  set -e
 3  
 4  # Remove the first '--' if pnpm adds it
 5  if [ "$1" = "--" ]; then
 6    shift
 7  fi
 8  
 9  # Detect docker or podman
10  if command -v docker &> /dev/null; then
11    CONTAINER_CMD="docker"
12  elif command -v podman &> /dev/null; then
13    CONTAINER_CMD="podman"
14  else
15    echo "Error: Neither docker nor podman is installed"
16    exit 1
17  fi
18  
19  # Load test environment variables
20  set -a
21  source .env.test
22  set +a
23  
24  cleanup_webserver() {
25    if command -v lsof &> /dev/null; then
26      local pids=$(lsof -ti:${PLAYWRIGHT_WEBSERVER_PORT} 2>/dev/null || true)
27      if [ -n "$pids" ]; then
28        echo "Killing processes on port ${PLAYWRIGHT_WEBSERVER_PORT}: $pids"
29        kill $pids 2>/dev/null || true
30        sleep 1
31        kill -9 $pids 2>/dev/null || true
32      fi
33    fi
34  }
35  
36  cleanup_containers() {
37    local containers=$($CONTAINER_CMD ps -a --filter "name=$TEST_DB_CONTAINER_NAME" --filter "name=$STRIPE_MOCK_CONTAINER_NAME" -q 2>/dev/null || true)
38  
39    if [ -n "$containers" ]; then
40      echo "Cleaning up lingering test containers..."
41      if ! output=$($CONTAINER_CMD compose --env-file .env.test -f docker-compose.test.yml down -v 2>&1); then
42        echo "$output"
43        return 1
44      fi
45    fi
46  }
47  
48  setup_services() {
49    cleanup_webserver
50    cleanup_containers
51  
52    local output
53  
54    if ! output=$($CONTAINER_CMD compose --env-file .env.test -f docker-compose.test.yml up -d 2>&1); then
55      echo "$output"
56      exit 1
57    fi
58  
59    SECONDS=0
60    until $CONTAINER_CMD compose -f docker-compose.test.yml exec -T postgres-test pg_isready -U "$POSTGRES_USER" > /dev/null 2>&1; do
61      if [ $SECONDS -gt 30 ]; then
62        echo "Timeout waiting for PostgreSQL to be ready"
63        exit 1
64      fi
65      sleep 1
66    done
67  
68    SECONDS=0
69    until curl -s "$STRIPE_API_BASE" > /dev/null 2>&1; do
70      if [ $SECONDS -gt 30 ]; then
71        echo "Timeout waiting for Stripe Mock to be ready"
72        exit 1
73      fi
74      sleep 1
75    done
76  
77    if ! output=$(pnpm db:migrate 2>&1); then
78      echo "$output"
79      exit 1
80    fi
81  }
82  
83  teardown_services() {
84    cleanup_webserver
85    cleanup_containers
86  }
87  
88  trap teardown_services EXIT INT TERM
89  setup_services
90  pnpm exec playwright test "$@"