...

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

Documentation: cmd/go/testdata/script

     1# Integration test for golang.org/issue/33848: automatically check and use vendored packages.
     2
     3env GO111MODULE=on
     4
     5[short] skip
     6
     7cd $WORK/auto
     8cp go.mod go.mod.orig
     9cp $WORK/modules-1.13.txt $WORK/auto/modules.txt
    10
    11# An explicit -mod=vendor should force use of the vendor directory.
    12env GOFLAGS=-mod=vendor
    13
    14# Pass -e to permit an error: tools.go imports a main package
    15# "example.com/printversion".
    16# TODO(#59186): investigate why it didn't fail without -e.
    17go list -f {{.Dir}} -tags tools -e all
    18stdout '^'$WORK'[/\\]auto$'
    19stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
    20stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
    21
    22! go list -m all
    23stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
    24
    25! go list -m -f '{{.Dir}}' all
    26stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
    27
    28# An explicit -mod=mod should force the vendor directory to be ignored.
    29env GOFLAGS=-mod=mod
    30
    31go list -f {{.Dir}} -tags tools -e all
    32stdout '^'$WORK'[/\\]auto$'
    33stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    34stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    35
    36go list -m all
    37stdout '^example.com/auto$'
    38stdout 'example.com/printversion v1.0.0'
    39stdout 'example.com/version v1.0.0'
    40
    41go list -m -f '{{.Dir}}' all
    42stdout '^'$WORK'[/\\]auto$'
    43stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    44stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    45
    46# If the main module's "go" directive says 1.13, we should default to -mod=mod.
    47env GOFLAGS=
    48go mod edit -go=1.13
    49
    50go list -f {{.Dir}} -tags tools -e all
    51stdout '^'$WORK'[/\\]auto$'
    52stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    53stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    54
    55go list -m -f '{{.Dir}}' all
    56stdout '^'$WORK'[/\\]auto$'
    57stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    58stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    59
    60# A 'go 1.14' directive in the main module's go.mod file should enable
    61# -mod=vendor by default, along with stronger checks for consistency
    62# between the go.mod file and vendor/modules.txt.
    63# A 'go 1.13' vendor/modules.txt file is not usually sufficient
    64# to pass those checks.
    65go mod edit -go=1.14
    66
    67! go list -f {{.Dir}} -tags tools all
    68stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
    69stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
    70stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
    71stderr '^\texample.com/version@v1.2.0: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
    72stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
    73
    74# Module-specific subcommands should continue to load the full module graph.
    75go mod graph
    76stdout '^example.com/printversion@v1.0.0 example.com/version@v1.0.0$'
    77
    78# An explicit -mod=mod should still force the vendor directory to be ignored.
    79env GOFLAGS=-mod=mod
    80
    81go list -f {{.Dir}} -tags tools -e all
    82stdout '^'$WORK'[/\\]auto$'
    83stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    84stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    85
    86go list -m all
    87stdout '^example.com/auto$'
    88stdout 'example.com/printversion v1.0.0'
    89stdout 'example.com/version v1.0.0'
    90
    91go list -m -f '{{.Dir}}' all
    92stdout '^'$WORK'[/\\]auto$'
    93stdout '^'$GOPATH'[/\\]pkg[/\\]mod[/\\]example.com[/\\]printversion@v1.0.0$'
    94stdout '^'$WORK'[/\\]auto[/\\]replacement-version$'
    95
    96# 'go mod vendor' should repair vendor/modules.txt so that the implicit
    97# -mod=vendor works again.
    98env GOFLAGS=
    99
   100go mod edit -go=1.14
   101go mod vendor
   102
   103go list -f {{.Dir}} -tags tools -e all
   104stdout '^'$WORK'[/\\]auto$'
   105stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   106stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   107
   108# ...but 'go list -m' should continue to fail, this time without
   109# referring to a -mod default that the user didn't set.
   110! go list -m all
   111stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
   112
   113! go list -m -f '{{.Dir}}' all
   114stderr 'go: can''t compute ''all'' using the vendor directory\n\t\(Use -mod=mod or -mod=readonly to bypass.\)'
   115
   116
   117# 'go mod init' should work if there is already a GOPATH-mode vendor directory
   118# present. If there are no module dependencies, -mod=vendor should be used by
   119# default and should not fail the consistency check even though no module
   120# information is present.
   121
   122rm go.mod
   123rm vendor/modules.txt
   124
   125go mod init example.com/auto
   126go list -f {{.Dir}} -tags tools -e all
   127stdout '^'$WORK'[/\\]auto$'
   128stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   129stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   130
   131# If information about dependencies is added to a 1.14 go.mod file, subsequent
   132# list commands should error out if vendor/modules.txt is missing or incomplete.
   133
   134cp go.mod.orig go.mod
   135go mod edit -go=1.14
   136! go list -f {{.Dir}} -tags tools -e all
   137stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
   138stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but not marked as explicit in vendor/modules.txt'
   139stderr '^\texample.com/unused: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
   140stderr '^\texample.com/version@v1.2.0: is replaced in go.mod, but not marked as replaced in vendor/modules.txt'
   141stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
   142
   143# If -mod=vendor is set, limited consistency checks should apply even when
   144# the go version is 1.13 or earlier.
   145# An incomplete or missing vendor/modules.txt should resolve the vendored packages...
   146go mod edit -go=1.13
   147go list -mod=vendor -f {{.Dir}} -tags tools -e all
   148stdout '^'$WORK'[/\\]auto$'
   149stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   150stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   151
   152# ...but a version mismatch for an explicit dependency should be noticed.
   153cp $WORK/modules-bad-1.13.txt vendor/modules.txt
   154! go list -mod=vendor -f {{.Dir}} -tags tools all
   155stderr '^go: inconsistent vendoring in '$WORK[/\\]auto':$'
   156stderr '^\texample.com/printversion@v1.0.0: is explicitly required in go.mod, but vendor/modules.txt indicates example.com/printversion@v1.1.0$'
   157stderr '^\tTo ignore the vendor directory, use -mod=readonly or -mod=mod.\n\tTo sync the vendor directory, run:\n\t\tgo mod vendor$'
   158
   159# If the go version is still 1.13, 'go mod vendor' should write a
   160# matching vendor/modules.txt containing the corrected 1.13 data.
   161go mod vendor
   162cmp $WORK/modules-1.13.txt vendor/modules.txt
   163
   164go list -mod=vendor -f {{.Dir}} -tags tools -e all
   165stdout '^'$WORK'[/\\]auto$'
   166stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   167stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   168
   169# When the version is upgraded to 1.14, 'go mod vendor' should write a
   170# vendor/modules.txt with the updated 1.14 annotations.
   171go mod edit -go=1.14
   172go mod vendor
   173cmp $WORK/modules-1.14.txt vendor/modules.txt
   174
   175# Then, -mod=vendor should kick in automatically and succeed.
   176go list -f {{.Dir}} -tags tools -e all
   177stdout '^'$WORK'[/\\]auto$'
   178stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]printversion$'
   179stdout '^'$WORK'[/\\]auto[/\\]vendor[/\\]example.com[/\\]version$'
   180
   181# 'go get' should update from the network or module cache,
   182# even if a vendor directory is present.
   183go get example.com/version@v1.1.0
   184! go list -f {{.Dir}} -tags tools all
   185stderr '^go: inconsistent vendoring'
   186
   187-- $WORK/auto/go.mod --
   188module example.com/auto
   189
   190go 1.13
   191
   192require example.com/printversion v1.0.0
   193
   194replace (
   195	example.com/unused => nonexistent.example.com/unused v1.0.0-whatever
   196	example.com/version v1.0.0 => ./replacement-version
   197	example.com/version v1.2.0 => nonexistent.example.com/version v1.2.0
   198)
   199-- $WORK/auto/tools.go --
   200// +build tools
   201
   202package auto
   203
   204import _ "example.com/printversion"
   205-- $WORK/auto/auto.go --
   206package auto
   207-- $WORK/auto/replacement-version/go.mod --
   208module example.com/version
   209-- $WORK/auto/replacement-version/version.go --
   210package version
   211
   212const V = "v1.0.0-replaced"
   213-- $WORK/modules-1.14.txt --
   214# example.com/printversion v1.0.0
   215## explicit
   216example.com/printversion
   217# example.com/version v1.0.0 => ./replacement-version
   218example.com/version
   219# example.com/unused => nonexistent.example.com/unused v1.0.0-whatever
   220# example.com/version v1.2.0 => nonexistent.example.com/version v1.2.0
   221-- $WORK/modules-1.13.txt --
   222# example.com/printversion v1.0.0
   223example.com/printversion
   224# example.com/version v1.0.0 => ./replacement-version
   225example.com/version
   226-- $WORK/modules-bad-1.13.txt --
   227# example.com/printversion v1.1.0
   228example.com/printversion
   229# example.com/version v1.1.0
   230example.com/version
   231-- $WORK/auto/vendor/example.com/printversion/go.mod --
   232module example.com/printversion
   233
   234require example.com/version v1.0.0
   235replace example.com/version v1.0.0 => ../oops v0.0.0
   236exclude example.com/version v1.0.1
   237-- $WORK/auto/vendor/example.com/printversion/printversion.go --
   238package main
   239
   240import (
   241	"fmt"
   242	"os"
   243	"runtime/debug"
   244
   245	_ "example.com/version"
   246)
   247
   248func main() {
   249	info, _ := debug.ReadBuildInfo()
   250	fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
   251	fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
   252	for _, m := range info.Deps {
   253		fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
   254	}
   255}
   256-- $WORK/auto/vendor/example.com/version/version.go --
   257package version
   258
   259const V = "v1.0.0-replaced"

View as plain text