1
+ package version
2
+
3
+ import (
4
+ "runtime/debug"
5
+ "testing"
6
+ )
7
+
8
+ func TestString (t * testing.T ) {
9
+ // Save original values
10
+ origTag := tag
11
+ origCommit := commit
12
+ origDate := date
13
+ origBuildInfoReader := buildInfoReader
14
+
15
+ // Restore original values after the test
16
+ defer func () {
17
+ tag = origTag
18
+ commit = origCommit
19
+ date = origDate
20
+ buildInfoReader = origBuildInfoReader
21
+ }()
22
+
23
+ // Test 1: With preset values (simulating ldflags setting)
24
+ t .Run ("with preset values" , func (t * testing.T ) {
25
+ // Set known values for testing
26
+ tag = "v1.0.0"
27
+ commit = "abc123"
28
+ date = "2025-04-15"
29
+
30
+ // Mock the buildInfoReader to return false so that preset values are used
31
+ buildInfoReader = func () (* debug.BuildInfo , bool ) {
32
+ return nil , false
33
+ }
34
+
35
+ result := String ()
36
+
37
+ // Test the full format
38
+ expected := "v1.0.0 (abc123) built at 2025-04-15\n https://github.com/github/gh-combine/releases/tag/v1.0.0"
39
+ if result != expected {
40
+ t .Errorf ("Expected version string to be:\n %q\n but got:\n %q" , expected , result )
41
+ }
42
+ })
43
+
44
+ // Test 2: With mock build info that updates commit and date
45
+ t .Run ("with mock build info" , func (t * testing.T ) {
46
+ // Set initial values
47
+ tag = "dev"
48
+ commit = "initial-commit"
49
+ date = "initial-date"
50
+
51
+ // Create mock build info with specific values
52
+ mockSettings := []debug.BuildSetting {
53
+ {Key : "vcs.revision" , Value : "mock-commit-hash" },
54
+ {Key : "vcs.time" , Value : "mock-build-time" },
55
+ {Key : "other.key" , Value : "other-value" },
56
+ }
57
+
58
+ buildInfoReader = func () (* debug.BuildInfo , bool ) {
59
+ return & debug.BuildInfo {
60
+ Settings : mockSettings ,
61
+ }, true
62
+ }
63
+
64
+ result := String ()
65
+
66
+ // Check if the values from build info were used
67
+ expected := "dev (mock-commit-hash) built at mock-build-time\n https://github.com/github/gh-combine/releases/tag/dev"
68
+ if result != expected {
69
+ t .Errorf ("Expected version string to be:\n %q\n but got:\n %q" , expected , result )
70
+ }
71
+ })
72
+
73
+ // Test 3: With empty build info settings
74
+ t .Run ("with empty build info settings" , func (t * testing.T ) {
75
+ // Set initial values
76
+ tag = "dev"
77
+ commit = "unchanged-commit"
78
+ date = "unchanged-date"
79
+
80
+ // Empty build settings
81
+ buildInfoReader = func () (* debug.BuildInfo , bool ) {
82
+ return & debug.BuildInfo {
83
+ Settings : []debug.BuildSetting {},
84
+ }, true
85
+ }
86
+
87
+ result := String ()
88
+
89
+ // The values should remain unchanged
90
+ expected := "dev (unchanged-commit) built at unchanged-date\n https://github.com/github/gh-combine/releases/tag/dev"
91
+ if result != expected {
92
+ t .Errorf ("Expected version string to be:\n %q\n but got:\n %q" , expected , result )
93
+ }
94
+ })
95
+ }
0 commit comments