beads/cmd/bd/store_selection_failloud_nocgo_test.go
matt wilkie 64cdff3921 fix(storage): scope metadata.json fail-loud to store-selecting paths
Maintainer follow-up on the fail-loud contract: the hard error must fire
where a store is actually selected (store init, store factories), but the
initial patch also fataled in the two pre-run contexts that serve no-DB
commands. That bricked the repair path for the very corruption being
reported: bd doctor, bd init, bd version, bootstrap, context all exited 1
on a corrupt metadata.json, while the fatal's own message says to "fix or
restore metadata.json and retry".

- prepareSelectedCommandContext and the skipsStoreInit pre-run branch now
  warn-and-continue on a metadata load failure; data commands remain
  protected by the store-init fatal and the factory errors.
- Reword the loadServerModeFromBeadsDir error so it reads correctly in
  warn context (mode detection skipped, data commands will refuse to run).
- Drop a comment describing the now-unreachable cfgErr != nil fallthrough.
- Add an end-to-end regression test: with corrupt metadata.json, version
  and doctor run, list fails loud naming the file, bd init repairs, list
  works again with data intact. Add a nocgo variant covering the factory
  error message.

Found in maintainer review of #4366; two independent reviewers flagged
the repair-path regression.

Agent-Signature: claude-fable-5-high on behalf of matt wilkie
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-06 13:19:23 -07:00

44 lines
1.3 KiB
Go

//go:build !cgo
package main
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
)
// The nocgo factories must also fail loud on a present-but-unloadable
// metadata.json — naming the real cause instead of the misleading
// "embedded requires CGO" fallback message.
func TestNocgoStoreFactoriesCorruptMetadataFailLoud(t *testing.T) {
beadsDir := filepath.Join(t.TempDir(), ".beads")
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"dolt_mode":"serv`), 0o600); err != nil {
t.Fatalf("write corrupt metadata.json: %v", err)
}
for name, open := range map[string]func() (interface{ Close() error }, error){
"newDoltStoreFromConfig": func() (interface{ Close() error }, error) {
return newDoltStoreFromConfig(context.Background(), beadsDir)
},
"newReadOnlyStoreFromConfig": func() (interface{ Close() error }, error) {
return newReadOnlyStoreFromConfig(context.Background(), beadsDir)
},
} {
store, err := open()
if err == nil {
if store != nil {
_ = store.Close()
}
t.Fatalf("%s: want error for corrupt metadata.json, got nil", name)
}
if !strings.Contains(err.Error(), "metadata.json") {
t.Fatalf("%s: error should name metadata.json, got: %v", name, err)
}
}
}