-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackages_test.go
207 lines (170 loc) · 4.68 KB
/
packages_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (C) 2017, 2018 Damon Revoe. All rights reserved.
// Use of this source code is governed by the MIT
// license, which can be found in the LICENSE file.
package main
import (
"path"
"strings"
"testing"
)
func TestNoPackages(t *testing.T) {
pi, err := buildPackageIndex(false,
packageDefinitionList{}, [][]string{})
if err != nil {
t.Error("Building index for an empty list returned an error")
}
if pi.packageByName == nil || pi.orderedPackages == nil ||
len(pi.packageByName) != 0 || len(pi.orderedPackages) != 0 {
t.Error("Index structures are not properly initialized")
}
}
func makePackageIndexForTesting(packagesAndDependencies []string, quiet bool) (
*packageIndex, error) {
var packages packageDefinitionList
var deps [][]string
for _, packageLine := range packagesAndDependencies {
split := strings.SplitN(packageLine, ":", 2)
packages = append(packages, &packageDefinition{
PackageName: split[0],
pathname: path.Join(split[0],
packageDefinitionFilename)})
if len(split) > 1 {
deps = append(deps, strings.Split(split[1], ","))
} else {
deps = append(deps, []string{})
}
}
return buildPackageIndex(quiet, packages, deps)
}
func TestDuplicateDefinition(t *testing.T) {
pi, err := makePackageIndexForTesting([]string{
"base", "client", "base"}, false)
if pi != nil || err == nil || !strings.Contains(err.Error(),
"duplicate package name: base") {
t.Error("Package duplicate was not detected")
}
}
func confirmCircularDependencyError(t *testing.T, err error, cycle string) {
if err == nil {
t.Error("Circular dependency was not detected")
} else if !strings.Contains(err.Error(),
"circular dependency detected: "+cycle) {
t.Error("Unexpected circular dependency error: " +
err.Error())
}
}
func TestCircularDependency(t *testing.T) {
_, err := makePackageIndexForTesting([]string{
"a:b", "b:c", "c:a"}, false)
confirmCircularDependencyError(t, err, "a -> b -> c -> a")
_, err = makePackageIndexForTesting([]string{
"a:b", "b:c", "c:b,d", "d"}, false)
confirmCircularDependencyError(t, err, "b -> c -> b")
_, err = makePackageIndexForTesting([]string{
"a:b,a", "b"}, false)
confirmCircularDependencyError(t, err, "a -> a")
}
func TestDiamondDependency(t *testing.T) {
pi, err := makePackageIndexForTesting([]string{
"d:b,c", "b:a", "c:a", "a"}, false)
if err != nil {
t.Error("Unexpected error")
}
if len(pi.packageByName) != len(pi.orderedPackages) {
t.Error("Index size mismatch")
}
packageOrder := packageNames(pi.orderedPackages)
if packageOrder != "a, b, c, d" {
t.Error("Invalid package order: " + packageOrder)
}
checkIndirectDependencies := func(pkgName, expectedDeps string) {
deps := packageNames(pi.packageByName[pkgName].allRequired)
if deps != expectedDeps {
t.Error("Indirect dependencies for " + pkgName +
" do not match: expected=" + expectedDeps +
"; actual=" + deps)
}
}
checkIndirectDependencies("a", "")
checkIndirectDependencies("b", "a")
checkIndirectDependencies("c", "a")
checkIndirectDependencies("d", "a, b, c")
}
func checkSelectionGraph(t *testing.T,
packagesAndDependencies []string,
expected map[string]string) {
pi, err := makePackageIndexForTesting(packagesAndDependencies, true)
if err != nil {
t.Error("Unexpected error")
}
var selection packageDefinitionList
for name := range expected {
pd, found := pi.packageByName[name]
if !found {
t.Error("Invalid test case inputs")
}
selection = append(selection, pd)
}
selectionGraph := establishDependenciesInSelection(selection, pi)
if len(selectionGraph) != len(expected) {
t.Error("Unexpected number of selected vertices")
}
for pd, deps := range selectionGraph {
name := pd.PackageName
expectedDepNames, match := expected[name]
if !match {
t.Error("Unexpected package returned: " + name)
}
var depNames string
for _, dep := range deps {
if depNames != "" {
depNames += ", "
}
depNames += dep.PackageName
}
if depNames != expectedDepNames {
t.Error("Unexpected dependencies for " + name + ": " +
depNames + "; expected: " + expectedDepNames)
}
}
}
func TestSelectionGraph(t *testing.T) {
checkSelectionGraph(t,
[]string{"d:a,b,c", "b:a", "c:a", "a"},
map[string]string{"a": "", "d": "a"})
packageDependencies := []string{
"a",
"b:a",
"c:b",
"d:b",
"e:c",
"f:b",
"g:e,f,d",
"h:g",
"i:f,h",
"j:i,h",
}
checkSelectionGraph(t,
packageDependencies,
map[string]string{
"b": "",
"c": "b",
"d": "b",
"g": "c, d",
"i": "g",
})
checkSelectionGraph(t,
packageDependencies,
map[string]string{
"a": "",
"b": "a",
"c": "b",
"d": "b",
"e": "c",
"f": "b",
"g": "e, f, d",
"h": "g",
"i": "h",
"j": "i",
})
}