Files
code-utils/bootstrap_test.go

55 lines
1.3 KiB
Go
Raw Normal View History

2026-08-11 19:07:05 +08:00
package main
import (
"os"
"path/filepath"
"testing"
)
func TestBootstrapRoundTrip(t *testing.T) {
p := filepath.Join(t.TempDir(), "bootstrap.json")
want := BootstrapConfig{DatabasePath: filepath.Join(t.TempDir(), "data.db"), Initialized: true}
if e := writeBootstrap(p, want); e != nil {
t.Fatal(e)
}
got, e := readBootstrap(p)
if e != nil {
t.Fatal(e)
}
if got != want {
t.Fatalf("got %#v want %#v", got, want)
}
}
func TestInvalidBootstrap(t *testing.T) {
p := filepath.Join(t.TempDir(), "bootstrap.json")
if e := os.WriteFile(p, []byte("{"), 0600); e != nil {
t.Fatal(e)
}
if _, e := readBootstrap(p); e == nil {
t.Fatal("invalid bootstrap accepted")
}
}
func TestCreateValidatedStore(t *testing.T) {
p := filepath.Join(t.TempDir(), "nested", "code-count.db")
s, e := createValidatedStore(p)
if e != nil {
t.Fatal(e)
}
defer s.db.Close()
if _, e = os.Stat(p); e != nil {
t.Fatal(e)
}
var result string
if e = s.db.QueryRow(`PRAGMA integrity_check`).Scan(&result); e != nil || result != "ok" {
t.Fatalf("integrity: %q %v", result, e)
}
if _, e = os.Stat(p + ".initializing"); !os.IsNotExist(e) {
t.Fatal("temporary database remains")
}
}
func TestCreateValidatedStoreRejectsEmptyPath(t *testing.T) {
if _, e := createValidatedStore(""); e == nil {
t.Fatal("empty path accepted")
}
}