From 0bf5a322479700bd87eed5ce5e010ba369fe7161 Mon Sep 17 00:00:00 2001 From: Suzy Mueller Date: Fri, 20 Jul 2018 11:40:29 -0400 Subject: [PATCH] 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 TryBot-Result: Gobot Gobot Reviewed-by: Michael Matloob Reviewed-by: Ian Cottrell --- go/packages/packages_test.go | 60 +++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/go/packages/packages_test.go b/go/packages/packages_test.go index cba7bd0c..d475f710 100644 --- a/go/packages/packages_test.go +++ b/go/packages/packages_test.go @@ -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" `,