4
4
"path/filepath"
5
5
"testing"
6
6
7
+ "github.com/github/codeql-go/extractor/util"
7
8
"golang.org/x/mod/modfile"
8
9
)
9
10
@@ -28,14 +29,18 @@ func TestStartsWithAnyOf(t *testing.T) {
28
29
testStartsWithAnyOf (t , filepath .Join ("foo" , "bar" ), filepath .Join ("foo" , "baz" ), false )
29
30
}
30
31
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 )
33
34
34
35
if err != nil {
35
36
t .Errorf ("Unable to parse %s: %s.\n " , contents , err .Error ())
36
37
}
37
38
38
- return hasInvalidToolchainVersion (modFile )
39
+ return modFile
40
+ }
41
+
42
+ func testHasInvalidToolchainVersion (t * testing.T , contents string ) bool {
43
+ return hasInvalidToolchainVersion (parseModFile (t , contents ))
39
44
}
40
45
41
46
func TestHasInvalidToolchainVersion (t * testing.T ) {
@@ -62,3 +67,74 @@ func TestHasInvalidToolchainVersion(t *testing.T) {
62
67
}
63
68
}
64
69
}
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