Files
simplexcalc/internal/cli/cli_test.go
T
clawbot 54596ef54b
check / check (push) Successful in 1m10s
Keep cobra's output streams at their defaults in cli.Main
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
2026-09-28 23:42:59 +00:00

52 lines
1.0 KiB
Go

package cli_test
import (
"bytes"
"runtime"
"strings"
"testing"
"sneak.berlin/go/simplexcalc/internal/cli"
)
func TestVersionPrintsVersion(t *testing.T) {
t.Parallel()
var stdout, stderr bytes.Buffer
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())
}
want := "simplexcalc 1.2.3 " + runtime.GOOS + "/" + runtime.GOARCH + "\n"
if stdout.String() != want {
t.Errorf("stdout = %q, want %q", stdout.String(), want)
}
}
func TestUnknownCommandFails(t *testing.T) {
t.Parallel()
var stdout, stderr bytes.Buffer
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")
}
if !strings.HasPrefix(stderr.String(), "exiting: ") {
t.Errorf("stderr = %q, want the error after \"exiting: \"", stderr.String())
}
}