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
This commit is contained in:
2026-10-16 01:42:43 +00:00
parent 8c9623f56c
commit 1911489815
@@ -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)
}
}