test(cmd): fix jsonOutput global leak breaking noticeSharedMigrateRefusal subtests in full-package runs (#6073)

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.
This commit is contained in:
Julian Knutsen 2026-08-30 05:47:47 -07:00 • committed by GitHub
commit f35a0fb4b4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 21 additions and 12 deletions

View file

@ -138,13 +138,18 @@ func runConcludeCommand(t *testing.T, commandStore storage.DoltStorage, asJSON b
defer func() { os.Stdout, os.Stderr = oldStdout, oldStderr }()
store, rootCtx, jsonOutput = commandStore, context.Background(), asJSON
conflictsConclude = true
// Deferred, like the stdio restore above it: RunE below can panic or
// t.Fatal, and a plain statement after the call would then leave jsonOutput
// set to asJSON — which callers pass as true — for the rest of the package.
defer func() {
store, rootCtx, jsonOutput = oldStore, oldRootCtx, oldJSON
conflictsConclude = oldConclude
}()
commandErr := conflictsResolveCmd.RunE(conflictsResolveCmd, nil)
_ = wOut.Close()
_ = wErr.Close()
store, rootCtx, jsonOutput = oldStore, oldRootCtx, oldJSON
conflictsConclude = oldConclude
var stdout, stderr bytes.Buffer
_, _ = stdout.ReadFrom(rOut)

View file

@ -160,7 +160,7 @@ func TestGetNotionConfigPrefersStoreOverEnv(t *testing.T) {
func TestRunNotionStatusJSONWithMissingConfig(t *testing.T) {
saveAndRestoreGlobals(t)
jsonOutput = true
pinJSONOutput(t, true)
store = nil
dbPath = ""
t.Setenv("NOTION_TOKEN", "")
@ -383,7 +383,7 @@ func TestRunNotionStatusUsesHTTPClient(t *testing.T) {
newNotionStatusClient = func(token string) *notion.Client {
return notion.NewClient(token).WithBaseURL(server.URL)
}
jsonOutput = true
pinJSONOutput(t, true)
store = nil
dbPath = ""
t.Setenv("NOTION_TOKEN", "env-token")

View file

@ -65,6 +65,14 @@ func TestIsSchemaMigrateVerbScope(t *testing.T) {
// returns one error type for several decisions whose correct actions differ,
// and the migrate verb does not unlock the remote-backed ones at all.
func TestNoticeSharedMigrateRefusalPerDecision(t *testing.T) {
// The notice self-suppresses under --json, so if jsonOutput arrives here
// already true, every case below reads as "the notice printed nothing" —
// which is exactly how a leak from an earlier test in the package
// presents, and is how this test failed in full-package runs while passing
// under a narrow -run. Pin it, so a failure here can only ever be about
// this code.
pinJSONOutput(t, false)
for _, tt := range []struct {
decision string
want string
@ -100,9 +108,7 @@ func TestNoticeSharedMigrateRefusalPerDecision(t *testing.T) {
// --json puts a machine-readable gate block on this same stream a moment
// later; prose prepended to it makes the documented contract unparseable.
t.Run("suppressed in json mode", func(t *testing.T) {
orig := jsonOutput
jsonOutput = true
defer func() { jsonOutput = orig }()
pinJSONOutput(t, true)
out := captureNoticeStderr(t, func() {
noticeSharedMigrateRefusal(&schema.RemoteMigrateGateError{
CurrentVersion: 65, LatestVersion: 66, Pending: 1,

View file

@ -51,7 +51,7 @@ func TestVersionCommand(t *testing.T) {
t.Fatalf("Failed to create pipe: %v", err)
}
os.Stdout = w
jsonOutput = true
pinJSONOutput(t, true)
// Run version command
if err := versionCmd.RunE(versionCmd, []string{}); err != nil {
@ -83,8 +83,6 @@ func TestVersionCommand(t *testing.T) {
}
})
// Restore default
jsonOutput = false
}
func TestResolveCommitHash(t *testing.T) {
@ -158,7 +156,7 @@ func TestVersionOutputWithCommitAndBranch(t *testing.T) {
t.Fatalf("Failed to create pipe: %v", err)
}
os.Stdout = w
jsonOutput = false
pinJSONOutput(t, false)
if err := versionCmd.RunE(versionCmd, []string{}); err != nil {
t.Fatalf("versionCmd.RunE: %v", err)
@ -187,7 +185,7 @@ func TestVersionOutputWithCommitAndBranch(t *testing.T) {
t.Fatalf("Failed to create pipe: %v", err)
}
os.Stdout = w
jsonOutput = true
pinJSONOutput(t, true)
if err := versionCmd.RunE(versionCmd, []string{}); err != nil {
t.Fatalf("versionCmd.RunE: %v", err)