Files
vaultik/internal/cli/version_test.go
T
sneak b4a539c8f9
check / check (pull_request) Successful in 1m45s
Route direct-stdout command output through internal/ui (closes #149)
version, info, remote info, config, and database delete wrote plain text
straight to stdout, so they were unstyled and ignored --quiet. Output now
falls in two buckets, both governed by internal/ui.

Status lines and confirmations (config init, config set, database
delete) go through the ui message methods: styled, and --quiet silences
them. The data a command exists to produce is written plain, since a
marker would corrupt a table or a parsed document: the version, info, and
remote info reports, the snapshot list table, config get values, and the
--json documents. --quiet silences the human reports and tables but never
the config get value or the --json documents, which a script depends on.
The database delete confirmation prompt is always shown; it is an
interactive exchange the operator must see.

The pure-cli commands reach internal/ui through a small commandUI helper
that builds a ui.Writer on the command's stdout in quiet mode when
--quiet is set. NewForTesting now supplies a UI writer so the quiet gate
is never nil. The README output-style section states the resulting rule.

Model: opus-4-8
2026-09-22 18:03:45 +00:00

78 lines
2.2 KiB
Go

package cli_test
import (
"bytes"
"strings"
"testing"
"sneak.berlin/go/vaultik/internal/cli"
"sneak.berlin/go/vaultik/internal/globals"
)
// runVersionCommand executes `vaultik version` with its output
// captured, and returns what it printed.
func runVersionCommand(t *testing.T) string {
t.Helper()
cmd := cli.NewVersionCommand()
var out bytes.Buffer
cmd.SetOut(&out)
cmd.SetErr(&out)
cmd.SetArgs([]string{})
err := cmd.Execute()
if err != nil {
t.Fatalf("version command failed: %v", err)
}
return out.String()
}
// TestVersionCommandReportsBuildVersion checks that the first line of
// the report is the version the binary was actually built with. The
// test binary carries no -ldflags, so that is the "dev" default -- the
// same string an untagged `make vaultik` build stamps a prefix of.
//
//nolint:paralleltest // executes a command that reads the global rootFlags
func TestVersionCommandReportsBuildVersion(t *testing.T) {
out := runVersionCommand(t)
wantFirst := "vaultik " + globals.Version
if first, _, _ := strings.Cut(out, "\n"); first != wantFirst {
t.Errorf("first line = %q, want %q", first, wantFirst)
}
if !strings.Contains(out, "commit:") {
t.Error("output does not report the commit")
}
}
// TestVersionCommandFlagsDevelopmentBuild is the regression test for
// the thing this command exists to prevent: a build that is not a
// release must say so. The notice used to be gated on the version
// being exactly "dev", so once untagged builds started carrying their
// commit sha it would have gone silent and an unreleased binary would
// have looked like a release.
//
//nolint:paralleltest // executes a command that reads the global rootFlags
func TestVersionCommandFlagsDevelopmentBuild(t *testing.T) {
if !globals.IsDevVersion(globals.Version) {
t.Skipf("test binary was stamped with release version %q",
globals.Version)
}
out := runVersionCommand(t)
if !strings.Contains(out, "development build") {
t.Errorf("dev build did not print the development-build notice:\n%s",
out)
}
if !strings.Contains(out, globals.ReleasesURL) {
t.Errorf("development-build notice does not point at %s:\n%s",
globals.ReleasesURL, out)
}
}