diff --git a/.gitea/workflows/release.yml b/.gitea/workflows/release.yml index 0a2ac9c0..00632083 100644 --- a/.gitea/workflows/release.yml +++ b/.gitea/workflows/release.yml @@ -286,16 +286,24 @@ jobs: fi # Apply embedded migrations to the fresh test DB, then run the - # full suite (no -short → integration tests execute). -p 1: - # every integration package TRUNCATEs the one shared test DB; - # concurrent package binaries → TRUNCATE deadlocks. Serialize - # package execution (the documented local invocation too). - # -timeout 20m: internal/api alone takes several minutes under - # -race on an idle runner, and a dev and a main run sharing the - # runner can push it past go test's default 10m with nothing - # hung. + # packages that hold database tests (no -short, so they execute). + # The `go` lane already runs every unit test; packages with no + # database test are left to it (ci/db-test-packages.sh). + # + # -p 1: every database package TRUNCATEs the one shared test DB, + # and concurrent package binaries deadlock on TRUNCATE. -p also + # bounds compiling, so the test binaries are built first with + # full parallelism (-run '^$' runs no test), and the serial pass + # reuses the build cache. + # + # -timeout 20m: internal/api alone takes minutes under -race on a + # busy runner, and a dev and a main run sharing the runner can + # push it past go test's default 10m with nothing hung. MINSTREL_DATABASE_URL="$MINSTREL_TEST_DATABASE_URL" go run ./cmd/minstrel migrate - go test -p 1 -race -timeout 20m ./... + PKGS=$(sh ci/db-test-packages.sh) + echo "database test packages: $(echo "$PKGS" | wc -l)" + go test -race -run '^$' $PKGS + go test -p 1 -race -timeout 20m $PKGS web: runs-on: go-ci diff --git a/Makefile b/Makefile index 9a35be0b..8ef937eb 100644 --- a/Makefile +++ b/Makefile @@ -38,9 +38,10 @@ test-short: test-integration: docker compose up -d postgres -docker compose exec -T postgres createdb -U minstrel minstrel_test - # -p 1: integration packages share one test DB and each TRUNCATEs it; - # concurrent package binaries deadlock on TRUNCATE. Serialize packages. - MINSTREL_TEST_DATABASE_URL=postgres://minstrel:minstrel@localhost:5432/minstrel_test?sslmode=disable go test -p 1 -race ./... + # Only packages with database tests; test-short covers the rest. + # -p 1: they share one test DB and each TRUNCATEs it; concurrent + # package binaries deadlock on TRUNCATE. Serialize packages. + MINSTREL_TEST_DATABASE_URL=postgres://minstrel:minstrel@localhost:5432/minstrel_test?sslmode=disable go test -p 1 -race $$(sh ci/db-test-packages.sh) lint: golangci-lint run ./... diff --git a/README.md b/README.md index 635c05a2..b8824d3b 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ Minstrel is not affiliated with or endorsed by ListenBrainz, MusicBrainz or Subs - **Backend:** `docker compose up` in this repo runs Postgres and Minstrel on `:4533`. - **Web:** `cd web && npm install && npm run dev` starts Vite on `:5173`. It proxies `/api/*` and `/rest/*` to `:4533`. -- **Tests:** `make test-short` runs the unit tests. `make test-integration` also runs the database tests, against a separate `minstrel_test` database so your dev data is never truncated. +- **Tests:** `make test-short` runs the unit tests. `make test-integration` runs the database tests, against a separate `minstrel_test` database so your dev data is never truncated. - **Image:** `docker build -t minstrel .` builds the web app and embeds it in the server binary. - **Branches:** work lands on `dev`; `main` changes through pull requests from `dev`. diff --git a/ci/db-test-packages.sh b/ci/db-test-packages.sh new file mode 100755 index 00000000..5920d6a3 --- /dev/null +++ b/ci/db-test-packages.sh @@ -0,0 +1,19 @@ +#!/bin/sh +# +# Prints the Go packages that hold database tests, one import path per line. +# +# A database test gets its Postgres from MINSTREL_TEST_DATABASE_URL, either +# through internal/dbtest or by reading the variable itself. A package whose +# tests mention neither cannot reach the database, so its tests are all unit +# tests: the `go` lane already runs them, and the `integration` lane skips the +# package instead of compiling and running it a second time, serially. +# +# Usage: go test -p 1 -race $(sh ci/db-test-packages.sh) +set -eu + +go list -f '{{if or .TestGoFiles .XTestGoFiles}}{{.ImportPath}} {{.Dir}}{{end}}' ./... | + while read -r pkg dir; do + if grep -qs -e 'dbtest\.' -e 'MINSTREL_TEST_DATABASE_URL' "$dir"/*_test.go; then + echo "$pkg" + fi + done diff --git a/internal/dbtest/reset.go b/internal/dbtest/reset.go index db47fdc7..92a16a6b 100644 --- a/internal/dbtest/reset.go +++ b/internal/dbtest/reset.go @@ -11,11 +11,6 @@ // 2. Always create test user rows with a username that begins with // TestUserPrefix; otherwise those rows survive across runs and // leak into other tests. -// -// One test legitimately needs to wipe the entire users table: -// internal/auth/bootstrap_test.go, which exercises first-time admin -// bootstrap. That file does its own TRUNCATE and does not use this -// helper. package dbtest import (