-
Notifications
You must be signed in to change notification settings - Fork 0
/
repoowners_test.go
401 lines (385 loc) · 8.16 KB
/
repoowners_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package repoowners
import (
"bytes"
"fmt"
"path/filepath"
"reflect"
"strconv"
"testing"
"github.com/spf13/afero"
)
func newMemFS() *afero.Afero {
return &afero.Afero{Fs: afero.NewMemMapFs()}
}
func TestLoadLocalOnlyOneOwners(t *testing.T) {
const basePath = "src/github.com/nasa9084/test_repository"
fs = newMemFS()
if err := fs.MkdirAll(basePath, 0755); err != nil {
t.Fatal(err)
}
f, err := fs.Create(filepath.Join(basePath, DefaultOwnersFilename))
if err != nil {
t.Fatal(err)
}
fmt.Fprint(f, `---
approvers:
- alice
- bob
reviewers:
- charlie
- dave
- ellen`)
if err := f.Close(); err != nil {
t.Fatal(err)
}
o, err := LoadLocal(basePath)
if err != nil {
t.Fatal(err)
}
wantApprovers := map[string]UsernameSet{
".": newUsernameSet("alice", "bob"),
}
wantReviewers := map[string]UsernameSet{
".": newUsernameSet("charlie", "dave", "ellen"),
}
if !reflect.DeepEqual(o.approvers, wantApprovers) {
t.Errorf("unexpected approvers:\n got: %+v\n want: %+v", o.approvers, wantApprovers)
return
}
if !reflect.DeepEqual(o.reviewers, wantReviewers) {
t.Errorf("unexpected reviewers:\n got: %+v\n want: %+v", o.reviewers, wantReviewers)
return
}
}
func TestLoadLocalOnlyAliases(t *testing.T) {
const basePath = "src/github.com/nasa9084/test_repository"
fs = newMemFS()
if err := fs.MkdirAll(basePath, 0755); err != nil {
t.Fatal(err)
}
f, err := fs.Create(filepath.Join(basePath, DefaultAliasesFilename))
if err != nil {
t.Fatal(err)
}
fmt.Fprint(f, `---
aliases:
managers:
- alice
members:
- bob
- charlie
- dave`)
if err := f.Close(); err != nil {
t.Fatal(err)
}
o, err := LoadLocal(basePath)
if err != nil {
t.Fatal(err)
}
wantAliases := map[string]UsernameSet{
"managers": newUsernameSet("alice"),
"members": newUsernameSet("bob", "charlie", "dave"),
}
if !reflect.DeepEqual(o.aliases, wantAliases) {
t.Errorf("unexpected aliases:\n got: %+v\n want: %+v", o.aliases, wantAliases)
return
}
}
func TestApprovers(t *testing.T) {
owners := Owners{
approvers: map[string]UsernameSet{
"foo/bar": newUsernameSet("bob"),
"foo/bar/baz": newUsernameSet("charlie", "dave"),
"foo/bar/qux": newUsernameSet("ellen"),
},
}
tests := []struct {
got UsernameSet
want UsernameSet
}{
{
got: owners.Approvers("foo/bar/baz"),
want: newUsernameSet("bob", "charlie", "dave"),
},
{
got: owners.Approvers("foo/bar/qux"),
want: newUsernameSet("bob", "ellen"),
},
{
got: owners.Approvers("foo/bar"),
want: newUsernameSet("bob"),
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if !reflect.DeepEqual(tt.got, tt.want) {
t.Errorf("unexpected approvers:\n got: %+v\n want: %+v", tt.got, tt.want)
return
}
})
}
}
func TestApproversWithNoInheritance(t *testing.T) {
owners := Owners{
options: map[string]options{
"foo/bar/baz": options{
NoInheritance: true,
},
},
approvers: map[string]UsernameSet{
"foo/bar": newUsernameSet("alice"),
"foo/bar/baz": newUsernameSet("bob"),
"foo/bar/qux": newUsernameSet("charlie"),
"foo/bar/baz/quux": newUsernameSet("dave"),
},
}
tests := []struct {
got UsernameSet
want UsernameSet
}{
{
got: owners.Approvers("foo"),
want: newUsernameSet(),
},
{
got: owners.Approvers("foo/bar"),
want: newUsernameSet("alice"),
},
{
got: owners.Approvers("foo/bar/baz"),
want: newUsernameSet("bob"),
},
{
got: owners.Approvers("foo/bar/baz/qux"),
want: newUsernameSet("bob"),
},
{
got: owners.Approvers("foo/bar/qux"),
want: newUsernameSet("alice", "charlie"),
},
{
got: owners.Approvers("foo/bar/baz/quux"),
want: newUsernameSet("bob", "dave"),
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if !reflect.DeepEqual(tt.got, tt.want) {
t.Errorf("unexpected approvers:\n got: %+v\n want: %+v", tt.got, tt.want)
return
}
})
}
}
func TestApproversWithAliases(t *testing.T) {
owners := Owners{
approvers: map[string]UsernameSet{
"foo/bar": newUsernameSet("alice", "admins"),
"foo/bar/baz": newUsernameSet("bob", "members"),
"foo/qux": newUsernameSet("alice"),
"foo/qux/quux": newUsernameSet("bob", "admins"),
},
aliases: map[string]UsernameSet{
"admins": newUsernameSet("charlie"),
"members": newUsernameSet("dave", "ellen"),
},
}
tests := []struct {
got UsernameSet
want UsernameSet
}{
{
got: owners.Approvers("foo/bar"),
want: newUsernameSet("alice", "charlie"),
},
{
got: owners.Approvers("foo/bar/baz"),
want: newUsernameSet("alice", "bob", "charlie", "dave", "ellen"),
},
{
got: owners.Approvers("foo/qux/quux"),
want: newUsernameSet("alice", "bob", "charlie"),
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if !reflect.DeepEqual(tt.got, tt.want) {
t.Errorf("unexpected approvers:\n got: %+v\n want: %+v", tt.got, tt.want)
return
}
})
}
}
func TestIsApprover(t *testing.T) {
owners := Owners{
approvers: map[string]UsernameSet{
"foo/bar": newUsernameSet("alice"),
"foo/bar/baz": newUsernameSet("bob"),
},
}
tests := []struct {
got bool
want bool
}{
{
got: owners.IsApprover("alice", "foo/bar"),
want: true,
},
{
got: owners.IsApprover("alice", "foo"),
want: false,
},
{
got: owners.IsApprover("alice", "foo/bar/qux"),
want: true,
},
{
got: owners.IsApprover("bob", "foo/bar"),
want: false,
},
{
got: owners.IsApprover("bob", "foo/bar/qux"),
want: false,
},
{
got: owners.IsApprover("bob", "foo/bar/baz"),
want: true,
},
{
got: owners.IsApprover("bob", "foo/bar/baz/qux"),
want: true,
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
if tt.got != tt.want {
t.Errorf("%t != %t", tt.got, tt.want)
return
}
})
}
}
func TestParseOwners(t *testing.T) {
tests := []struct {
label string
in string
want ownersConfig
}{
{
label: "only approvers",
in: `approvers:
- alice
- bob`,
want: ownersConfig{
Approvers: []string{"alice", "bob"},
},
},
{
label: "approvers and reviewers and required reviewers",
in: `approvers:
- alice
- bob
reviewers:
- charlie
- dave
required_reviewers:
- ellen`,
want: ownersConfig{
Approvers: []string{"alice", "bob"},
Reviewers: []string{"charlie", "dave"},
RequiredReviewers: []string{"ellen"},
},
},
{
label: "with no_inherit option",
in: `no_inherit: true
approvers:
- alice
reviewers:
- bob`,
want: ownersConfig{
Options: options{
NoInheritance: true,
},
Approvers: []string{"alice"},
Reviewers: []string{"bob"},
},
},
{
label: "with comment",
in: `---
# This is comment
approvers:
- alice # foo-team
reviewers:
- bob
- charlie # bar-team`,
want: ownersConfig{
Approvers: []string{"alice"},
Reviewers: []string{"bob", "charlie"},
},
},
{
label: "empty yaml document",
in: `---`,
want: ownersConfig{},
},
{
label: "empty",
in: ``,
want: ownersConfig{},
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i)+"/"+tt.label, func(t *testing.T) {
got, err := parseOwners(bytes.NewBufferString(tt.in))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("unexpected ownerConfig:\n got: %+v\n want: %+v", got, tt.want)
return
}
})
}
}
func TestParseAliases(t *testing.T) {
tests := []struct {
label string
in string
want aliasesConfig
}{
{
label: "aliases",
in: `aliases:
managers:
- alice
- bob`,
want: aliasesConfig{
Aliases: map[string][]string{"managers": []string{"alice", "bob"}},
},
},
{
label: "empty yaml document",
in: `---`,
want: aliasesConfig{},
},
{
label: "empty",
in: ``,
want: aliasesConfig{},
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i)+"/"+tt.label, func(t *testing.T) {
got, err := parseAliases(bytes.NewBufferString(tt.in))
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("unexpected aliasesConfig:\n got: %+v\n want: %+v", got, tt.want)
return
}
})
}
}