|
1 | 1 | package version |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strings" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
7 | 9 | ) |
8 | 10 |
|
9 | 11 | func TestSet(t *testing.T) { |
@@ -57,6 +59,63 @@ func TestSet(t *testing.T) { |
57 | 59 | } |
58 | 60 | } |
59 | 61 |
|
| 62 | +func TestBuildVersionString(t *testing.T) { |
| 63 | + t.Run("all fields provided", func(t *testing.T) { |
| 64 | + got := BuildVersionString("v1.2.3", "abc1234", "2024-01-01") |
| 65 | + assert.Equal(t, "v1.2.3, commit: abc1234, built: 2024-01-01", got) |
| 66 | + }) |
| 67 | + |
| 68 | + t.Run("empty mainVersion uses dev prefix", func(t *testing.T) { |
| 69 | + got := BuildVersionString("", "abc1234", "2024-01-01") |
| 70 | + assert.Equal(t, "dev, commit: abc1234, built: 2024-01-01", got) |
| 71 | + }) |
| 72 | + |
| 73 | + t.Run("mainVersion and gitCommit without buildDate", func(t *testing.T) { |
| 74 | + got := BuildVersionString("v1.2.3", "abc1234", "") |
| 75 | + // buildDate is empty so build info fallback may or may not add a date |
| 76 | + assert.True(t, strings.HasPrefix(got, "v1.2.3, commit: abc1234"), |
| 77 | + "result should start with version and commit: got %q", got) |
| 78 | + }) |
| 79 | + |
| 80 | + t.Run("mainVersion and buildDate without gitCommit", func(t *testing.T) { |
| 81 | + got := BuildVersionString("v1.2.3", "", "2024-01-01") |
| 82 | + // gitCommit is empty so build info fallback may or may not add a commit |
| 83 | + assert.True(t, strings.HasPrefix(got, "v1.2.3"), |
| 84 | + "result should start with version: got %q", got) |
| 85 | + assert.True(t, strings.HasSuffix(got, "built: 2024-01-01"), |
| 86 | + "result should end with built date: got %q", got) |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("only mainVersion", func(t *testing.T) { |
| 90 | + got := BuildVersionString("v2.0.0", "", "") |
| 91 | + // No explicit commit or date; build info fallback may add them |
| 92 | + assert.True(t, strings.HasPrefix(got, "v2.0.0"), |
| 93 | + "result should start with mainVersion: got %q", got) |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("all empty uses dev", func(t *testing.T) { |
| 97 | + got := BuildVersionString("", "", "") |
| 98 | + assert.True(t, strings.HasPrefix(got, "dev"), |
| 99 | + "result should start with 'dev' when mainVersion is empty: got %q", got) |
| 100 | + }) |
| 101 | + |
| 102 | + t.Run("result is comma-separated", func(t *testing.T) { |
| 103 | + got := BuildVersionString("v1.0.0", "deadbeef", "2025-06-01") |
| 104 | + parts := strings.Split(got, ", ") |
| 105 | + require.GreaterOrEqual(t, len(parts), 3, "should have at least 3 comma-separated parts") |
| 106 | + assert.Equal(t, "v1.0.0", parts[0]) |
| 107 | + assert.Equal(t, "commit: deadbeef", parts[1]) |
| 108 | + assert.Equal(t, "built: 2025-06-01", parts[2]) |
| 109 | + }) |
| 110 | + |
| 111 | + t.Run("explicit gitCommit is not truncated", func(t *testing.T) { |
| 112 | + longHash := "abcdef1234567890" |
| 113 | + got := BuildVersionString("v1.0.0", longHash, "2024-01-01") |
| 114 | + // When gitCommit is explicitly provided it is used as-is (no truncation) |
| 115 | + assert.Contains(t, got, "commit: "+longHash) |
| 116 | + }) |
| 117 | +} |
| 118 | + |
60 | 119 | func TestGet(t *testing.T) { |
61 | 120 | t.Run("returns default version", func(t *testing.T) { |
62 | 121 | original := gatewayVersion |
|
0 commit comments