Skip to content

Commit f33d7ee

Browse files
committed
Go: Add unit tests for hasInvalidToolchainVersion
1 parent 15a6308 commit f33d7ee

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

go/extractor/project/project_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package project
33
import (
44
"path/filepath"
55
"testing"
6+
7+
"golang.org/x/mod/modfile"
68
)
79

810
func testStartsWithAnyOf(t *testing.T, path string, prefix string, expectation bool) {
@@ -25,3 +27,38 @@ func TestStartsWithAnyOf(t *testing.T) {
2527
testStartsWithAnyOf(t, filepath.Join("foo", "bar"), "bar", false)
2628
testStartsWithAnyOf(t, filepath.Join("foo", "bar"), filepath.Join("foo", "baz"), false)
2729
}
30+
31+
func testHasInvalidToolchainVersion(t *testing.T, contents string) bool {
32+
modFile, err := modfile.Parse("test.go", []byte(contents), nil)
33+
34+
if err != nil {
35+
t.Errorf("Unable to parse %s: %s.\n", contents, err.Error())
36+
}
37+
38+
return hasInvalidToolchainVersion(modFile)
39+
}
40+
41+
func TestHasInvalidToolchainVersion(t *testing.T) {
42+
invalid := []string{
43+
"go 1.21\n",
44+
"go 1.22\n",
45+
}
46+
47+
for _, v := range invalid {
48+
if !testHasInvalidToolchainVersion(t, v) {
49+
t.Errorf("Expected testHasInvalidToolchainVersion(\"%s\") to be true, but got false", v)
50+
}
51+
}
52+
53+
valid := []string{
54+
"go 1.20\n",
55+
"go 1.21.1\n",
56+
"go 1.22\n\ntoolchain go1.22.0\n",
57+
}
58+
59+
for _, v := range valid {
60+
if testHasInvalidToolchainVersion(t, v) {
61+
t.Errorf("Expected testHasInvalidToolchainVersion(\"%s\") to be false, but got true", v)
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)