初始化
This commit is contained in:
54
bootstrap_test.go
Normal file
54
bootstrap_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user