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_testscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/
10
11 import (
12 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
13 "go/buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/"
14 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
15 "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/"
16 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
17 "testingscip-go gomod github.com/golang/go/src go1.25.0 testing/"
18
19 "golang.org/x/tools/internal/packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/"
20 "golang.org/x/tools/refactor/importgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/"
21
22 _ "crypto/hmacscip-go gomod github.com/golang/go/src go1.25.0 `crypto/hmac`/" // just for test, below
23 )
24
25 const thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this. = "golang.org/x/tools/refactor/importgraph"
26
27 func TestBuildscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/TestBuild().(t *testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Tscip-go gomod github.com/golang/go/src go1.25.0 testing/T#) {
28 exported := packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/.Exportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Export().(t, packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/.GOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/GOPATH., []packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/.Modulescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#{
29 {Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Name.: "golang.org/x/tools/refactor/importgraph", Filesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Module#Files.: packagestestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/.MustCopyFileTreescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/MustCopyFileTree().(".")}})
30 defer exported.Cleanupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Cleanup().()
31
32 var gopath string
33 for _, env := range exported.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env. {
34 eq := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 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 = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(env[1:], "=") + 1
39 }
40 if eq < 0 {
41 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getenvscip-go gomod github.com/golang/go/src go1.25.0 os/Getenv().(k) == v {
50 continue
51 }
52 defer func(prev string, prevOK bool) {
53 if !prevOK {
54 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Unsetenvscip-go gomod github.com/golang/go/src go1.25.0 os/Unsetenv().(k); err != nil {
55 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
56 }
57 } else {
58 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.0 os/Setenv().(k, prev); err != nil {
59 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
60 }
61 }
62 }(osscip-go gomod github.com/golang/go/src go1.25.0 os/.LookupEnvscip-go gomod github.com/golang/go/src go1.25.0 os/LookupEnv().(k))
63
64 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Setenvscip-go gomod github.com/golang/go/src go1.25.0 os/Setenv().(k, v); err != nil {
65 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().(err)
66 }
67 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("%s=%s", k, v)
68 }
69 if gopath == "" {
70 t.Fatalscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatal().("Failed to fish GOPATH out of env: ", exported.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Env.)
71 }
72
73 var buildContext = buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Default.
74 buildContext.GOPATHscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#GOPATH. = gopath
75 buildContext.Dirscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#Dir. = exported.Configscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/packagestest`/Exported#Config..Dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/packages`/Config#Dir.
76
77 forward, reverse, errs := importgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/.Buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Build().(&buildContext)
78 for path, err := range errs {
79 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("%s: %s", path, err)
80 }
81 if t.Failedscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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 := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s[%q]", direction, from)
90 if nodePrinted[key] {
91 return
92 }
93 nodePrinted[key] = true
94
95 var g importgraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/.Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#
96 switch direction {
97 case "forward":
98 g = forward
99 case "reverse":
100 g = reverse
101 default:
102 t.Helperscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Helper().()
103 t.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Fatalf().("bad direction: %q", direction)
104 }
105
106 t.Logscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Log().(key)
107 var pkgs []string
108 for pkg := range g[from] {
109 pkgs = append(pkgs, pkg)
110 }
111 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(pkgs)
112 for _, pkg := range pkgs {
113 t.Logfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Logf().("\t%s", pkg)
114 }
115 }
116
117 if testingscip-go gomod github.com/golang/go/src go1.25.0 testing/.Verbosescip-go gomod github.com/golang/go/src go1.25.0 testing/Verbose().() {
118 printNode("forward", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)
119 printNode("reverse", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/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[thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.][p] {
127 printNode("forward", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)
128 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("forward[%q][%q] not found", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this., p)
129 }
130 if !reverse[p][thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.] {
131 printNode("reverse", p)
132 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("reverse[%q][%q] not found", p, thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)
133 }
134 }
135
136 // Test non-existent direct edges
137 for _, p := range []string{"errors", "reflect"} {
138 if forward[thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.][p] {
139 printNode("forward", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)
140 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected: forward[%q][%q] found", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this., p)
141 }
142 if reverse[p][thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.] {
143 printNode("reverse", p)
144 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("unexpected: reverse[%q][%q] found", p, thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)
145 }
146 }
147
148 // Test Search is reflexive.
149 if !forward.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#Search().(thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)[thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.] {
150 printNode("forward", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)
151 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("irreflexive: forward.Search(importgraph)[importgraph] not found")
152 }
153 if !reverse.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#Search().(thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)[thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.] {
154 printNode("reverse", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)
155 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#Search().(thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)[p] {
161 printNode("forward", thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.)
162 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("intransitive: forward.Search(importgraph)[%s] not found", p)
163 }
164 if !reverse.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#Search().(p)[thisscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph_test`/this.] {
165 printNode("reverse", p)
166 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#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.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#Search().("fmt")["io"] ||
178 !forward.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#Search().("io")["fmt"] ||
179 !reverse.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#Search().("fmt")["io"] ||
180 !reverse.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/refactor/importgraph`/Graph#Search().("io")["fmt"] {
181 printNode("forward", "fmt")
182 printNode("forward", "io")
183 printNode("reverse", "fmt")
184 printNode("reverse", "io")
185 t.Errorfscip-go gomod github.com/golang/go/src go1.25.0 testing/common#Errorf().("fmt and io are not mutually reachable despite being in the same SCC")
186 }
187 }