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