1 env GO111MODULE=on
2
3 # Test that go mod edits and related mod flags work.
4 # Also test that they can use a dummy name that isn't resolvable. golang.org/issue/24100
5
6 # go mod init
7 ! go mod init
8 stderr 'cannot determine module path'
9 ! exists go.mod
10
11 go mod init x.x/y/z
12 stderr 'creating new go.mod: module x.x/y/z'
13 cmpenv go.mod $WORK/go.mod.init
14
15 ! go mod init
16 cmpenv go.mod $WORK/go.mod.init
17
18 # go mod edits
19 go mod edit -droprequire=x.1 -require=x.1@v1.0.0 -require=x.2@v1.1.0 -droprequire=x.2 -exclude='x.1 @ v1.2.0' -exclude=x.1@v1.2.1 -exclude=x.1@v2.0.0+incompatible -replace=x.1@v1.3.0=y.1@v1.4.0 -replace='x.1@v1.4.0 = ../z' -retract=v1.6.0 -retract=[v1.1.0,v1.2.0] -retract=[v1.3.0,v1.4.0] -retract=v1.0.0
20 cmpenv go.mod $WORK/go.mod.edit1
21 go mod edit -droprequire=x.1 -dropexclude=x.1@v1.2.1 -dropexclude=x.1@v2.0.0+incompatible -dropreplace=x.1@v1.3.0 -require=x.3@v1.99.0 -dropretract=v1.0.0 -dropretract=[v1.1.0,v1.2.0]
22 cmpenv go.mod $WORK/go.mod.edit2
23
24 # -exclude and -retract reject invalid versions.
25 ! go mod edit -exclude=example.com/m@bad
26 stderr '^go: -exclude=example.com/m@bad: version "bad" invalid: must be of the form v1.2.3$'
27 ! go mod edit -retract=bad
28 stderr '^go: -retract=bad: version "bad" invalid: must be of the form v1.2.3$'
29
30 ! go mod edit -exclude=example.com/m@v2.0.0
31 stderr '^go: -exclude=example.com/m@v2\.0\.0: version "v2\.0\.0" invalid: should be v2\.0\.0\+incompatible \(or module example\.com/m/v2\)$'
32
33 ! go mod edit -exclude=example.com/m/v2@v1.0.0
34 stderr '^go: -exclude=example.com/m/v2@v1\.0\.0: version "v1\.0\.0" invalid: should be v2, not v1$'
35
36 ! go mod edit -exclude=gopkg.in/example.v1@v2.0.0
37 stderr '^go: -exclude=gopkg\.in/example\.v1@v2\.0\.0: version "v2\.0\.0" invalid: should be v1, not v2$'
38
39 cmpenv go.mod $WORK/go.mod.edit2
40
41 # go mod edit -json
42 go mod edit -json
43 cmpenv stdout $WORK/go.mod.json
44
45 # go mod edit -json (retractions with rationales)
46 go mod edit -json $WORK/go.mod.retractrationale
47 cmp stdout $WORK/go.mod.retractrationale.json
48
49 # go mod edit -json (deprecation)
50 go mod edit -json $WORK/go.mod.deprecation
51 cmp stdout $WORK/go.mod.deprecation.json
52
53 # go mod edit -json (empty mod file)
54 go mod edit -json $WORK/go.mod.empty
55 cmp stdout $WORK/go.mod.empty.json
56
57 # go mod edit -replace
58 go mod edit -replace=x.1@v1.3.0=y.1/v2@v2.3.5 -replace=x.1@v1.4.0=y.1/v2@v2.3.5
59 cmpenv go.mod $WORK/go.mod.edit3
60 go mod edit -replace=x.1=y.1/v2@v2.3.6
61 cmpenv go.mod $WORK/go.mod.edit4
62 go mod edit -dropreplace=x.1
63 cmpenv go.mod $WORK/go.mod.edit5
64 go mod edit -replace=x.1=../y.1/@v2
65 cmpenv go.mod $WORK/go.mod.edit6
66 ! go mod edit -replace=x.1=y.1/@v2
67 stderr '^go: -replace=x.1=y.1/@v2: invalid new path: malformed import path "y.1/": trailing slash$'
68
69 # go mod edit -fmt
70 cp $WORK/go.mod.badfmt go.mod
71 go mod edit -fmt -print # -print should avoid writing file
72 cmpenv stdout $WORK/go.mod.goodfmt
73 cmp go.mod $WORK/go.mod.badfmt
74 go mod edit -fmt # without -print, should write file (and nothing to stdout)
75 ! stdout .
76 cmpenv go.mod $WORK/go.mod.goodfmt
77
78 # go mod edit -module
79 cd $WORK/m
80 go mod init a.a/b/c
81 go mod edit -module x.x/y/z
82 cmpenv go.mod go.mod.edit
83
84 # golang.org/issue/30513: don't require go-gettable module paths.
85 cd $WORK/local
86 go mod init foo
87 go mod edit -module local-only -require=other-local@v1.0.0 -replace other-local@v1.0.0=./other
88 cmpenv go.mod go.mod.edit
89
90 # go mod edit -godebug
91 cd $WORK/g
92 cp go.mod.start go.mod
93 go mod edit -godebug key=value
94 cmpenv go.mod go.mod.edit
95 go mod edit -dropgodebug key2
96 cmpenv go.mod go.mod.edit
97 go mod edit -dropgodebug key
98 cmpenv go.mod go.mod.start
99
100 # go mod edit -tool
101 cd $WORK/h
102 cp go.mod.start go.mod
103 go mod edit -tool example.com/tool
104 cmpenv go.mod go.mod.edit
105 go mod edit -droptool example.com/tool2
106 cmpenv go.mod go.mod.edit
107 go mod edit -droptool example.com/tool
108 cmpenv go.mod go.mod.start
109
110 # go mod edit -ignore
111 cd $WORK/i
112 cp go.mod.start go.mod
113 go mod edit -ignore example.com/ignore
114 cmpenv go.mod go.mod.edit
115 go mod edit -dropignore example.com/ignore2
116 cmpenv go.mod go.mod.edit
117 go mod edit -dropignore example.com/ignore
118 cmpenv go.mod go.mod.start
119
120 -- x.go --
121 package x
122
123 -- w/w.go --
124 package w
125
126 -- $WORK/go.mod.init --
127 module x.x/y/z
128
129 go $goversion
130 -- $WORK/go.mod.edit1 --
131 module x.x/y/z
132
133 go $goversion
134
135 require x.1 v1.0.0
136
137 exclude (
138 x.1 v1.2.0
139 x.1 v1.2.1
140 x.1 v2.0.0+incompatible
141 )
142
143 replace (
144 x.1 v1.3.0 => y.1 v1.4.0
145 x.1 v1.4.0 => ../z
146 )
147
148 retract (
149 v1.6.0
150 [v1.3.0, v1.4.0]
151 [v1.1.0, v1.2.0]
152 v1.0.0
153 )
154 -- $WORK/go.mod.edit2 --
155 module x.x/y/z
156
157 go $goversion
158
159 exclude x.1 v1.2.0
160
161 replace x.1 v1.4.0 => ../z
162
163 retract (
164 v1.6.0
165 [v1.3.0, v1.4.0]
166 )
167
168 require x.3 v1.99.0
169 -- $WORK/go.mod.json --
170 {
171 "Module": {
172 "Path": "x.x/y/z"
173 },
174 "Go": "$goversion",
175 "Require": [
176 {
177 "Path": "x.3",
178 "Version": "v1.99.0"
179 }
180 ],
181 "Exclude": [
182 {
183 "Path": "x.1",
184 "Version": "v1.2.0"
185 }
186 ],
187 "Replace": [
188 {
189 "Old": {
190 "Path": "x.1",
191 "Version": "v1.4.0"
192 },
193 "New": {
194 "Path": "../z"
195 }
196 }
197 ],
198 "Retract": [
199 {
200 "Low": "v1.6.0",
201 "High": "v1.6.0"
202 },
203 {
204 "Low": "v1.3.0",
205 "High": "v1.4.0"
206 }
207 ]
208 }
209 -- $WORK/go.mod.edit3 --
210 module x.x/y/z
211
212 go $goversion
213
214 exclude x.1 v1.2.0
215
216 replace (
217 x.1 v1.3.0 => y.1/v2 v2.3.5
218 x.1 v1.4.0 => y.1/v2 v2.3.5
219 )
220
221 retract (
222 v1.6.0
223 [v1.3.0, v1.4.0]
224 )
225
226 require x.3 v1.99.0
227 -- $WORK/go.mod.edit4 --
228 module x.x/y/z
229
230 go $goversion
231
232 exclude x.1 v1.2.0
233
234 replace x.1 => y.1/v2 v2.3.6
235
236 retract (
237 v1.6.0
238 [v1.3.0, v1.4.0]
239 )
240
241 require x.3 v1.99.0
242 -- $WORK/go.mod.edit5 --
243 module x.x/y/z
244
245 go $goversion
246
247 exclude x.1 v1.2.0
248
249 retract (
250 v1.6.0
251 [v1.3.0, v1.4.0]
252 )
253
254 require x.3 v1.99.0
255 -- $WORK/go.mod.edit6 --
256 module x.x/y/z
257
258 go $goversion
259
260 exclude x.1 v1.2.0
261
262 retract (
263 v1.6.0
264 [v1.3.0, v1.4.0]
265 )
266
267 require x.3 v1.99.0
268
269 replace x.1 => ../y.1/@v2
270 -- $WORK/local/go.mod.edit --
271 module local-only
272
273 go $goversion
274
275 require other-local v1.0.0
276
277 replace other-local v1.0.0 => ./other
278 -- $WORK/go.mod.badfmt --
279 module x.x/y/z
280
281 go 1.10
282
283 exclude x.1 v1.2.0
284
285 replace x.1 => y.1/v2 v2.3.6
286
287 require x.3 v1.99.0
288
289 retract [ "v1.8.1" , "v1.8.2" ]
290 -- $WORK/go.mod.goodfmt --
291 module x.x/y/z
292
293 go 1.10
294
295 exclude x.1 v1.2.0
296
297 replace x.1 => y.1/v2 v2.3.6
298
299 require x.3 v1.99.0
300
301 retract [v1.8.1, v1.8.2]
302 -- $WORK/m/go.mod.edit --
303 module x.x/y/z
304
305 go $goversion
306 -- $WORK/go.mod.retractrationale --
307 module x.x/y/z
308
309 go 1.15
310
311 // a
312 retract v1.0.0
313
314 // b
315 retract (
316 v1.0.1
317 v1.0.2 // c
318 )
319 -- $WORK/go.mod.retractrationale.json --
320 {
321 "Module": {
322 "Path": "x.x/y/z"
323 },
324 "Go": "1.15",
325 "Retract": [
326 {
327 "Low": "v1.0.0",
328 "High": "v1.0.0",
329 "Rationale": "a"
330 },
331 {
332 "Low": "v1.0.1",
333 "High": "v1.0.1",
334 "Rationale": "b"
335 },
336 {
337 "Low": "v1.0.2",
338 "High": "v1.0.2",
339 "Rationale": "c"
340 }
341 ]
342 }
343 -- $WORK/go.mod.deprecation --
344 // Deprecated: and the new one is not ready yet
345 module m
346 -- $WORK/go.mod.deprecation.json --
347 {
348 "Module": {
349 "Path": "m",
350 "Deprecated": "and the new one is not ready yet"
351 }
352 }
353 -- $WORK/go.mod.empty --
354 -- $WORK/go.mod.empty.json --
355 {
356 "Module": {
357 "Path": ""
358 }
359 }
360 -- $WORK/g/go.mod.start --
361 module g
362
363 go 1.10
364 -- $WORK/g/go.mod.edit --
365 module g
366
367 go 1.10
368
369 godebug key=value
370 -- $WORK/h/go.mod.start --
371 module g
372
373 go 1.24
374 -- $WORK/h/go.mod.edit --
375 module g
376
377 go 1.24
378
379 tool example.com/tool
380 -- $WORK/i/go.mod.start --
381 module g
382
383 go 1.24
384 -- $WORK/i/go.mod.edit --
385 module g
386
387 go 1.24
388
389 ignore example.com/ignore
390
View as plain text