diff --git a/go/extractor/project/project.go b/go/extractor/project/project.go index f1789b17cfc9..852c78ea108f 100644 --- a/go/extractor/project/project.go +++ b/go/extractor/project/project.go @@ -122,7 +122,7 @@ func RequiredGoVersion(workspaces *[]GoWorkspace) GoVersionInfo { greatestGoVersion := VersionNotFound for _, workspace := range *workspaces { goVersionInfo := workspace.RequiredGoVersion() - if goVersionInfo.Found && (!greatestGoVersion.Found || semver.Compare("v"+goVersionInfo.Version, "v"+greatestGoVersion.Version) > 0) { + if goVersionInfo.Found && (!greatestGoVersion.Found || semver.Compare(util.FormatSemVer(goVersionInfo.Version), util.FormatSemVer(greatestGoVersion.Version)) > 0) { greatestGoVersion = goVersionInfo } } diff --git a/go/extractor/project/project_test.go b/go/extractor/project/project_test.go index 62bf88c6cccc..72f1dcde0686 100644 --- a/go/extractor/project/project_test.go +++ b/go/extractor/project/project_test.go @@ -77,13 +77,34 @@ func parseWorkFile(t *testing.T, contents string) *modfile.WorkFile { return workFile } -func TestRequiredGoVersion(t *testing.T) { - type ModVersionPair struct { - FileContents string - ExpectedVersion string +type FileVersionPair struct { + FileContents string + ExpectedVersion string +} + +func checkRequiredGoVersionResult(t *testing.T, fun string, file string, testData FileVersionPair, result GoVersionInfo) { + if !result.Found { + t.Errorf( + "Expected %s to return %s for the below `%s` file, but got nothing:\n%s", + fun, + testData.ExpectedVersion, + file, + testData.FileContents, + ) + } else if result.Version != testData.ExpectedVersion { + t.Errorf( + "Expected %s to return %s for the below `%s` file, but got %s:\n%s", + fun, + testData.ExpectedVersion, + file, + result.Version, + testData.FileContents, + ) } +} - modules := []ModVersionPair{ +func TestRequiredGoVersion(t *testing.T) { + testFiles := []FileVersionPair{ {"go 1.20", "v1.20.0"}, {"go 1.21.2", "v1.21.2"}, {"go 1.21rc1", "v1.21.0-rc1"}, @@ -91,49 +112,67 @@ func TestRequiredGoVersion(t *testing.T) { {"go 1.21rc1\ntoolchain go1.22rc1", "v1.22.0-rc1"}, } - for _, testData := range modules { + var modules []*GoModule = []*GoModule{} + + for _, testData := range testFiles { // `go.mod` and `go.work` files have mostly the same format modFile := parseModFile(t, testData.FileContents) workFile := parseWorkFile(t, testData.FileContents) - mod := GoModule{ + mod := &GoModule{ Path: "test", // irrelevant Module: modFile, } - work := GoWorkspace{ + work := &GoWorkspace{ WorkspaceFile: workFile, } result := mod.RequiredGoVersion() - if !result.Found { - t.Errorf( - "Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got nothing:\n%s", - testData.ExpectedVersion, - testData.FileContents, - ) - } else if result.Version != testData.ExpectedVersion { - t.Errorf( - "Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got %s:\n%s", - testData.ExpectedVersion, - result.Version, - testData.FileContents, - ) - } + checkRequiredGoVersionResult(t, "mod.RequiredGoVersion()", "go.mod", testData, result) result = work.RequiredGoVersion() - if !result.Found { - t.Errorf( - "Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got nothing:\n%s", - testData.ExpectedVersion, - testData.FileContents, - ) - } else if result.Version != testData.ExpectedVersion { - t.Errorf( - "Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got %s:\n%s", - testData.ExpectedVersion, - result.Version, - testData.FileContents, - ) - } + checkRequiredGoVersionResult(t, "work.RequiredGoVersion()", "go.work", testData, result) + + modules = append(modules, mod) + } + + // Create a test workspace with all the modules in one workspace. + workspace := GoWorkspace{ + Modules: modules, + } + workspaceVer := "v1.22.0" + + result := RequiredGoVersion(&[]GoWorkspace{workspace}) + if !result.Found { + t.Errorf( + "Expected RequiredGoVersion to return %s, but got nothing.", + workspaceVer, + ) + } else if result.Version != workspaceVer { + t.Errorf( + "Expected RequiredGoVersion to return %s, but got %s.", + workspaceVer, + result.Version, + ) } + // Create test workspaces for each module. + workspaces := []GoWorkspace{} + + for _, mod := range modules { + workspaces = append(workspaces, GoWorkspace{Modules: []*GoModule{mod}}) + } + + result = RequiredGoVersion(&workspaces) + if !result.Found { + t.Errorf( + "Expected RequiredGoVersion to return %s, but got nothing.", + workspaceVer, + ) + } else if result.Version != workspaceVer { + t.Errorf( + "Expected RequiredGoVersion to return %s, but got %s.", + workspaceVer, + result.Version, + ) + } }