1 # Tests the behavior of the -modfile flag in commands that support it.
2 # The go.mod file exists but should not be read or written.
3 # Same with go.sum.
4
5 env GOFLAGS=-modfile=go.alt.mod
6 cp go.mod go.mod.orig
7 cp go.sum go.sum.orig
8
9
10 # go mod init should create a new file, even though go.mod already exists.
11 go mod init example.com/m
12 grep example.com/m go.alt.mod
13
14 # 'go env GOMOD' should print the path to the real file.
15 # 'go env' does not recognize the '-modfile' flag.
16 go env GOMOD
17 stdout '^'$WORK${/}gopath${/}src${/}'go\.mod$'
18
19 # 'go list -m' should print the effective go.mod file as GoMod though.
20 go list -m -f '{{.GoMod}}'
21 stdout '^go.alt.mod$'
22
23 # go mod edit should operate on the alternate file
24 go mod edit -require rsc.io/quote@v1.5.2
25 grep rsc.io/quote go.alt.mod
26
27 # 'go list -m' should add sums to the alternate go.sum.
28 go list -m -mod=mod all
29 grep '^rsc.io/quote v1.5.2/go.mod ' go.alt.sum
30 ! grep '^rsc.io/quote v1.5.2 ' go.alt.sum
31
32 # other 'go mod' commands should work. 'go mod vendor' is tested later.
33 go mod download rsc.io/quote
34 go mod graph
35 stdout rsc.io/quote
36 go mod tidy
37 grep rsc.io/quote go.alt.sum
38 go mod verify
39 go mod why rsc.io/quote
40
41
42 # 'go list' and other commands with build flags should work.
43 # They should update the alternate go.mod when a dependency is missing.
44 go mod edit -droprequire rsc.io/quote
45 go list -mod=mod .
46 grep rsc.io/quote go.alt.mod
47 go build -n -mod=mod .
48 go test -n -mod=mod .
49 go get rsc.io/quote
50
51
52 # 'go mod vendor' should work.
53 go mod vendor
54 exists vendor
55
56 # Automatic vendoring should be broken by editing an explicit requirement
57 # in the alternate go.mod file.
58 go mod edit -require rsc.io/quote@v1.5.1
59 ! go list .
60 go list -mod=mod
61 rm vendor
62
63
64 # 'go generate' should use the alternate file when resolving packages.
65 # Recursive go commands started with 'go generate' should not get an explicitly
66 # passed -modfile, but they should see arguments from GOFLAGS.
67 cp go.alt.mod go.gen.mod
68 env OLD_GOFLAGS=$GOFLAGS
69 env GOFLAGS=-modfile=go.gen.mod
70 go generate -modfile=go.alt.mod .
71 env GOFLAGS=$OLD_GOFLAGS
72 grep example.com/exclude go.gen.mod
73 ! grep example.com/exclude go.alt.mod
74
75
76 # The original files should not have been modified.
77 cmp go.mod go.mod.orig
78 cmp go.sum go.sum.orig
79
80
81 # If the alternate mod file does not have a ".mod" suffix, an error
82 # should be reported.
83 cp go.alt.mod goaltmod
84 ! go mod tidy -modfile=goaltmod
85 stderr '-modfile=goaltmod: file does not have .mod extension'
86
87 -- go.mod --
88 ʕ◔ϖ◔ʔ
89 -- go.sum --
90 ʕ◔ϖ◔ʔ
91 -- use.go --
92 package main
93
94 import _ "rsc.io/quote"
95 -- gen.go --
96 //go:generate go mod edit -exclude example.com/exclude@v1.0.0
97
98 package main
99
View as plain text