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. func TestVersionCommandReportsBuildVersion(t *testing.T) { t.Parallel() 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. func TestVersionCommandFlagsDevelopmentBuild(t *testing.T) { t.Parallel() 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) } }