|
5 | 5 | "errors" |
6 | 6 | "fmt" |
7 | 7 | "net/http" |
8 | | - "os" |
9 | | - "strings" |
10 | 8 | "testing" |
11 | 9 | "time" |
12 | 10 |
|
@@ -163,64 +161,21 @@ func Test_GetVersion(t *testing.T) { |
163 | 161 | assert.Equal(t, v, v2) |
164 | 162 | } |
165 | 163 |
|
166 | | -func Test_GetVersionFromGit(t *testing.T) { |
167 | | - // This test will depend on the git environment |
168 | | - // It should either return a version or empty string |
169 | | - gitVersion := getVersionFromGit() |
170 | | - |
171 | | - // If we have git and are in a git repo, should return something |
172 | | - // If not, should return empty string - both are valid |
173 | | - if gitVersion != "" { |
174 | | - // If we get a version, it should not start with 'v' |
175 | | - assert.False(t, strings.HasPrefix(gitVersion, "v")) |
176 | | - // Should be a valid semantic version format |
177 | | - assert.NotEmpty(t, gitVersion) |
178 | | - } |
179 | | -} |
180 | | - |
181 | | -func Test_GetCommitHash(t *testing.T) { |
182 | | - // Test that getCommitHash returns a valid commit hash |
183 | | - commitHash := getCommitHash() |
184 | | - |
185 | | - // Should not be empty unless we're not in a git repo |
186 | | - if commitHash != unknownVersion { |
187 | | - // Should be a valid git commit hash (hex characters) |
188 | | - assert.Regexp(t, `^[a-f0-9]+$`, commitHash) |
189 | | - // Short hash is typically 7 characters, but can vary |
190 | | - assert.Greater(t, len(commitHash), 4) |
191 | | - assert.LessOrEqual(t, len(commitHash), 40) // Full hash is 40 chars max |
192 | | - } |
193 | | -} |
194 | | - |
195 | 164 | func Test_GetVersionFallback(t *testing.T) { |
196 | | - // Test that even if git detection fails, we get commit hash |
| 165 | + // Test that the new runtime/debug.ReadBuildInfo() approach works |
197 | 166 | // Reset version cache |
198 | 167 | version = "" |
199 | | - |
200 | | - // Change to a directory without git to test fallback |
201 | | - oldWd, err := os.Getwd() |
202 | | - require.NoError(t, err) |
203 | 168 | defer func() { |
204 | | - err := os.Chdir(oldWd) |
205 | | - require.NoError(t, err) |
206 | 169 | version = "" // Reset for other tests |
207 | 170 | }() |
208 | 171 |
|
209 | | - // Use cross-platform temporary directory |
210 | | - tempDir := os.TempDir() |
211 | | - err = os.Chdir(tempDir) |
212 | | - require.NoError(t, err) |
213 | | - |
214 | 172 | v := getVersion() |
215 | | - // Should fall back to "unknown" only if even commit hash fails |
216 | | - // In most cases it should return a commit hash |
| 173 | + // With runtime/debug.ReadBuildInfo(), the result depends on how the test is built |
| 174 | + // It could be module version, VCS tag, VCS revision, or "unknown" |
217 | 175 | assert.NotEqual(t, "", v) |
218 | | - if v == unknownVersion { |
219 | | - // This means both git tag detection and commit hash detection failed |
220 | | - // which is acceptable in non-git environments |
221 | | - assert.Equal(t, unknownVersion, v) |
222 | | - } else { |
223 | | - // Should be a commit hash - typically 7 characters |
224 | | - assert.Regexp(t, `^[a-f0-9]+$`, v) |
225 | | - } |
| 176 | + |
| 177 | + // Test that caching works |
| 178 | + version = "cached-version" |
| 179 | + v2 := getVersion() |
| 180 | + assert.Equal(t, "cached-version", v2) |
226 | 181 | } |
0 commit comments