From 6d72c85766a8355e1d660ff0c19b43e93faad4d8 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Sat, 1 Aug 2026 14:49:15 +0200 Subject: add testing/pgruntime Testing pgruntime has quickly turned out to be a nightmare, because so many parts of the test logic are not easily reproducible (e.g. `stopDBIfLingering`). I decided to not waste a bunch of time testing code that is relatively compact and easy to verify by careful examination, and that is unlikely to change a lot in the future. --- pgruntime/connector.go | 10 ++++++---- pgruntime/main_test.go | 14 -------------- pgruntime/pgruntime.go | 4 +--- pgruntime/testdb.go | 10 +++++++--- 4 files changed, 14 insertions(+), 24 deletions(-) delete mode 100644 pgruntime/main_test.go (limited to 'pgruntime') diff --git a/pgruntime/connector.go b/pgruntime/connector.go index 92780f6..5276a26 100644 --- a/pgruntime/connector.go +++ b/pgruntime/connector.go @@ -164,10 +164,12 @@ func resetTestDatabase(ctx context.Context, db gsql.Handle, params testSetupPara } // truncate all tables at once - query = fmt.Sprintf(`TRUNCATE %s RESTART IDENTITY CASCADE`, strings.Join(quotedTableNames, ", ")) - _, err = execQuery(ctx, db, query, nil) - if err != nil { - return fmt.Errorf("during %s: %w", query, err) + if len(quotedTableNames) > 0 { + query = fmt.Sprintf(`TRUNCATE %s RESTART IDENTITY CASCADE`, strings.Join(quotedTableNames, ", ")) + _, err = execQuery(ctx, db, query, nil) + if err != nil { + return fmt.Errorf("during %s: %w", query, err) + } } return nil diff --git a/pgruntime/main_test.go b/pgruntime/main_test.go deleted file mode 100644 index db6b0ac..0000000 --- a/pgruntime/main_test.go +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-FileCopyrightText: 2026 Stefan Majewsky -// SPDX-License-Identifier: Apache-2.0 - -package pgruntime_test - -import ( - "testing" - - "go.xyrillian.de/gg/pgruntime" -) - -func TestMain(m *testing.M) { - pgruntime.WithTestDB(m, m.Run) -} diff --git a/pgruntime/pgruntime.go b/pgruntime/pgruntime.go index 17a6c94..0283eaa 100644 --- a/pgruntime/pgruntime.go +++ b/pgruntime/pgruntime.go @@ -20,12 +20,10 @@ // This is a clean-room reimplementation of one half of [easypg] with several interface improvements and cleanups, most notably: // - The hard dependency on lib/pq has been removed. // - Support for creating databases on first use has been removed (except in ConnectForTest). -// - ConnectForTest now recreates databases instead of just wiping their contents. +// - The reset logic in ConnectForTest is completely reworked and massively simplified. // // [easypg]: https://pkg.go.dev/github.com/sapcc/go-bits/easypg // [lib/pq]: https://pkg.go.dev/github.com/lib/pq // [pgx]: https://pkg.go.dev/github.com/jackc/pgx/v5 // [gg-pgx]: https://git.xyrillian.de/go-gg-pgx/ package pgruntime - -// TODO: test coverage via separate module importing github.com/lib/pq diff --git a/pgruntime/testdb.go b/pgruntime/testdb.go index 261f88a..808784c 100644 --- a/pgruntime/testdb.go +++ b/pgruntime/testdb.go @@ -69,8 +69,7 @@ func WithTestDB(m *testing.M, action func() int) int { if err == nil { return result } else { - fmt.Fprintln(os.Stderr, err.Error()) - return 1 + panic(err.Error()) } } @@ -135,7 +134,12 @@ func findTestdbPath() (string, error) { // if there is an override path, report the override path (so that initDBIfNecessary can create it), // but put a symlink at the standard path for convenient access - return overridePath, os.Symlink(overridePath, testdbPath) + err = os.Symlink(overridePath, testdbPath) + if os.IsExist(err) { + // do not complain if the symlink already exists + err = nil + } + return overridePath, err } func findModuleRootDir(dirPath string) (Option[string], error) { -- cgit v1.3.1