TestNoticeSharedMigrateRefusalPerDecision passed under a narrow -run and failed six subtests in any full cmd/bd package run. The notice it covers self-suppresses under --json, so when the jsonOutput global arrived already true every case read as "the notice printed nothing" — the failure was never about the notice. Three tests set jsonOutput and never put it back. Two of them call saveAndRestoreGlobals, which does not cover jsonOutput despite promising callers "can't forget one": - notion_test.go TestRunNotionStatusJSONWithMissingConfig - notion_test.go TestRunNotionStatusUsesHTTPClient - version_test.go TestVersionOutputWithCommitAndBranch All three now use pinJSONOutput, the helper the package already has for exactly this (ready_input_test.go), whose doc comment diagnosed this bug class before it bit. TestNoticeSharedMigrateRefusalPerDecision pins the global itself too, so a future leak cannot silently turn it green-when- narrow and red-when-full again. Two adjacent instances of the same defect, fixed while here: version_test's TestVersionCommand "restored" by assigning the literal false rather than the saved value, and conflicts_conclude_integration_test's helper restored store/rootCtx/jsonOutput with a plain statement after a RunE that can panic — now deferred, like the stdio restore directly above it. Diagnosis credit: #6056's agent isolated this to a global leak by diffing a full-package run against a pristine git archive of the release tip, which is what ruled out the notice code itself.
285 lines
7 KiB
Go
285 lines
7 KiB
Go
//go:build cgo
|
|
|
|
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestVersionCommand(t *testing.T) {
|
|
// Save original stdout
|
|
oldStdout := os.Stdout
|
|
defer func() { os.Stdout = oldStdout }()
|
|
|
|
t.Run("plain text version output", func(t *testing.T) {
|
|
// Create a pipe to capture output
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
jsonOutput = false
|
|
|
|
// Run version command
|
|
if err := versionCmd.RunE(versionCmd, []string{}); err != nil {
|
|
t.Fatalf("versionCmd.RunE: %v", err)
|
|
}
|
|
|
|
// Close writer and read output
|
|
w.Close()
|
|
var buf bytes.Buffer
|
|
buf.ReadFrom(r)
|
|
output := buf.String()
|
|
|
|
// Verify output contains version info
|
|
if !strings.Contains(output, "bd version") {
|
|
t.Errorf("Expected output to contain 'bd version', got: %s", output)
|
|
}
|
|
if !strings.Contains(output, Version) {
|
|
t.Errorf("Expected output to contain version %s, got: %s", Version, output)
|
|
}
|
|
})
|
|
|
|
t.Run("json version output", func(t *testing.T) {
|
|
// Create a pipe to capture output
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
pinJSONOutput(t, true)
|
|
|
|
// Run version command
|
|
if err := versionCmd.RunE(versionCmd, []string{}); err != nil {
|
|
t.Fatalf("versionCmd.RunE: %v", err)
|
|
}
|
|
|
|
// Close writer and read output
|
|
w.Close()
|
|
var buf bytes.Buffer
|
|
buf.ReadFrom(r)
|
|
output := buf.String()
|
|
|
|
// Parse JSON output
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal([]byte(output), &result); err != nil {
|
|
t.Fatalf("Failed to parse JSON output: %v", err)
|
|
}
|
|
|
|
// Verify JSON contains version and build
|
|
if result["version"] != Version {
|
|
t.Errorf("Expected version %s, got %s", Version, result["version"])
|
|
}
|
|
if result["build"] == "" {
|
|
t.Error("Expected build field to be non-empty")
|
|
}
|
|
// cgo field removed — server-only operation, no CGO bifurcation
|
|
if _, ok := result["cgo"]; ok {
|
|
t.Error("cgo field should no longer be present in version output")
|
|
}
|
|
})
|
|
|
|
}
|
|
|
|
func TestResolveCommitHash(t *testing.T) {
|
|
// Save original Commit value
|
|
origCommit := Commit
|
|
defer func() { Commit = origCommit }()
|
|
|
|
t.Run("returns ldflag value when set", func(t *testing.T) {
|
|
testCommit := "abc123def456"
|
|
Commit = testCommit
|
|
result := resolveCommitHash()
|
|
if result != testCommit {
|
|
t.Errorf("Expected %q, got %q", testCommit, result)
|
|
}
|
|
})
|
|
|
|
t.Run("returns empty string when not set", func(t *testing.T) {
|
|
Commit = ""
|
|
result := resolveCommitHash()
|
|
// Result could be from git or empty - just verify it doesn't panic
|
|
if result == "" || len(result) >= 7 {
|
|
// Either empty or looks like a git hash
|
|
return
|
|
}
|
|
t.Errorf("Unexpected result format: %q", result)
|
|
})
|
|
}
|
|
|
|
func TestResolveBranch(t *testing.T) {
|
|
// Save original Branch value
|
|
origBranch := Branch
|
|
defer func() { Branch = origBranch }()
|
|
|
|
t.Run("returns ldflag value when set", func(t *testing.T) {
|
|
testBranch := "main"
|
|
Branch = testBranch
|
|
result := resolveBranch()
|
|
if result != testBranch {
|
|
t.Errorf("Expected %q, got %q", testBranch, result)
|
|
}
|
|
})
|
|
|
|
t.Run("returns empty string or git branch when not set", func(t *testing.T) {
|
|
Branch = ""
|
|
result := resolveBranch()
|
|
// Result could be from git or empty - just verify it doesn't panic
|
|
if result == "" || result == "main" || strings.Contains(result, "detached") {
|
|
return
|
|
}
|
|
t.Logf("Got branch: %q", result)
|
|
})
|
|
}
|
|
|
|
func TestVersionOutputWithCommitAndBranch(t *testing.T) {
|
|
// Save original values
|
|
oldStdout := os.Stdout
|
|
origCommit := Commit
|
|
origBranch := Branch
|
|
defer func() {
|
|
os.Stdout = oldStdout
|
|
Commit = origCommit
|
|
Branch = origBranch
|
|
}()
|
|
|
|
t.Run("text output includes commit and branch when available", func(t *testing.T) {
|
|
Commit = "7e709405b38c472d8cbc996c7cd26df7e3b438d0"
|
|
Branch = "main"
|
|
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
pinJSONOutput(t, false)
|
|
|
|
if err := versionCmd.RunE(versionCmd, []string{}); err != nil {
|
|
t.Fatalf("versionCmd.RunE: %v", err)
|
|
}
|
|
|
|
w.Close()
|
|
var buf bytes.Buffer
|
|
buf.ReadFrom(r)
|
|
output := buf.String()
|
|
|
|
// Should contain both branch and commit
|
|
if !strings.Contains(output, "main@") {
|
|
t.Errorf("Expected output to contain 'main@', got: %s", output)
|
|
}
|
|
if !strings.Contains(output, "7e70940") { // first 7 chars of commit
|
|
t.Errorf("Expected output to contain commit hash, got: %s", output)
|
|
}
|
|
})
|
|
|
|
t.Run("json output includes commit and branch when available", func(t *testing.T) {
|
|
Commit = "7e709405b38c472d8cbc996c7cd26df7e3b438d0"
|
|
Branch = "main"
|
|
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
pinJSONOutput(t, true)
|
|
|
|
if err := versionCmd.RunE(versionCmd, []string{}); err != nil {
|
|
t.Fatalf("versionCmd.RunE: %v", err)
|
|
}
|
|
|
|
w.Close()
|
|
var buf bytes.Buffer
|
|
buf.ReadFrom(r)
|
|
output := buf.String()
|
|
|
|
var result map[string]interface{}
|
|
if err := json.Unmarshal([]byte(output), &result); err != nil {
|
|
t.Fatalf("Failed to parse JSON output: %v", err)
|
|
}
|
|
|
|
if result["commit"] != Commit {
|
|
t.Errorf("Expected commit %q, got %q", Commit, result["commit"])
|
|
}
|
|
if result["branch"] != Branch {
|
|
t.Errorf("Expected branch %q, got %q", Branch, result["branch"])
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestVersionFlag(t *testing.T) {
|
|
// Reset global state for test isolation
|
|
ensureCleanGlobalState(t)
|
|
|
|
// Ensure cleanup after running cobra commands
|
|
t.Cleanup(func() {
|
|
resetCommandContext()
|
|
})
|
|
|
|
// Save original stdout
|
|
oldStdout := os.Stdout
|
|
defer func() { os.Stdout = oldStdout }()
|
|
|
|
t.Run("--version flag", func(t *testing.T) {
|
|
// Create a pipe to capture output
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
|
|
// Set version flag and run root command
|
|
rootCmd.SetArgs([]string{"--version"})
|
|
rootCmd.Execute()
|
|
|
|
// Close writer and read output
|
|
w.Close()
|
|
var buf bytes.Buffer
|
|
buf.ReadFrom(r)
|
|
output := buf.String()
|
|
|
|
// Verify output contains version info
|
|
if !strings.Contains(output, "bd version") {
|
|
t.Errorf("Expected output to contain 'bd version', got: %s", output)
|
|
}
|
|
if !strings.Contains(output, Version) {
|
|
t.Errorf("Expected output to contain version %s, got: %s", Version, output)
|
|
}
|
|
|
|
// Reset args
|
|
rootCmd.SetArgs(nil)
|
|
})
|
|
|
|
t.Run("-v shorthand", func(t *testing.T) {
|
|
// Create a pipe to capture output
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatalf("Failed to create pipe: %v", err)
|
|
}
|
|
os.Stdout = w
|
|
|
|
// Set version flag and run root command
|
|
rootCmd.SetArgs([]string{"-v"})
|
|
rootCmd.Execute()
|
|
|
|
// Close writer and read output
|
|
w.Close()
|
|
var buf bytes.Buffer
|
|
buf.ReadFrom(r)
|
|
output := buf.String()
|
|
|
|
// Verify output contains version info
|
|
if !strings.Contains(output, "bd version") {
|
|
t.Errorf("Expected output to contain 'bd version', got: %s", output)
|
|
}
|
|
if !strings.Contains(output, Version) {
|
|
t.Errorf("Expected output to contain version %s, got: %s", Version, output)
|
|
}
|
|
|
|
// Reset args
|
|
rootCmd.SetArgs(nil)
|
|
})
|
|
}
|