The PR introduced exportIncludeMemories as a new package-level flag but did not add it to saveAndRestoreGlobals, making test outcomes order-dependent when export tests toggle it. Add all six export flag globals (exportOutput, exportAll, exportIncludeInfra, exportScrub, exportNoMemories, exportIncludeMemories) to the savedGlobals snapshot so that saveAndRestoreGlobals atomically covers all export state. Tests that already call saveAndRestoreGlobals now get full isolation without manual t.Cleanup per-global. Addresses maintainer feedback on GH#3660.
449 lines
14 KiB
Go
449 lines
14 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/config"
|
|
"github.com/steveyegge/beads/internal/configfile"
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/storage/dolt"
|
|
"github.com/steveyegge/beads/internal/storage/doltutil"
|
|
"github.com/steveyegge/beads/internal/testutil"
|
|
)
|
|
|
|
// testDoltServerPort is the port of the shared test Dolt server (0 = not running).
|
|
var testDoltServerPort int
|
|
|
|
// uniqueTestDBName generates a unique database name for test isolation.
|
|
func uniqueTestDBName(t *testing.T) string {
|
|
t.Helper()
|
|
h := sha256.Sum256([]byte(t.Name() + fmt.Sprintf("%d", time.Now().UnixNano())))
|
|
return "testdb_" + hex.EncodeToString(h[:6])
|
|
}
|
|
|
|
// testIDCounter ensures unique IDs across all test runs
|
|
var testIDCounter atomic.Uint64
|
|
|
|
// doltNewMutex serializes dolt.New() calls in tests. The Dolt embedded engine's
|
|
// InitStatusVariables() has an internal race condition when called concurrently
|
|
// from multiple goroutines (writes to a shared global map without synchronization).
|
|
// Serializing store creation prevents this race while allowing tests to run their
|
|
// assertions in parallel after the store is created.
|
|
var doltNewMutex sync.Mutex
|
|
|
|
// stdioMutex serializes tests that redirect os.Stdout or os.Stderr.
|
|
// These process-global file descriptors cannot be safely redirected from
|
|
// concurrent goroutines.
|
|
//
|
|
// IMPORTANT: Any test that calls cobra's Help(), Execute(), or Print*()
|
|
// MUST NOT be parallel (no t.Parallel()), OR must serialize those calls
|
|
// under stdioMutex. Setting cmd.SetOut() is NOT sufficient because cobra's
|
|
// OutOrStdout() eagerly evaluates os.Stdout as the default argument even
|
|
// when outWriter is set — the Go race detector catches this read.
|
|
//
|
|
// TestCobraParallelPolicyGuard in stdio_race_guard_test.go enforces this.
|
|
var stdioMutex sync.Mutex
|
|
|
|
// generateUniqueTestID creates a globally unique test ID using prefix, test name, and atomic counter.
|
|
// This prevents ID collisions when multiple tests manipulate global state.
|
|
func generateUniqueTestID(t *testing.T, prefix string, index int) string {
|
|
t.Helper()
|
|
counter := testIDCounter.Add(1)
|
|
// include test name, counter, and index for uniqueness
|
|
data := []byte(t.Name() + prefix + string(rune(counter)) + string(rune(index)))
|
|
hash := sha256.Sum256(data)
|
|
return prefix + "-" + hex.EncodeToString(hash[:])[:8]
|
|
}
|
|
|
|
const windowsOS = "windows"
|
|
|
|
// initConfigForTest initializes viper config for a test and ensures cleanup.
|
|
// main.go's init() calls config.Initialize() which picks up the real .beads/config.yaml.
|
|
// TestMain resets viper, but any test calling config.Initialize() re-loads the real config.
|
|
// This helper ensures viper is reset after the test completes, preventing state pollution
|
|
// (e.g., repo config values leaking into JSONL export tests).
|
|
func initConfigForTest(t *testing.T) {
|
|
t.Helper()
|
|
config.ResetForTesting()
|
|
if err := config.Initialize(); err != nil {
|
|
t.Fatalf("config.Initialize: %v", err)
|
|
}
|
|
t.Cleanup(config.ResetForTesting)
|
|
}
|
|
|
|
// ensureTestMode is a no-op; BEADS_TEST_MODE is set once in TestMain.
|
|
// Previously each test set/unset the env var, which raced under t.Parallel().
|
|
func ensureTestMode(t *testing.T) {
|
|
t.Helper()
|
|
// BEADS_TEST_MODE is set in TestMain and stays set for the entire test run.
|
|
}
|
|
|
|
// ensureCleanGlobalState resets global state that may have been modified by other tests.
|
|
// Call this at the start of tests that manipulate globals directly.
|
|
func ensureCleanGlobalState(t *testing.T) {
|
|
t.Helper()
|
|
// Reset CommandContext so accessor functions fall back to globals
|
|
resetCommandContext()
|
|
}
|
|
|
|
// savedGlobals holds a snapshot of package-level globals for safe restoration.
|
|
// Used by saveAndRestoreGlobals to ensure test isolation.
|
|
type savedGlobals struct {
|
|
dbPath string
|
|
store storage.DoltStorage
|
|
storeActive bool
|
|
exportOutput string
|
|
exportAll bool
|
|
exportIncludeInfra bool
|
|
exportScrub bool
|
|
exportNoMemories bool
|
|
exportIncludeMemories bool
|
|
}
|
|
|
|
// saveAndRestoreGlobals snapshots all commonly-mutated package-level globals
|
|
// and registers a t.Cleanup() to restore them when the test completes.
|
|
// This replaces the fragile manual save/defer pattern:
|
|
//
|
|
// oldDBPath := dbPath
|
|
// defer func() { dbPath = oldDBPath }()
|
|
//
|
|
// With the safer:
|
|
//
|
|
// saveAndRestoreGlobals(t)
|
|
//
|
|
// Benefits:
|
|
// - All globals saved atomically (can't forget one)
|
|
// - t.Cleanup runs even on panic (no risk of missed defer registration)
|
|
// - Single call replaces multiple save/defer pairs
|
|
func saveAndRestoreGlobals(t *testing.T) *savedGlobals {
|
|
t.Helper()
|
|
saved := &savedGlobals{
|
|
dbPath: dbPath,
|
|
store: store,
|
|
storeActive: storeActive,
|
|
exportOutput: exportOutput,
|
|
exportAll: exportAll,
|
|
exportIncludeInfra: exportIncludeInfra,
|
|
exportScrub: exportScrub,
|
|
exportNoMemories: exportNoMemories,
|
|
exportIncludeMemories: exportIncludeMemories,
|
|
}
|
|
t.Cleanup(func() {
|
|
dbPath = saved.dbPath
|
|
store = saved.store
|
|
storeMutex.Lock()
|
|
storeActive = saved.storeActive
|
|
storeMutex.Unlock()
|
|
exportOutput = saved.exportOutput
|
|
exportAll = saved.exportAll
|
|
exportIncludeInfra = saved.exportIncludeInfra
|
|
exportScrub = saved.exportScrub
|
|
exportNoMemories = saved.exportNoMemories
|
|
exportIncludeMemories = saved.exportIncludeMemories
|
|
})
|
|
return saved
|
|
}
|
|
|
|
// writeTestMetadata writes metadata.json in the .beads directory (parent of dbPath)
|
|
// so that NewFromConfig can find the correct database name and server settings when
|
|
// routing reopens a store by path.
|
|
func writeTestMetadata(t *testing.T, dbPath string, database string) {
|
|
t.Helper()
|
|
beadsDir := filepath.Dir(dbPath)
|
|
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
|
t.Fatalf("Failed to create beads dir: %v", err)
|
|
}
|
|
cfg := &configfile.Config{
|
|
Database: "dolt",
|
|
Backend: configfile.BackendDolt,
|
|
DoltMode: configfile.DoltModeServer,
|
|
DoltDatabase: database,
|
|
DoltServerHost: "127.0.0.1",
|
|
DoltServerPort: testDoltServerPort,
|
|
}
|
|
if err := cfg.Save(beadsDir); err != nil {
|
|
t.Fatalf("Failed to write test metadata.json: %v", err)
|
|
}
|
|
}
|
|
|
|
// newTestStore creates a dolt store with issue_prefix configured (bd-166).
|
|
// Uses shared database with branch-per-test isolation (bd-xmf) to avoid
|
|
// the overhead of CREATE/DROP DATABASE per test.
|
|
// Falls back to per-test databases if the shared DB is not available.
|
|
func newTestStore(t *testing.T, dbPath string) *dolt.DoltStore {
|
|
t.Helper()
|
|
return newTestStoreWithPrefix(t, dbPath, "test")
|
|
}
|
|
|
|
// newTestStoreIsolatedDB creates a dolt store with its own dedicated database.
|
|
// Use this instead of newTestStoreWithPrefix when the test needs a truly separate
|
|
// database (e.g., routing tests that create multiple stores with different paths
|
|
// and expect routing to reopen them by path via metadata.json).
|
|
func newTestStoreIsolatedDB(t *testing.T, dbPath string, prefix string) *dolt.DoltStore {
|
|
t.Helper()
|
|
|
|
ensureTestMode(t)
|
|
|
|
if testDoltServerPort == 0 {
|
|
t.Skip("Dolt test server not available, skipping")
|
|
}
|
|
if testutil.DoltContainerCrashed() {
|
|
t.Skipf("Dolt test server crashed: %v", testutil.DoltContainerCrashError())
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
cfg := &dolt.Config{
|
|
Path: dbPath,
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testDoltServerPort,
|
|
Database: uniqueTestDBName(t),
|
|
CreateIfMissing: true,
|
|
}
|
|
writeTestMetadata(t, dbPath, cfg.Database)
|
|
|
|
doltNewMutex.Lock()
|
|
s, err := dolt.New(ctx, cfg)
|
|
doltNewMutex.Unlock()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create dolt store: %v", err)
|
|
}
|
|
|
|
if err := s.SetConfig(ctx, "issue_prefix", prefix); err != nil {
|
|
s.Close()
|
|
t.Fatalf("Failed to set issue_prefix: %v", err)
|
|
}
|
|
if err := s.SetConfig(ctx, "types.custom", "molecule,gate,convoy,merge-request,slot,agent,role,rig,event,message"); err != nil {
|
|
s.Close()
|
|
t.Fatalf("Failed to set types.custom: %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
s.Close()
|
|
if cfg.Database != "" {
|
|
dropTestDatabase(cfg.Database, testDoltServerPort)
|
|
}
|
|
})
|
|
return s
|
|
}
|
|
|
|
// newTestStoreWithPrefix creates a dolt store with custom issue_prefix configured.
|
|
// Uses shared database with branch-per-test isolation (bd-xmf) when available,
|
|
// falling back to per-test databases otherwise.
|
|
func newTestStoreWithPrefix(t *testing.T, dbPath string, prefix string) *dolt.DoltStore {
|
|
t.Helper()
|
|
|
|
ensureTestMode(t)
|
|
|
|
if testDoltServerPort == 0 {
|
|
t.Skip("Dolt test server not available, skipping")
|
|
}
|
|
if testutil.DoltContainerCrashed() {
|
|
t.Skipf("Dolt test server crashed: %v", testutil.DoltContainerCrashError())
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
// Fast path: use shared DB with branch-per-test isolation (bd-xmf)
|
|
if testSharedDB != "" {
|
|
return newTestStoreSharedBranch(t, dbPath, prefix)
|
|
}
|
|
|
|
// Fallback: per-test database (original slow path)
|
|
cfg := &dolt.Config{
|
|
Path: dbPath,
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testDoltServerPort,
|
|
Database: uniqueTestDBName(t),
|
|
CreateIfMissing: true,
|
|
}
|
|
writeTestMetadata(t, dbPath, cfg.Database)
|
|
|
|
doltNewMutex.Lock()
|
|
s, err := dolt.New(ctx, cfg)
|
|
doltNewMutex.Unlock()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create dolt store: %v", err)
|
|
}
|
|
|
|
if err := s.SetConfig(ctx, "issue_prefix", prefix); err != nil {
|
|
s.Close()
|
|
t.Fatalf("Failed to set issue_prefix: %v", err)
|
|
}
|
|
if err := s.SetConfig(ctx, "types.custom", "molecule,gate,convoy,merge-request,slot,agent,role,rig,event,message"); err != nil {
|
|
s.Close()
|
|
t.Fatalf("Failed to set types.custom: %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
s.Close()
|
|
if cfg.Database != "" {
|
|
dropTestDatabase(cfg.Database, testDoltServerPort)
|
|
}
|
|
})
|
|
return s
|
|
}
|
|
|
|
// newTestStoreSharedBranch creates a store using the shared database with
|
|
// branch-per-test isolation. Each test gets its own Dolt branch, avoiding
|
|
// the expensive CREATE DATABASE + schema init + DROP DATABASE + PURGE cycle.
|
|
func newTestStoreSharedBranch(t *testing.T, dbPath string, prefix string) *dolt.DoltStore {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
|
|
// Write metadata.json pointing to the shared database
|
|
writeTestMetadata(t, dbPath, testSharedDB)
|
|
|
|
// Open store against the shared database with MaxOpenConns=1
|
|
// (required for DOLT_CHECKOUT session affinity)
|
|
doltNewMutex.Lock()
|
|
s, err := dolt.New(ctx, &dolt.Config{
|
|
Path: dbPath,
|
|
ServerHost: "127.0.0.1",
|
|
ServerPort: testDoltServerPort,
|
|
Database: testSharedDB,
|
|
MaxOpenConns: 1,
|
|
})
|
|
doltNewMutex.Unlock()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create dolt store (shared): %v", err)
|
|
}
|
|
|
|
// Create isolated branch for this test
|
|
_, branchCleanup := testutil.StartTestBranch(t, s.DB(), testSharedDB)
|
|
|
|
// Create ignored tables on this branch
|
|
if err := dolt.CreateIgnoredTables(s.DB()); err != nil {
|
|
branchCleanup()
|
|
s.Close()
|
|
t.Fatalf("CreateIgnoredTables: %v", err)
|
|
}
|
|
|
|
// Set prefix for this test (overrides the shared schema's default)
|
|
if err := s.SetConfig(ctx, "issue_prefix", prefix); err != nil {
|
|
branchCleanup()
|
|
s.Close()
|
|
t.Fatalf("Failed to set issue_prefix: %v", err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
branchCleanup()
|
|
s.Close()
|
|
})
|
|
return s
|
|
}
|
|
|
|
// dropTestDatabase drops a test database from the shared server (best-effort cleanup).
|
|
func dropTestDatabase(dbName string, port int) {
|
|
dsn := doltutil.ServerDSN{Host: "127.0.0.1", Port: port, User: "root"}.String()
|
|
db, err := sql.Open("mysql", dsn)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer db.Close()
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
//nolint:gosec // G201: dbName is generated by uniqueTestDBName (testdb_ + random hex)
|
|
_, _ = db.ExecContext(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS `%s`", dbName))
|
|
// Purge dropped databases from Dolt's trash directory to reclaim disk space
|
|
_, _ = db.ExecContext(ctx, "CALL dolt_purge_dropped_databases()")
|
|
}
|
|
|
|
// openExistingTestDB reopens an existing Dolt store for verification in tests.
|
|
// It tries NewFromConfig first (reads metadata.json for correct database name),
|
|
// then falls back to direct open for BEADS_DB or other non-standard paths.
|
|
func openExistingTestDB(t *testing.T, dbPath string) (*dolt.DoltStore, error) {
|
|
t.Helper()
|
|
// Serialize dolt.New() to avoid race in Dolt's InitStatusVariables (bd-cqjoi)
|
|
doltNewMutex.Lock()
|
|
defer doltNewMutex.Unlock()
|
|
ctx := context.Background()
|
|
// Try NewFromConfig which reads metadata.json for correct database name
|
|
beadsDir := filepath.Dir(dbPath)
|
|
if store, err := dolt.NewFromConfig(ctx, beadsDir); err == nil {
|
|
return store, nil
|
|
}
|
|
// Fallback: open directly with test server config
|
|
cfg := &dolt.Config{Path: dbPath}
|
|
if testDoltServerPort != 0 {
|
|
cfg.ServerHost = "127.0.0.1"
|
|
cfg.ServerPort = testDoltServerPort
|
|
}
|
|
return dolt.New(ctx, cfg)
|
|
}
|
|
|
|
// runCommandInDir runs a command in the specified directory
|
|
func runCommandInDir(dir string, name string, args ...string) error {
|
|
cmd := exec.Command(name, args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = testEnvNoPrompt()
|
|
return cmd.Run()
|
|
}
|
|
|
|
// runCommandInDirWithOutput runs a command in the specified directory and returns its output
|
|
func runCommandInDirWithOutput(dir string, name string, args ...string) (string, error) {
|
|
cmd := exec.Command(name, args...)
|
|
cmd.Dir = dir
|
|
cmd.Env = testEnvNoPrompt()
|
|
output, err := cmd.Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return strings.TrimSpace(string(output)), nil
|
|
}
|
|
|
|
// testEnvNoPrompt returns the current environment with git auth prompts
|
|
// suppressed. Prevents ksshaskpass/SSH_ASKPASS popups during tests that
|
|
// configure fake git remotes (e.g. github.com/test/repo.git).
|
|
func testEnvNoPrompt() []string {
|
|
env := os.Environ()
|
|
env = append(env, "GIT_TERMINAL_PROMPT=0", "SSH_ASKPASS=", "GIT_ASKPASS=")
|
|
return env
|
|
}
|
|
|
|
// captureStderr captures stderr output from fn and returns it as a string.
|
|
// Uses stdioMutex to prevent races with concurrent os.Stderr redirection.
|
|
func captureStderr(t *testing.T, fn func()) string {
|
|
t.Helper()
|
|
|
|
stdioMutex.Lock()
|
|
defer stdioMutex.Unlock()
|
|
|
|
old := os.Stderr
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("os.Pipe: %v", err)
|
|
}
|
|
os.Stderr = w
|
|
|
|
var buf bytes.Buffer
|
|
done := make(chan struct{})
|
|
go func() {
|
|
_, _ = io.Copy(&buf, r)
|
|
close(done)
|
|
}()
|
|
|
|
fn()
|
|
_ = w.Close()
|
|
os.Stderr = old
|
|
<-done
|
|
_ = r.Close()
|
|
|
|
return buf.String()
|
|
}
|