Debian Codegraph @

gopls/internal/cache/metadata/cycle_test.go

raw symbols SCIP
1 // Copyright 2023 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 package metadata
6
7 import (
8 "maps"
9 "sort"
10 "strings"
11 "testing"
12
13 "golang.org/x/tools/gopls/internal/util/bug"
14 )
15
16 func init() {
17 bug.PanicOnBugs = true
18 }
19
20 // This is an internal test of the breakImportCycles logic.
21 func TestBreakImportCycles(t *testing.T) {
22
23 // parse parses an import dependency graph.
24 // The input is a semicolon-separated list of node descriptions.
25 // Each node description is a package ID, optionally followed by
26 // "->" and a comma-separated list of successor IDs.
27 // Thus "a->b;b->c,d;e" represents the set of nodes {a,b,e}
28 // and the set of edges {a->b, b->c, b->d}.
29 parse := func(s string) map[PackageID]*Package {
30 m := make(map[PackageID]*Package)
31 makeNode := func(name string) *Package {
32 id := PackageID(name)
33 n, ok := m[id]
34 if !ok {
35 n = &Package{
36 ID: id,
37 DepsByPkgPath: make(map[PackagePath]PackageID),
38 }
39 m[id] = n
40 }
41 return n
42 }
43 if s != "" {
44 for item := range strings.SplitSeq(s, ";") {
45 nodeID, succIDs, ok := strings.Cut(item, "->")
46 node := makeNode(nodeID)
47 if ok {
48 for succID := range strings.SplitSeq(succIDs, ",") {
49 node.DepsByPkgPath[PackagePath(succID)] = PackageID(succID)
50 }
51 }
52 }
53 }
54 return m
55 }
56
57 // Sanity check of cycle detector.
58 {
59 got := cyclic(parse("a->b;b->c;c->a,d"))
60 has := func(s string) bool { return strings.Contains(got, s) }
61 if !(has("a->b") && has("b->c") && has("c->a") && !has("d")) {
62 t.Fatalf("cyclic: got %q, want a->b->c->a or equivalent", got)
63 }
64 }
65
66 // format formats an import graph, in lexicographic order,
67 // in the notation of parse, but with a "!" after the name
68 // of each node that has errors.
69 format := func(graph map[PackageID]*Package) string {
70 var items []string
71 for _, mp := range graph {
72 item := string(mp.ID)
73 if len(mp.Errors) > 0 {
74 item += "!"
75 }
76 var succs []string
77 for _, depID := range mp.DepsByPkgPath {
78 succs = append(succs, string(depID))
79 }
80 if succs != nil {
81 sort.Strings(succs)
82 item += "->" + strings.Join(succs, ",")
83 }
84 items = append(items, item)
85 }
86 sort.Strings(items)
87 return strings.Join(items, ";")
88 }
89
90 // We needn't test self-cycles as they are eliminated at Metadata construction.
91 for _, test := range []struct {
92 metadata, updates, want string
93 }{
94 // Simple 2-cycle.
95 {"a->b", "b->a",
96 "a->b;b!"}, // broke b->a
97
98 {"a->b;b->c;c", "b->a,c",
99 "a->b;b!->c;c"}, // broke b->a
100
101 // Reversing direction of p->s edge creates pqrs cycle.
102 {"a->p,q,r,s;p->q,s,z;q->r,z;r->s,z;s->z", "p->q,z;s->p,z",
103 "a->p,q,r,s;p!->z;q->r,z;r->s,z;s!->z"}, // broke p->q, s->p
104
105 // We break all intra-SCC edges from updated nodes,
106 // which may be more than necessary (e.g. a->b).
107 {"a->b;b->c;c;d->a", "a->b,e;c->d",
108 "a!->e;b->c;c!;d->a"}, // broke a->b, c->d
109 } {
110 metadata := parse(test.metadata)
111 updates := parse(test.updates)
112
113 if cycle := cyclic(metadata); cycle != "" {
114 t.Errorf("initial metadata %s has cycle %s: ", format(metadata), cycle)
115 continue
116 }
117
118 t.Log("initial", format(metadata))
119
120 // Apply updates.
121 // (parse doesn't have a way to express node deletions,
122 // but they aren't very interesting.)
123 maps.Copy(metadata, updates)
124
125 t.Log("updated", format(metadata))
126
127 // breakImportCycles accesses only these fields of Metadata:
128 // DepsByImpPath, ID - read
129 // DepsByPkgPath - read, updated
130 // Errors - updated
131 breakImportCycles(metadata, updates)
132
133 t.Log("acyclic", format(metadata))
134
135 if cycle := cyclic(metadata); cycle != "" {
136 t.Errorf("resulting metadata %s has cycle %s: ", format(metadata), cycle)
137 }
138
139 got := format(metadata)
140 if got != test.want {
141 t.Errorf("test.metadata=%s test.updates=%s: got=%s want=%s",
142 test.metadata, test.updates, got, test.want)
143 }
144 }
145 }