File: meta_test.go

package info (click to toggle)
golang-github-nicholas-fedor-shoutrrr 0.8.17-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,332 kB
  • sloc: sh: 61; makefile: 5
file content (282 lines) | stat: -rw-r--r-- 6,729 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package meta

import (
	"regexp"
	"runtime/debug"
	"strings"
	"testing"
	"time"
)

func TestGetVersionInfo(t *testing.T) {
	tests := []struct {
		name         string
		setVars      func()
		expect       Info
		partialMatch bool
	}{
		{
			name: "GoReleaser build",
			setVars: func() {
				Version = "0.0.1"
				Commit = "abc123456789"
				Date = "2025-05-07T00:00:00Z"
			},
			expect: Info{
				Version: "v0.0.1",
				Commit:  "abc1234",
				Date:    "2025-05-07",
			},
		},
		{
			name: "Source build with default values",
			setVars: func() {
				Version = devVersion
				Commit = unknownValue
				Date = unknownValue
			},
			expect: Info{
				Version: unknownValue,
				Commit:  unknownValue,
				Date:    time.Now().UTC().Format("2006-01-02"),
			},
			partialMatch: true,
		},
		{
			name: "Source build with empty values",
			setVars: func() {
				Version = ""
				Commit = ""
				Date = ""
			},
			expect: Info{
				Version: unknownValue,
				Commit:  unknownValue,
				Date:    time.Now().UTC().Format("2006-01-02"),
			},
		},
		{
			name: "Invalid GoReleaser version",
			setVars: func() {
				Version = "v"
				Commit = ""
				Date = ""
			},
			expect: Info{
				Version: unknownValue,
				Commit:  unknownValue,
				Date:    time.Now().UTC().Format("2006-01-02"),
			},
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.setVars()

			info := GetMetaInfo()

			if !tt.partialMatch {
				if info.Version != tt.expect.Version {
					t.Errorf("Version = %q, want %q", info.Version, tt.expect.Version)
				}

				if info.Commit != tt.expect.Commit {
					t.Errorf("Commit = %q, want %q", info.Commit, tt.expect.Commit)
				}

				// Validate Date format (YYYY-MM-DD) instead of exact match
				if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(info.Date) {
					t.Errorf("Date = %q, want valid YYYY-MM-DD format", info.Date)
				}
			} else if info.Version != tt.expect.Version && !strings.Contains(info.Version, "+dirty") {
				t.Errorf("Version = %q, want %q or dirty variant", info.Version, tt.expect.Version)
			}
		})
	}
}

func TestGetVersionInfo_VCSData(t *testing.T) {
	Version = devVersion
	Commit = unknownValue
	Date = unknownValue

	info := GetMetaInfo()

	if buildInfo, ok := debug.ReadBuildInfo(); ok {
		var vcsRevision, vcsTime, vcsModified string

		for _, setting := range buildInfo.Settings {
			switch setting.Key {
			case "vcs.revision":
				vcsRevision = setting.Value
			case "vcs.time":
				vcsTime = setting.Value
			case "vcs.modified":
				vcsModified = setting.Value
			}
		}

		if vcsRevision != "" {
			expectedCommit := vcsRevision
			if len(vcsRevision) >= 7 {
				expectedCommit = vcsRevision[:7]
			}

			if info.Commit == unknownValue {
				t.Errorf(
					"Expected commit %q, got %q; ensure repository has commit history",
					expectedCommit,
					info.Commit,
				)
			} else if info.Commit != expectedCommit {
				t.Errorf("Commit = %q, want %q", info.Commit, expectedCommit)
			}
		} else {
			t.Logf("No vcs.revision found; ensure repository has Git metadata to cover commit assignment")
		}

		if vcsTime != "" {
			if parsedTime, err := time.Parse(time.RFC3339, vcsTime); err == nil {
				expectedDate := parsedTime.Format("2006-01-02")
				if info.Date == unknownValue {
					t.Errorf(
						"Expected date %q, got %q; ensure vcs.time is a valid RFC3339 timestamp",
						expectedDate,
						info.Date,
					)
				} else if info.Date != expectedDate {
					t.Errorf("Date = %q, want %q", info.Date, expectedDate)
				}
			} else {
				t.Logf("vcs.time %q is invalid; date should be in YYYY-MM-DD format", vcsTime)

				if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(info.Date) {
					t.Errorf("Date = %q, want valid YYYY-MM-DD format", info.Date)
				}
			}
		} else {
			t.Logf("No vcs.time found; date should be in YYYY-MM-DD format")

			if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(info.Date) {
				t.Errorf("Date = %q, want valid YYYY-MM-DD format", info.Date)
			}
		}

		if vcsModified == trueValue && info.Version != unknownValue {
			if !strings.Contains(info.Version, "+dirty") {
				t.Errorf(
					"Expected version to contain '+dirty', got %q; ensure repository has uncommitted changes",
					info.Version,
				)
			}
		} else if vcsModified != trueValue {
			t.Logf("Repository is clean (vcs.modified=%q); make uncommitted changes to cover '+dirty' case", vcsModified)
		}
	} else {
		t.Logf("debug.ReadBuildInfo() failed; ensure tests run in a Git repository to cover VCS parsing")
	}
}

func TestGetVersionInfo_InvalidVCSTime(t *testing.T) {
	Version = devVersion
	Commit = unknownValue
	Date = unknownValue

	info := GetMetaInfo()

	if !regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`).MatchString(info.Date) {
		t.Errorf("Date = %q, want valid YYYY-MM-DD format", info.Date)
	}
}

func TestGetMetaStr(t *testing.T) {
	tests := []struct {
		name    string
		setVars func()
		expect  string
	}{
		{
			name: "With commit (GoReleaser build)",
			setVars: func() {
				Version = "0.8.10"
				Commit = "a6fcf77abcdef"
				Date = "2025-05-27T00:00:00Z"
			},
			expect: "v0.8.10 (Built on 2025-05-27 from Git SHA a6fcf77)",
		},
		{
			name: "Without commit (go install build)",
			setVars: func() {
				Version = "0.8.10"
				Commit = unknownValue
				Date = unknownValue
			},
			expect: "v0.8.10 (Built on " + time.Now().UTC().Format("2006-01-02") + ")",
		},
		{
			name: "Invalid version",
			setVars: func() {
				Version = "v"
				Commit = unknownValue
				Date = unknownValue
			},
			expect: "unknown (Built on " + time.Now().UTC().Format("2006-01-02") + ")",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.setVars()

			result := GetMetaStr()
			if !strings.HasPrefix(result, strings.Split(tt.expect, " (")[0]) ||
				!regexp.MustCompile(`\d{4}-\d{2}-\d{2}`).MatchString(result) {
				t.Errorf("GetMetaStr() = %q, want format like %q", result, tt.expect)
			}
		})
	}
}

func TestContains(t *testing.T) {
	tests := []struct {
		name     string
		s        string
		substr   string
		expected bool
	}{
		{
			name:     "Substring found",
			s:        "v1.0.0+dirty",
			substr:   "+dirty",
			expected: true,
		},
		{
			name:     "Substring not found",
			s:        "v1.0.0",
			substr:   "+dirty",
			expected: false,
		},
		{
			name:     "Empty string",
			s:        "",
			substr:   "+dirty",
			expected: false,
		},
		{
			name:     "Empty substring",
			s:        "v1.0.0",
			substr:   "",
			expected: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := contains(tt.s, tt.substr)
			if result != tt.expected {
				t.Errorf("contains(%q, %q) = %v, want %v", tt.s, tt.substr, result, tt.expected)
			}
		})
	}
}