44 "path/filepath"
55 "testing"
66
7+ "github.com/github/codeql-go/extractor/util"
78 "golang.org/x/mod/modfile"
89)
910
@@ -28,14 +29,18 @@ func TestStartsWithAnyOf(t *testing.T) {
2829 testStartsWithAnyOf (t , filepath .Join ("foo" , "bar" ), filepath .Join ("foo" , "baz" ), false )
2930}
3031
31- func testHasInvalidToolchainVersion (t * testing.T , contents string ) bool {
32- modFile , err := modfile .Parse ("test.go " , []byte (contents ), nil )
32+ func parseModFile (t * testing.T , contents string ) * modfile. File {
33+ modFile , err := modfile .Parse ("go.mod " , []byte (contents ), nil )
3334
3435 if err != nil {
3536 t .Errorf ("Unable to parse %s: %s.\n " , contents , err .Error ())
3637 }
3738
38- return hasInvalidToolchainVersion (modFile )
39+ return modFile
40+ }
41+
42+ func testHasInvalidToolchainVersion (t * testing.T , contents string ) bool {
43+ return hasInvalidToolchainVersion (parseModFile (t , contents ))
3944}
4045
4146func TestHasInvalidToolchainVersion (t * testing.T ) {
@@ -62,3 +67,74 @@ func TestHasInvalidToolchainVersion(t *testing.T) {
6267 }
6368 }
6469}
70+
71+ func parseWorkFile (t * testing.T , contents string ) * modfile.WorkFile {
72+ workFile , err := modfile .ParseWork ("go.work" , []byte (contents ), nil )
73+
74+ if err != nil {
75+ t .Errorf ("Unable to parse %s: %s.\n " , contents , err .Error ())
76+ }
77+
78+ return workFile
79+ }
80+
81+ func TestRequiredGoVersion (t * testing.T ) {
82+ type ModVersionPair struct {
83+ FileContents string
84+ ExpectedVersion string
85+ }
86+
87+ modules := []ModVersionPair {
88+ {"go 1.20" , "v1.20" },
89+ {"go 1.21.2" , "v1.21.2" },
90+ {"go 1.21rc1" , "v1.21.0-rc1" },
91+ {"go 1.21rc1\n toolchain go1.22.0" , "v1.22.0" },
92+ {"go 1.21rc1\n toolchain go1.22rc1" , "v1.22.0-rc1" },
93+ }
94+
95+ for _ , testData := range modules {
96+ // `go.mod` and `go.work` files have mostly the same format
97+ modFile := parseModFile (t , testData .FileContents )
98+ workFile := parseWorkFile (t , testData .FileContents )
99+ mod := GoModule {
100+ Path : "test" , // irrelevant
101+ Module : modFile ,
102+ }
103+ work := GoWorkspace {
104+ WorkspaceFile : workFile ,
105+ }
106+
107+ result := mod .RequiredGoVersion ()
108+ if result == nil {
109+ t .Errorf (
110+ "Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got nothing:\n %s" ,
111+ testData .ExpectedVersion ,
112+ testData .FileContents ,
113+ )
114+ } else if result != util .NewSemVer (testData .ExpectedVersion ) {
115+ t .Errorf (
116+ "Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got %s:\n %s" ,
117+ testData .ExpectedVersion ,
118+ result ,
119+ testData .FileContents ,
120+ )
121+ }
122+
123+ result = work .RequiredGoVersion ()
124+ if result == nil {
125+ t .Errorf (
126+ "Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got nothing:\n %s" ,
127+ testData .ExpectedVersion ,
128+ testData .FileContents ,
129+ )
130+ } else if result != util .NewSemVer (testData .ExpectedVersion ) {
131+ t .Errorf (
132+ "Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got %s:\n %s" ,
133+ testData .ExpectedVersion ,
134+ result ,
135+ testData .FileContents ,
136+ )
137+ }
138+ }
139+
140+ }
0 commit comments