Skip to content

Commit f64e617

Browse files
committed
Go: Make RequiredGoVersion resilient to different version formats and add tests
1 parent 97b9533 commit f64e617

File tree

2 files changed

+76
-37
lines changed

2 files changed

+76
-37
lines changed

go/extractor/project/project.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func RequiredGoVersion(workspaces *[]GoWorkspace) GoVersionInfo {
122122
greatestGoVersion := VersionNotFound
123123
for _, workspace := range *workspaces {
124124
goVersionInfo := workspace.RequiredGoVersion()
125-
if goVersionInfo.Found && (!greatestGoVersion.Found || semver.Compare("v"+goVersionInfo.Version, "v"+greatestGoVersion.Version) > 0) {
125+
if goVersionInfo.Found && (!greatestGoVersion.Found || semver.Compare(util.FormatSemVer(goVersionInfo.Version), util.FormatSemVer(greatestGoVersion.Version)) > 0) {
126126
greatestGoVersion = goVersionInfo
127127
}
128128
}

go/extractor/project/project_test.go

+75-36
Original file line numberDiff line numberDiff line change
@@ -77,63 +77,102 @@ func parseWorkFile(t *testing.T, contents string) *modfile.WorkFile {
7777
return workFile
7878
}
7979

80-
func TestRequiredGoVersion(t *testing.T) {
81-
type ModVersionPair struct {
82-
FileContents string
83-
ExpectedVersion string
80+
type FileVersionPair struct {
81+
FileContents string
82+
ExpectedVersion string
83+
}
84+
85+
func checkRequiredGoVersionResult(t *testing.T, fun string, file string, testData FileVersionPair, result GoVersionInfo) {
86+
if !result.Found {
87+
t.Errorf(
88+
"Expected %s to return %s for the below `%s` file, but got nothing:\n%s",
89+
fun,
90+
testData.ExpectedVersion,
91+
file,
92+
testData.FileContents,
93+
)
94+
} else if result.Version != testData.ExpectedVersion {
95+
t.Errorf(
96+
"Expected %s to return %s for the below `%s` file, but got %s:\n%s",
97+
fun,
98+
testData.ExpectedVersion,
99+
file,
100+
result.Version,
101+
testData.FileContents,
102+
)
84103
}
104+
}
85105

86-
modules := []ModVersionPair{
106+
func TestRequiredGoVersion(t *testing.T) {
107+
testFiles := []FileVersionPair{
87108
{"go 1.20", "v1.20.0"},
88109
{"go 1.21.2", "v1.21.2"},
89110
{"go 1.21rc1", "v1.21.0-rc1"},
90111
{"go 1.21rc1\ntoolchain go1.22.0", "v1.22.0"},
91112
{"go 1.21rc1\ntoolchain go1.22rc1", "v1.22.0-rc1"},
92113
}
93114

94-
for _, testData := range modules {
115+
var modules []*GoModule = []*GoModule{}
116+
117+
for _, testData := range testFiles {
95118
// `go.mod` and `go.work` files have mostly the same format
96119
modFile := parseModFile(t, testData.FileContents)
97120
workFile := parseWorkFile(t, testData.FileContents)
98-
mod := GoModule{
121+
mod := &GoModule{
99122
Path: "test", // irrelevant
100123
Module: modFile,
101124
}
102-
work := GoWorkspace{
125+
work := &GoWorkspace{
103126
WorkspaceFile: workFile,
104127
}
105128

106129
result := mod.RequiredGoVersion()
107-
if !result.Found {
108-
t.Errorf(
109-
"Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got nothing:\n%s",
110-
testData.ExpectedVersion,
111-
testData.FileContents,
112-
)
113-
} else if result.Version != testData.ExpectedVersion {
114-
t.Errorf(
115-
"Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got %s:\n%s",
116-
testData.ExpectedVersion,
117-
result.Version,
118-
testData.FileContents,
119-
)
120-
}
130+
checkRequiredGoVersionResult(t, "mod.RequiredGoVersion()", "go.mod", testData, result)
121131

122132
result = work.RequiredGoVersion()
123-
if !result.Found {
124-
t.Errorf(
125-
"Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got nothing:\n%s",
126-
testData.ExpectedVersion,
127-
testData.FileContents,
128-
)
129-
} else if result.Version != testData.ExpectedVersion {
130-
t.Errorf(
131-
"Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got %s:\n%s",
132-
testData.ExpectedVersion,
133-
result.Version,
134-
testData.FileContents,
135-
)
136-
}
133+
checkRequiredGoVersionResult(t, "work.RequiredGoVersion()", "go.work", testData, result)
134+
135+
modules = append(modules, mod)
136+
}
137+
138+
// Create a test workspace with all the modules in one workspace.
139+
workspace := GoWorkspace{
140+
Modules: modules,
141+
}
142+
workspaceVer := "v1.22.0"
143+
144+
result := RequiredGoVersion(&[]GoWorkspace{workspace})
145+
if !result.Found {
146+
t.Errorf(
147+
"Expected RequiredGoVersion to return %s, but got nothing.",
148+
workspaceVer,
149+
)
150+
} else if result.Version != workspaceVer {
151+
t.Errorf(
152+
"Expected RequiredGoVersion to return %s, but got %s.",
153+
workspaceVer,
154+
result.Version,
155+
)
137156
}
138157

158+
// Create test workspaces for each module.
159+
workspaces := []GoWorkspace{}
160+
161+
for _, mod := range modules {
162+
workspaces = append(workspaces, GoWorkspace{Modules: []*GoModule{mod}})
163+
}
164+
165+
result = RequiredGoVersion(&workspaces)
166+
if !result.Found {
167+
t.Errorf(
168+
"Expected RequiredGoVersion to return %s, but got nothing.",
169+
workspaceVer,
170+
)
171+
} else if result.Version != workspaceVer {
172+
t.Errorf(
173+
"Expected RequiredGoVersion to return %s, but got %s.",
174+
workspaceVer,
175+
result.Version,
176+
)
177+
}
139178
}

0 commit comments

Comments
 (0)