...
1# Test overlays that affect go.mod files
2
3# The go.mod file can exist only in the overlay.
4cd $WORK/gopath/src/no-go-mod
5go list -overlay overlay.json .
6stdout example.com/simple
7
8# Check content of overlaid go.mod is used.
9cd $WORK/gopath/src/overlay-go-mod
10go list -overlay overlay.json .
11stdout use.this/module/name
12
13# Check content of overlaid go.mod in a replacement module is used.
14# The go.mod in the replacement module is missing a requirement
15# that the overlay has, so it will fail to list without the overlay.
16cd $WORK/gopath/src/overlay-replaced-go-mod
17! go list -deps .
18go list -deps -overlay overlay.json .
19
20# Overlaid go.mod is not rewritten by 'go get'.
21cd $WORK/gopath/src/get-doesnt-add-dep
22cp $WORK/overlay/get_doesnt_add_dep_go_mod $WORK/want_go_mod
23! go get -overlay overlay.json .
24stderr '^go: updates to go.mod needed, but go.mod is part of the overlay specified with -overlay$'
25cmp $WORK/overlay/get_doesnt_add_dep_go_mod $WORK/want_go_mod
26
27# Content of overlaid go.sum is used.
28# The go.sum in the module directory has garbage values for its
29# hashes, but the overlaid file has the correct values. If
30# the correct go.sum is used with the overlay, 'go get .' should
31# not report a security error.
32cd $WORK/gopath/src/overlay-sum-used
33! go get .
34stderr 'SECURITY ERROR'
35! go mod verify
36stderr 'SECURITY ERROR'
37go get -overlay overlay.json .
38go mod verify -overlay overlay.json
39# Overlaid go.sum is not rewritten.
40# Copy an incomplete file to the overlay file, and expect an error
41# attempting to update the file
42cp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums
43! go get -overlay overlay.json .
44stderr '^go: updates to go.sum needed, but go.sum is part of the overlay specified with -overlay$'
45cmp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums
46! go mod tidy -overlay overlay.json
47stderr '^go: updates to go.sum needed, but go.sum is part of the overlay specified with -overlay$'
48cmp incomplete-sum-file $WORK/overlay/overlay-sum-used-correct-sums
49
50# -overlay works with -modfile.
51# There's an empty go.mod file in the directory, and the file alternate.mod is
52# overlaid to the true go.mod file, so the -modfile flag and the overlay
53# mechanism need to work together to determine the name of the module.
54cd $WORK/gopath/src/overlay-and-dash-modfile
55go list -modfile=alternate.mod -overlay overlay.json .
56stdout 'found.the/module'
57# Even with -modfile, overlaid files can't be opened for write.
58! go get -modfile=alternate.mod -overlay overlay.json rsc.io/quote
59stderr '^go: updates to go.mod needed, but go.mod is part of the overlay specified with -overlay$'
60
61# Carving out a module by adding an overlaid go.mod file
62cd $WORK/gopath/src/carve
63go list ./... # without an overlay, hasmod is carved out and nomod isn't
64stdout carve/nomod
65! stdout carve/hasmod
66go list -overlay overlay_carve_module.json ./... # The overlay carves out nomod, leaving nothing
67! stdout .
68stderr 'matched no packages'
69go list -overlay overlay_uncarve_module.json ./... # The overlay uncarves out hasmod
70stdout carve/nomod
71stdout carve/hasmod
72
73# Carving out a module by adding an overlaid go.mod file and using
74# -modfile to write to that file.
75cd $WORK/gopath/src/carve2/nomod
76go list -overlay overlay.json all
77! stdout ^carve2$
78stdout ^carve2/nomod$
79# Editing go.mod file fails because overlay is read only
80! go get -overlay overlay.json rsc.io/quote
81stderr '^go: updates to go.mod needed, but go.mod is part of the overlay specified with -overlay$'
82! grep rsc.io/quote $WORK/overlay/carve2-nomod-go.mod
83# Editing go.mod file succeeds because we use -modfile to redirect to same file
84go get -overlay overlay.json -modfile $WORK/overlay/carve2-nomod-go.mod rsc.io/quote
85grep rsc.io/quote $WORK/overlay/carve2-nomod-go.mod
86
87-- no-go-mod/file.go --
88package simple
89-- no-go-mod/overlay.json --
90{
91 "Replace": {
92 "go.mod": "../../../overlay/simple_go_mod"
93 }
94}
95-- $WORK/overlay/simple_go_mod --
96module example.com/simple
97-- overlay-go-mod/file.go --
98package name
99-- overlay-go-mod/go.mod --
100module dont.use/this/module/name
101-- overlay-go-mod/overlay.json --
102{
103 "Replace": {
104 "go.mod": "../../../overlay/use_this_go_mod"
105 }
106}
107-- $WORK/overlay/use_this_go_mod --
108module use.this/module/name
109-- overlay-replaced-go-mod/go.mod --
110module m
111
112go 1.15
113
114require replaced/mod v1.0.0
115replace replaced/mod v1.0.0 => ../replaced-mod
116replace dep/mod v1.0.0 => ../dep-mod
117-- overlay-replaced-go-mod/source.go --
118package m
119
120import "replaced/mod/foo"
121
122func main() {
123 foo.f()
124}
125-- overlay-replaced-go-mod/overlay.json --
126{
127 "Replace": {
128 "../replaced-mod/go.mod": "../../../overlay/replacement_module_go_mod"
129 }
130}
131-- replaced-mod/go.mod --
132module replaced/mod
133-- replaced-mod/foo/foo.go --
134package foo
135
136import "dep/mod/foo"
137
138func f() { foo.g() }
139-- dep-mod/go.mod --
140invalid
141-- dep-mod/foo/foo.go --
142package foo
143
144func g() { fmt.Println("hello") }
145-- $WORK/overlay/replacement_module_go_mod --
146module replaced/mod
147
148require dep/mod v1.0.0
149
150-- get-doesnt-add-dep/overlay.json --
151{
152 "Replace": {
153 "go.mod": "../../../overlay/get_doesnt_add_dep_go_mod"
154 }
155}
156-- get-doesnt-add-dep/p.go --
157package p
158
159import "dependency/mod"
160
161func f() { mod.G() }
162-- get-doesnt-add-dep-dependency/go.mod --
163module dependency/mod
164-- get-doesnt-add-dep-dependency/mod.go --
165package mod
166
167func G() {}
168-- $WORK/overlay/get_doesnt_add_dep_go_mod --
169module get.doesnt/add/dep
170
171replace dependency/mod v1.0.0 => ../get-doesnt-add-dep-dependency
172-- overlay-sum-used/go.mod --
173module overlay.sum/used
174
175require rsc.io/quote v1.5.0
176-- overlay-sum-used/p.go --
177package p
178
179import "rsc.io/quote"
180
181func f() string {
182 return quote.Hello()
183}
184-- overlay-sum-used/incomplete-sum-file --
185golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:pvCbr/wm8HzDD3fVywevekufpn6tCGPY3spdHeZJEsw=
186rsc.io/quote v1.5.0 h1:6fJa6E+wGadANKkUMlZ0DhXFpoKlslOQDCo259XtdIE=
187rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
188-- overlay-sum-used/overlay.json --
189{
190 "Replace": {
191 "go.sum": "../../../overlay/overlay-sum-used-correct-sums"
192 }
193}
194-- overlay-sum-used/go.sum --
195golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:garbage+hash
196golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:garbage+hash
197rsc.io/quote v1.5.0 h1:garbage+hash
198rsc.io/quote v1.5.0/go.mod h1:garbage+hash
199rsc.io/sampler v1.3.0 h1:garbage+hash
200rsc.io/sampler v1.3.0/go.mod h1:garbage+hash
201-- $WORK/overlay/overlay-sum-used-correct-sums --
202golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c h1:pvCbr/wm8HzDD3fVywevekufpn6tCGPY3spdHeZJEsw=
203golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
204rsc.io/quote v1.5.0 h1:6fJa6E+wGadANKkUMlZ0DhXFpoKlslOQDCo259XtdIE=
205rsc.io/quote v1.5.0/go.mod h1:LzX7hefJvL54yjefDEDHNONDjII0t9xZLPXsUe+TKr0=
206rsc.io/sampler v1.3.0 h1:HLGR/BgEtI3r0uymSP/nl2uPLsUnNJX8toRyhfpBTII=
207rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
208-- overlay-and-dash-modfile/p.go --
209package module
210-- overlay-and-dash-modfile/go.mod --
211-- overlay-and-dash-modfile/overlay.json --
212{
213 "Replace": {
214 "alternate.mod": "../../../overlay/overlay-and-dash-modfile-alternate-mod"
215 }
216}
217-- $WORK/overlay/overlay-and-dash-modfile-alternate-mod --
218module found.the/module
219-- carve/go.mod --
220module carve
221-- carve/overlay_carve_module.json --
222{
223 "Replace": {
224 "nomod/go.mod": "../../../overlay/carve-nomod-go-mod"
225 }
226}
227-- carve/overlay_uncarve_module.json --
228{
229 "Replace": {
230 "hasmod/go.mod": ""
231 }
232}
233-- carve/hasmod/a.go --
234package hasmod
235-- carve/hasmod/go.mod --
236module carve/hasmod
237-- carve/nomod/b.go --
238package nomod
239-- $WORK/overlay/carve-nomod-go-mod --
240module carve/nomod
241-- carve2/go.mod --
242module carve2
243-- carve2/p.go --
244package p
245-- carve2/nomod/overlay.json --
246{
247 "Replace": {
248 "go.mod": "../../../../overlay/carve2-nomod-go.mod"
249 }
250}
251-- carve2/nomod/b.go --
252package nomod
253-- $WORK/overlay/carve2-nomod-go.mod --
254module carve2/nomod
View as plain text