| 1 | package meta |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "runtime/debug" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | func TestGetVersionInfo(t *testing.T) { |
| 12 | t.Parallel() |
| 13 | |
| 14 | tests := []struct { |
| 15 | name string |
| 16 | setVars func() |
| 17 | expect Info |
| 18 | partialMatch bool |
| 19 | }{ |
| 20 | { |
| 21 | name: "GoReleaser build", |
| 22 | setVars: func() { |
| 23 | Version = "0.0.1" |
| 24 | Commit = "abc123456789" |
| 25 | Date = "2025-05-07T00:00:00Z" |
| 26 | }, |
| 27 | expect: Info{ |
| 28 | Version: "v0.0.1", |
| 29 | Commit: "abc1234", |
| 30 | Date: "2025-05-07", |
| 31 | }, |
| 32 | partialMatch: false, |
| 33 | }, |
| 34 | { |
| 35 | name: "Source build with default values", |
| 36 | setVars: func() { |
| 37 | Version = devVersion |
| 38 | Commit = unknownValue |
| 39 | Date = unknownValue |
| 40 | }, |
| 41 | expect: Info{ |
| 42 | Version: unknownValue, |
| 43 | Commit: unknownValue, |
| 44 | Date: time.Now().UTC().Format("2006-01-02"), |
| 45 | }, |
| 46 | partialMatch: true, |
| 47 | }, |
| 48 | { |
| 49 | name: "Source build with empty values", |
| 50 | setVars: func() { |
| 51 | Version = "" |
| 52 | Commit = "" |
| 53 | Date = "" |
| 54 | }, |
| 55 | expect: Info{ |
| 56 | Version: unknownValue, |
| 57 | Commit: unknownValue, |
| 58 | Date: time.Now().UTC().Format("2006-01-02"), |
| 59 | }, |
| 60 | partialMatch: false, |
| 61 | }, |
| 62 | { |
| 63 | name: "Invalid GoReleaser version", |
| 64 | setVars: func() { |
| 65 | Version = "v" |
| 66 | Commit = "" |
| 67 | Date = "" |
| 68 | }, |
| 69 | expect: Info{ |
| 70 | Version: unknownValue, |
| 71 | Commit: unknownValue, |
| 72 | Date: time.Now().UTC().Format("2006-01-02"), |
| 73 | }, |
| 74 | partialMatch: false, |
| 75 | }, |
| 76 | } |
| 77 | |
| 78 | for _, tt := range tests { |
| 79 | t.Run(tt.name, func(t *testing.T) { |
| 80 | t.Parallel() |
| 81 | tt.setVars() |
| 82 | |
| 83 | info := GetMetaInfo() |
| 84 | |
| 85 | if !tt.partialMatch { |
| 86 | if info.Version != tt.expect.Version { |
| 87 | t.Errorf("Version = %q, want %q", info.Version, tt.expect.Version) |
| 88 | } |
| 89 | |
| 90 | if info.Commit != tt.expect.Commit { |
| 91 | t.Errorf("Commit = %q, want %q", info.Commit, tt.expect.Commit) |
| 92 | } |
| 93 | |
| 94 | // Validate Date format (YYYY-MM-DD) instead of exact match |
| 95 | if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(info.Date) { |
| 96 | t.Errorf("Date = %q, want valid YYYY-MM-DD format", info.Date) |
| 97 | } |
| 98 | } else if info.Version != tt.expect.Version && !strings.Contains(info.Version, "+dirty") { |
| 99 | t.Errorf("Version = %q, want %q or dirty variant", info.Version, tt.expect.Version) |
| 100 | } |
| 101 | }) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | func TestGetVersionInfo_VCSData(t *testing.T) { |
| 106 | t.Parallel() |
| 107 | |
| 108 | Version = devVersion |
| 109 | Commit = unknownValue |
| 110 | Date = unknownValue |
| 111 | |
| 112 | info := GetMetaInfo() |
| 113 | |
| 114 | if buildInfo, ok := debug.ReadBuildInfo(); ok { |
| 115 | var vcsRevision, vcsTime, vcsModified string |
| 116 | |
| 117 | for _, setting := range buildInfo.Settings { |
| 118 | switch setting.Key { |
| 119 | case "vcs.revision": |
| 120 | vcsRevision = setting.Value |
| 121 | case "vcs.time": |
| 122 | vcsTime = setting.Value |
| 123 | case "vcs.modified": |
| 124 | vcsModified = setting.Value |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if vcsRevision != "" { |
| 129 | expectedCommit := vcsRevision |
| 130 | if len(vcsRevision) >= 7 { |
| 131 | expectedCommit = vcsRevision[:7] |
| 132 | } |
| 133 | |
| 134 | if info.Commit == unknownValue { |
| 135 | t.Errorf( |
| 136 | "Expected commit %q, got %q; ensure repository has commit history", |
| 137 | expectedCommit, |
| 138 | info.Commit, |
| 139 | ) |
| 140 | } else if info.Commit != expectedCommit { |
| 141 | t.Errorf("Commit = %q, want %q", info.Commit, expectedCommit) |
| 142 | } |
| 143 | } else { |
| 144 | t.Logf( |
| 145 | "No vcs.revision found; ensure repository has Git metadata to cover commit assignment", |
| 146 | ) |
| 147 | } |
| 148 | |
| 149 | if vcsTime != "" { |
| 150 | if parsedTime, err := time.Parse(time.RFC3339, vcsTime); err == nil { |
| 151 | expectedDate := parsedTime.Format("2006-01-02") |
| 152 | if info.Date == unknownValue { |
| 153 | t.Errorf( |
| 154 | "Expected date %q, got %q; ensure vcs.time is a valid RFC3339 timestamp", |
| 155 | expectedDate, |
| 156 | info.Date, |
| 157 | ) |
| 158 | } else if info.Date != expectedDate { |
| 159 | t.Errorf("Date = %q, want %q", info.Date, expectedDate) |
| 160 | } |
| 161 | } else { |
| 162 | t.Logf("vcs.time %q is invalid; date should be in YYYY-MM-DD format", vcsTime) |
| 163 | |
| 164 | if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(info.Date) { |
| 165 | t.Errorf("Date = %q, want valid YYYY-MM-DD format", info.Date) |
| 166 | } |
| 167 | } |
| 168 | } else { |
| 169 | t.Logf("No vcs.time found; date should be in YYYY-MM-DD format") |
| 170 | |
| 171 | if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(info.Date) { |
| 172 | t.Errorf("Date = %q, want valid YYYY-MM-DD format", info.Date) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if vcsModified == trueValue && info.Version != unknownValue { |
| 177 | if !strings.Contains(info.Version, "+dirty") { |
| 178 | t.Errorf( |
| 179 | "Expected version to contain '+dirty', got %q; ensure repository has uncommitted changes", |
| 180 | info.Version, |
| 181 | ) |
| 182 | } |
| 183 | } else if vcsModified != trueValue { |
| 184 | t.Logf( |
| 185 | "Repository is clean (vcs.modified=%q); make uncommitted changes to cover '+dirty' case", |
| 186 | vcsModified, |
| 187 | ) |
| 188 | } |
| 189 | } else { |
| 190 | t.Logf( |
| 191 | "debug.ReadBuildInfo() failed; ensure tests run in a Git repository to cover VCS parsing", |
| 192 | ) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | func TestGetVersionInfo_InvalidVCSTime(t *testing.T) { |
| 197 | t.Parallel() |
| 198 | |
| 199 | Version = devVersion |
| 200 | Commit = unknownValue |
| 201 | Date = unknownValue |
| 202 | |
| 203 | info := GetMetaInfo() |
| 204 | |
| 205 | if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(info.Date) { |
| 206 | t.Errorf("Date = %q, want valid YYYY-MM-DD format", info.Date) |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | func TestGetMetaStr(t *testing.T) { |
| 211 | t.Parallel() |
| 212 | |
| 213 | tests := []struct { |
| 214 | name string |
| 215 | setVars func() |
| 216 | expect string |
| 217 | }{ |
| 218 | { |
| 219 | name: "With commit (GoReleaser build)", |
| 220 | setVars: func() { |
| 221 | Version = "0.8.10" |
| 222 | Commit = "a6fcf77abcdef" |
| 223 | Date = "2025-05-27T00:00:00Z" |
| 224 | }, |
| 225 | expect: "v0.8.10 (Built on 2025-05-27 from Git SHA a6fcf77)", |
| 226 | }, |
| 227 | { |
| 228 | name: "Without commit (go install build)", |
| 229 | setVars: func() { |
| 230 | Version = "0.8.10" |
| 231 | Commit = unknownValue |
| 232 | Date = unknownValue |
| 233 | }, |
| 234 | expect: "v0.8.10 (Built on " + time.Now().UTC().Format("2006-01-02") + ")", |
| 235 | }, |
| 236 | { |
| 237 | name: "Invalid version", |
| 238 | setVars: func() { |
| 239 | Version = "v" |
| 240 | Commit = unknownValue |
| 241 | Date = unknownValue |
| 242 | }, |
| 243 | expect: "unknown (Built on " + time.Now().UTC().Format("2006-01-02") + ")", |
| 244 | }, |
| 245 | } |
| 246 | |
| 247 | for _, tt := range tests { |
| 248 | t.Run(tt.name, func(t *testing.T) { |
| 249 | t.Parallel() |
| 250 | tt.setVars() |
| 251 | |
| 252 | result := GetMetaStr() |
| 253 | if !strings.HasPrefix(result, strings.Split(tt.expect, " (")[0]) || |
| 254 | !regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(result) { |
| 255 | t.Errorf("GetMetaStr() = %q, want format like %q", result, tt.expect) |
| 256 | } |
| 257 | }) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | func TestContains(t *testing.T) { |
| 262 | t.Parallel() |
| 263 | |
| 264 | tests := []struct { |
| 265 | name string |
| 266 | s string |
| 267 | substr string |
| 268 | expected bool |
| 269 | }{ |
| 270 | { |
| 271 | name: "Substring found", |
| 272 | s: "v1.0.0+dirty", |
| 273 | substr: "+dirty", |
| 274 | expected: true, |
| 275 | }, |
| 276 | { |
| 277 | name: "Substring not found", |
| 278 | s: "v1.0.0", |
| 279 | substr: "+dirty", |
| 280 | expected: false, |
| 281 | }, |
| 282 | { |
| 283 | name: "Empty string", |
| 284 | s: "", |
| 285 | substr: "+dirty", |
| 286 | expected: false, |
| 287 | }, |
| 288 | { |
| 289 | name: "Empty substring", |
| 290 | s: "v1.0.0", |
| 291 | substr: "", |
| 292 | expected: true, |
| 293 | }, |
| 294 | } |
| 295 | |
| 296 | for _, tt := range tests { |
| 297 | t.Run(tt.name, func(t *testing.T) { |
| 298 | t.Parallel() |
| 299 | |
| 300 | result := contains(tt.s, tt.substr) |
| 301 | if result != tt.expected { |
| 302 | t.Errorf("contains(%q, %q) = %v, want %v", tt.s, tt.substr, result, tt.expected) |
| 303 | } |
| 304 | }) |
| 305 | } |
| 306 | } |