...

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

Documentation: cmd/go/testdata/script

     1# This test demonstrates dependency resolution when the main module imports a
     2# new package from a previously-test-only dependency.
     3#
     4# When lazy loading is active, the loader will not load dependencies of any
     5# module whose packages are *only* imported by tests outside the main module. If
     6# the main module is changed to import a package from such a module, the
     7# dependencies of that module will need to be reloaded.
     8
     9# The import graph used in this test looks like:
    10#
    11# m ---- a
    12#  \     |
    13#   \    a_test ---- b/x
    14#    \
    15#      --------------b/y (new) ---- c
    16#
    17# Where b/x and b/y are disjoint packages, but both contained in module b.
    18#
    19# The module dependency graph initially looks like:
    20#
    21# m ---- a.1 ---- b.1 ---- c.1
    22#
    23# This configuration is similar to that used in mod_lazy_new_import,
    24# but the new import is from what is initially a test-only dependency.
    25
    26# Control case: in Go 1.14, the original go.mod is tidy,
    27# and the dependency on c is eagerly loaded.
    28
    29cp go.mod go.mod.orig
    30go mod tidy
    31cmp go.mod.orig go.mod
    32
    33go list -m all
    34stdout '^a v0.1.0 '
    35stdout '^b v0.1.0 '
    36stdout '^c v0.1.0 '
    37
    38# After adding a new import of b/y,
    39# the import of c from b/y should resolve to the version required by b.
    40
    41cp m.go m.go.orig
    42cp m.go.new m.go
    43go mod tidy
    44cmp go.mod.new go.mod
    45
    46go list -m all
    47stdout '^a v0.1.0 '
    48stdout '^b v0.1.0 '
    49stdout '^c v0.1.0 '
    50
    51# With lazy loading, the go.mod requirements are the same,
    52# but the dependency on c is initially pruned out.
    53
    54cp m.go.orig m.go
    55cp go.mod.orig go.mod
    56go mod edit -go=1.17
    57go mod edit -go=1.17 go.mod.new
    58
    59cp go.mod go.mod.orig
    60go mod tidy
    61cmp go.mod.orig go.mod
    62
    63go list -m all
    64stdout '^a v0.1.0 '
    65stdout '^b v0.1.0 '
    66! stdout '^c '
    67
    68# After adding a new direct import of b/y,
    69# the existing version of b should be promoted to a root,
    70# bringing the version of c required by b into the build list.
    71
    72cp m.go.new m.go
    73go mod tidy
    74cmp go.mod.lazy go.mod
    75
    76go list -m all
    77stdout '^a v0.1.0 '
    78stdout '^b v0.1.0 '
    79stdout '^c v0.1.0 '
    80
    81-- m.go --
    82package main
    83
    84import (
    85	"fmt"
    86
    87	_ "a"  // a_test imports b/x.
    88)
    89
    90func main() {
    91}
    92-- m.go.new --
    93package main
    94
    95import (
    96	"fmt"
    97
    98	_ "a"  // a_test imports b/x.
    99	"b/y"  // This is a new import, not yet reflected in the go.mod file.
   100)
   101
   102func main() {
   103	fmt.Println(b.CVersion())
   104}
   105-- go.mod --
   106module m
   107
   108go 1.14
   109
   110require a v0.1.0
   111
   112replace (
   113	a v0.1.0 => ./a1
   114	b v0.1.0 => ./b1
   115	c v0.1.0 => ./c1
   116	c v0.2.0 => ./c2
   117)
   118-- go.mod.new --
   119module m
   120
   121go 1.14
   122
   123require (
   124	a v0.1.0
   125	b v0.1.0
   126)
   127
   128replace (
   129	a v0.1.0 => ./a1
   130	b v0.1.0 => ./b1
   131	c v0.1.0 => ./c1
   132	c v0.2.0 => ./c2
   133)
   134-- go.mod.lazy --
   135module m
   136
   137go 1.17
   138
   139require (
   140	a v0.1.0
   141	b v0.1.0
   142)
   143
   144require c v0.1.0 // indirect
   145
   146replace (
   147	a v0.1.0 => ./a1
   148	b v0.1.0 => ./b1
   149	c v0.1.0 => ./c1
   150	c v0.2.0 => ./c2
   151)
   152-- a1/go.mod --
   153module a
   154
   155go 1.17
   156
   157require b v0.1.0
   158-- a1/a.go --
   159package a
   160-- a1/a_test.go --
   161package a_test
   162
   163import _ "b/x"
   164-- b1/go.mod --
   165module b
   166
   167go 1.17
   168
   169require c v0.1.0
   170-- b1/x/x.go --
   171package x
   172-- b1/y/y.go --
   173package y
   174
   175import "c"
   176
   177func CVersion() string {
   178	return c.Version
   179}
   180-- c1/go.mod --
   181module c
   182
   183go 1.17
   184-- c1/c.go --
   185package c
   186
   187const Version = "v0.1.0"
   188-- c2/go.mod --
   189This file should be unused.
   190-- c2/c.go --
   191This file should be unused.

View as plain text