Skip to content

Commit 1fd30d2

Browse files
author
Bryan C. Mills
committed
refactor/importgraph: set env from packagestest.Export and check errors from Build
This test was written for GOPATH mode, and subsequently updated to use packagestest at a time when GO111MODULE defaulted to "auto" (in this case, off). As a result, the test was failing to set environment variables relevant to the test, such as GO111MODULE=off. The missing environment was causing errors that were subsequently masked by a missing error check: the errors returned by importgraph.Build were only read in an "if false" block. This CL adds the missing environment variables and error checks. Using the correct environment may or may not fix the observed test failures. For golang/go#37823 Change-Id: I75844e6fdf47222aa4f953c8c4fbcc93cec606c9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/367846 Trust: Bryan C. Mills <[email protected]> Run-TryBot: Bryan C. Mills <[email protected]> Reviewed-by: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 2c9b078 commit 1fd30d2

File tree

1 file changed

+95
-27
lines changed

1 file changed

+95
-27
lines changed

refactor/importgraph/graph_test.go

Lines changed: 95 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
package importgraph_test
1111

1212
import (
13+
"fmt"
1314
"go/build"
15+
"os"
1416
"sort"
1517
"strings"
1618
"testing"
@@ -30,56 +32,138 @@ func TestBuild(t *testing.T) {
3032

3133
var gopath string
3234
for _, env := range exported.Config.Env {
33-
if !strings.HasPrefix(env, "GOPATH=") {
35+
eq := strings.Index(env, "=")
36+
if eq == 0 {
37+
// We sometimes see keys with a single leading "=" in the environment on Windows.
38+
// TODO(#49886): What is the correct way to parse them in general?
39+
eq = strings.Index(env[1:], "=") + 1
40+
}
41+
if eq < 0 {
42+
t.Fatalf("invalid variable in exported.Config.Env: %q", env)
43+
}
44+
k := env[:eq]
45+
v := env[eq+1:]
46+
if k == "GOPATH" {
47+
gopath = v
48+
}
49+
50+
if os.Getenv(k) == v {
3451
continue
3552
}
36-
gopath = strings.TrimPrefix(env, "GOPATH=")
53+
defer func(prev string, prevOK bool) {
54+
if !prevOK {
55+
if err := os.Unsetenv(k); err != nil {
56+
t.Fatal(err)
57+
}
58+
} else {
59+
if err := os.Setenv(k, prev); err != nil {
60+
t.Fatal(err)
61+
}
62+
}
63+
}(os.LookupEnv(k))
64+
65+
if err := os.Setenv(k, v); err != nil {
66+
t.Fatal(err)
67+
}
68+
t.Logf("%s=%s", k, v)
3769
}
3870
if gopath == "" {
3971
t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
4072
}
4173

4274
var buildContext = build.Default
4375
buildContext.GOPATH = gopath
76+
buildContext.Dir = exported.Config.Dir
77+
78+
forward, reverse, errs := importgraph.Build(&buildContext)
79+
for path, err := range errs {
80+
t.Errorf("%s: %s", path, err)
81+
}
82+
if t.Failed() {
83+
return
84+
}
85+
86+
// Log the complete graph before the errors, so that the errors are near the
87+
// end of the log (where we expect them to be).
88+
nodePrinted := map[string]bool{}
89+
printNode := func(direction string, from string) {
90+
key := fmt.Sprintf("%s[%q]", direction, from)
91+
if nodePrinted[key] {
92+
return
93+
}
94+
nodePrinted[key] = true
95+
96+
var g importgraph.Graph
97+
switch direction {
98+
case "forward":
99+
g = forward
100+
case "reverse":
101+
g = reverse
102+
default:
103+
t.Helper()
104+
t.Fatalf("bad direction: %q", direction)
105+
}
106+
107+
t.Log(key)
108+
var pkgs []string
109+
for pkg := range g[from] {
110+
pkgs = append(pkgs, pkg)
111+
}
112+
sort.Strings(pkgs)
113+
for _, pkg := range pkgs {
114+
t.Logf("\t%s", pkg)
115+
}
116+
}
44117

45-
forward, reverse, errors := importgraph.Build(&buildContext)
118+
if testing.Verbose() {
119+
printNode("forward", this)
120+
printNode("reverse", this)
121+
}
46122

47123
// Test direct edges.
48124
// We throw in crypto/hmac to prove that external test files
49125
// (such as this one) are inspected.
50126
for _, p := range []string{"go/build", "testing", "crypto/hmac"} {
51127
if !forward[this][p] {
52-
t.Errorf("forward[importgraph][%s] not found", p)
128+
printNode("forward", this)
129+
t.Errorf("forward[%q][%q] not found", this, p)
53130
}
54131
if !reverse[p][this] {
55-
t.Errorf("reverse[%s][importgraph] not found", p)
132+
printNode("reverse", p)
133+
t.Errorf("reverse[%q][%q] not found", p, this)
56134
}
57135
}
58136

59137
// Test non-existent direct edges
60138
for _, p := range []string{"errors", "reflect"} {
61139
if forward[this][p] {
62-
t.Errorf("unexpected: forward[importgraph][%s] found", p)
140+
printNode("forward", this)
141+
t.Errorf("unexpected: forward[%q][%q] found", this, p)
63142
}
64143
if reverse[p][this] {
65-
t.Errorf("unexpected: reverse[%s][importgraph] found", p)
144+
printNode("reverse", p)
145+
t.Errorf("unexpected: reverse[%q][%q] found", p, this)
66146
}
67147
}
68148

69149
// Test Search is reflexive.
70150
if !forward.Search(this)[this] {
151+
printNode("forward", this)
71152
t.Errorf("irreflexive: forward.Search(importgraph)[importgraph] not found")
72153
}
73154
if !reverse.Search(this)[this] {
155+
printNode("reverse", this)
74156
t.Errorf("irrefexive: reverse.Search(importgraph)[importgraph] not found")
75157
}
76158

77159
// Test Search is transitive. (There is no direct edge to these packages.)
78160
for _, p := range []string{"errors", "reflect", "unsafe"} {
79161
if !forward.Search(this)[p] {
162+
printNode("forward", this)
80163
t.Errorf("intransitive: forward.Search(importgraph)[%s] not found", p)
81164
}
82165
if !reverse.Search(p)[this] {
166+
printNode("reverse", p)
83167
t.Errorf("intransitive: reverse.Search(%s)[importgraph] not found", p)
84168
}
85169
}
@@ -95,26 +179,10 @@ func TestBuild(t *testing.T) {
95179
!forward.Search("io")["fmt"] ||
96180
!reverse.Search("fmt")["io"] ||
97181
!reverse.Search("io")["fmt"] {
182+
printNode("forward", "fmt")
183+
printNode("forward", "io")
184+
printNode("reverse", "fmt")
185+
printNode("reverse", "io")
98186
t.Errorf("fmt and io are not mutually reachable despite being in the same SCC")
99187
}
100-
101-
// debugging
102-
if false {
103-
for path, err := range errors {
104-
t.Logf("%s: %s", path, err)
105-
}
106-
printSorted := func(direction string, g importgraph.Graph, start string) {
107-
t.Log(direction)
108-
var pkgs []string
109-
for pkg := range g.Search(start) {
110-
pkgs = append(pkgs, pkg)
111-
}
112-
sort.Strings(pkgs)
113-
for _, pkg := range pkgs {
114-
t.Logf("\t%s", pkg)
115-
}
116-
}
117-
printSorted("forward", forward, this)
118-
printSorted("reverse", reverse, this)
119-
}
120188
}

0 commit comments

Comments
 (0)