Keep cobra's output streams at their defaults in cli.Main
check / check (push) Successful in 1m10s

cli.Main no longer calls SetOut or SetErr. SetOut also moved the usage
and an unknown help topic, which cobra writes to stderr, onto stdout.
execute now takes the command tree, so the tests build it, set its
arguments and output streams, and run it; the exit line goes to the
command's error stream, which outside a test is stderr as before.

Model: opus-5-5
This commit is contained in:
clawbot
2026-09-28 23:42:59 +00:00
parent 86863653f8
commit 54596ef54b
3 changed files with 31 additions and 18 deletions
+12 -2
View File
@@ -14,7 +14,12 @@ func TestVersionPrintsVersion(t *testing.T) {
var stdout, stderr bytes.Buffer
status := cli.Execute("1.2.3", []string{"version"}, &stdout, &stderr)
root := cli.RootCmd("1.2.3")
root.SetArgs([]string{"version"})
root.SetOut(&stdout)
root.SetErr(&stderr)
status := cli.Execute(root)
if status != 0 {
t.Fatalf("status = %d, want 0; stderr: %q", status, stderr.String())
}
@@ -30,7 +35,12 @@ func TestUnknownCommandFails(t *testing.T) {
var stdout, stderr bytes.Buffer
status := cli.Execute("1.2.3", []string{"frobnicate"}, &stdout, &stderr)
root := cli.RootCmd("1.2.3")
root.SetArgs([]string{"frobnicate"})
root.SetOut(&stdout)
root.SetErr(&stderr)
status := cli.Execute(root)
if status == 0 {
t.Fatal("status = 0, want non-zero")
}