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