...

Text file src/cmd/go/testdata/script/modfile_flag.txt

Documentation: cmd/go/testdata/script

     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
    52# 'go mod vendor' should work.
    53go mod vendor
    54exists vendor
    55
    56# Automatic vendoring should be broken by editing an explicit requirement
    57# in the alternate go.mod file.
    58go mod edit -require rsc.io/quote@v1.5.1
    59! go list .
    60go list -mod=mod
    61rm 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.
    67cp go.alt.mod go.gen.mod
    68env OLD_GOFLAGS=$GOFLAGS
    69env GOFLAGS=-modfile=go.gen.mod
    70go generate -modfile=go.alt.mod .
    71env GOFLAGS=$OLD_GOFLAGS
    72grep 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.
    77cmp go.mod go.mod.orig
    78cmp 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.
    83cp go.alt.mod goaltmod
    84! go mod tidy -modfile=goaltmod
    85stderr '-modfile=goaltmod: file does not have .mod extension'
    86
    87-- go.mod --
    88ʕ◔ϖ◔ʔ
    89-- go.sum --
    90ʕ◔ϖ◔ʔ
    91-- use.go --
    92package main
    93
    94import _ "rsc.io/quote"
    95-- gen.go --
    96//go:generate go mod edit -exclude example.com/exclude@v1.0.0
    97
    98package main

View as plain text