Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.0.0-20201105173854-bc9fc8d8c4bc / refactor / importgraph / graph_test.go
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 // +build !android
8
9 package importgraph_test
10
11 import (
12         "go/build"
13         "sort"
14         "strings"
15         "testing"
16
17         "golang.org/x/tools/go/packages/packagestest"
18         "golang.org/x/tools/refactor/importgraph"
19
20         _ "crypto/hmac" // just for test, below
21 )
22
23 const this = "golang.org/x/tools/refactor/importgraph"
24
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()
29
30         var gopath string
31         for _, env := range exported.Config.Env {
32                 if !strings.HasPrefix(env, "GOPATH=") {
33                         continue
34                 }
35                 gopath = strings.TrimPrefix(env, "GOPATH=")
36         }
37         if gopath == "" {
38                 t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
39         }
40
41         var buildContext = build.Default
42         buildContext.GOPATH = gopath
43
44         forward, reverse, errors := importgraph.Build(&buildContext)
45
46         // Test direct edges.
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)
52                 }
53                 if !reverse[p][this] {
54                         t.Errorf("reverse[%s][importgraph] not found", p)
55                 }
56         }
57
58         // Test non-existent direct edges
59         for _, p := range []string{"errors", "reflect"} {
60                 if forward[this][p] {
61                         t.Errorf("unexpected: forward[importgraph][%s] found", p)
62                 }
63                 if reverse[p][this] {
64                         t.Errorf("unexpected: reverse[%s][importgraph] found", p)
65                 }
66         }
67
68         // Test Search is reflexive.
69         if !forward.Search(this)[this] {
70                 t.Errorf("irreflexive: forward.Search(importgraph)[importgraph] not found")
71         }
72         if !reverse.Search(this)[this] {
73                 t.Errorf("irrefexive: reverse.Search(importgraph)[importgraph] not found")
74         }
75
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)
80                 }
81                 if !reverse.Search(p)[this] {
82                         t.Errorf("intransitive: reverse.Search(%s)[importgraph] not found", p)
83                 }
84         }
85
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.
90         //
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")
98         }
99
100         // debugging
101         if false {
102                 for path, err := range errors {
103                         t.Logf("%s: %s", path, err)
104                 }
105                 printSorted := func(direction string, g importgraph.Graph, start string) {
106                         t.Log(direction)
107                         var pkgs []string
108                         for pkg := range g.Search(start) {
109                                 pkgs = append(pkgs, pkg)
110                         }
111                         sort.Strings(pkgs)
112                         for _, pkg := range pkgs {
113                                 t.Logf("\t%s", pkg)
114                         }
115                 }
116                 printSorted("forward", forward, this)
117                 printSorted("reverse", reverse, this)
118         }
119 }