File: codeowners_test.go

package info (click to toggle)
golang-github-hairyhenderson-go-codeowners 0.2.2%2Bgit20201026.cdc7c07-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 100 kB
  • sloc: makefile: 2
file content (310 lines) | stat: -rw-r--r-- 7,937 bytes parent folder | download
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
package codeowners

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"path"
	"runtime"
	"strings"
	"testing"

	"github.com/spf13/afero"

	"github.com/stretchr/testify/assert"
)

var (
	sample = `# comment
*	@everyone

   foobar/  someone@else.com

docs/**	@org/docteam @joe`
	sample2 = `* @hairyhenderson`
	sample3 = `baz/* @baz @qux`

	codeowners []Codeowner
)

func TestParseCodeowners(t *testing.T) {
	t.Parallel()
	r := bytes.NewBufferString(sample)
	c := parseCodeowners(r)
	expected := []Codeowner{
		co("*", []string{"@everyone"}),
		co("foobar/", []string{"someone@else.com"}),
		co("docs/**", []string{"@org/docteam", "@joe"}),
	}
	assert.Equal(t, expected, c)
}

func BenchmarkParseCodeowners(b *testing.B) {
	r := bytes.NewBufferString(sample)
	var c []Codeowner

	for n := 0; n < b.N; n++ {
		c = parseCodeowners(r)
	}

	codeowners = c
}

func TestFindCodeownersFile(t *testing.T) {
	oldfs := fs
	defer func() {
		fs = oldfs
	}()
	fs = afero.NewMemMapFs()
	_ = fs.Mkdir("/src/.github", 0755)
	_ = fs.MkdirAll("/src/foo/bar/baz", 0755)
	_ = fs.MkdirAll("/src/foo/qux/docs", 0755)
	_ = fs.MkdirAll("/src/foo/qux/quux", 0755)
	f, _ := fs.Create("/src/.github/CODEOWNERS")
	_, _ = f.WriteString(sample)

	f, _ = fs.Create("/src/foo/CODEOWNERS")
	_, _ = f.WriteString(sample2)

	f, _ = fs.Create("/src/foo/qux/docs/CODEOWNERS")
	_, _ = f.WriteString(sample3)

	r, root, err := findCodeownersFile("/src")
	assert.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "/src", root)
	if r != nil {
		b, _ := ioutil.ReadAll(r)
		assert.Equal(t, sample, string(b))
	}

	r, root, err = findCodeownersFile("/src/foo/bar")
	assert.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "/src/foo", root)
	if r != nil {
		b, _ := ioutil.ReadAll(r)
		assert.Equal(t, sample2, string(b))
	}

	r, root, err = findCodeownersFile("/src/foo/qux/quux")
	assert.NoError(t, err)
	assert.NotNil(t, r)
	assert.Equal(t, "/src/foo/qux", root)
	if r != nil {
		b, _ := ioutil.ReadAll(r)
		assert.Equal(t, sample3, string(b))
	}

	r, _, err = findCodeownersFile("/")
	assert.NoError(t, err)
	assert.Nil(t, r)
}

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()
	// based on https://help.github.com/en/github/creating-cloning-and-archiving-repositories/about-code-owners#codeowners-syntax
	// with a few unimportant modifications
	example := `# 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 docs@example.com

# 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

# The 'docs/*' pattern will match files like
# 'docs/getting-started.md' but not further nested files like
# 'docs/build-app/troubleshooting.md'.
docs/*  docs@example.com

# 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
`
	c := parseCodeowners(strings.NewReader(example))
	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{"docs@example.com"}},
		{"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{"docs@example.com"}},
		{"foo/bar.go",
			[]string{"docs@example.com"}},
		// 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{"docs@example.com"}},
		// docs anywhere
		{"foo/docs/foo.js",
			[]string{"docs@example.com"}},
		{"foo/bar/docs/foo.js",
			[]string{"docs@example.com"}},
		// 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"}},
	}

	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},
	}

	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 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
}