Debian Codegraph @

internal/meta/meta_test.go

raw
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 TestUserAgent(t *testing.T) { //nolint:paralleltest // Mutates package-global Version.
211 orig := Version
212
213 t.Cleanup(func() {
214 Version = orig
215 })
216
217 Version = "dev"
218
219 if got := UserAgent(); got != "shoutrrr/dev" {
220 t.Errorf("UserAgent() = %q, want %q", got, "shoutrrr/dev")
221 }
222 }
223
224 func TestGetMetaStr(t *testing.T) {
225 t.Parallel()
226
227 tests := []struct {
228 name string
229 setVars func()
230 expect string
231 }{
232 {
233 name: "With commit (GoReleaser build)",
234 setVars: func() {
235 Version = "0.8.10"
236 Commit = "a6fcf77abcdef"
237 Date = "2025-05-27T00:00:00Z"
238 },
239 expect: "v0.8.10 (Built on 2025-05-27 from Git SHA a6fcf77)",
240 },
241 {
242 name: "Without commit (go install build)",
243 setVars: func() {
244 Version = "0.8.10"
245 Commit = unknownValue
246 Date = unknownValue
247 },
248 expect: "v0.8.10 (Built on " + time.Now().UTC().Format("2006-01-02") + ")",
249 },
250 {
251 name: "Invalid version",
252 setVars: func() {
253 Version = "v"
254 Commit = unknownValue
255 Date = unknownValue
256 },
257 expect: "unknown (Built on " + time.Now().UTC().Format("2006-01-02") + ")",
258 },
259 }
260
261 for _, tt := range tests {
262 t.Run(tt.name, func(t *testing.T) {
263 t.Parallel()
264 tt.setVars()
265
266 result := GetMetaStr()
267 if !strings.HasPrefix(result, strings.Split(tt.expect, " (")[0]) ||
268 !regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(result) {
269 t.Errorf("GetMetaStr() = %q, want format like %q", result, tt.expect)
270 }
271 })
272 }
273 }
274
275 func TestContains(t *testing.T) {
276 t.Parallel()
277
278 tests := []struct {
279 name string
280 s string
281 substr string
282 expected bool
283 }{
284 {
285 name: "Substring found",
286 s: "v1.0.0+dirty",
287 substr: "+dirty",
288 expected: true,
289 },
290 {
291 name: "Substring not found",
292 s: "v1.0.0",
293 substr: "+dirty",
294 expected: false,
295 },
296 {
297 name: "Empty string",
298 s: "",
299 substr: "+dirty",
300 expected: false,
301 },
302 {
303 name: "Empty substring",
304 s: "v1.0.0",
305 substr: "",
306 expected: true,
307 },
308 }
309
310 for _, tt := range tests {
311 t.Run(tt.name, func(t *testing.T) {
312 t.Parallel()
313
314 result := contains(tt.s, tt.substr)
315 if result != tt.expected {
316 t.Errorf("contains(%q, %q) = %v, want %v", tt.s, tt.substr, result, tt.expected)
317 }
318 })
319 }
320 }