1 env GO111MODULE=on
2
3 # Without vendoring, a build should succeed unless -mod=vendor is set.
4 [!short] go build
5 [!short] ! go build -mod=vendor
6
7 # Without vendoring, 'go list' should report the replacement directory for
8 # a package in a replaced module.
9 go list -f {{.Dir}} x
10 stdout 'src[\\/]x'
11
12 # 'go mod vendor' should copy all replaced modules to the vendor directory.
13 go mod vendor -v
14 stderr '^# x v1.0.0 => ./x'
15 stderr '^x'
16 stderr '^# y v1.0.0 => ./y'
17 stderr '^y'
18 stderr '^# z v1.0.0 => ./z'
19 stderr '^z'
20 ! stderr '^w'
21 grep 'a/foo/bar/b\na/foo/bar/c' vendor/modules.txt # must be sorted
22
23 # An explicit '-mod=mod' should ignore the vendor directory.
24 go list -mod=mod -f {{.Dir}} x
25 stdout 'src[\\/]x'
26
27 go list -mod=mod -f {{.Dir}} -m x
28 stdout 'src[\\/]x'
29
30 # An explicit '-mod=vendor' should report package directories within
31 # the vendor directory.
32 go list -mod=vendor -f {{.Dir}} x
33 stdout 'src[\\/]vendor[\\/]x'
34
35 # 'go list -mod=vendor -m' should successfully list vendored modules,
36 # but should not provide a module directory because no directory contains
37 # the complete module.
38 go list -mod=vendor -f '{{.Version}} {{.Dir}}' -m x
39 stdout '^v1.0.0 $'
40
41 # -mod=vendor should cause 'go list' flags that look up versions to fail.
42 ! go list -mod=vendor -versions -m x
43 stderr '^go: can''t determine available versions using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)$'
44 ! go list -mod=vendor -u -m x
45 stderr '^go: can''t determine available upgrades using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)$'
46
47 # 'go list -mod=vendor -m' on a transitive dependency that does not
48 # provide vendored packages should give a helpful error rather than
49 # 'not a known dependency'.
50 ! go list -mod=vendor -f '{{.Version}} {{.Dir}}' -m diamondright
51 stderr 'go: module diamondright: can''t resolve module using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
52
53 # 'go list -mod=mod' should report packages outside the import graph,
54 # but 'go list -mod=vendor' should error out for them.
55 go list -mod=mod -f {{.Dir}} w
56 stdout 'src[\\/]w'
57 ! go list -mod=vendor -f {{.Dir}} w
58 stderr 'src[\\/]vendor[\\/]w'
59
60 go list -mod=mod -f {{.Dir}} diamondright
61 stdout 'src[\\/]diamondright'
62
63 # Test dependencies should not be copied.
64 ! exists vendor/x/testdata
65 ! exists vendor/a/foo/bar/b/ignored.go
66 ! exists vendor/a/foo/bar/b/main_test.go
67
68 # Licenses and other metadata for each module should be copied
69 # if any package within their module is copied.
70 exists vendor/a/foo/AUTHORS.txt
71 exists vendor/a/foo/CONTRIBUTORS
72 exists vendor/a/foo/LICENSE
73 exists vendor/a/foo/PATENTS
74 exists vendor/a/foo/COPYING
75 exists vendor/a/foo/COPYLEFT
76 exists vendor/x/NOTICE!
77 exists vendor/mysite/myname/mypkg/LICENSE.txt
78
79 ! exists vendor/a/foo/licensed-to-kill
80 ! exists vendor/w
81 ! exists vendor/w/LICENSE
82 ! exists vendor/x/x2
83 ! exists vendor/x/x2/LICENSE
84
85 # 'go mod vendor' should work with an alternative vendor directory if the -o flag is provided.
86 go mod vendor -v -o alternative-vendor-dir
87 exists alternative-vendor-dir/modules.txt
88 exists alternative-vendor-dir/a/foo/LICENSE
89
90 # 'go mod vendor' should interpret paths relative to the current working directory when the -o flag is provided.
91 mkdir dir1
92 mkdir dir2
93
94 cd dir1
95 go mod vendor -v -o relative-vendor-dir
96
97 go mod vendor -v -o ../dir2/relative-vendor-dir
98
99 cd ..
100 exists dir1/relative-vendor-dir/modules.txt
101 exists dir1/relative-vendor-dir/a/foo/LICENSE
102 exists dir2/relative-vendor-dir/modules.txt
103 exists dir2/relative-vendor-dir/a/foo/LICENSE
104
105 # 'go mod vendor' should fall back to the default 'vendor' directory when an empty argument is passed to the -o flag
106 # the same behavior should be exhibited both on the module root directory, as well as nested subdirectories
107
108 go mod vendor -v -o ''
109 exists vendor/modules.txt
110
111 env GOFLAGS=-o=foo
112 go mod vendor -v -o ''
113 exists vendor/modules.txt
114 env GOFLAGS=''
115
116 mkdir -p nested/dir
117 cd nested/dir
118 go mod vendor -v -o ''
119 ! exists vendor/
120 exists ../../vendor/modules.txt
121 cd ../..
122
123 # 'go mod vendor' should work with absolute paths as well
124 go mod vendor -v -o $WORK/tmp/absolute-vendor-dir
125 exists $WORK/tmp/absolute-vendor-dir/modules.txt
126
127 [short] stop
128
129 # 'go build' and 'go test' using vendored packages should succeed.
130 go build -mod=mod
131 go build -mod=vendor
132 go test -mod=vendor . ./subdir
133 go test -mod=vendor ./...
134 go fmt -mod=vendor ./...
135
136 -- go.mod --
137 module m
138
139 go 1.13
140
141 require (
142 a v1.0.0
143 diamondroot v0.0.0
144 mysite/myname/mypkg v1.0.0
145 w v1.0.0 // indirect
146 x v1.0.0
147 y v1.0.0
148 z v1.0.0
149 )
150
151 replace (
152 a v1.0.0 => ./a
153 diamondleft => ./diamondleft
154 diamondpoint => ./diamondpoint
155 diamondright => ./diamondright
156 diamondroot => ./diamondroot
157 mysite/myname/mypkg v1.0.0 => ./mypkg
158 w v1.0.0 => ./w
159 x v1.0.0 => ./x
160 y v1.0.0 => ./y
161 z v1.0.0 => ./z
162 )
163
164 -- a/foo/AUTHORS.txt --
165 -- a/foo/CONTRIBUTORS --
166 -- a/foo/LICENSE --
167 -- a/foo/PATENTS --
168 -- a/foo/COPYING --
169 -- a/foo/COPYLEFT --
170 -- a/foo/licensed-to-kill --
171 -- w/LICENSE --
172 -- x/NOTICE! --
173 -- x/x2/LICENSE --
174 -- mypkg/LICENSE.txt --
175
176 -- a/foo/bar/b/main.go --
177 package b
178 -- a/foo/bar/b/ignored.go --
179 // This file is intended for use with "go run"; it isn't really part of the package.
180
181 // +build ignore
182
183 package main
184
185 func main() {}
186 -- a/foo/bar/b/main_test.go --
187 package b
188
189 import (
190 "os"
191 "testing"
192 )
193
194 func TestDir(t *testing.T) {
195 if _, err := os.Stat("../testdata/1"); err != nil {
196 t.Fatalf("testdata: %v", err)
197 }
198 }
199 -- a/foo/bar/c/main.go --
200 package c
201 import _ "a/foo/bar/b"
202 -- a/foo/bar/c/main_test.go --
203 package c
204
205 import (
206 "os"
207 "testing"
208 )
209
210 func TestDir(t *testing.T) {
211 if _, err := os.Stat("../../../testdata/1"); err != nil {
212 t.Fatalf("testdata: %v", err)
213 }
214 if _, err := os.Stat("./testdata/1"); err != nil {
215 t.Fatalf("testdata: %v", err)
216 }
217 }
218 -- a/foo/bar/c/testdata/1 --
219 -- a/foo/bar/testdata/1 --
220 -- a/go.mod --
221 module a
222 -- a/main.go --
223 package a
224 -- a/main_test.go --
225 package a
226
227 import (
228 "os"
229 "testing"
230 )
231
232 func TestDir(t *testing.T) {
233 if _, err := os.Stat("./testdata/1"); err != nil {
234 t.Fatalf("testdata: %v", err)
235 }
236 }
237 -- a/testdata/1 --
238 -- appengine.go --
239 // +build appengine
240
241 package m
242
243 import _ "appengine"
244 import _ "appengine/datastore"
245 -- mypkg/go.mod --
246 module me
247 -- mypkg/mydir/d.go --
248 package mydir
249 -- subdir/v1_test.go --
250 package m
251
252 import _ "mysite/myname/mypkg/mydir"
253 -- testdata1.go --
254 package m
255
256 import _ "a"
257 -- testdata2.go --
258 package m
259
260 import _ "a/foo/bar/c"
261 -- v1.go --
262 package m
263
264 import _ "x"
265 -- v2.go --
266 // +build abc
267
268 package mMmMmMm
269
270 import _ "y"
271 -- v3.go --
272 // +build !abc
273
274 package m
275
276 import _ "z"
277 -- v4.go --
278 // +build notmytag
279
280 package m
281
282 import _ "x/x1"
283 -- importdiamond.go --
284 package m
285
286 import _ "diamondroot"
287 -- w/go.mod --
288 module w
289 -- w/w.go --
290 package w
291 -- x/go.mod --
292 module x
293 -- x/testdata/x.txt --
294 placeholder - want directory with no go files
295 -- x/x.go --
296 package x
297 -- x/x1/x1.go --
298 // +build notmytag
299
300 package x1
301 -- x/x2/dummy.txt --
302 dummy
303 -- x/x_test.go --
304 package x
305
306 import _ "w"
307 -- y/go.mod --
308 module y
309 -- y/y.go --
310 package y
311 -- z/go.mod --
312 module z
313 -- z/z.go --
314 package z
315
316 -- diamondroot/go.mod --
317 module diamondroot
318
319 require (
320 diamondleft v0.0.0
321 diamondright v0.0.0
322 )
323 -- diamondroot/x.go --
324 package diamondroot
325
326 import _ "diamondleft"
327 -- diamondroot/unused/unused.go --
328 package unused
329
330 import _ "diamondright"
331 -- diamondleft/go.mod --
332 module diamondleft
333
334 require (
335 diamondpoint v0.0.0
336 )
337 -- diamondleft/x.go --
338 package diamondleft
339
340 import _ "diamondpoint"
341 -- diamondright/go.mod --
342 module diamondright
343
344 require (
345 diamondpoint v0.0.0
346 )
347 -- diamondright/x.go --
348 package diamondright
349
350 import _ "diamondpoint"
351 -- diamondpoint/go.mod --
352 module diamondpoint
353 -- diamondpoint/x.go --
354 package diamondpoint
355
View as plain text