github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/cmd/go/vendor_test.go (about)

     1  // Copyright 2015 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Tests for vendoring semantics.
     6  
     7  package main_test
     8  
     9  import (
    10  	"bytes"
    11  	"fmt"
    12  	"internal/testenv"
    13  	"path/filepath"
    14  	"regexp"
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  func TestVendorImports(t *testing.T) {
    20  	tg := testgo(t)
    21  	defer tg.cleanup()
    22  	tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
    23  	tg.setenv("GO15VENDOREXPERIMENT", "1")
    24  	tg.run("list", "-f", "{{.ImportPath}} {{.Imports}}", "vend/...")
    25  	want := `
    26  		vend [vend/vendor/p r]
    27  		vend/hello [fmt vend/vendor/strings]
    28  		vend/subdir [vend/vendor/p r]
    29  		vend/vendor/p []
    30  		vend/vendor/q []
    31  		vend/vendor/strings []
    32  		vend/x [vend/x/vendor/p vend/vendor/q vend/x/vendor/r]
    33  		vend/x/invalid [vend/x/invalid/vendor/foo]
    34  		vend/x/vendor/p []
    35  		vend/x/vendor/p/p [notfound]
    36  		vend/x/vendor/r []
    37  	`
    38  	want = strings.Replace(want+"\t", "\n\t\t", "\n", -1)
    39  	want = strings.TrimPrefix(want, "\n")
    40  
    41  	have := tg.stdout.String()
    42  
    43  	if have != want {
    44  		t.Errorf("incorrect go list output:\n%s", diffSortedOutputs(have, want))
    45  	}
    46  }
    47  
    48  func TestVendorRun(t *testing.T) {
    49  	tg := testgo(t)
    50  	defer tg.cleanup()
    51  	tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
    52  	tg.setenv("GO15VENDOREXPERIMENT", "1")
    53  	tg.cd(filepath.Join(tg.pwd(), "testdata/src/vend/hello"))
    54  	tg.run("run", "hello.go")
    55  	tg.grepStdout("hello, world", "missing hello world output")
    56  }
    57  
    58  func TestVendorGOPATH(t *testing.T) {
    59  	tg := testgo(t)
    60  	defer tg.cleanup()
    61  	changeVolume := func(s string, f func(s string) string) string {
    62  		vol := filepath.VolumeName(s)
    63  		return f(vol) + s[len(vol):]
    64  	}
    65  	gopath := changeVolume(filepath.Join(tg.pwd(), "testdata"), strings.ToLower)
    66  	tg.setenv("GOPATH", gopath)
    67  	tg.setenv("GO15VENDOREXPERIMENT", "1")
    68  	cd := changeVolume(filepath.Join(tg.pwd(), "testdata/src/vend/hello"), strings.ToUpper)
    69  	tg.cd(cd)
    70  	tg.run("run", "hello.go")
    71  	tg.grepStdout("hello, world", "missing hello world output")
    72  }
    73  
    74  func TestVendorTest(t *testing.T) {
    75  	tg := testgo(t)
    76  	defer tg.cleanup()
    77  	tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
    78  	tg.setenv("GO15VENDOREXPERIMENT", "1")
    79  	tg.cd(filepath.Join(tg.pwd(), "testdata/src/vend/hello"))
    80  	tg.run("test", "-v")
    81  	tg.grepStdout("TestMsgInternal", "missing use in internal test")
    82  	tg.grepStdout("TestMsgExternal", "missing use in external test")
    83  }
    84  
    85  func TestVendorInvalid(t *testing.T) {
    86  	tg := testgo(t)
    87  	defer tg.cleanup()
    88  	tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
    89  	tg.setenv("GO15VENDOREXPERIMENT", "1")
    90  
    91  	tg.runFail("build", "vend/x/invalid")
    92  	tg.grepStderr("must be imported as foo", "missing vendor import error")
    93  }
    94  
    95  func TestVendorImportError(t *testing.T) {
    96  	tg := testgo(t)
    97  	defer tg.cleanup()
    98  	tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata"))
    99  	tg.setenv("GO15VENDOREXPERIMENT", "1")
   100  
   101  	tg.runFail("build", "vend/x/vendor/p/p")
   102  
   103  	re := regexp.MustCompile(`cannot find package "notfound" in any of:
   104  	.*[\\/]testdata[\\/]src[\\/]vend[\\/]x[\\/]vendor[\\/]notfound \(vendor tree\)
   105  	.*[\\/]testdata[\\/]src[\\/]vend[\\/]vendor[\\/]notfound \(vendor tree\)
   106  	.*[\\/]src[\\/]notfound \(from \$GOROOT\)
   107  	.*[\\/]testdata[\\/]src[\\/]notfound \(from \$GOPATH\)`)
   108  
   109  	if !re.MatchString(tg.stderr.String()) {
   110  		t.Errorf("did not find expected search list in error text")
   111  	}
   112  }
   113  
   114  // diffSortedOutput prepares a diff of the already sorted outputs haveText and wantText.
   115  // The diff shows common lines prefixed by a tab, lines present only in haveText
   116  // prefixed by "unexpected: ", and lines present only in wantText prefixed by "missing: ".
   117  func diffSortedOutputs(haveText, wantText string) string {
   118  	var diff bytes.Buffer
   119  	have := splitLines(haveText)
   120  	want := splitLines(wantText)
   121  	for len(have) > 0 || len(want) > 0 {
   122  		if len(want) == 0 || len(have) > 0 && have[0] < want[0] {
   123  			fmt.Fprintf(&diff, "unexpected: %s\n", have[0])
   124  			have = have[1:]
   125  			continue
   126  		}
   127  		if len(have) == 0 || len(want) > 0 && want[0] < have[0] {
   128  			fmt.Fprintf(&diff, "missing: %s\n", want[0])
   129  			want = want[1:]
   130  			continue
   131  		}
   132  		fmt.Fprintf(&diff, "\t%s\n", want[0])
   133  		want = want[1:]
   134  		have = have[1:]
   135  	}
   136  	return diff.String()
   137  }
   138  
   139  func splitLines(s string) []string {
   140  	x := strings.Split(s, "\n")
   141  	if x[len(x)-1] == "" {
   142  		x = x[:len(x)-1]
   143  	}
   144  	return x
   145  }
   146  
   147  func TestVendorGet(t *testing.T) {
   148  	tg := testgo(t)
   149  	defer tg.cleanup()
   150  	tg.tempFile("src/v/m.go", `
   151  		package main
   152  		import ("fmt"; "vendor.org/p")
   153  		func main() {
   154  			fmt.Println(p.C)
   155  		}`)
   156  	tg.tempFile("src/v/m_test.go", `
   157  		package main
   158  		import ("fmt"; "testing"; "vendor.org/p")
   159  		func TestNothing(t *testing.T) {
   160  			fmt.Println(p.C)
   161  		}`)
   162  	tg.tempFile("src/v/vendor/vendor.org/p/p.go", `
   163  		package p
   164  		const C = 1`)
   165  	tg.setenv("GOPATH", tg.path("."))
   166  	tg.setenv("GO15VENDOREXPERIMENT", "1")
   167  	tg.cd(tg.path("src/v"))
   168  	tg.run("run", "m.go")
   169  	tg.run("test")
   170  	tg.run("list", "-f", "{{.Imports}}")
   171  	tg.grepStdout("v/vendor/vendor.org/p", "import not in vendor directory")
   172  	tg.run("list", "-f", "{{.TestImports}}")
   173  	tg.grepStdout("v/vendor/vendor.org/p", "test import not in vendor directory")
   174  	tg.run("get")
   175  	tg.run("get", "-t")
   176  }
   177  
   178  func TestVendorGetUpdate(t *testing.T) {
   179  	testenv.MustHaveExternalNetwork(t)
   180  
   181  	tg := testgo(t)
   182  	defer tg.cleanup()
   183  	tg.makeTempdir()
   184  	tg.setenv("GOPATH", tg.path("."))
   185  	tg.setenv("GO15VENDOREXPERIMENT", "1")
   186  	tg.run("get", "github.com/rsc/go-get-issue-11864")
   187  	tg.run("get", "-u", "github.com/rsc/go-get-issue-11864")
   188  }
   189  
   190  func TestVendorCache(t *testing.T) {
   191  	tg := testgo(t)
   192  	defer tg.cleanup()
   193  	tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/testvendor"))
   194  	tg.setenv("GO15VENDOREXPERIMENT", "1")
   195  	tg.runFail("build", "p")
   196  	tg.grepStderr("must be imported as x", "did not fail to build p")
   197  }
   198  
   199  func TestVendorTest2(t *testing.T) {
   200  	testenv.MustHaveExternalNetwork(t)
   201  
   202  	tg := testgo(t)
   203  	defer tg.cleanup()
   204  	tg.makeTempdir()
   205  	tg.setenv("GOPATH", tg.path("."))
   206  	tg.setenv("GO15VENDOREXPERIMENT", "1")
   207  	tg.run("get", "github.com/rsc/go-get-issue-11864")
   208  
   209  	// build -i should work
   210  	tg.run("build", "-i", "github.com/rsc/go-get-issue-11864")
   211  	tg.run("build", "-i", "github.com/rsc/go-get-issue-11864/t")
   212  
   213  	// test -i should work like build -i (golang.org/issue/11988)
   214  	tg.run("test", "-i", "github.com/rsc/go-get-issue-11864")
   215  	tg.run("test", "-i", "github.com/rsc/go-get-issue-11864/t")
   216  
   217  	// test should work too
   218  	tg.run("test", "github.com/rsc/go-get-issue-11864")
   219  	tg.run("test", "github.com/rsc/go-get-issue-11864/t")
   220  
   221  	// external tests should observe internal test exports (golang.org/issue/11977)
   222  	tg.run("test", "github.com/rsc/go-get-issue-11864/vendor/vendor.org/tx2")
   223  }
   224  
   225  func TestVendorList(t *testing.T) {
   226  	testenv.MustHaveExternalNetwork(t)
   227  
   228  	tg := testgo(t)
   229  	defer tg.cleanup()
   230  	tg.makeTempdir()
   231  	tg.setenv("GOPATH", tg.path("."))
   232  	tg.setenv("GO15VENDOREXPERIMENT", "1")
   233  	tg.run("get", "github.com/rsc/go-get-issue-11864")
   234  
   235  	tg.run("list", "-f", `{{join .TestImports "\n"}}`, "github.com/rsc/go-get-issue-11864/t")
   236  	tg.grepStdout("go-get-issue-11864/vendor/vendor.org/p", "did not find vendor-expanded p")
   237  
   238  	tg.run("list", "-f", `{{join .XTestImports "\n"}}`, "github.com/rsc/go-get-issue-11864/tx")
   239  	tg.grepStdout("go-get-issue-11864/vendor/vendor.org/p", "did not find vendor-expanded p")
   240  
   241  	tg.run("list", "-f", `{{join .XTestImports "\n"}}`, "github.com/rsc/go-get-issue-11864/vendor/vendor.org/tx2")
   242  	tg.grepStdout("go-get-issue-11864/vendor/vendor.org/tx2", "did not find vendor-expanded tx2")
   243  
   244  	tg.run("list", "-f", `{{join .XTestImports "\n"}}`, "github.com/rsc/go-get-issue-11864/vendor/vendor.org/tx3")
   245  	tg.grepStdout("go-get-issue-11864/vendor/vendor.org/tx3", "did not find vendor-expanded tx3")
   246  }
   247  
   248  func TestVendor12156(t *testing.T) {
   249  	// Former index out of range panic.
   250  	tg := testgo(t)
   251  	defer tg.cleanup()
   252  	tg.setenv("GOPATH", filepath.Join(tg.pwd(), "testdata/testvendor2"))
   253  	tg.setenv("GO15VENDOREXPERIMENT", "1")
   254  	tg.cd(filepath.Join(tg.pwd(), "testdata/testvendor2/src/p"))
   255  	tg.runFail("build", "p.go")
   256  	tg.grepStderrNot("panic", "panicked")
   257  	tg.grepStderr(`cannot find package "x"`, "wrong error")
   258  }