Skip to content

Commit 9fe3aa4

Browse files
committed
feat(version-checker): enhance CheckLatestVersion to support Go module paths
Updated CheckLatestVersion to accept Go module paths, improving compatibility with Go proxy tools. Added integration tests for Go proxy and benchstat tool configurations.
1 parent 9756b2b commit 9fe3aa4

1 file changed

Lines changed: 99 additions & 12 deletions

File tree

magefiles/version_checker_test.go

Lines changed: 99 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@ func NewMockVersionChecker() *MockVersionChecker {
3737
}
3838

3939
// CheckLatestVersion returns the mocked version or error.
40-
func (m *MockVersionChecker) CheckLatestVersion(_ context.Context, repoURL string) (string, error) {
41-
m.calls = append(m.calls, repoURL)
42-
if err, ok := m.errors[repoURL]; ok {
40+
func (m *MockVersionChecker) CheckLatestVersion(_ context.Context, repoURL, goModulePath string) (string, error) {
41+
// Use module path as key for Go proxy tools, otherwise use repo URL
42+
key := repoURL
43+
if goModulePath != "" {
44+
key = goModulePath
45+
}
46+
m.calls = append(m.calls, key)
47+
if err, ok := m.errors[key]; ok {
4348
return "", err
4449
}
45-
if version, ok := m.versions[repoURL]; ok {
50+
if version, ok := m.versions[key]; ok {
4651
return version, nil
4752
}
4853
return "", errNotFound
@@ -171,6 +176,7 @@ func TestGetToolDefinitions(t *testing.T) {
171176
"swag",
172177
"yamlfmt",
173178
"go-pre-commit",
179+
"benchstat",
174180
}
175181

176182
assert.Len(t, tools, len(expectedTools), "should have correct number of tools")
@@ -179,9 +185,14 @@ func TestGetToolDefinitions(t *testing.T) {
179185
tool, ok := tools[toolName]
180186
require.True(t, ok, "tool %s should exist", toolName)
181187
assert.NotEmpty(t, tool.EnvVars, "tool %s should have env vars", toolName)
182-
assert.NotEmpty(t, tool.RepoURL, "tool %s should have repo URL", toolName)
183-
assert.NotEmpty(t, tool.RepoOwner, "tool %s should have repo owner", toolName)
184-
assert.NotEmpty(t, tool.RepoName, "tool %s should have repo name", toolName)
188+
// Go proxy-based tools (like benchstat) don't have GitHub repo info
189+
if tool.GoModulePath == "" {
190+
assert.NotEmpty(t, tool.RepoURL, "tool %s should have repo URL", toolName)
191+
assert.NotEmpty(t, tool.RepoOwner, "tool %s should have repo owner", toolName)
192+
assert.NotEmpty(t, tool.RepoName, "tool %s should have repo name", toolName)
193+
} else {
194+
assert.NotEmpty(t, tool.GoModulePath, "tool %s should have Go module path", toolName)
195+
}
185196
}
186197

187198
// Test specific tool configurations
@@ -705,9 +716,9 @@ func TestMockVersionChecker_CallTracking(t *testing.T) {
705716
ctx := context.Background()
706717

707718
// Make calls
708-
_, _ = mock.CheckLatestVersion(ctx, "https://github.com/owner/repo1")
709-
_, _ = mock.CheckLatestVersion(ctx, "https://github.com/owner/repo2")
710-
_, _ = mock.CheckLatestVersion(ctx, "https://github.com/owner/repo1")
719+
_, _ = mock.CheckLatestVersion(ctx, "https://github.com/owner/repo1", "")
720+
_, _ = mock.CheckLatestVersion(ctx, "https://github.com/owner/repo2", "")
721+
_, _ = mock.CheckLatestVersion(ctx, "https://github.com/owner/repo1", "")
711722

712723
// Verify calls
713724
calls := mock.GetCalls()
@@ -737,8 +748,8 @@ func TestVersionChecker_Integration(t *testing.T) {
737748
checker := NewVersionChecker(false)
738749
ctx := context.Background()
739750

740-
// Test with a known stable repo
741-
version, err := checker.CheckLatestVersion(ctx, "https://github.com/magefile/mage")
751+
// Test with a known stable repo (GitHub releases)
752+
version, err := checker.CheckLatestVersion(ctx, "https://github.com/magefile/mage", "")
742753
if err != nil {
743754
// Network errors are ok in integration tests
744755
t.Logf("Network error (expected in some envs): %v", err)
@@ -748,3 +759,79 @@ func TestVersionChecker_Integration(t *testing.T) {
748759
assert.NotEmpty(t, version)
749760
t.Logf("Found version: %s", version)
750761
}
762+
763+
func TestVersionChecker_GoProxy_Integration(t *testing.T) {
764+
if testing.Short() {
765+
t.Skip("skipping integration test")
766+
}
767+
768+
// Test Go proxy API
769+
checker := NewVersionChecker(false)
770+
ctx := context.Background()
771+
772+
// Test with a Go module that uses pseudo-versions
773+
version, err := checker.CheckLatestVersion(ctx, "", "golang.org/x/perf")
774+
if err != nil {
775+
// Network errors are ok in integration tests
776+
t.Logf("Network error (expected in some envs): %v", err)
777+
return
778+
}
779+
780+
assert.NotEmpty(t, version)
781+
assert.Contains(t, version, "v0.0.0-", "should be a pseudo-version")
782+
t.Logf("Found Go proxy version: %s", version)
783+
}
784+
785+
func TestVersionUpdateService_CheckVersions_PinRecommended(t *testing.T) {
786+
checker := NewMockVersionChecker()
787+
updater := NewMockFileUpdater()
788+
logger := NewMockLogger()
789+
service := NewVersionUpdateService(checker, updater, logger, true, 0)
790+
791+
// Mock Go proxy response for benchstat
792+
checker.SetVersion("golang.org/x/perf", "v0.0.0-20251208221838-04cf7a2dca90")
793+
794+
tools := map[string]*ToolInfo{
795+
"benchstat": {
796+
EnvVars: []string{"MAGE_X_BENCHSTAT_VERSION"},
797+
GoModulePath: "golang.org/x/perf",
798+
},
799+
}
800+
801+
currentVersions := map[string]string{
802+
"benchstat": "latest",
803+
}
804+
805+
ctx := context.Background()
806+
results := service.checkVersions(ctx, tools, currentVersions)
807+
808+
require.Len(t, results, 1)
809+
assert.Equal(t, "pin-recommended", results[0].Status)
810+
assert.Equal(t, "v0.0.0-20251208221838-04cf7a2dca90", results[0].LatestVersion)
811+
assert.Equal(t, "latest", results[0].CurrentVersion)
812+
}
813+
814+
func TestVersionUpdateService_HasUpdates_IncludesPinRecommended(t *testing.T) {
815+
checker := NewMockVersionChecker()
816+
updater := NewMockFileUpdater()
817+
logger := NewMockLogger()
818+
service := NewVersionUpdateService(checker, updater, logger, true, 0)
819+
820+
t.Run("has pin-recommended", func(t *testing.T) {
821+
results := []CheckResult{
822+
{Status: "up-to-date"},
823+
{Status: "pin-recommended"},
824+
{Status: "up-to-date"},
825+
}
826+
assert.True(t, service.hasUpdates(results))
827+
})
828+
829+
t.Run("only up-to-date and errors", func(t *testing.T) {
830+
results := []CheckResult{
831+
{Status: "up-to-date"},
832+
{Status: "up-to-date"},
833+
{Status: "error"},
834+
}
835+
assert.False(t, service.hasUpdates(results))
836+
})
837+
}

0 commit comments

Comments
 (0)