| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestBuildInfoSingleLine(t *testing.T) { |
| 10 | info := BuildInfo{Version: "v1.2.3"} |
| 11 | if got, want := info.singleLine(), "reasonix v1.2.3"; got != want { |
| 12 | t.Fatalf("singleLine = %q, want %q", got, want) |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | func TestBuildInfoVerboseOmitsConfigAndCST(t *testing.T) { |
| 17 | info := BuildInfo{ |
| 18 | Version: "v9.9.9", |
| 19 | GitCommit: "abcdef0123456789", |
| 20 | BuildTimeUTC: "2026-08-06T12:00:00Z", |
| 21 | BuildTarget: "darwin/arm64", |
| 22 | } |
| 23 | text := info.verboseText() |
| 24 | if !strings.Contains(text, "reasonix v9.9.9") { |
| 25 | t.Fatalf("verbose missing version line:\n%s", text) |
| 26 | } |
| 27 | if !strings.Contains(text, "git_commit: abcdef012345") { |
| 28 | t.Fatalf("verbose missing short commit:\n%s", text) |
| 29 | } |
| 30 | if !strings.Contains(text, "build_time_utc: 2026-08-06T12:00:00Z") { |
| 31 | t.Fatalf("verbose missing utc time:\n%s", text) |
| 32 | } |
| 33 | if !strings.Contains(text, "build_target: darwin/arm64") { |
| 34 | t.Fatalf("verbose missing target:\n%s", text) |
| 35 | } |
| 36 | if strings.Contains(text, "config_path") || strings.Contains(text, "build_time_cst") || strings.Contains(text, "CST") { |
| 37 | t.Fatalf("verbose must not include config path or CST:\n%s", text) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestBuildInfoJSONShape(t *testing.T) { |
| 42 | info := BuildInfo{ |
| 43 | Version: "v1.0.0", |
| 44 | GitCommit: "deadbeefcafebabe", |
| 45 | BuildTimeUTC: "2026-01-02T03:04:05Z", |
| 46 | BuildTarget: "linux/amd64", |
| 47 | } |
| 48 | raw, err := json.Marshal(info.jsonPayload()) |
| 49 | if err != nil { |
| 50 | t.Fatal(err) |
| 51 | } |
| 52 | var payload map[string]any |
| 53 | if err := json.Unmarshal(raw, &payload); err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | for _, key := range []string{"version", "git_commit", "build_time_utc", "build_target", "go"} { |
| 57 | if _, ok := payload[key]; !ok { |
| 58 | t.Fatalf("json missing %q: %s", key, raw) |
| 59 | } |
| 60 | } |
| 61 | for _, key := range []string{"config_path", "build_time_cst", "build_number"} { |
| 62 | if _, ok := payload[key]; ok { |
| 63 | t.Fatalf("json must not include %q: %s", key, raw) |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestVersionCommandFlags(t *testing.T) { |
| 69 | info := BuildInfo{Version: "v2.0.0", GitCommit: "abc", BuildTimeUTC: "2026-08-06T00:00:00Z", BuildTarget: "test/arch"} |
| 70 | out := captureStdout(t, func() { |
| 71 | if code := versionCommand(nil, info, false); code != 0 { |
| 72 | t.Fatalf("code = %d", code) |
| 73 | } |
| 74 | }) |
| 75 | if strings.TrimSpace(out) != "reasonix v2.0.0" { |
| 76 | t.Fatalf("--version line = %q", out) |
| 77 | } |
| 78 | |
| 79 | out = captureStdout(t, func() { |
| 80 | if code := versionCommand([]string{"--verbose"}, info, true); code != 0 { |
| 81 | t.Fatalf("verbose code = %d", code) |
| 82 | } |
| 83 | }) |
| 84 | if !strings.Contains(out, "git_commit:") { |
| 85 | t.Fatalf("verbose output = %q", out) |
| 86 | } |
| 87 | |
| 88 | out = captureStdout(t, func() { |
| 89 | if code := versionCommand([]string{"--json"}, info, true); code != 0 { |
| 90 | t.Fatalf("json code = %d", code) |
| 91 | } |
| 92 | }) |
| 93 | if !strings.Contains(out, `"version": "v2.0.0"`) && !strings.Contains(out, `"version":"v2.0.0"`) { |
| 94 | t.Fatalf("json output = %q", out) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestBuildInfoDoesNotUseVCSTimeAsBuildTime(t *testing.T) { |
| 99 | // Empty BuildTimeUTC must stay "unknown" rather than silently adopting |
| 100 | // vcs.time (commit timestamp) from the Go build info. |
| 101 | info := BuildInfo{Version: "vdev", GitCommit: "deadbeef"}.withDefaults() |
| 102 | if info.BuildTimeUTC != "unknown" { |
| 103 | t.Fatalf("BuildTimeUTC without ldflags = %q, want unknown", info.BuildTimeUTC) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func TestReleaseStyleLdflagsAppearInVerbose(t *testing.T) { |
| 108 | // Simulates official release injection of commit + real UTC build time. |
| 109 | info := BuildInfo{ |
| 110 | Version: "vtest", |
| 111 | GitCommit: "cafebabef00d", |
| 112 | BuildTimeUTC: "2026-08-06T12:34:56Z", |
| 113 | BuildTarget: "linux/amd64", |
| 114 | } |
| 115 | text := info.verboseText() |
| 116 | if !strings.Contains(text, "git_commit: cafebabef00d") { |
| 117 | t.Fatalf("missing injected commit:\n%s", text) |
| 118 | } |
| 119 | if !strings.Contains(text, "build_time_utc: 2026-08-06T12:34:56Z") { |
| 120 | t.Fatalf("missing injected build time:\n%s", text) |
| 121 | } |
| 122 | if strings.Contains(text, "unknown") { |
| 123 | t.Fatalf("injected identity still unknown:\n%s", text) |
| 124 | } |
| 125 | } |
| 126 |