Skip to content

Commit dabd60e

Browse files
authored
chore(test): gate integration tests behind build tag, add emulator runner (#50)
* chore(test): gate integration tests behind build tag, add emulator runner - Add `//go:build integration` to rename_integration_test.go so it is excluded from `go test ./...` and only compiled with `-tags integration`. The existing FIRESTORE_EMULATOR_HOST skip guard is retained as a friendly fallback. - Add scripts/run-integration-tests.sh: boots the Firestore emulator in its own process group (set -m), waits for "Dev App Server is now running", exports FIRESTORE_EMULATOR_HOST, runs the tagged tests, and tears the emulator down on exit via an EXIT trap — even on test failure. - Add Makefile with an `integration-test` target (PORT overridable). - Add .github/workflows/integration-test.yml to run integration tests in CI via Java + gcloud setup. - Add "Running Integration Tests" section to README. Verified locally: `go test ./...` stays green (4 integration tests excluded); `make integration-test` boots the emulator, runs all 4 tests without skips, and releases port 8765 on exit. * fix(ci): bump Java to 21 for Firestore emulator requirement --------- Co-authored-by: Gustavo Hoirisch <355877+gugahoi@users.noreply.github.com>
1 parent 5bb478f commit dabd60e

5 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Integration Tests
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches:
7+
- main
8+
9+
permissions:
10+
contents: read
11+
12+
jobs:
13+
integration-test:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v6
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Set up Go
22+
uses: actions/setup-go@v6
23+
with:
24+
go-version-file: go.mod
25+
cache: true
26+
27+
- name: Set up Java
28+
uses: actions/setup-java@v4
29+
with:
30+
java-version: '21'
31+
distribution: 'temurin'
32+
33+
- name: Set up gcloud
34+
uses: google-github-actions/setup-gcloud@v2
35+
36+
- name: Install Firestore emulator
37+
run: gcloud components install cloud-firestore-emulator --quiet
38+
39+
- name: Run integration tests
40+
run: make integration-test

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
PORT ?= 8765
2+
3+
.PHONY: integration-test
4+
integration-test:
5+
PORT=$(PORT) ./scripts/run-integration-tests.sh $(ARGS)

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,22 @@ firestore --host localhost:9090 document get /my-collection/my-document
137137
```
138138

139139
The `--host` flag will override the environment variable if both are set.
140+
141+
## Running Integration Tests
142+
143+
Integration tests live in `pkg/cmd/browse/rename_integration_test.go` and are gated by the `integration` build tag, so they are excluded from the normal `go test ./...` run.
144+
145+
**Prerequisites:** `gcloud` CLI with the `cloud-firestore-emulator` component and Java installed.
146+
147+
```bash
148+
# Install the emulator component once
149+
gcloud components install cloud-firestore-emulator
150+
151+
# Run all integration tests (boots the emulator automatically on port 8765)
152+
make integration-test
153+
154+
# Use a different port
155+
make integration-test PORT=9000
156+
```
157+
158+
The `make integration-test` target starts the emulator in the background, exports `FIRESTORE_EMULATOR_HOST`, runs `go test -tags integration -v ./...`, and tears the emulator down on exit — even if the tests fail.

pkg/cmd/browse/rename_integration_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build integration
2+
13
package browse
24

35
import (

scripts/run-integration-tests.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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

Comments
 (0)