What
persistentPreRun in cli.go unconditionally sets c.writer = tabwriter.NewWriter(os.Stdout, ...). Tests use newTestCLI to inject a buffer-backed writer, but if a test ever calls c.rootCmd.Execute() instead of directly invoking cmd.Run(), the Cobra hook chain fires persistentPreRun which overwrites the test writer with os.Stdout, silently breaking buffer assertions.
Why
This is a latent trap for future test authors. Current tests work because they bypass the hook chain, but the divergence between test and production execution paths is fragile.
Suggested fix
Guard the writer assignment in persistentPreRun to only set it when nil:
if c.writer == nil {
c.writer = tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
}
Or document the constraint in newTestCLI.
What
persistentPreRunincli.gounconditionally setsc.writer = tabwriter.NewWriter(os.Stdout, ...). Tests usenewTestCLIto inject a buffer-backed writer, but if a test ever callsc.rootCmd.Execute()instead of directly invokingcmd.Run(), the Cobra hook chain firespersistentPreRunwhich overwrites the test writer withos.Stdout, silently breaking buffer assertions.Why
This is a latent trap for future test authors. Current tests work because they bypass the hook chain, but the divergence between test and production execution paths is fragile.
Suggested fix
Guard the writer assignment in
persistentPreRunto only set it when nil:Or document the constraint in
newTestCLI.