From c593eae60315deaf95137f893743d0a4b7c9b632 Mon Sep 17 00:00:00 2001 From: sneak Date: Fri, 16 Oct 2026 01:18:18 +0000 Subject: [PATCH] Test the db_url derived from a state_dir starting with // The test sets state_dir to a temporary directory with a / put in front, opens the derived db_url with the SQLite driver, and checks that the database is state.sqlite3 in that directory, in WAL mode. It fails while the URL starts file://, where SQLite reads the first directory name as a host. The open-and-check part of the test before it moves into a helper both tests call. Model: opus-5-5 --- .../config/config_validation_internal_test.go | 42 +++++++++++++++---- 1 file changed, 35 insertions(+), 7 deletions(-) diff --git a/internal/config/config_validation_internal_test.go b/internal/config/config_validation_internal_test.go index 2e06fc9..3f4bc3c 100644 --- a/internal/config/config_validation_internal_test.go +++ b/internal/config/config_validation_internal_test.go @@ -158,9 +158,39 @@ func TestDerivedDBURLOpensStateDirHoldingURLCharacters(t *testing.T) { t.Logf("state_dir %q gives db_url %q", stateDir, c.DBURL) - db, err := sql.Open("sqlite", c.DBURL) + checkDBURLOpensFileInWALMode(t, c.DBURL, filepath.Join(stateDir, "state.sqlite3")) +} + +// TestDerivedDBURLOpensStateDirStartingWithTwoSlashes opens the db_url +// derived from a state_dir that starts with //, and checks that the +// database is state.sqlite3 in that directory, in WAL mode. Written right +// after file:, such a name gives a URL starting file://, and SQLite reads +// the first directory name as a host and refuses to open it. +func TestDerivedDBURLOpensStateDirStartingWithTwoSlashes(t *testing.T) { + t.Parallel() + + dir := t.TempDir() + stateDir := "/" + dir + + c, err := configFromYAML(t, signingKeyLine+"state_dir: "+stateDir+"\n") if err != nil { - t.Fatalf("failed to open %q: %v", c.DBURL, err) + t.Fatalf("config with state_dir %q should be valid, got: %v", stateDir, err) + } + + t.Logf("state_dir %q gives db_url %q", stateDir, c.DBURL) + + checkDBURLOpensFileInWALMode(t, c.DBURL, filepath.Join(dir, "state.sqlite3")) +} + +// checkDBURLOpensFileInWALMode opens dbURL with the SQLite driver pixad +// uses, and checks that the database is in WAL mode and is the file +// dbFile. +func checkDBURLOpensFileInWALMode(t *testing.T, dbURL, dbFile string) { + t.Helper() + + db, err := sql.Open("sqlite", dbURL) + if err != nil { + t.Fatalf("failed to open %q: %v", dbURL, err) } t.Cleanup(func() { _ = db.Close() }) @@ -169,18 +199,16 @@ func TestDerivedDBURLOpensStateDirHoldingURLCharacters(t *testing.T) { err = db.QueryRowContext(t.Context(), "PRAGMA journal_mode").Scan(&journalMode) if err != nil { - t.Fatalf("failed to read the journal mode of %q: %v", c.DBURL, err) + t.Fatalf("failed to read the journal mode of %q: %v", dbURL, err) } if journalMode != "wal" { - t.Errorf("journal mode of %q = %q, want wal", c.DBURL, journalMode) + t.Errorf("journal mode of %q = %q, want wal", dbURL, journalMode) } - dbFile := filepath.Join(stateDir, "state.sqlite3") - _, err = os.Stat(dbFile) if err != nil { - t.Errorf("opening %q did not create %q: %v", c.DBURL, dbFile, err) + t.Errorf("opening %q did not create %q: %v", dbURL, dbFile, err) } }