beads/cmd/bd/shared_server_cfgnil_test.go
Robin Carswell dd47e2ed50
test(shared-server): disable metrics flusher to stop t.TempDir cleanup race (#5032)
TestSharedServerCfgNilHonorsSharedServer intermittently failed in its own
t.TempDir() cleanup with "unlinkat …: directory not empty" (seen on the PR
Core job). The test body passes; the failure is teardown.

Root cause: bd spawns a detached `bd send-metrics` child
(internal/metrics/spawn.go) that keeps writing $HOME/.beads/eventsData after
the parent exits. HOME here is a t.TempDir(), so the child races Go's
automatic RemoveAll. Confirmed by process inspection: the child lingers
without a disable flag and is absent with BD_DISABLE_EVENT_FLUSH=1.

Add BD_DISABLE_EVENT_FLUSH=1 to the test env, matching the sibling
shared_server_integration and protocol tests, which disable it for the same
documented reason (08881217b, 2026-07-13).

Fixes #5031

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-07-26 19:53:22 -07:00

93 lines
3.7 KiB
Go

//go:build cgo
package main
import (
"context"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
)
// TestSharedServerCfgNilHonorsSharedServer is a regression test for GH#3817.
//
// When the resolved beadsDir has no metadata.json/config.yaml, configfile.Load
// returns (nil, nil). The shared-server override in cmd/bd/main.go used to live
// only inside the `cfg != nil` branch, so an exported BEADS_DOLT_SHARED_SERVER
// was silently ignored: doltCfg.ServerMode stayed false and newDoltStore fell
// through to embeddeddolt.Open. With a leftover embedded data directory present
// (e.g. a clone or recovery dir that has no metadata.json yet) bd would read
// that phantom embedded DB and silently report success, fragmenting data away
// from the shared server instead of connecting to it.
//
// The fix honors shared-server mode regardless of whether project config was
// found. This test reproduces the config-less case with no reachable server and
// asserts bd attempts the server (clear error) rather than silently falling
// back to the embedded database.
//
// Hermetic: no container required. Auto-start is disabled and the server port
// points nowhere, so the "honored" path fails fast with a connection error.
func TestSharedServerCfgNilHonorsSharedServer(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("not supported on Windows")
}
bdBinary := buildSharedServerTestBinary(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
// Config-less beads dir (no metadata.json, no config.yaml) with a leftover
// embedded data directory — the configfile.Load -> (nil, nil) case where an
// embedded read could otherwise silently succeed.
beadsDir := filepath.Join(t.TempDir(), ".beads", "shared-server")
if err := os.MkdirAll(filepath.Join(beadsDir, "dolt"), 0o755); err != nil {
t.Fatalf("mkdir config-less beads dir: %v", err)
}
env := []string{
"PATH=" + os.Getenv("PATH"),
"HOME=" + t.TempDir(),
"BEADS_DIR=" + beadsDir,
"BEADS_DOLT_SHARED_SERVER=1",
// Disable auto-start and point at a port nothing listens on so the
// shared-server path fails fast instead of spinning up a server.
"BEADS_DOLT_AUTO_START=0",
"BEADS_DOLT_SERVER_PORT=59999",
// Disable the metrics flusher: bd otherwise spawns a DETACHED
// `bd send-metrics` child that keeps writing $HOME/.beads/eventsData
// after the parent exits. HOME here is a t.TempDir(), so that child
// races Go's automatic RemoveAll and the test fails its own cleanup with
// "unlinkat …: directory not empty" (matches the sibling shared-server
// and protocol tests, which disable it for the same reason).
"BD_DISABLE_EVENT_FLUSH=1",
"BEADS_TEST_MODE=1",
"GIT_TERMINAL_PROMPT=0",
"GIT_ASKPASS=",
"SSH_ASKPASS=",
"GT_ROOT=",
}
neutralCwd := t.TempDir()
out, err := ssExec(ctx, bdBinary, neutralCwd, env, "list")
// Before the fix: bd silently ignored shared-server mode, read the leftover
// embedded DB, and exited 0 with "No issues found". After: shared-server is
// honored, so bd tries the (unreachable) server and fails clearly.
if err == nil {
t.Fatalf("bd list silently succeeded under BEADS_DOLT_SHARED_SERVER with no "+
"reachable server and no project config; expected a server-connection "+
"error — bd fell through to the embedded backend (GH#3817 regression)\n"+
"output:\n%s", out)
}
if strings.Contains(out, "No issues found") {
t.Fatalf("bd list returned embedded \"No issues found\" instead of honoring "+
"shared-server mode (GH#3817 regression)\noutput:\n%s", out)
}
if !strings.Contains(out, "Dolt server") {
t.Fatalf("expected a Dolt server connection error (shared-server mode honored), got:\n%s", out)
}
}