Skip to content

Commit 7f8caea

Browse files
committed
bolt things down with tests
1 parent aed9070 commit 7f8caea

File tree

3 files changed

+153
-1
lines changed

3 files changed

+153
-1
lines changed

internal/common/common_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package common
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestNormalizeArray(t *testing.T) {
9+
tests := []struct {
10+
name string
11+
input []string
12+
want []string
13+
}{
14+
{
15+
name: "Empty array",
16+
input: []string{},
17+
want: []string{},
18+
},
19+
{
20+
name: "Already lowercase",
21+
input: []string{"test", "already", "lowercase"},
22+
want: []string{"test", "already", "lowercase"},
23+
},
24+
{
25+
name: "Mixed case",
26+
input: []string{"Test", "UPPERCASE", "lowercase", "MixedCase"},
27+
want: []string{"test", "uppercase", "lowercase", "mixedcase"},
28+
},
29+
{
30+
name: "Special characters",
31+
input: []string{"TEST-123", "Feature/branch", "BUG_FIX"},
32+
want: []string{"test-123", "feature/branch", "bug_fix"},
33+
},
34+
{
35+
name: "Single item",
36+
input: []string{"SINGLE"},
37+
want: []string{"single"},
38+
},
39+
}
40+
41+
for _, tt := range tests {
42+
t.Run(tt.name, func(t *testing.T) {
43+
got := NormalizeArray(tt.input)
44+
if !reflect.DeepEqual(got, tt.want) {
45+
t.Errorf("NormalizeArray() = %v, want %v", got, tt.want)
46+
}
47+
})
48+
}
49+
}

internal/version/version.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@ var (
1313

1414
const template = "%s (%s) built at %s\nhttps://github.com/github/gh-combine/releases/tag/%s"
1515

16+
// buildInfoReader is a function type that can be mocked in tests
17+
var buildInfoReader = defaultBuildInfoReader
18+
19+
// defaultBuildInfoReader is the actual implementation using debug.ReadBuildInfo
20+
func defaultBuildInfoReader() (*debug.BuildInfo, bool) {
21+
return debug.ReadBuildInfo()
22+
}
23+
1624
func String() string {
17-
info, ok := debug.ReadBuildInfo()
25+
info, ok := buildInfoReader()
1826

1927
if ok {
2028
for _, setting := range info.Settings {

internal/version/version_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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\nhttps://github.com/github/gh-combine/releases/tag/v1.0.0"
39+
if result != expected {
40+
t.Errorf("Expected version string to be:\n%q\nbut 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\nhttps://github.com/github/gh-combine/releases/tag/dev"
68+
if result != expected {
69+
t.Errorf("Expected version string to be:\n%q\nbut 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\nhttps://github.com/github/gh-combine/releases/tag/dev"
91+
if result != expected {
92+
t.Errorf("Expected version string to be:\n%q\nbut got:\n%q", expected, result)
93+
}
94+
})
95+
}

0 commit comments

Comments
 (0)