github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/cmd/go/testdata/script/mod_outside.txt (about)

     1  env GO111MODULE=on
     2  
     3  # This script tests commands in module mode outside of any module.
     4  #
     5  # First, ensure that we really are in module mode, and that we really don't have
     6  # a go.mod file.
     7  go env GOMOD
     8  stdout 'NUL|/dev/null'
     9  
    10  
    11  # 'go list' without arguments implicitly operates on the current directory,
    12  # which is not in a module.
    13  ! go list
    14  stderr 'cannot find main module'
    15  go list -m
    16  stdout '^command-line-arguments$'
    17  # 'go list' in the working directory should fail even if there is a a 'package
    18  # main' present: without a main module, we do not know its package path.
    19  ! go list ./foo
    20  stderr 'cannot find main module'
    21  
    22  # 'go list all' lists the transitive import graph of the main module,
    23  # which is empty if there is no main module.
    24  go list all
    25  ! stdout .
    26  stderr 'warning: "all" matched no packages'
    27  go list -m all
    28  stderr 'warning: pattern "all" matched no module dependencies'
    29  
    30  # 'go list' on standard-library packages should work, since they do not depend
    31  # on the contents of any module.
    32  go list -deps cmd
    33  stdout '^fmt$'
    34  stdout '^cmd/go$'
    35  
    36  go list $GOROOT/src/fmt
    37  stdout '^fmt$'
    38  
    39  # 'go list' should work with file arguments.
    40  go list ./foo/foo.go
    41  stdout 'command-line-arguments'
    42  
    43  # 'go list -m' with an explicit version should resolve that version.
    44  go list -m example.com/version@latest
    45  stdout 'example.com/version v1.1.0'
    46  
    47  # 'go list -m -versions' should succeed even without an explicit version.
    48  go list -m -versions example.com/version
    49  stdout 'v1.0.0\s+v1.0.1\s+v1.1.0'
    50  
    51  # 'go list -m <mods> all' does not include the dependencies of <mods> in the computation of 'all'.
    52  go list -m example.com/printversion@v1.0.0 all
    53  stdout 'example.com/printversion v1.0.0'
    54  stderr 'warning: pattern "all" matched no module dependencies'
    55  ! stdout 'example.com/version'
    56  
    57  
    58  # 'go clean' should skip the current directory if it isn't in a module.
    59  go clean -n
    60  ! stdout .
    61  ! stderr .
    62  
    63  # 'go mod graph' should not display anything, since there are no active modules.
    64  go mod graph
    65  ! stdout .
    66  ! stderr .
    67  
    68  # 'go mod why' should report that nothing is a dependency.
    69  go mod why -m example.com/version
    70  stdout 'does not need'
    71  
    72  
    73  # 'go mod edit', 'go mod tidy', and 'go mod fmt' should fail:
    74  # there is no go.mod file to edit.
    75  ! go mod tidy
    76  stderr 'cannot find main module'
    77  ! go mod edit -fmt
    78  stderr 'cannot find main module'
    79  ! go mod edit -require example.com/version@v1.0.0
    80  stderr 'cannot find main module'
    81  
    82  
    83  # 'go mod download' should download exactly the requested module without dependencies.
    84  rm -r $GOPATH/pkg/mod/cache/download/example.com
    85  go mod download example.com/printversion@v1.0.0
    86  exists $GOPATH/pkg/mod/cache/download/example.com/printversion/@v/v1.0.0.zip
    87  ! exists $GOPATH/pkg/mod/cache/download/example.com/version/@v/v1.0.0.zip
    88  
    89  # 'go mod vendor' should fail: it starts by clearing the existing vendor
    90  # directory, and we don't know where that is.
    91  ! go mod vendor
    92  stderr 'cannot find main module'
    93  
    94  # 'go mod verify' should succeed: we have no modules to verify.
    95  go mod verify
    96  stdout 'all modules verified'
    97  ! stderr .
    98  
    99  
   100  # 'go get' without arguments implicitly operates on the main module, and thus
   101  # should fail.
   102  ! go get
   103  stderr 'cannot find main module'
   104  ! go get -u
   105  stderr 'cannot find main module'
   106  ! go get -u ./foo
   107  stderr 'cannot find main module'
   108  
   109  # 'go get -u all' upgrades the transitive import graph of the main module,
   110  # which is empty.
   111  go get -u all
   112  ! stdout .
   113  stderr 'warning: "all" matched no packages'
   114  
   115  # 'go get -m' should check the proposed module graph for consistency,
   116  # even though it will not be saved anywhere.
   117  ! go get -m example.com/printversion@v1.0.0 example.com/version@none
   118  stderr 'inconsistent versions'
   119  
   120  # 'go get -d' should download and extract the source code needed to build the requested version.
   121  rm -r $GOPATH/pkg/mod/example.com
   122  go get -d example.com/printversion@v1.0.0
   123  exists $GOPATH/pkg/mod/example.com/printversion@v1.0.0
   124  exists $GOPATH/pkg/mod/example.com/version@v1.0.0
   125  
   126  
   127  # 'go build' without arguments implicitly operates on the current directory, and should fail.
   128  cd foo
   129  ! go build
   130  stderr 'cannot find main module'
   131  cd ..
   132  
   133  # 'go build' of a non-module directory should fail too.
   134  ! go build ./foo
   135  stderr 'cannot find main module'
   136  
   137  # However, 'go build' should succeed for standard-library packages.
   138  go build -n fmt
   139  
   140  
   141  # TODO(golang.org/issue/28992): 'go doc' should document the latest version.
   142  # For now it does not.
   143  ! go doc example.com/version
   144  stderr 'no such package'
   145  
   146  # 'go install' with a version should fail due to syntax.
   147  ! go install example.com/printversion@v1.0.0
   148  stderr 'can only use path@version syntax with'
   149  
   150  
   151  # 'go fmt' should be able to format files outside of a module.
   152  go fmt foo/foo.go
   153  
   154  
   155  # The remainder of the test checks dependencies by linking and running binaries.
   156  [short] stop
   157  
   158  # 'go get' of a binary without a go.mod should install the requested version,
   159  # resolving outside dependencies to the latest available versions.
   160  go get example.com/printversion@v0.1.0
   161  exec ../bin/printversion
   162  stdout 'path is example.com/printversion'
   163  stdout 'main is example.com/printversion v0.1.0'
   164  stdout 'using example.com/version v1.1.0'
   165  
   166  # 'go get' of a versioned binary should build and install the latest version
   167  # using its minimal module requirements, ignoring replacements and exclusions.
   168  go get example.com/printversion
   169  exec ../bin/printversion
   170  stdout 'path is example.com/printversion'
   171  stdout 'main is example.com/printversion v1.0.0'
   172  stdout 'using example.com/version v1.0.0'
   173  
   174  # 'go get -u=patch' should patch dependencies before installing,
   175  # again ignoring replacements and exclusions.
   176  go get -u=patch example.com/printversion@v1.0.0
   177  exec ../bin/printversion
   178  stdout 'path is example.com/printversion'
   179  stdout 'main is example.com/printversion v1.0.0'
   180  stdout 'using example.com/version v1.0.1'
   181  
   182  # 'go install' without a version should install the latest version
   183  # using its minimal module requirements.
   184  go install example.com/printversion
   185  exec ../bin/printversion
   186  stdout 'path is example.com/printversion'
   187  stdout 'main is example.com/printversion v1.0.0'
   188  stdout 'using example.com/version v1.0.0'
   189  
   190  # 'go run' should use 'main' as the effective module and import path.
   191  go run ./foo/foo.go
   192  stdout 'path is command-line-arguments$'
   193  stdout 'main is command-line-arguments \(devel\)'
   194  stdout 'using example.com/version v1.1.0'
   195  
   196  # 'go generate' should work with file arguments.
   197  [exec:touch] go generate ./foo/foo.go
   198  [exec:touch] exists ./foo/gen.txt
   199  
   200  # 'go install' should work with file arguments.
   201  go install ./foo/foo.go
   202  
   203  # 'go test' should work with file arguments.
   204  go test -v ./foo/foo_test.go
   205  stdout 'foo was tested'
   206  
   207  # 'go vet' should work with file arguments.
   208  go vet ./foo/foo.go
   209  
   210  
   211  -- README.txt --
   212  There is no go.mod file in the working directory.
   213  
   214  -- foo/foo.go --
   215  //go:generate touch gen.txt
   216  
   217  package main
   218  
   219  import (
   220  	"fmt"
   221  	"os"
   222  	"runtime/debug"
   223  
   224  	_ "example.com/version"
   225  )
   226  
   227  func main() {
   228  	info, ok := debug.ReadBuildInfo()
   229  	if !ok {
   230  		panic("missing build info")
   231  	}
   232  	fmt.Fprintf(os.Stdout, "path is %s\n", info.Path)
   233  	fmt.Fprintf(os.Stdout, "main is %s %s\n", info.Main.Path, info.Main.Version)
   234  	for _, m := range info.Deps {
   235  		fmt.Fprintf(os.Stdout, "using %s %s\n", m.Path, m.Version)
   236  	}
   237  }
   238  
   239  -- foo/foo_test.go --
   240  package main
   241  
   242  import (
   243  	"fmt"
   244  	"testing"
   245  )
   246  
   247  func TestFoo(t *testing.T) {
   248  	fmt.Println("foo was tested")
   249  }