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