-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
249 lines (231 loc) · 5.18 KB
/
parse_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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package codeowners
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFormatting(t *testing.T) {
examples := []struct {
label string
ownersFile string
expected string
}{
{
label: "end-to-end",
ownersFile: `# Alpha should be consolidated.
alpha/ @test/user
alpha/a/b @test/user
# Comment on delta
delta @test/a @test/b
delta/a @test/a
delta/b/* @test/b
## Beta should come before delta.
beta @test/beta ## With a trailing comment.
`,
expected: `# Alpha should be consolidated.
alpha/ @test/user
## Beta should come before delta.
beta @test/beta ## With a trailing comment.
# Comment on delta
delta @test/a @test/b
delta/a @test/a
delta/b/* @test/b
`,
},
}
for _, tc := range examples {
t.Run(tc.label, func(t *testing.T) {
file := strings.NewReader(tc.ownersFile)
rules, err := ParseFile(file)
assert.NoError(t, err)
tree := NewFileTree(rules)
ConsolidateTree(tree)
formatted := tree.String()
assert.Equal(t, tc.expected, formatted)
})
}
}
// TestParseRule is ported from the original github.com/hmarr/codeowners test suite.
func TestParseRule(t *testing.T) {
examples := []struct {
name string
rule string
expected []Rule
err string
}{
// Success cases
{
name: "leading comments",
rule: "# comment a\n\n# comment b\n\nfile.txt @user",
expected: []Rule{
{
SourceLine: 5,
pattern: mustBuildPattern(t, "file.txt"),
Owners: []string{"@user"},
leadingComment: "# comment a\n\n# comment b\n\n",
},
},
},
{
name: "username owners",
rule: "file.txt @user",
expected: []Rule{
{
SourceLine: 1,
pattern: mustBuildPattern(t, "file.txt"),
Owners: []string{"@user"},
},
},
},
{
name: "team owners",
rule: "file.txt @org/team",
expected: []Rule{
{
SourceLine: 1,
pattern: mustBuildPattern(t, "file.txt"),
Owners: []string{"@org/team"},
},
},
},
{
name: "email owners",
rule: "file.txt foo@example.com",
expected: []Rule{
{
SourceLine: 1,
pattern: mustBuildPattern(t, "file.txt"),
Owners: []string{"foo@example.com"},
},
},
},
{
name: "multiple owners",
rule: "file.txt @user @org/team foo@example.com",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "file.txt"),
Owners: []string{
"@user",
"@org/team",
"foo@example.com",
},
},
},
},
{
name: "complex patterns",
rule: "d?r/* @user",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "d?r/*"),
Owners: []string{"@user"},
},
},
},
{
name: "pattern with space",
rule: "foo\\ bar @user",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "foo\\ bar"),
Owners: []string{"@user"},
}},
},
{
name: "comments",
rule: "file.txt @user # some comment",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "file.txt"),
Owners: []string{"@user"},
trailingComment: "# some comment",
}},
},
{
name: "pattern with no owners",
rule: "pattern",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "pattern"),
Owners: []string{},
trailingComment: "",
}},
},
{
name: "pattern with no owners and comment",
rule: "pattern # but no more",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "pattern"),
Owners: []string{},
trailingComment: "# but no more",
}},
},
{
name: "pattern with no owners with whitespace",
rule: "pattern ",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "pattern"),
Owners: []string{},
trailingComment: "",
}},
},
{
name: "pattern with leading and trailing whitespace",
rule: " pattern @user ",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "pattern"),
Owners: []string{"@user"},
trailingComment: "",
}},
},
{
name: "pattern with leading and trailing whitespace and no owner",
rule: " pattern ",
expected: []Rule{{
SourceLine: 1,
pattern: mustBuildPattern(t, "pattern"),
Owners: []string{},
trailingComment: "",
}},
},
// Error cases
{
name: "malformed patterns",
rule: "file.{txt @user",
err: "line 1: unexpected character '{' at position 6",
},
{
name: "patterns with brackets",
rule: "file.[cC] @user",
err: "line 1: unexpected character '[' at position 6",
},
{
name: "malformed owners",
rule: "file.txt missing-at-sign",
err: "line 1: invalid owner format 'missing-at-sign' at position 10",
},
}
for _, e := range examples {
t.Run("parses "+e.name, func(t *testing.T) {
file := strings.NewReader(e.rule)
actual, err := ParseFile(file)
if e.err != "" {
assert.EqualError(t, err, e.err)
} else {
assert.NoError(t, err)
assert.Equal(t, e.expected, actual)
}
})
}
}
func mustBuildPattern(t *testing.T, pat string) pattern {
p, err := newPattern(pat)
if err != nil {
t.Fatal(err)
}
return p
}