github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/testdata/script/mod_outside.txt (about)

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