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.
5 // Incomplete std lib sources on Android.
10 package importgraph_test
18 "golang.org/x/tools/go/packages/packagestest"
19 "golang.org/x/tools/refactor/importgraph"
21 _ "crypto/hmac" // just for test, below
24 const this = "golang.org/x/tools/refactor/importgraph"
26 func TestBuild(t *testing.T) {
27 exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
28 {Name: "golang.org/x/tools/refactor/importgraph", Files: packagestest.MustCopyFileTree(".")}})
29 defer exported.Cleanup()
32 for _, env := range exported.Config.Env {
33 if !strings.HasPrefix(env, "GOPATH=") {
36 gopath = strings.TrimPrefix(env, "GOPATH=")
39 t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
42 var buildContext = build.Default
43 buildContext.GOPATH = gopath
45 forward, reverse, errors := importgraph.Build(&buildContext)
48 // We throw in crypto/hmac to prove that external test files
49 // (such as this one) are inspected.
50 for _, p := range []string{"go/build", "testing", "crypto/hmac"} {
51 if !forward[this][p] {
52 t.Errorf("forward[importgraph][%s] not found", p)
54 if !reverse[p][this] {
55 t.Errorf("reverse[%s][importgraph] not found", p)
59 // Test non-existent direct edges
60 for _, p := range []string{"errors", "reflect"} {
62 t.Errorf("unexpected: forward[importgraph][%s] found", p)
65 t.Errorf("unexpected: reverse[%s][importgraph] found", p)
69 // Test Search is reflexive.
70 if !forward.Search(this)[this] {
71 t.Errorf("irreflexive: forward.Search(importgraph)[importgraph] not found")
73 if !reverse.Search(this)[this] {
74 t.Errorf("irrefexive: reverse.Search(importgraph)[importgraph] not found")
77 // Test Search is transitive. (There is no direct edge to these packages.)
78 for _, p := range []string{"errors", "reflect", "unsafe"} {
79 if !forward.Search(this)[p] {
80 t.Errorf("intransitive: forward.Search(importgraph)[%s] not found", p)
82 if !reverse.Search(p)[this] {
83 t.Errorf("intransitive: reverse.Search(%s)[importgraph] not found", p)
87 // Test strongly-connected components. Because A's external
88 // test package can depend on B, and vice versa, most of the
89 // standard libraries are mutually dependent when their external
90 // tests are considered.
92 // For any nodes x, y in the same SCC, y appears in the results
93 // of both forward and reverse searches starting from x
94 if !forward.Search("fmt")["io"] ||
95 !forward.Search("io")["fmt"] ||
96 !reverse.Search("fmt")["io"] ||
97 !reverse.Search("io")["fmt"] {
98 t.Errorf("fmt and io are not mutually reachable despite being in the same SCC")
103 for path, err := range errors {
104 t.Logf("%s: %s", path, err)
106 printSorted := func(direction string, g importgraph.Graph, start string) {
109 for pkg := range g.Search(start) {
110 pkgs = append(pkgs, pkg)
113 for _, pkg := range pkgs {
117 printSorted("forward", forward, this)
118 printSorted("reverse", reverse, this)