-
Notifications
You must be signed in to change notification settings - Fork 5
/
filter_test.go
206 lines (169 loc) · 4.76 KB
/
filter_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
package main
import (
"github.com/stretchr/testify/assert"
"path/filepath"
"testing"
)
func TestUnwrap(t *testing.T) {
var deps []dep
var labels []string
var foundDeps []dep
// Test if nothing is okay
foundDeps = unwrap(deps, "", labels, nil)
assert.Empty(t, foundDeps)
testDep := dep{
Name: "sample",
Kind: "git",
Location: "sample",
DepList: nil,
Labels: nil,
Params: nil,
}
// Tests single-level dep
deps = append(deps, testDep)
deps[0].Params = map[string]string{"kind": testDep.Kind, "location": testDep.Location, "name": testDep.Name}
foundDeps = unwrap(deps, "./", labels, nil)
assert.Equal(t, deps, foundDeps)
//Making a nest
testDep = dep{
Name: "parent",
Kind: "git",
Location: "parent-dir",
DepList: []dep{
{
Name: "child",
Kind: "git",
Location: "child-dir",
Labels: []string{"first"},
DepList: []dep{
{
Name: "grandchild",
Kind: "git",
Location: "grandchild-dir",
Labels: []string{"second"},
},
},
},
{
Name: "sibling",
Kind: "git",
Location: "sample",
},
},
}
deps = append(deps, testDep)
foundDeps = unwrap(deps, "./append/", labels, nil)
// Test flattening of deps:
assert.Equal(t, 5, len(foundDeps))
// Test dir expanding
assert.Equal(t, filepath.Clean("./append/parent-dir"), foundDeps[4].Location) // First in, least nested, last in array
assert.Equal(t, filepath.Clean("./append/sample"), foundDeps[0].Location) // Last in, most nested
assert.Equal(t, filepath.Clean("./append/parent-dir/child-dir/grandchild-dir"), foundDeps[1].Location) // Last in, most nested
// Test label inheritance
assert.Empty(t, foundDeps[4].Labels)
assert.Equal(t, []string{"first"}, foundDeps[2].Labels)
assert.Equal(t, []string{"second", "first"}, foundDeps[1].Labels)
}
func TestApplyFilterKind(t *testing.T) {
testDeps := []dep{
{
Labels: nil,
},
{
Labels: []string{"one"},
},
{
Labels: []string{"one", "two"},
},
{
Labels: []string{"three", "two"},
},
}
perform := perform{
Labels: "",
Exclusive: false,
Force: true,
}
result := applyFilterLabel(testDeps, perform)
assert.Equal(t, 4, len(result))
perform.Labels = "one"
perform.Exclusive = true
result = applyFilterLabel(testDeps, perform)
assert.Equal(t, 2, len(result))
perform.Exclusive = false
result = applyFilterLabel(testDeps, perform)
assert.Equal(t, 2, len(result))
perform.Labels = "one,two"
perform.Exclusive = true
result = applyFilterLabel(testDeps, perform)
assert.Equal(t, 1, len(result))
perform.Labels = "two,one"
perform.Exclusive = false
result = applyFilterLabel(testDeps, perform)
assert.Equal(t, 3, len(result))
perform.Labels = "zero"
perform.Exclusive = false
result = applyFilterLabel(testDeps, perform)
assert.Equal(t, 0, len(result))
}
func TestApplyFilterLabel(t *testing.T) {
testDeps := []dep{
{
Kind: "git",
},
{
Kind: "git",
},
{
Kind: "git",
},
{
Kind: "other",
},
}
result := applyFilterKind(testDeps, "git")
assert.Equal(t, 3, len(result))
result = applyFilterKind(testDeps, "other")
assert.Equal(t, 1, len(result))
result = applyFilterKind(testDeps, "none")
assert.Equal(t, 0, len(result))
}
func TestIsExclusive(t *testing.T) {
what := []string{"one", "two"} // dep.labels
against := []string{"one"} // labels
// dep does have "one"
assert.True(t, isExclusive(what, against))
// dep does not have "three"
against = []string{"one", "three"} // labels
assert.False(t, isExclusive(what, against))
// Testing no labels, no filter, all comes back
against = []string{} // labels
assert.True(t, isExclusive(what, against))
// Testing no dep.labels, and no labels
what = []string{} // dep.labels
assert.True(t, isExclusive(what, against))
// Testing no dep.labels, but with labels
against = []string{"one", "three"} // labels
assert.False(t, isExclusive(what, against))
}
func TestIsInclusive(t *testing.T) {
what := []string{"one", "two"} // dep.labels
against := []string{"one"} // labels
// dep does have "one"
assert.True(t, isInclusive(what, against))
// dep does not have "three", but does have "one"
against = []string{"one", "three"} // labels
assert.True(t, isInclusive(what, against))
// dep does not have "four"
against = []string{"four"} // labels
assert.False(t, isInclusive(what, against))
// Testing no labels, no filter, all comes back
against = []string{} // labels
assert.True(t, isInclusive(what, against))
// Testing no dep.labels, and no labels
what = []string{} // dep.labels
assert.True(t, isInclusive(what, against))
// Testing no dep.labels, but with labels
against = []string{"one", "three"} // labels
assert.False(t, isInclusive(what, against))
}