Debian Codegraph @

refactor/importgraph/graph_test.go

raw symbols SCIP
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 // Incomplete std lib sources on Android.
6
7 //go:build !android
8
9 package importgraph_test
10
11 import (
12 "fmt"
13 "go/build"
14 "os"
15 "sort"
16 "strings"
17 "testing"
18
19 "golang.org/x/tools/internal/packagestest"
20 "golang.org/x/tools/refactor/importgraph"
21
22 _ "crypto/hmac" // just for test, below
23 )
24
25 const this = "golang.org/x/tools/refactor/importgraph"
26
27 func TestBuild(t *testing.T) {
28 exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
29 {Name: "golang.org/x/tools/refactor/importgraph", Files: packagestest.MustCopyFileTree(".")}})
30 defer exported.Cleanup()
31
32 var gopath string
33 for _, env := range exported.Config.Env {
34 eq := strings.Index(env, "=")
35 if eq == 0 {
36 // We sometimes see keys with a single leading "=" in the environment on Windows.
37 // TODO(#49886): What is the correct way to parse them in general?
38 eq = strings.Index(env[1:], "=") + 1
39 }
40 if eq < 0 {
41 t.Fatalf("invalid variable in exported.Config.Env: %q", env)
42 }
43 k := env[:eq]
44 v := env[eq+1:]
45 if k == "GOPATH" {
46 gopath = v
47 }
48
49 if os.Getenv(k) == v {
50 continue
51 }
52 defer func(prev string, prevOK bool) {
53 if !prevOK {
54 if err := os.Unsetenv(k); err != nil {
55 t.Fatal(err)
56 }
57 } else {
58 if err := os.Setenv(k, prev); err != nil {
59 t.Fatal(err)
60 }
61 }
62 }(os.LookupEnv(k))
63
64 if err := os.Setenv(k, v); err != nil {
65 t.Fatal(err)
66 }
67 t.Logf("%s=%s", k, v)
68 }
69 if gopath == "" {
70 t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
71 }
72
73 var buildContext = build.Default
74 buildContext.GOPATH = gopath
75 buildContext.Dir = exported.Config.Dir
76
77 forward, reverse, errs := importgraph.Build(&buildContext)
78 for path, err := range errs {
79 t.Errorf("%s: %s", path, err)
80 }
81 if t.Failed() {
82 return
83 }
84
85 // Log the complete graph before the errors, so that the errors are near the
86 // end of the log (where we expect them to be).
87 nodePrinted := map[string]bool{}
88 printNode := func(direction string, from string) {
89 key := fmt.Sprintf("%s[%q]", direction, from)
90 if nodePrinted[key] {
91 return
92 }
93 nodePrinted[key] = true
94
95 var g importgraph.Graph
96 switch direction {
97 case "forward":
98 g = forward
99 case "reverse":
100 g = reverse
101 default:
102 t.Helper()
103 t.Fatalf("bad direction: %q", direction)
104 }
105
106 t.Log(key)
107 var pkgs []string
108 for pkg := range g[from] {
109 pkgs = append(pkgs, pkg)
110 }
111 sort.Strings(pkgs)
112 for _, pkg := range pkgs {
113 t.Logf("\t%s", pkg)
114 }
115 }
116
117 if testing.Verbose() {
118 printNode("forward", this)
119 printNode("reverse", this)
120 }
121
122 // Test direct edges.
123 // We throw in crypto/hmac to prove that external test files
124 // (such as this one) are inspected.
125 for _, p := range []string{"go/build", "testing", "crypto/hmac"} {
126 if !forward[this][p] {
127 printNode("forward", this)
128 t.Errorf("forward[%q][%q] not found", this, p)
129 }
130 if !reverse[p][this] {
131 printNode("reverse", p)
132 t.Errorf("reverse[%q][%q] not found", p, this)
133 }
134 }
135
136 // Test non-existent direct edges
137 for _, p := range []string{"errors", "reflect"} {
138 if forward[this][p] {
139 printNode("forward", this)
140 t.Errorf("unexpected: forward[%q][%q] found", this, p)
141 }
142 if reverse[p][this] {
143 printNode("reverse", p)
144 t.Errorf("unexpected: reverse[%q][%q] found", p, this)
145 }
146 }
147
148 // Test Search is reflexive.
149 if !forward.Search(this)[this] {
150 printNode("forward", this)
151 t.Errorf("irreflexive: forward.Search(importgraph)[importgraph] not found")
152 }
153 if !reverse.Search(this)[this] {
154 printNode("reverse", this)
155 t.Errorf("irreflexive: reverse.Search(importgraph)[importgraph] not found")
156 }
157
158 // Test Search is transitive. (There is no direct edge to these packages.)
159 for _, p := range []string{"errors", "reflect", "unsafe"} {
160 if !forward.Search(this)[p] {
161 printNode("forward", this)
162 t.Errorf("intransitive: forward.Search(importgraph)[%s] not found", p)
163 }
164 if !reverse.Search(p)[this] {
165 printNode("reverse", p)
166 t.Errorf("intransitive: reverse.Search(%s)[importgraph] not found", p)
167 }
168 }
169
170 // Test strongly-connected components. Because A's external
171 // test package can depend on B, and vice versa, most of the
172 // standard libraries are mutually dependent when their external
173 // tests are considered.
174 //
175 // For any nodes x, y in the same SCC, y appears in the results
176 // of both forward and reverse searches starting from x
177 if !forward.Search("fmt")["io"] ||
178 !forward.Search("io")["fmt"] ||
179 !reverse.Search("fmt")["io"] ||
180 !reverse.Search("io")["fmt"] {
181 printNode("forward", "fmt")
182 printNode("forward", "io")
183 printNode("reverse", "fmt")
184 printNode("reverse", "io")
185 t.Errorf("fmt and io are not mutually reachable despite being in the same SCC")
186 }
187 }