Skip to content

Commit

Permalink
Go: Make RequiredGoVersion resilient to different version formats a…
Browse files Browse the repository at this point in the history
…nd add tests
  • Loading branch information
mbg committed May 8, 2024
1 parent 97b9533 commit f64e617
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go/extractor/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
111 changes: 75 additions & 36 deletions go/extractor/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,63 +77,102 @@ 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"},
{"go 1.21rc1\ntoolchain go1.22.0", "v1.22.0"},
{"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,
)
}
}

0 comments on commit f64e617

Please sign in to comment.