-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
codeowners_test.go
389 lines (332 loc) · 10.3 KB
/
codeowners_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
package codeowners
import (
"bytes"
"fmt"
"io"
"os"
"path"
"runtime"
"strings"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
//nolint:gochecknoglobals
var (
sample = `# comment
* @everyone
foobar/ [email protected] # inline comment
docs/** @org/docteam @joe`
sample2 = `* @hairyhenderson`
sample3 = `baz/* @baz @qux`
sample4 = `[test]
* @everyone
[test2][2]
*/foo @everyoneelse`
// based on https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#codeowners-syntax
// with a few unimportant modifications
fullSample = `# This is a comment.
# Each line is a file pattern followed by one or more owners.
# These owners will be the default owners for everything in
# the repo. Unless a later match takes precedence,
# @global-owner1 and @global-owner2 will be requested for
# review when someone opens a pull request.
* @global-owner1 @global-owner2
# Order is important; the last matching pattern takes the most
# precedence. When someone opens a pull request that only
# modifies JS files, only @js-owner and not the global
# owner(s) will be requested for a review.
*.js @js-owner
# You can also use email addresses if you prefer. They'll be
# used to look up users just like we do for commit author
# emails.
*.go [email protected]
# In this example, @doctocat owns any files in the build/logs
# directory at the root of the repository and any of its
# subdirectories.
/build/logs/ @doctocat
# In this example, @fooowner owns any files in the /cells/foo
# directory at the root of the repository and any of its
# subdirectories and files.
/cells/foo @fooowner
# The 'docs/*' pattern will match files like
# 'docs/getting-started.md' but not further nested files like
# 'docs/build-app/troubleshooting.md'.
docs/* [email protected]
# In this example, @octocat owns any file in an apps directory
# anywhere in your repository.
apps/ @octocat
# In this example, @doctocat owns any file in the '/docs'
# directory in the root of your repository.
/docs/ @doctocat
foobar/ @fooowner
\#foo/ @hashowner
docs/*.md @mdowner
# this example tests an escaped space in the path
space/test\ space/ @spaceowner
# In this example, @infra owns any file and directory in the
# '/terraform' directory in the root of your repository.
/terraform @infra
`
gitlabSections = `# This is a GitLab section with default owners.
[Team 1][1] @default1 @default2
*.js @js-owner
*.txt
# This is another section with new defaults.
[Team 2] @default3 @default4
*.java @java-owner
*
# This is an optional sections without defaults.
^[Team 3]
*.go @team-1
`
codeowners []Codeowner
)
func TestParseGitLabSectionsWithDefaults(t *testing.T) {
t.Parallel()
r := bytes.NewBufferString(gitlabSections)
c, _ := parseCodeowners(r)
expected := []Codeowner{
co("*.js", []string{"@js-owner"}),
co("*.txt", []string{"@default1", "@default2"}),
co("*.java", []string{"@java-owner"}),
co("*", []string{"@default3", "@default4"}),
co("*.go", []string{"@team-1"}),
}
assert.Equal(t, expected, c)
}
func TestParseCodeowners(t *testing.T) {
t.Parallel()
r := bytes.NewBufferString(sample)
c, _ := parseCodeowners(r)
expected := []Codeowner{
co("*", []string{"@everyone"}),
co("foobar/", []string{"[email protected]"}),
co("docs/**", []string{"@org/docteam", "@joe"}),
}
assert.Equal(t, expected, c)
}
func TestParseCodeownersSections(t *testing.T) {
t.Parallel()
r := bytes.NewBufferString(sample4)
c, _ := parseCodeowners(r)
expected := []Codeowner{
co("*", []string{"@everyone"}),
co("*/foo", []string{"@everyoneelse"}),
}
assert.Equal(t, expected, c)
}
func BenchmarkParseCodeowners(b *testing.B) {
var c []Codeowner
for n := 0; n < b.N; n++ {
b.StopTimer()
r := bytes.NewBufferString(sample)
b.StartTimer()
c, _ = parseCodeowners(r)
}
codeowners = c
}
func BenchmarkOwners(b *testing.B) {
c, _ := FromReader(strings.NewReader(fullSample), "")
data := []string{
"#foo/bar.go",
"blah/docs/README.md",
"foo/bar/docs/foo/foo.js",
"/space/test space/doc1.txt",
"/terraform/kubernetes",
}
for _, d := range data {
b.Run(d, func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = c.Owners(d)
}
})
}
}
func TestFindCodeownersFile(t *testing.T) {
fsys := fstest.MapFS{
"src/.github/CODEOWNERS": &fstest.MapFile{Data: []byte(sample)},
"src/foo/CODEOWNERS": &fstest.MapFile{Data: []byte(sample2)},
"src/foo/qux/docs/CODEOWNERS": &fstest.MapFile{Data: []byte(sample3)},
"src/bar/CODEOWNERS": &fstest.MapFile{Data: []byte(sample2)},
"src/bar/.github/CODEOWNERS": &fstest.MapFile{Data: []byte(sample)},
"src/bar/.gitlab/CODEOWNERS": &fstest.MapFile{Data: []byte(sample3)},
}
r, root, err := findCodeownersFile(fsys, "src")
require.NoError(t, err)
assert.NotNil(t, r)
assert.Equal(t, "src", root)
b, _ := io.ReadAll(r)
assert.Equal(t, sample, string(b))
r, root, err = findCodeownersFile(fsys, "src/foo/bar")
require.NoError(t, err)
assert.NotNil(t, r)
assert.Equal(t, "src/foo", root)
b, _ = io.ReadAll(r)
assert.Equal(t, sample2, string(b))
r, root, err = findCodeownersFile(fsys, "src/foo/qux/quux")
require.NoError(t, err)
assert.NotNil(t, r)
assert.Equal(t, "src/foo/qux", root)
b, _ = io.ReadAll(r)
assert.Equal(t, sample3, string(b))
r, _, err = findCodeownersFile(fsys, ".")
require.NoError(t, err)
assert.Nil(t, r)
r, root, err = findCodeownersFile(fsys, "src/bar")
require.NoError(t, err)
assert.NotNil(t, r)
assert.Equal(t, "src/bar", root)
b, _ = io.ReadAll(r)
assert.Equal(t, sample, string(b))
}
func co(pattern string, owners []string) Codeowner {
c, err := NewCodeowner(pattern, owners)
if err != nil {
panic(err)
}
return c
}
func TestFullParseCodeowners(t *testing.T) {
t.Parallel()
c, _ := parseCodeowners(strings.NewReader(fullSample))
codeowners := &Codeowners{
repoRoot: "/build",
Patterns: c,
}
// these tests were ported from https://github.com/softprops/codeowners
data := []struct {
path string
owners []string
}{
{"#foo/bar.go", []string{"@hashowner"}},
{"foobar/baz.go", []string{"@fooowner"}},
{"/docs/README.md", []string{"@mdowner"}},
// XXX: uncertain about this one
{"blah/docs/README.md", []string{"[email protected]"}},
{"foo.txt", []string{"@global-owner1", "@global-owner2"}},
{"foo/bar.txt", []string{"@global-owner1", "@global-owner2"}},
{"foo.js", []string{"@js-owner"}},
{"foo/bar.js", []string{"@js-owner"}},
{"foo.go", []string{"[email protected]"}},
{"foo/bar.go", []string{"[email protected]"}},
// relative to root
{"build/logs/foo.go", []string{"@doctocat"}},
{"build/logs/foo/bar.go", []string{"@doctocat"}},
// not relative to root
{"foo/build/logs/foo.go", []string{"[email protected]"}},
// docs anywhere
{"foo/docs/foo.js", []string{"[email protected]"}},
{"foo/bar/docs/foo.js", []string{"[email protected]"}},
// but not nested
{"foo/bar/docs/foo/foo.js", []string{"@js-owner"}},
{"foo/apps/foo.js", []string{"@octocat"}},
{"docs/foo.js", []string{"@doctocat"}},
{"/docs/foo.js", []string{"@doctocat"}},
{"/space/test space/doc1.txt", []string{"@spaceowner"}},
{"/terraform/kubernetes", []string{"@infra"}},
{"/cells/foo", []string{"@fooowner"}},
{"/cells/foo/", []string{"@fooowner"}},
{"/cells/foo/bar", []string{"@fooowner"}},
{"/cells/foo/bar/quux", []string{"@fooowner"}},
}
for _, d := range data {
t.Run(fmt.Sprintf("%q==%#v", d.path, d.owners), func(t *testing.T) {
assert.EqualValues(t, d.owners, codeowners.Owners(d.path))
})
}
}
func TestOwners(t *testing.T) {
foo := []string{"@foo"}
bar := []string{"@bar"}
baz := []string{"@baz"}
data := []struct {
patterns []Codeowner
path string
expected []string
}{
{[]Codeowner{co("a/*", foo)}, "c/b", nil},
{[]Codeowner{co("**", foo)}, "a/b", foo},
{[]Codeowner{co("**", foo), co("a/b/*", bar)}, "a/b/c", bar},
{[]Codeowner{co("**", foo), co("a/b/*", bar), co("a/b/c", baz)}, "a/b/c", baz},
{[]Codeowner{co("**", foo), co("a/*/c", bar), co("a/b/*", baz)}, "a/b/c", baz},
{[]Codeowner{co("**", foo), co("a/b/*", bar), co("a/b/", baz)}, "a/b/bar", baz},
{[]Codeowner{co("**", foo), co("a/b/*", bar), co("a/b/", baz)}, "/someroot/a/b/bar", baz},
{[]Codeowner{
co("*", foo),
co("/a/*", bar),
co("/b/**", baz)}, "/a/aa/file", foo},
{[]Codeowner{
co("*", foo),
co("/a/**", bar)}, "/a/bb/file", bar},
{[]Codeowner{
co("*", []string{"@foo", "@bar"}),
co("/bar/", bar)}, "/bar/quux", bar},
{[]Codeowner{
co("*", []string{"@foo", "@bar"}),
co("/bar", bar)}, "/bar", bar},
{[]Codeowner{
co("*", []string{"@foo", "@bar"}),
co("/bar", bar)}, "/bar/quux", bar},
}
for _, d := range data {
t.Run(fmt.Sprintf("%s==%s", d.path, d.expected), func(t *testing.T) {
c := &Codeowners{Patterns: d.patterns, repoRoot: "/someroot"}
owners := c.Owners(d.path)
assert.Equal(t, d.expected, owners)
})
}
}
func TestCombineEscapedSpaces(t *testing.T) {
data := []struct {
fields []string
expected []string
}{
{[]string{"docs/", "@owner"}, []string{"docs/", "@owner"}},
{[]string{"docs/bob/**", "@owner"}, []string{"docs/bob/**", "@owner"}},
{[]string{"docs/bob\\", "test/", "@owner"}, []string{"docs/bob test/", "@owner"}},
{[]string{"docs/bob\\", "test/sub/final\\", "space/", "@owner"}, []string{"docs/bob test/sub/final space/", "@owner"}},
{[]string{"docs/bob\\", "test/another\\", "test/**", "@owner"}, []string{"docs/bob test/another test/**", "@owner"}},
}
for _, d := range data {
t.Run(fmt.Sprintf("%s==%s", d.fields, d.expected), func(t *testing.T) {
assert.Equal(t, d.expected, combineEscapedSpaces(d.fields))
})
}
}
func cwd() string {
_, filename, _, _ := runtime.Caller(0)
cwd := path.Dir(filename)
return cwd
}
func ExampleFromFile() {
c, _ := FromFile(cwd())
fmt.Println(c.Patterns[0])
// Output:
// * @hairyhenderson
}
func ExampleFromFileWithFS() {
// open filesystem rooted at current working directory
fsys := os.DirFS(cwd())
c, _ := FromFileWithFS(fsys, ".")
fmt.Println(c.Patterns[0])
// Output:
// * @hairyhenderson
}
func ExampleFromReader() {
reader := strings.NewReader(sample2)
c, _ := FromReader(reader, "")
fmt.Println(c.Patterns[0])
// Output:
// * @hairyhenderson
}
func ExampleCodeowners_Owners() {
c, _ := FromFile(cwd())
owners := c.Owners("README.md")
for i, o := range owners {
fmt.Printf("Owner #%d is %s\n", i, o)
}
// Output:
// Owner #0 is @hairyhenderson
}