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.
9 package importgraph_test
17 "golang.org/x/tools/go/packages/packagestest"
18 "golang.org/x/tools/refactor/importgraph"
20 _ "crypto/hmac" // just for test, below
23 const this = "golang.org/x/tools/refactor/importgraph"
25 func TestBuild(t *testing.T) {
26 exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
27 {Name: "golang.org/x/tools/refactor/importgraph", Files: packagestest.MustCopyFileTree(".")}})
28 defer exported.Cleanup()
31 for _, env := range exported.Config.Env {
32 if !strings.HasPrefix(env, "GOPATH=") {
35 gopath = strings.TrimPrefix(env, "GOPATH=")
38 t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
41 var buildContext = build.Default
42 buildContext.GOPATH = gopath
44 forward, reverse, errors := importgraph.Build(&buildContext)
47 // We throw in crypto/hmac to prove that external test files
48 // (such as this one) are inspected.
49 for _, p := range []string{"go/build", "testing", "crypto/hmac"} {
50 if !forward[this][p] {
51 t.Errorf("forward[importgraph][%s] not found", p)
53 if !reverse[p][this] {
54 t.Errorf("reverse[%s][importgraph] not found", p)
58 // Test non-existent direct edges
59 for _, p := range []string{"errors", "reflect"} {
61 t.Errorf("unexpected: forward[importgraph][%s] found", p)
64 t.Errorf("unexpected: reverse[%s][importgraph] found", p)
68 // Test Search is reflexive.
69 if !forward.Search(this)[this] {
70 t.Errorf("irreflexive: forward.Search(importgraph)[importgraph] not found")
72 if !reverse.Search(this)[this] {
73 t.Errorf("irrefexive: reverse.Search(importgraph)[importgraph] not found")
76 // Test Search is transitive. (There is no direct edge to these packages.)
77 for _, p := range []string{"errors", "reflect", "unsafe"} {
78 if !forward.Search(this)[p] {
79 t.Errorf("intransitive: forward.Search(importgraph)[%s] not found", p)
81 if !reverse.Search(p)[this] {
82 t.Errorf("intransitive: reverse.Search(%s)[importgraph] not found", p)
86 // Test strongly-connected components. Because A's external
87 // test package can depend on B, and vice versa, most of the
88 // standard libraries are mutually dependent when their external
89 // tests are considered.
91 // For any nodes x, y in the same SCC, y appears in the results
92 // of both forward and reverse searches starting from x
93 if !forward.Search("fmt")["io"] ||
94 !forward.Search("io")["fmt"] ||
95 !reverse.Search("fmt")["io"] ||
96 !reverse.Search("io")["fmt"] {
97 t.Errorf("fmt and io are not mutually reachable despite being in the same SCC")
102 for path, err := range errors {
103 t.Logf("%s: %s", path, err)
105 printSorted := func(direction string, g importgraph.Graph, start string) {
108 for pkg := range g.Search(start) {
109 pkgs = append(pkgs, pkg)
112 for _, pkg := range pkgs {
116 printSorted("forward", forward, this)
117 printSorted("reverse", reverse, this)