|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Boots the Firestore emulator, runs integration tests, then tears it down. |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +PORT="${PORT:-8765}" |
| 6 | +LOG=$(mktemp) |
| 7 | +PGID="" |
| 8 | + |
| 9 | +cleanup() { |
| 10 | + [[ -n "$PGID" ]] && kill -- "-$PGID" 2>/dev/null || true |
| 11 | + rm -f "$LOG" |
| 12 | +} |
| 13 | +trap cleanup EXIT |
| 14 | + |
| 15 | +# ── prerequisites ────────────────────────────────────────────────────────────── |
| 16 | +if ! command -v gcloud &>/dev/null; then |
| 17 | + echo "error: gcloud not found — install the Google Cloud SDK" >&2 |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | +if ! gcloud components list --filter="id=cloud-firestore-emulator" \ |
| 21 | + --format="value(state.name)" 2>/dev/null | grep -qi "installed"; then |
| 22 | + echo "error: cloud-firestore-emulator component not installed" >&2 |
| 23 | + echo " Run: gcloud components install cloud-firestore-emulator" >&2 |
| 24 | + exit 1 |
| 25 | +fi |
| 26 | + |
| 27 | +# ── start emulator ───────────────────────────────────────────────────────────── |
| 28 | +echo "Starting Firestore emulator on localhost:${PORT} ..." |
| 29 | +# set -m assigns each background job its own process group so kill -- -$PGID |
| 30 | +# also terminates the Java subprocess spawned by the gcloud wrapper. |
| 31 | +set -m |
| 32 | +gcloud emulators firestore start --host-port="localhost:${PORT}" 2>"$LOG" & |
| 33 | +EMULATOR_PID=$! |
| 34 | +PGID=$(ps -o pgid= -p "$EMULATOR_PID" 2>/dev/null | tr -d ' ') || PGID=$EMULATOR_PID |
| 35 | +set +m |
| 36 | + |
| 37 | +# ── wait for ready (up to 30 s) ──────────────────────────────────────────────── |
| 38 | +for i in $(seq 1 30); do |
| 39 | + if grep -q "Dev App Server is now running" "$LOG" 2>/dev/null; then |
| 40 | + echo "Emulator ready." |
| 41 | + break |
| 42 | + fi |
| 43 | + if ! kill -0 "$EMULATOR_PID" 2>/dev/null; then |
| 44 | + echo "error: emulator exited before becoming ready. Log:" >&2 |
| 45 | + cat "$LOG" >&2 |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + if [[ $i -eq 30 ]]; then |
| 49 | + echo "error: emulator did not become ready within 30 s. Log:" >&2 |
| 50 | + cat "$LOG" >&2 |
| 51 | + exit 1 |
| 52 | + fi |
| 53 | + sleep 1 |
| 54 | +done |
| 55 | + |
| 56 | +# ── run tests ────────────────────────────────────────────────────────────────── |
| 57 | +export FIRESTORE_EMULATOR_HOST="localhost:${PORT}" |
| 58 | +go test -tags integration -v ./... "$@" |
0 commit comments