From cb6fac587b86573dd7b0bf25a045d3073184c1ac Mon Sep 17 00:00:00 2001 From: Aska Date: Sun, 6 Sep 2026 10:40:06 +0800 Subject: [PATCH] =?UTF-8?q?test(db):=20Open=20=E8=87=AA=E5=8A=A8=E5=BB=BA?= =?UTF-8?q?=E7=88=B6=E7=9B=AE=E5=BD=95=E6=94=B9=E4=B8=BA=E6=96=AD=E8=A8=80?= =?UTF-8?q?=E6=88=90=E5=8A=9F=EF=BC=88=E8=A1=8C=E4=B8=BA=E6=94=B9=E8=BF=9B?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/db_test.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/internal/db/db_test.go b/internal/db/db_test.go index 42c9bfc..4c127ca 100644 --- a/internal/db/db_test.go +++ b/internal/db/db_test.go @@ -5,6 +5,7 @@ import ( "errors" "io" "log/slog" + "os" "path/filepath" "testing" ) @@ -124,13 +125,22 @@ func TestOpen_BlankPath_异常(t *testing.T) { } } -func TestOpen_MissingParentDir_异常(t *testing.T) { +func TestOpen_CreatesParentDir_正常(t *testing.T) { t.Parallel() ctx := context.Background() - // Parent directory does not exist, so the connection cannot be established. - path := filepath.Join(t.TempDir(), "does-not-exist", "cyrene.db") - if _, err := Open(ctx, path, testLogger()); err == nil { - t.Fatalf("Open(%q) expected error for missing parent dir, got nil", path) + // Parent directory does not exist yet: Open must create it so a custom + // DATA_DIR works on first run. + dir := filepath.Join(t.TempDir(), "does-not-exist") + path := filepath.Join(dir, "cyrene.db") + db, err := Open(ctx, path, testLogger()) + if err != nil { + t.Fatalf("Open(%q) error creating parent dir: %v", path, err) + } + defer db.Close() + if fi, err := os.Stat(dir); err != nil { + t.Fatalf("parent dir not created: %v", err) + } else if !fi.IsDir() { + t.Fatalf("parent path is not a dir: %v", fi) } }