check / check (push) Successful in 1m40s
`cmd/simplexcalc/` now holds only `main.go`, whose body is one call, `os.Exit(cli.Main(version))`, as the Go style guide requires. The command tree and the `run` and `version` commands moved unchanged into `internal/cli`, with tests for `version` and for an unknown command. Output streams, exit statuses and the `-X main.version` build flag are as before. Disclosure: the comment in `execute` saying cobra has already printed the error is false, since the root command silences cobra's errors; it moved unchanged and is left for a follow-up. Model: opus-5-5
52 lines
1.0 KiB
Go
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())
|
|
}
|
|
}
|