-
Notifications
You must be signed in to change notification settings - Fork 20
/
example_test.go
102 lines (88 loc) · 2.45 KB
/
example_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
package codeowners_test
import (
"bytes"
"fmt"
"regexp"
"github.com/hmarr/codeowners"
)
func Example() {
f := bytes.NewBufferString("src/**/*.c @acme/c-developers")
ruleset, err := codeowners.ParseFile(f)
if err != nil {
panic(err)
}
match, err := ruleset.Match("src/foo.c")
fmt.Println(match.Owners)
match, err = ruleset.Match("src/foo.rs")
fmt.Println(match)
// Output:
// [@acme/c-developers]
// <nil>
}
func ExampleParseFile() {
f := bytes.NewBufferString("src/**/*.go @acme/go-developers # Go code")
ruleset, err := codeowners.ParseFile(f)
if err != nil {
panic(err)
}
fmt.Println(len(ruleset))
fmt.Println(ruleset[0].RawPattern())
fmt.Println(ruleset[0].Owners[0].String())
fmt.Println(ruleset[0].Comment)
// Output:
// 1
// src/**/*.go
// @acme/go-developers
// Go code
}
func ExampleParseFile_customOwnerMatchers() {
validUsernames := []string{"the-a-team", "the-b-team"}
usernameRegexp := regexp.MustCompile(`\A@([a-zA-Z0-9\-]+)\z`)
f := bytes.NewBufferString("src/**/*.go @the-a-team # Go code")
ownerMatchers := []codeowners.OwnerMatcher{
codeowners.OwnerMatchFunc(codeowners.MatchEmailOwner),
codeowners.OwnerMatchFunc(func(s string) (codeowners.Owner, error) {
// Custom owner matcher that only matches valid usernames
match := usernameRegexp.FindStringSubmatch(s)
if match == nil {
return codeowners.Owner{}, codeowners.ErrNoMatch
}
for _, t := range validUsernames {
if t == match[1] {
return codeowners.Owner{Value: match[1], Type: codeowners.TeamOwner}, nil
}
}
return codeowners.Owner{}, codeowners.ErrNoMatch
}),
}
ruleset, err := codeowners.ParseFile(f, codeowners.WithOwnerMatchers(ownerMatchers))
if err != nil {
panic(err)
}
fmt.Println(len(ruleset))
fmt.Println(ruleset[0].RawPattern())
fmt.Println(ruleset[0].Owners[0].String())
fmt.Println(ruleset[0].Comment)
// Output:
// 1
// src/**/*.go
// @the-a-team
// Go code
}
func ExampleRuleset_Match() {
f := bytes.NewBufferString("src/**/*.go @acme/go-developers # Go code")
ruleset, _ := codeowners.ParseFile(f)
match, _ := ruleset.Match("src")
fmt.Println("src", match != nil)
match, _ = ruleset.Match("src/foo.go")
fmt.Println("src/foo.go", match != nil)
match, _ = ruleset.Match("src/foo/bar.go")
fmt.Println("src/foo/bar.go", match != nil)
match, _ = ruleset.Match("src/foo.rs")
fmt.Println("src/foo.rs", match != nil)
// Output:
// src false
// src/foo.go true
// src/foo/bar.go true
// src/foo.rs false
}