Skip to content

Commit

Permalink
Go: Add tests for RequiredGoVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
mbg committed Jun 7, 2024
1 parent 504a233 commit 881b258
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
5 changes: 4 additions & 1 deletion go/extractor/project/BUILD.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 79 additions & 3 deletions go/extractor/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"path/filepath"
"testing"

"github.com/github/codeql-go/extractor/util"
"golang.org/x/mod/modfile"
)

Expand All @@ -28,14 +29,18 @@ func TestStartsWithAnyOf(t *testing.T) {
testStartsWithAnyOf(t, filepath.Join("foo", "bar"), filepath.Join("foo", "baz"), false)
}

func testHasInvalidToolchainVersion(t *testing.T, contents string) bool {
modFile, err := modfile.Parse("test.go", []byte(contents), nil)
func parseModFile(t *testing.T, contents string) *modfile.File {
modFile, err := modfile.Parse("go.mod", []byte(contents), nil)

if err != nil {
t.Errorf("Unable to parse %s: %s.\n", contents, err.Error())
}

return hasInvalidToolchainVersion(modFile)
return modFile
}

func testHasInvalidToolchainVersion(t *testing.T, contents string) bool {
return hasInvalidToolchainVersion(parseModFile(t, contents))
}

func TestHasInvalidToolchainVersion(t *testing.T) {
Expand All @@ -62,3 +67,74 @@ func TestHasInvalidToolchainVersion(t *testing.T) {
}
}
}

func parseWorkFile(t *testing.T, contents string) *modfile.WorkFile {
workFile, err := modfile.ParseWork("go.work", []byte(contents), nil)

if err != nil {
t.Errorf("Unable to parse %s: %s.\n", contents, err.Error())
}

return workFile
}

func TestRequiredGoVersion(t *testing.T) {
type ModVersionPair struct {
FileContents string
ExpectedVersion string
}

modules := []ModVersionPair{
{"go 1.20", "v1.20"},
{"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 {
// `go.mod` and `go.work` files have mostly the same format
modFile := parseModFile(t, testData.FileContents)
workFile := parseWorkFile(t, testData.FileContents)
mod := GoModule{
Path: "test", // irrelevant
Module: modFile,
}
work := GoWorkspace{
WorkspaceFile: workFile,
}

result := mod.RequiredGoVersion()
if result == nil {
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 != util.NewSemVer(testData.ExpectedVersion) {
t.Errorf(
"Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got %s:\n%s",
testData.ExpectedVersion,
result,
testData.FileContents,
)
}

result = work.RequiredGoVersion()
if result == nil {
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 != util.NewSemVer(testData.ExpectedVersion) {
t.Errorf(
"Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got %s:\n%s",
testData.ExpectedVersion,
result,
testData.FileContents,
)
}
}

}

0 comments on commit 881b258

Please sign in to comment.