-
Notifications
You must be signed in to change notification settings - Fork 1
/
depgraph_test.go
115 lines (90 loc) · 2.62 KB
/
depgraph_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package depgraph
import (
"os"
"path"
"strings"
"testing"
"github.com/luno/jettison/jtest"
"github.com/sebdah/goldie/v2"
"github.com/stretchr/testify/require"
)
//go:generate go test -update
func TestSelf(t *testing.T) {
wd, err := os.Getwd()
jtest.RequireNil(t, err)
mod := Mod{
Path: "github.com/luno/depgraph",
Dir: strings.TrimSuffix(wd, "/lib/depgraph"),
}
res, err := Make(mod, nil, true, "github.com/luno/depgraph")
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name(), makeTestNodes(res))
}
func TestTags(t *testing.T) {
wd, err := os.Getwd()
jtest.RequireNil(t, err)
root := "github.com/luno/depgraph"
mod := Mod{
Path: root,
Dir: path.Join(wd, "testdata"),
}
pkgs := []string{root, path.Join(root, "childa"), path.Join(root, "childb")}
res, err := Make(mod, map[string]bool{"tag": true}, true, pkgs...)
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name()+"True", makeTestNodes(res))
res, err = Make(mod, map[string]bool{"tag": false}, true, pkgs...)
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name()+"False", makeTestNodes(res))
res, err = Make(mod, nil, true, pkgs...)
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name()+"None", makeTestNodes(res))
res, err = Make(mod, nil, false, pkgs...)
jtest.RequireNil(t, err)
goldie.New(t).AssertJson(t, t.Name()+"NoneInternal", makeTestNodes(res))
}
func TestCircular(t *testing.T) {
wd, err := os.Getwd()
jtest.RequireNil(t, err)
root := "github.com/luno/depgraph/testdata"
mod := Mod{
Path: root,
Dir: path.Join(wd, "testdata"),
}
pkgs := []string{root, path.Join(root, "circular_a")}
_, err = Make(mod, nil, true, pkgs...)
require.EqualError(t, err, "cyclic import detected; descendant imports ancestor")
}
func makeTestNodes(nl []*Node) []testnode {
var res []testnode
for _, n := range nl {
res = append(res, makeTestNode(n))
}
return res
}
func makeTestNode(n *Node) testnode {
res := testnode{
Name: n.Name,
Parents: len(n.Parents),
FileImports: n.FileImports,
Height: n.Height,
}
for _, child := range n.Children {
c := makeTestNode(child)
res.Children = append(res.Children, c)
}
res.Descendants = make(map[string]bool)
for desc := range n.Descendants {
res.Descendants[desc.Name] = true
}
return res
}
type testnode struct {
Name string
// Children are direct downstream packages this package imports.
Children []testnode `json:",omitempty"`
// Descendants is union of all downstream packages by total number of occurrences.
Descendants map[string]bool `json:",omitempty"`
FileImports map[string][]string `json:",omitempty"`
Height int
Parents int
}