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