check / check (push) Successful in 1m4s
cmd/simplexcalc/ now holds only main.go: it passes the version the linker sets to cli.Main and exits with the status that returns. root.go and run.go move to internal/cli with their comments, changed only to take the version as a parameter. Main reads the process's arguments and output; execute takes them as parameters so the new tests can check the version line and the non-zero status of an unknown command. Commands, flags, output, log lines and exit status are unchanged, and the -X main.version flag in the Dockerfile still applies. Model: opus-5-5
42 lines
913 B
Go
42 lines
913 B
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
|
|
|
|
status := cli.Execute("1.2.3", []string{"version"}, &stdout, &stderr)
|
|
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
|
|
|
|
status := cli.Execute("1.2.3", []string{"frobnicate"}, &stdout, &stderr)
|
|
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())
|
|
}
|
|
}
|