beads/cmd/bd/info_embedded_test.go
matt wilkie f443df1f2a
fix: resolve CI failures from flock, version bump, and lint (#2981)
* fix: resolve CI failures from flock, version bump, and lint

Three categories of CI failure on main after the v1.0.0 release:

1. rules.go lint/format: Fix gofmt alignment, errcheck on tw.Flush(),
   gosec G304 (add nosec comment), gosec G306 (use 0o600 perms).

2. doctor_test.go version tracking: The 'slightly outdated version'
   test used 0.55.0 which now has a major version diff from 1.0.0,
   correctly triggering a warning. Update expected status to match.

3. Embedded Dolt concurrent tests: The exclusive flock from #2571
   causes concurrent bd processes to fail with 'one writer at a time'.
   Update 58 concurrent test files to tolerate this expected error
   while still failing on unexpected errors and panics.

Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>

* fix: concurrent test post-conditions tolerate flock contention

With exclusive flock (commit 543e1943), only some workers can acquire
the lock in concurrent tests. Update post-conditions in 5 tests to:
- Only verify side effects from workers that succeeded (r.err == nil)
- Require at least 1 worker succeeds (fail if all hit flock)
- Log success/failure counts for visibility
- Still verify correct behavior for successful operations

Tests fixed:
- TestEmbeddedCloseConcurrent: remove Fatalf on failures, skip total count check
- TestEmbeddedDeferConcurrent: only check deferred status for successful workers
- TestEmbeddedUndeferConcurrent: only check open status for successful workers
- TestEmbeddedReopenConcurrent: only check open status for successful workers
- TestEmbeddedPromoteCLIConcurrent: only check permanent for successful workers

Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>
Amp-Thread-ID: https://ampcode.com/threads/T-019d5386-b41e-7138-bc81-6ea331b80760
Co-authored-by: Amp <amp@ampcode.com>

---------

Co-authored-by: Amp <amp@ampcode.com>
2026-04-03 07:27:00 -07:00

154 lines
3.8 KiB
Go

//go:build cgo
package main
import (
"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)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("bd info %s failed: %v\n%s", strings.Join(args, " "), err, out)
}
return string(out)
}
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))])
}
})
// Note: --json tests skipped — info's local --json flag shadows the
// root persistent flag, causing it to not produce JSON output.
// This is an existing bug, not related to embedded mode.
}
// 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)
}
}
}