@@ -77,63 +77,102 @@ func parseWorkFile(t *testing.T, contents string) *modfile.WorkFile {
77
77
return workFile
78
78
}
79
79
80
- func TestRequiredGoVersion (t * testing.T ) {
81
- type ModVersionPair struct {
82
- FileContents string
83
- ExpectedVersion string
80
+ type FileVersionPair struct {
81
+ FileContents string
82
+ ExpectedVersion string
83
+ }
84
+
85
+ func checkRequiredGoVersionResult (t * testing.T , fun string , file string , testData FileVersionPair , result GoVersionInfo ) {
86
+ if ! result .Found {
87
+ t .Errorf (
88
+ "Expected %s to return %s for the below `%s` file, but got nothing:\n %s" ,
89
+ fun ,
90
+ testData .ExpectedVersion ,
91
+ file ,
92
+ testData .FileContents ,
93
+ )
94
+ } else if result .Version != testData .ExpectedVersion {
95
+ t .Errorf (
96
+ "Expected %s to return %s for the below `%s` file, but got %s:\n %s" ,
97
+ fun ,
98
+ testData .ExpectedVersion ,
99
+ file ,
100
+ result .Version ,
101
+ testData .FileContents ,
102
+ )
84
103
}
104
+ }
85
105
86
- modules := []ModVersionPair {
106
+ func TestRequiredGoVersion (t * testing.T ) {
107
+ testFiles := []FileVersionPair {
87
108
{"go 1.20" , "v1.20.0" },
88
109
{"go 1.21.2" , "v1.21.2" },
89
110
{"go 1.21rc1" , "v1.21.0-rc1" },
90
111
{"go 1.21rc1\n toolchain go1.22.0" , "v1.22.0" },
91
112
{"go 1.21rc1\n toolchain go1.22rc1" , "v1.22.0-rc1" },
92
113
}
93
114
94
- for _ , testData := range modules {
115
+ var modules []* GoModule = []* GoModule {}
116
+
117
+ for _ , testData := range testFiles {
95
118
// `go.mod` and `go.work` files have mostly the same format
96
119
modFile := parseModFile (t , testData .FileContents )
97
120
workFile := parseWorkFile (t , testData .FileContents )
98
- mod := GoModule {
121
+ mod := & GoModule {
99
122
Path : "test" , // irrelevant
100
123
Module : modFile ,
101
124
}
102
- work := GoWorkspace {
125
+ work := & GoWorkspace {
103
126
WorkspaceFile : workFile ,
104
127
}
105
128
106
129
result := mod .RequiredGoVersion ()
107
- if ! result .Found {
108
- t .Errorf (
109
- "Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got nothing:\n %s" ,
110
- testData .ExpectedVersion ,
111
- testData .FileContents ,
112
- )
113
- } else if result .Version != testData .ExpectedVersion {
114
- t .Errorf (
115
- "Expected mod.RequiredGoVersion() to return %s for the below `go.mod` file, but got %s:\n %s" ,
116
- testData .ExpectedVersion ,
117
- result .Version ,
118
- testData .FileContents ,
119
- )
120
- }
130
+ checkRequiredGoVersionResult (t , "mod.RequiredGoVersion()" , "go.mod" , testData , result )
121
131
122
132
result = work .RequiredGoVersion ()
123
- if ! result .Found {
124
- t .Errorf (
125
- "Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got nothing:\n %s" ,
126
- testData .ExpectedVersion ,
127
- testData .FileContents ,
128
- )
129
- } else if result .Version != testData .ExpectedVersion {
130
- t .Errorf (
131
- "Expected mod.RequiredGoVersion() to return %s for the below `go.work` file, but got %s:\n %s" ,
132
- testData .ExpectedVersion ,
133
- result .Version ,
134
- testData .FileContents ,
135
- )
136
- }
133
+ checkRequiredGoVersionResult (t , "work.RequiredGoVersion()" , "go.work" , testData , result )
134
+
135
+ modules = append (modules , mod )
136
+ }
137
+
138
+ // Create a test workspace with all the modules in one workspace.
139
+ workspace := GoWorkspace {
140
+ Modules : modules ,
141
+ }
142
+ workspaceVer := "v1.22.0"
143
+
144
+ result := RequiredGoVersion (& []GoWorkspace {workspace })
145
+ if ! result .Found {
146
+ t .Errorf (
147
+ "Expected RequiredGoVersion to return %s, but got nothing." ,
148
+ workspaceVer ,
149
+ )
150
+ } else if result .Version != workspaceVer {
151
+ t .Errorf (
152
+ "Expected RequiredGoVersion to return %s, but got %s." ,
153
+ workspaceVer ,
154
+ result .Version ,
155
+ )
137
156
}
138
157
158
+ // Create test workspaces for each module.
159
+ workspaces := []GoWorkspace {}
160
+
161
+ for _ , mod := range modules {
162
+ workspaces = append (workspaces , GoWorkspace {Modules : []* GoModule {mod }})
163
+ }
164
+
165
+ result = RequiredGoVersion (& workspaces )
166
+ if ! result .Found {
167
+ t .Errorf (
168
+ "Expected RequiredGoVersion to return %s, but got nothing." ,
169
+ workspaceVer ,
170
+ )
171
+ } else if result .Version != workspaceVer {
172
+ t .Errorf (
173
+ "Expected RequiredGoVersion to return %s, but got %s." ,
174
+ workspaceVer ,
175
+ result .Version ,
176
+ )
177
+ }
139
178
}
0 commit comments