1
0
mirror of https://github.com/golang/go synced 2024-11-18 22:34:45 -07:00

go/packages: add tests for vendoring

Test that the import graph for packages in the GOPATH that use vendoring
are correct and are keyed by the import path as it appears in the source
file.

Change-Id: Ibf9a516b4bfcc9f2d45f349d2ae49a0dbf958e30
Reviewed-on: https://go-review.googlesource.com/125298
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Suzy Mueller 2018-07-20 11:40:29 -04:00
parent 54429628ee
commit 0bf5a32247

View File

@ -32,7 +32,6 @@ var usesOldGolist = false
//
// - When the tests fail, make them print a 'cd & load' command
// that will allow the maintainer to interact with the failing scenario.
// - vendoring
// - errors in go-list metadata
// - a foo.test package that cannot be built for some reason (e.g.
// import error) will result in a JSON blob with no name and a
@ -211,6 +210,65 @@ func TestMetadataImportGraph(t *testing.T) {
}
}
func TestVendorImports(t *testing.T) {
tmp, cleanup := makeTree(t, map[string]string{
"src/a/a.go": `package a; import _ "b"; import _ "c";`,
"src/a/vendor/b/b.go": `package b; import _ "c"`,
"src/c/c.go": `package c; import _ "b"`,
"src/c/vendor/b/b.go": `package b`,
})
defer cleanup()
cfg := &packages.Config{
Mode: packages.LoadImports,
Env: append(os.Environ(), "GOPATH="+tmp),
}
initial, err := packages.Load(cfg, "a", "c")
if err != nil {
t.Fatal(err)
}
graph, all := importGraph(initial)
wantGraph := `
* a
a/vendor/b
* c
c/vendor/b
a -> a/vendor/b
a -> c
a/vendor/b -> c
c -> c/vendor/b
`[1:]
if graph != wantGraph {
t.Errorf("wrong import graph: got <<%s>>, want <<%s>>", graph, wantGraph)
}
for _, test := range []struct {
pattern string
wantImports string
}{
{"a", "b:a/vendor/b c:c"},
{"c", "b:c/vendor/b"},
{"a/vendor/b", "c:c"},
{"c/vendor/b", ""},
} {
// Test the import paths.
pkg := all[test.pattern]
if imports := strings.Join(imports(pkg), " "); imports != test.wantImports {
t.Errorf("package %q: got %s, want %s", test.pattern, imports, test.wantImports)
}
}
}
func imports(p *packages.Package) []string {
keys := make([]string, 0, len(p.Imports))
for k, v := range p.Imports {
keys = append(keys, fmt.Sprintf("%s:%s", k, v.ID))
}
sort.Strings(keys)
return keys
}
func TestOptionsDir(t *testing.T) {
tmp, cleanup := makeTree(t, map[string]string{
"src/a/a.go": `package a; const Name = "a" `,