...
1# This test illustrates the relationship between the 'all' pattern and
2# the dependencies of the main module.
3
4# The package import graph used in this test looks like:
5#
6# main --------- a --------- b
7# | |
8# | a_test ---- c
9# | |
10# | c_test ---- d
11# |
12# main_test ---- t --------- u
13# |
14# t_test ---- w
15# |
16# w_test ---- x
17#
18# main/testonly_test ---- q --------- r
19# |
20# q_test ---- s
21#
22# And the module dependency graph looks like:
23#
24# main --- a.1 ---- b.1
25# \ \ \
26# \ \ c.1 -- d.1
27# \ \
28# \ t.1 ---- u.1
29# \ \
30# \ w.1 -- x.1
31# \
32# q.1 ---- r.1
33# \
34# s.1
35
36env PKGFMT='{{if .Module}}{{.ImportPath}}{{end}}'
37env MODFMT='{{.Path}}'
38
39
40# 'go list -deps' lists packages and tests in the main module,
41# along with their transitive dependencies.
42
43go list -f $PKGFMT -deps ./...
44stdout -count=4 '^.'
45stdout '^example.com/a$'
46stdout '^example.com/b$'
47stdout '^example.com/main$'
48stdout '^example.com/main/testonly'
49
50
51# 'go list -deps -test' lists transitive imports of tests and non-tests in the
52# main module.
53
54go list -f $PKGFMT -deps -test ./...
55stdout -count=13 '^.'
56stdout '^example.com/a$'
57stdout '^example.com/b$'
58stdout '^example.com/main$'
59stdout '^example.com/main.test$'
60stdout '^example.com/main \[example.com/main.test\]$'
61stdout '^example.com/main_test \[example.com/main.test\]$'
62stdout '^example.com/main/testonly$'
63stdout '^example.com/main/testonly.test$'
64stdout '^example.com/main/testonly_test \[example.com/main/testonly.test\]$'
65stdout '^example.com/q$'
66stdout '^example.com/r$'
67stdout '^example.com/t$'
68stdout '^example.com/u$'
69
70
71# 'go list all' lists the fixpoint of iterating 'go list -deps -test' starting
72# with the packages in the main module, then reducing to only the non-test
73# variants of those packages.
74
75go list -f $PKGFMT all
76stdout -count=13 '^.'
77stdout '^example.com/a$'
78stdout '^example.com/b$'
79stdout '^example.com/c$'
80stdout '^example.com/d$'
81stdout '^example.com/main$'
82stdout '^example.com/main/testonly$'
83stdout '^example.com/q$'
84stdout '^example.com/r$'
85stdout '^example.com/s$'
86stdout '^example.com/t$'
87stdout '^example.com/u$'
88stdout '^example.com/w$'
89stdout '^example.com/x$'
90
91
92# 'go list -test all' is equivalent to 'go list -test $(go list all)'
93# and both should include tests for every package in 'all'.
94
95go list -test -f $PKGFMT example.com/a example.com/b example.com/c example.com/d example.com/main example.com/main/testonly example.com/q example.com/r example.com/s example.com/t example.com/u example.com/w example.com/x
96cp stdout list-test-explicit.txt
97
98go list -test -f $PKGFMT all
99cmp stdout list-test-explicit.txt
100stdout -count=36 '^.'
101stdout '^example.com/a$'
102stdout '^example.com/b$'
103stdout '^example.com/c$'
104stdout '^example.com/d$'
105stdout '^example.com/main$'
106stdout '^example.com/main/testonly$'
107stdout '^example.com/q$'
108stdout '^example.com/r$'
109stdout '^example.com/s$'
110stdout '^example.com/t$'
111stdout '^example.com/u$'
112stdout '^example.com/w$'
113stdout '^example.com/x$'
114stdout '^example.com/a.test$'
115stdout '^example.com/a_test \[example.com/a.test\]$'
116stdout '^example.com/b.test$'
117stdout '^example.com/b_test \[example.com/b.test\]$'
118stdout '^example.com/c.test$'
119stdout '^example.com/c_test \[example.com/c.test\]$'
120stdout '^example.com/main.test$'
121stdout '^example.com/main \[example.com/main.test\]$'
122stdout '^example.com/main_test \[example.com/main.test\]$'
123stdout '^example.com/main/testonly.test$'
124stdout '^example.com/main/testonly_test \[example.com/main/testonly.test\]$'
125stdout '^example.com/q.test$'
126stdout '^example.com/q_test \[example.com/q.test\]$'
127stdout '^example.com/r.test$'
128stdout '^example.com/r_test \[example.com/r.test\]$'
129stdout '^example.com/s.test$'
130stdout '^example.com/s_test \[example.com/s.test\]$'
131stdout '^example.com/t.test$'
132stdout '^example.com/t_test \[example.com/t.test\]$'
133stdout '^example.com/u.test$'
134stdout '^example.com/u_test \[example.com/u.test\]$'
135stdout '^example.com/w.test$'
136stdout '^example.com/w_test \[example.com/w.test\]$'
137
138
139# 'go list -m all' covers the packages in 'go list -test -deps all'.
140
141go list -m -f $MODFMT all
142stdout -count=12 '^.'
143stdout '^example.com/a$'
144stdout '^example.com/b$'
145stdout '^example.com/c$'
146stdout '^example.com/d$'
147stdout '^example.com/main$'
148stdout '^example.com/q$'
149stdout '^example.com/r$'
150stdout '^example.com/s$'
151stdout '^example.com/t$'
152stdout '^example.com/u$'
153stdout '^example.com/w$'
154stdout '^example.com/x$'
155
156
157# 'go mod vendor' copies in only the packages transitively imported by the main
158# module, and omits their tests. As a result, the 'all' and '...' patterns
159# report fewer packages when using '-mod=vendor'.
160
161go mod vendor
162
163go list -f $PKGFMT -mod=vendor all
164stdout -count=8 '^.'
165stdout '^example.com/a$'
166stdout '^example.com/b$'
167stdout '^example.com/main$'
168stdout '^example.com/main/testonly$'
169stdout '^example.com/q$'
170stdout '^example.com/r$'
171stdout '^example.com/t$'
172stdout '^example.com/u$'
173
174go list -test -f $PKGFMT -mod=vendor all
175stdout -count=13 '^.'
176stdout '^example.com/a$'
177stdout '^example.com/b$'
178stdout '^example.com/main$'
179stdout '^example.com/main/testonly$'
180stdout '^example.com/q$'
181stdout '^example.com/r$'
182stdout '^example.com/t$'
183stdout '^example.com/u$'
184stdout '^example.com/main.test$'
185stdout '^example.com/main \[example.com/main.test\]$'
186stdout '^example.com/main_test \[example.com/main.test\]$'
187stdout '^example.com/main/testonly.test$'
188stdout '^example.com/main/testonly_test \[example.com/main/testonly.test\]$'
189
190rm vendor
191
192# Convert all modules to go 1.17 to enable lazy loading.
193go mod edit -go=1.17 a/go.mod
194go mod edit -go=1.17 b/go.mod
195go mod edit -go=1.17 c/go.mod
196go mod edit -go=1.17 d/go.mod
197go mod edit -go=1.17 q/go.mod
198go mod edit -go=1.17 r/go.mod
199go mod edit -go=1.17 s/go.mod
200go mod edit -go=1.17 t/go.mod
201go mod edit -go=1.17 u/go.mod
202go mod edit -go=1.17 w/go.mod
203go mod edit -go=1.17 x/go.mod
204go mod edit -go=1.17
205cmp go.mod go.mod.beforetidy
206go mod tidy
207cmp go.mod go.mod.aftertidy
208
209# With lazy loading, 'go list all' with neither -mod=vendor nor -test should
210# match -mod=vendor without -test in 1.15.
211
212go list -f $PKGFMT all
213stdout -count=8 '^.'
214stdout '^example.com/a$'
215stdout '^example.com/b$'
216stdout '^example.com/main$'
217stdout '^example.com/main/testonly$'
218stdout '^example.com/q$'
219stdout '^example.com/r$'
220stdout '^example.com/t$'
221stdout '^example.com/u$'
222
223# 'go list -test all' should expand that to include the test variants of the
224# packages in 'all', but not the dependencies of outside tests.
225
226go list -test -f $PKGFMT all
227stdout -count=25 '^.'
228stdout '^example.com/a$'
229stdout '^example.com/b$'
230stdout '^example.com/main$'
231stdout '^example.com/main/testonly$'
232stdout '^example.com/q$'
233stdout '^example.com/r$'
234stdout '^example.com/t$'
235stdout '^example.com/u$'
236stdout '^example.com/a.test$'
237stdout '^example.com/a_test \[example.com/a.test\]$'
238stdout '^example.com/b.test$'
239stdout '^example.com/b_test \[example.com/b.test\]$'
240stdout '^example.com/main.test$'
241stdout '^example.com/main \[example.com/main.test\]$'
242stdout '^example.com/main_test \[example.com/main.test\]$'
243stdout '^example.com/main/testonly.test$'
244stdout '^example.com/main/testonly_test \[example.com/main/testonly.test\]$'
245stdout '^example.com/q.test$'
246stdout '^example.com/q_test \[example.com/q.test\]$'
247stdout '^example.com/r.test$'
248stdout '^example.com/r_test \[example.com/r.test\]$'
249stdout '^example.com/t.test$'
250stdout '^example.com/t_test \[example.com/t.test\]$'
251stdout '^example.com/u.test$'
252stdout '^example.com/u_test \[example.com/u.test\]$'
253
254# 'go list -test -deps all' should include the dependencies of those tests,
255# but not the tests of the dependencies of outside tests.
256
257go list -test -deps -f $PKGFMT all
258stdout -count=28 '^.'
259stdout '^example.com/a$'
260stdout '^example.com/b$'
261stdout '^example.com/c$'
262stdout '^example.com/main$'
263stdout '^example.com/main/testonly$'
264stdout '^example.com/q$'
265stdout '^example.com/r$'
266stdout '^example.com/s$'
267stdout '^example.com/t$'
268stdout '^example.com/u$'
269stdout '^example.com/w$'
270stdout '^example.com/a.test$'
271stdout '^example.com/a_test \[example.com/a.test\]$'
272stdout '^example.com/b.test$'
273stdout '^example.com/b_test \[example.com/b.test\]$'
274stdout '^example.com/main.test$'
275stdout '^example.com/main \[example.com/main.test\]$'
276stdout '^example.com/main_test \[example.com/main.test\]$'
277stdout '^example.com/main/testonly.test$'
278stdout '^example.com/main/testonly_test \[example.com/main/testonly.test\]$'
279stdout '^example.com/q.test$'
280stdout '^example.com/q_test \[example.com/q.test\]$'
281stdout '^example.com/r.test$'
282stdout '^example.com/r_test \[example.com/r.test\]$'
283stdout '^example.com/t.test$'
284stdout '^example.com/t_test \[example.com/t.test\]$'
285stdout '^example.com/u.test$'
286stdout '^example.com/u_test \[example.com/u.test\]$'
287
288# 'go list -m all' should cover all of the modules providing packages in
289# 'go list -test -deps all', but should exclude modules d and x,
290# which are not relevant to the main module and are outside of the
291# lazy-loading horizon.
292
293go list -m -f $MODFMT all
294stdout -count=10 '^.'
295stdout '^example.com/a$'
296stdout '^example.com/b$'
297stdout '^example.com/c$'
298! stdout '^example.com/d$'
299stdout '^example.com/main$'
300stdout '^example.com/q$'
301stdout '^example.com/r$'
302stdout '^example.com/s$'
303stdout '^example.com/t$'
304stdout '^example.com/u$'
305stdout '^example.com/w$'
306! stdout '^example.com/x$'
307
308-- go.mod --
309module example.com/main
310
311// Note: this go.mod file initially specifies go 1.15,
312// but includes some redundant roots so that it
313// also already obeys the 1.17 lazy loading invariants.
314go 1.15
315
316require (
317 example.com/a v0.1.0
318 example.com/b v0.1.0 // indirect
319 example.com/q v0.1.0
320 example.com/r v0.1.0 // indirect
321 example.com/t v0.1.0
322 example.com/u v0.1.0 // indirect
323)
324
325replace (
326 example.com/a v0.1.0 => ./a
327 example.com/b v0.1.0 => ./b
328 example.com/c v0.1.0 => ./c
329 example.com/d v0.1.0 => ./d
330 example.com/q v0.1.0 => ./q
331 example.com/r v0.1.0 => ./r
332 example.com/s v0.1.0 => ./s
333 example.com/t v0.1.0 => ./t
334 example.com/u v0.1.0 => ./u
335 example.com/w v0.1.0 => ./w
336 example.com/x v0.1.0 => ./x
337)
338-- main.go --
339package main
340
341import _ "example.com/a"
342
343func main() {}
344-- main_test.go --
345package main_test
346
347import _ "example.com/t"
348-- testonly/testonly_test.go --
349package testonly_test
350
351import _ "example.com/q"
352-- a/go.mod --
353module example.com/a
354
355go 1.15
356
357require (
358 example.com/b v0.1.0
359 example.com/c v0.1.0
360)
361-- a/a.go --
362package a
363
364import _ "example.com/b"
365-- a/a_test.go --
366package a_test
367
368import _ "example.com/c"
369-- b/go.mod --
370module example.com/b
371
372go 1.15
373-- b/b.go --
374package b
375-- b/b_test.go --
376package b_test
377-- c/go.mod --
378module example.com/c
379
380go 1.15
381
382require example.com/d v0.1.0
383-- c/c.go --
384package c
385-- c/c_test.go --
386package c_test
387
388import _ "example.com/d"
389-- d/go.mod --
390module example.com/d
391
392go 1.15
393-- d/d.go --
394package d
395-- q/go.mod --
396module example.com/q
397
398go 1.15
399
400require (
401 example.com/r v0.1.0
402 example.com/s v0.1.0
403)
404-- q/q.go --
405package q
406import _ "example.com/r"
407-- q/q_test.go --
408package q_test
409import _ "example.com/s"
410-- r/go.mod --
411module example.com/r
412
413go 1.15
414-- r/r.go --
415package r
416-- r/r_test.go --
417package r_test
418-- s/go.mod --
419module example.com/s
420
421go 1.15
422-- s/s.go --
423package s
424-- s/s_test.go --
425package s_test
426-- t/go.mod --
427module example.com/t
428
429go 1.15
430
431require (
432 example.com/u v0.1.0
433 example.com/w v0.1.0
434)
435-- t/t.go --
436package t
437
438import _ "example.com/u"
439-- t/t_test.go --
440package t_test
441
442import _ "example.com/w"
443-- u/go.mod --
444module example.com/u
445
446go 1.15
447-- u/u.go --
448package u
449-- u/u_test.go --
450package u_test
451-- w/go.mod --
452module example.com/w
453
454go 1.15
455
456require example.com/x v0.1.0
457-- w/w.go --
458package w
459-- w/w_test.go --
460package w_test
461
462import _ "example.com/x"
463-- x/go.mod --
464module example.com/x
465
466go 1.15
467-- x/x.go --
468package x
469-- go.mod.beforetidy --
470module example.com/main
471
472// Note: this go.mod file initially specifies go 1.15,
473// but includes some redundant roots so that it
474// also already obeys the 1.17 lazy loading invariants.
475go 1.17
476
477require (
478 example.com/a v0.1.0
479 example.com/b v0.1.0 // indirect
480 example.com/q v0.1.0
481 example.com/r v0.1.0 // indirect
482 example.com/t v0.1.0
483 example.com/u v0.1.0 // indirect
484)
485
486replace (
487 example.com/a v0.1.0 => ./a
488 example.com/b v0.1.0 => ./b
489 example.com/c v0.1.0 => ./c
490 example.com/d v0.1.0 => ./d
491 example.com/q v0.1.0 => ./q
492 example.com/r v0.1.0 => ./r
493 example.com/s v0.1.0 => ./s
494 example.com/t v0.1.0 => ./t
495 example.com/u v0.1.0 => ./u
496 example.com/w v0.1.0 => ./w
497 example.com/x v0.1.0 => ./x
498)
499-- go.mod.aftertidy --
500module example.com/main
501
502// Note: this go.mod file initially specifies go 1.15,
503// but includes some redundant roots so that it
504// also already obeys the 1.17 lazy loading invariants.
505go 1.17
506
507require (
508 example.com/a v0.1.0
509 example.com/q v0.1.0
510 example.com/t v0.1.0
511)
512
513require (
514 example.com/b v0.1.0 // indirect
515 example.com/r v0.1.0 // indirect
516 example.com/u v0.1.0 // indirect
517)
518
519replace (
520 example.com/a v0.1.0 => ./a
521 example.com/b v0.1.0 => ./b
522 example.com/c v0.1.0 => ./c
523 example.com/d v0.1.0 => ./d
524 example.com/q v0.1.0 => ./q
525 example.com/r v0.1.0 => ./r
526 example.com/s v0.1.0 => ./s
527 example.com/t v0.1.0 => ./t
528 example.com/u v0.1.0 => ./u
529 example.com/w v0.1.0 => ./w
530 example.com/x v0.1.0 => ./x
531)
View as plain text