diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3557d973..7f38d2ab 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -118,51 +118,78 @@ firebase_admin_sdk/ # Workspace root ### Running Tests -Tests are split into unit/emulator tests and production integration tests. +Tests use tag-based presets defined in each package's `dart_test.yaml`: -#### Unit and Emulator Tests +| Preset | Tag | What runs | +|---|---|---| +| _(default)_ | — | Unit tests only, no emulator or credentials needed | +| `-P firebase-emulator` | `firebase-emulator` | Tests that require emulators | +| `-P prod` | `prod` | Tests against real Firebase APIs (opt-in) | +| `-P wif` | `wif` | Workload Identity Federation tests (opt-in) | +| `-P ci` | — | Unit + emulator tests; excludes `prod` and `wif` | + +#### Unit tests + +No setup needed: ```bash -# From packages/firebase_admin_sdk +# From packages/firebase_admin_sdk or packages/google_cloud_firestore +dart test +``` -# Run all tests against emulators (requires Firebase CLI) -firebase emulators:exec --project dart-firebase-admin --only auth,firestore,functions,tasks,storage \ - "dart run coverage:test_with_coverage -- --concurrency=1" +#### `firebase_admin_sdk` emulator tests -# Or use the convenience script from the repo root -./scripts/coverage.sh +Requires the [Firebase CLI](https://firebase.google.com/docs/cli) and Java 21+. +The Firebase Cloud Tasks emulator also requires the functions fixture to be built first: -# Run a specific test file -dart test test/auth/auth_test.dart +```bash +cd packages/firebase_admin_sdk/test/fixtures/task_queue_functions +npm install && npm run build +cd ../../../../.. ``` -#### Integration Tests with Emulator Suite - -Start the emulators, then run with the relevant environment variables: +Then run the emulators and tests together: ```bash -firebase emulators:start --only firestore,auth +# From packages/firebase_admin_sdk +firebase emulators:exec --config test/firebase.json --project dart-firebase-admin \ + --only auth,firestore,functions,tasks,storage \ + "dart test -P firebase-emulator" -export FIRESTORE_EMULATOR_HOST=localhost:8080 -export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099 -dart test test/firestore/firestore_integration_test.dart +# Or use the convenience script from the repo root (also collects coverage) +../../scripts/coverage.sh ``` -#### Production Integration Tests +#### `google_cloud_firestore` emulator tests -Requires a real Firebase project and Google application default credentials. Authorise `gcloud` first: +Requires the [Firebase CLI](https://firebase.google.com/docs/cli) and Java 21+. +The `firebase.json` in the package root configures the Firestore emulator on port 8080. ```bash -gcloud beta auth application-default login +# From packages/google_cloud_firestore +firebase emulators:exec --project dart-firebase-admin --only firestore \ + "dart test -P firebase-emulator" + +# Or use the convenience script from the repo root (also collects coverage) +../../scripts/firestore-coverage.sh ``` -Then run with `RUN_PROD_TESTS=true`: +#### Production tests (opt-in) + +Requires a service account with access to the `dart-firebase-admin` Firebase project: ```bash -RUN_PROD_TESTS=true dart test test/app/firebase_app_prod_test.dart --concurrency=1 +GOOGLE_APPLICATION_CREDENTIALS=path/to/service-account.json dart test -P prod ``` -See [`README.md`](README.md) for Firebase project setup details. You can create a project in the [Firebase Console](https://console.firebase.google.com) if you don't have one already. +#### Workload Identity Federation tests (opt-in) + +Requires an `external_account` JSON credential (e.g. from `gcloud auth application-default login` +or `google-github-actions/auth` in CI). Only applies to `firebase_admin_sdk`: + +```bash +GOOGLE_APPLICATION_CREDENTIALS=path/to/external-account.json dart test -P wif +``` ### Code Formatting and Analysis diff --git a/packages/google_cloud_firestore/firebase.json b/packages/google_cloud_firestore/firebase.json new file mode 100644 index 00000000..31ca3893 --- /dev/null +++ b/packages/google_cloud_firestore/firebase.json @@ -0,0 +1,11 @@ +{ + "emulators": { + "firestore": { + "port": 8080 + }, + "ui": { + "enabled": true + }, + "singleProjectMode": true + } +} diff --git a/scripts/coverage.sh b/scripts/coverage.sh index 1317cc9f..d56acd67 100755 --- a/scripts/coverage.sh +++ b/scripts/coverage.sh @@ -25,14 +25,18 @@ cd ../../.. dart pub global activate coverage -# Exclude prod/wif tests unless a credential is available. -TEST_TAGS="-P ci" +# Use test_with_coverage which supports workspaces (dart test --coverage doesn't work with resolution: workspace) +firebase emulators:exec \ + --config test/firebase.json \ + --project dart-firebase-admin \ + --only auth,firestore,functions,tasks,storage \ + "dart run coverage:test_with_coverage -- --concurrency=1 -P ci" + +# Prod tests are opt-in: set GOOGLE_APPLICATION_CREDENTIALS to include them. +# wif tests are handled separately by the test-wif CI job. if [ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]; then - TEST_TAGS="" + dart test --concurrency=1 -P prod fi -# Use test_with_coverage which supports workspaces (dart test --coverage doesn't work with resolution: workspace) -firebase emulators:exec --config test/firebase.json --project dart-firebase-admin --only auth,firestore,functions,tasks,storage "dart run coverage:test_with_coverage -- --concurrency=1 $TEST_TAGS" - # test_with_coverage already generates lcov.info, just move it mv coverage/lcov.info coverage.lcov diff --git a/scripts/firestore-coverage.sh b/scripts/firestore-coverage.sh index 9fda6d25..fdc7a3ad 100755 --- a/scripts/firestore-coverage.sh +++ b/scripts/firestore-coverage.sh @@ -15,18 +15,16 @@ cd "$PACKAGE_DIR" dart pub global activate coverage -# Prod tests are opt-in: set GOOGLE_APPLICATION_CREDENTIALS to include them. -TAGS="-P ci" -if [ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]; then - TAGS="" -fi - # Run unit and emulator tests in a single pass inside the emulator. # Unit tests ignore the emulator; this avoids needing to merge separate lcov files. firebase emulators:exec \ - --config ../firebase_admin_sdk/test/firebase.json \ --project dart-firebase-admin \ --only firestore \ - "dart run coverage:test_with_coverage -- --concurrency=1 $TAGS" + "dart run coverage:test_with_coverage -- --concurrency=1 -P ci" + +# Prod tests are opt-in: set GOOGLE_APPLICATION_CREDENTIALS to include them. +if [ -n "${GOOGLE_APPLICATION_CREDENTIALS:-}" ]; then + dart test --concurrency=1 -P prod +fi mv coverage/lcov.info coverage.lcov