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