.gitignore added
[dotfiles/.git] / .config / coc / extensions / coc-go-data / tools / pkg / mod / golang.org / x / tools@v0.1.1-0.20210319172145-bda8f5cee399 / 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 //go:build !android
8 // +build !android
9
10 package importgraph_test
11
12 import (
13         "go/build"
14         "sort"
15         "strings"
16         "testing"
17
18         "golang.org/x/tools/go/packages/packagestest"
19         "golang.org/x/tools/refactor/importgraph"
20
21         _ "crypto/hmac" // just for test, below
22 )
23
24 const this = "golang.org/x/tools/refactor/importgraph"
25
26 func TestBuild(t *testing.T) {
27         exported := packagestest.Export(t, packagestest.GOPATH, []packagestest.Module{
28                 {Name: "golang.org/x/tools/refactor/importgraph", Files: packagestest.MustCopyFileTree(".")}})
29         defer exported.Cleanup()
30
31         var gopath string
32         for _, env := range exported.Config.Env {
33                 if !strings.HasPrefix(env, "GOPATH=") {
34                         continue
35                 }
36                 gopath = strings.TrimPrefix(env, "GOPATH=")
37         }
38         if gopath == "" {
39                 t.Fatal("Failed to fish GOPATH out of env: ", exported.Config.Env)
40         }
41
42         var buildContext = build.Default
43         buildContext.GOPATH = gopath
44
45         forward, reverse, errors := importgraph.Build(&buildContext)
46
47         // Test direct edges.
48         // We throw in crypto/hmac to prove that external test files
49         // (such as this one) are inspected.
50         for _, p := range []string{"go/build", "testing", "crypto/hmac"} {
51                 if !forward[this][p] {
52                         t.Errorf("forward[importgraph][%s] not found", p)
53                 }
54                 if !reverse[p][this] {
55                         t.Errorf("reverse[%s][importgraph] not found", p)
56                 }
57         }
58
59         // Test non-existent direct edges
60         for _, p := range []string{"errors", "reflect"} {
61                 if forward[this][p] {
62                         t.Errorf("unexpected: forward[importgraph][%s] found", p)
63                 }
64                 if reverse[p][this] {
65                         t.Errorf("unexpected: reverse[%s][importgraph] found", p)
66                 }
67         }
68
69         // Test Search is reflexive.
70         if !forward.Search(this)[this] {
71                 t.Errorf("irreflexive: forward.Search(importgraph)[importgraph] not found")
72         }
73         if !reverse.Search(this)[this] {
74                 t.Errorf("irrefexive: reverse.Search(importgraph)[importgraph] not found")
75         }
76
77         // Test Search is transitive.  (There is no direct edge to these packages.)
78         for _, p := range []string{"errors", "reflect", "unsafe"} {
79                 if !forward.Search(this)[p] {
80                         t.Errorf("intransitive: forward.Search(importgraph)[%s] not found", p)
81                 }
82                 if !reverse.Search(p)[this] {
83                         t.Errorf("intransitive: reverse.Search(%s)[importgraph] not found", p)
84                 }
85         }
86
87         // Test strongly-connected components.  Because A's external
88         // test package can depend on B, and vice versa, most of the
89         // standard libraries are mutually dependent when their external
90         // tests are considered.
91         //
92         // For any nodes x, y in the same SCC, y appears in the results
93         // of both forward and reverse searches starting from x
94         if !forward.Search("fmt")["io"] ||
95                 !forward.Search("io")["fmt"] ||
96                 !reverse.Search("fmt")["io"] ||
97                 !reverse.Search("io")["fmt"] {
98                 t.Errorf("fmt and io are not mutually reachable despite being in the same SCC")
99         }
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         }
120 }