beads/cmd/bd/info_embedded_test.go
Harry Miller 39b9ee1d81
fix(info): honor doctor.suppress.git-hooks in bd info (#6027) (#6710)
Root cause: renderInfo called CheckGitHooks()/FormatHookWarnings()
unconditionally, so setting doctor.suppress.git-hooks=true silenced the
check in bd doctor but bd info kept printing "Git hooks not installed".

Fix: split the config-map half of the doctor suppress reader out as
doctor.SuppressedChecksFromConfig (the store-backed helper now delegates
to it, same semantics), and have both bd info paths (direct and
proxied-server) feed it the config map they already load. renderInfo
skips the hook warning when the git-hooks slug is suppressed. bd info
--json never carried the warning and is unchanged.

Agent-Signature: claude-code-opus-5-5-unknown-reasoning on behalf of Harry Miller

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 05:44:48 -07:00

238 lines
6.6 KiB
Go

//go:build cgo
package main
import (
"encoding/json"
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
)
// bdInfo runs "bd info" with the given args and returns stdout.
func bdInfo(t *testing.T, bd, dir string, args ...string) string {
t.Helper()
fullArgs := append([]string{"info"}, args...)
cmd := exec.Command(bd, fullArgs...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
stdout, stderr, err := runCommandBuffers(t, cmd)
if err != nil {
t.Fatalf("bd info %s failed: %v\nstdout:\n%s\nstderr:\n%s", strings.Join(args, " "), err, stdout.String(), stderr.String())
}
return stdout.String()
}
func TestEmbeddedInfo(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "ti")
// ===== Default Output =====
t.Run("info_default", func(t *testing.T) {
out := bdInfo(t, bd, dir)
if !strings.Contains(out, ".beads") {
t.Errorf("expected .beads path in info output: %s", out)
}
if !strings.Contains(out, "Issue Count") {
t.Errorf("expected 'Issue Count' in info output: %s", out)
}
})
t.Run("info_with_issues", func(t *testing.T) {
bdCreate(t, bd, dir, "Info test issue 1", "--type", "task")
bdCreate(t, bd, dir, "Info test issue 2", "--type", "task")
out := bdInfo(t, bd, dir)
// Should show non-zero issue count
if strings.Contains(out, "Issue Count: 0") {
t.Errorf("expected non-zero issue count after creating issues: %s", out)
}
})
// ===== Schema Flag =====
t.Run("info_schema", func(t *testing.T) {
out := bdInfo(t, bd, dir, "--schema")
if !strings.Contains(out, "issues") {
t.Errorf("expected 'issues' table in schema output: %s", out)
}
if !strings.Contains(out, "Schema") {
t.Errorf("expected 'Schema' heading in output: %s", out)
}
})
// ===== Whats New =====
t.Run("info_whats_new", func(t *testing.T) {
out := bdInfo(t, bd, dir, "--whats-new")
if len(strings.TrimSpace(out)) == 0 {
t.Error("expected non-empty --whats-new output")
}
if !strings.Contains(out, "v0.") {
t.Errorf("expected version string in whats-new output: %s", out[:min(200, len(out))])
}
})
// ===== --json =====
infoJSON := func(t *testing.T, args ...string) map[string]interface{} {
t.Helper()
out := bdInfo(t, bd, dir, append([]string{"--json"}, args...)...)
var got map[string]interface{}
if err := json.Unmarshal([]byte(out), &got); err != nil {
t.Fatalf("info --json %v did not emit JSON: %v\n%s", args, err, out)
}
return got
}
t.Run("info_json", func(t *testing.T) {
got := infoJSON(t)
if got["mode"] != "direct" {
t.Errorf("mode = %v, want direct", got["mode"])
}
if path, ok := got["database_path"].(string); !ok || !strings.Contains(path, ".beads") {
t.Errorf("database_path missing or not a .beads path: %v", got["database_path"])
}
if _, ok := got["issue_count"].(float64); !ok {
t.Errorf("issue_count missing or not a number: %v", got["issue_count"])
}
})
t.Run("info_schema_json", func(t *testing.T) {
got := infoJSON(t, "--schema")
schema, ok := got["schema"].(map[string]interface{})
if !ok {
t.Fatalf("schema block missing: %v", got["schema"])
}
tables, ok := schema["tables"].([]interface{})
if !ok {
t.Fatalf("schema tables missing: %v", schema["tables"])
}
foundIssues := false
for _, tbl := range tables {
if tbl == "issues" {
foundIssues = true
break
}
}
if !foundIssues {
t.Errorf("expected 'issues' in schema tables: %v", tables)
}
})
t.Run("info_whats_new_json", func(t *testing.T) {
got := infoJSON(t, "--whats-new")
if _, ok := got["current_version"].(string); !ok {
t.Errorf("current_version missing or not a string: %v", got["current_version"])
}
changes, ok := got["recent_changes"].([]interface{})
if !ok || len(changes) == 0 {
t.Errorf("recent_changes missing or empty: %v", got["recent_changes"])
}
})
}
// TestEmbeddedInfoSuppressGitHooks pins GH#6027: bd info must honor
// doctor.suppress.git-hooks the same way bd doctor does, instead of printing
// the missing-hooks warning unconditionally.
func TestEmbeddedInfoSuppressGitHooks(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "ih")
bdRunOK(t, bd, dir, "hooks", "uninstall")
const hookWarning = "Git hooks not installed"
if out := bdInfo(t, bd, dir); !strings.Contains(out, hookWarning) {
t.Fatalf("precondition: expected %q with hooks uninstalled:\n%s", hookWarning, out)
}
bdConfig(t, bd, dir, "set", "doctor.suppress.git-hooks", "true")
if out := bdInfo(t, bd, dir); strings.Contains(out, hookWarning) {
t.Errorf("doctor.suppress.git-hooks=true should hide %q:\n%s", hookWarning, out)
}
bdConfig(t, bd, dir, "set", "doctor.suppress.git-hooks", "false")
if out := bdInfo(t, bd, dir); !strings.Contains(out, hookWarning) {
t.Errorf("doctor.suppress.git-hooks=false should keep %q:\n%s", hookWarning, out)
}
}
// TestEmbeddedInfoConcurrent exercises info operations concurrently.
func TestEmbeddedInfoConcurrent(t *testing.T) {
if os.Getenv("BEADS_TEST_EMBEDDED_DOLT") != "1" {
t.Skip("set BEADS_TEST_EMBEDDED_DOLT=1 to run embedded dolt integration tests")
}
t.Parallel()
bd := buildEmbeddedBD(t)
dir, _, _ := bdInit(t, bd, "--prefix", "ix")
for i := 0; i < 3; i++ {
bdCreate(t, bd, dir, fmt.Sprintf("info-concurrent-%d", i), "--type", "task")
}
const numWorkers = 8
type workerResult struct {
worker int
err error
}
results := make([]workerResult, numWorkers)
var wg sync.WaitGroup
wg.Add(numWorkers)
for w := 0; w < numWorkers; w++ {
go func(worker int) {
defer wg.Done()
r := workerResult{worker: worker}
var args []string
switch worker % 3 {
case 0:
args = []string{"info"}
case 1:
args = []string{"info", "--schema"}
case 2:
args = []string{"info", "--whats-new"}
}
cmd := exec.Command(bd, args...)
cmd.Dir = dir
cmd.Env = bdEnv(dir)
out, err := cmd.CombinedOutput()
if err != nil {
r.err = fmt.Errorf("info (worker %d): %v\n%s", worker, err, out)
results[worker] = r
return
}
if len(strings.TrimSpace(string(out))) == 0 {
r.err = fmt.Errorf("info (worker %d): empty output", worker)
results[worker] = r
return
}
results[worker] = r
}(w)
}
wg.Wait()
for _, r := range results {
if r.err != nil && !strings.Contains(r.err.Error(), "one writer at a time") {
t.Errorf("worker %d failed: %v", r.worker, r.err)
}
}
}