forked from hmarr/codeowners
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
166 lines (160 loc) · 3.56 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
package codeowners
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseRule(t *testing.T) {
examples := []struct {
name string
rule string
expected Rule
err string
}{
// Success cases
{
name: "username owners",
rule: "file.txt @user",
expected: Rule{
pattern: mustBuildPattern(t, "file.txt"),
Owners: []Owner{{Value: "user", Type: "username"}},
},
},
{
name: "team owners",
rule: "file.txt @org/team",
expected: Rule{
pattern: mustBuildPattern(t, "file.txt"),
Owners: []Owner{{Value: "org/team", Type: "team"}},
},
},
{
name: "email owners",
rule: "file.txt foo@example.com",
expected: Rule{
pattern: mustBuildPattern(t, "file.txt"),
Owners: []Owner{{Value: "foo@example.com", Type: "email"}},
},
},
{
name: "multiple owners",
rule: "file.txt @user @org/team foo@example.com",
expected: Rule{
pattern: mustBuildPattern(t, "file.txt"),
Owners: []Owner{
{Value: "user", Type: "username"},
{Value: "org/team", Type: "team"},
{Value: "foo@example.com", Type: "email"},
},
},
},
{
name: "complex patterns",
rule: "d?r/* @user",
expected: Rule{
pattern: mustBuildPattern(t, "d?r/*"),
Owners: []Owner{{Value: "user", Type: "username"}},
},
},
{
name: "pattern with space",
rule: "foo\\ bar @user",
expected: Rule{
pattern: mustBuildPattern(t, "foo\\ bar"),
Owners: []Owner{{Value: "user", Type: "username"}},
},
},
{
name: "comments",
rule: "file.txt @user # some comment",
expected: Rule{
pattern: mustBuildPattern(t, "file.txt"),
Owners: []Owner{{Value: "user", Type: "username"}},
Comment: "some comment",
},
},
{
name: "pattern with no owners",
rule: "pattern",
expected: Rule{
pattern: mustBuildPattern(t, "pattern"),
Owners: nil,
Comment: "",
},
},
{
name: "pattern with no owners and comment",
rule: "pattern # but no more",
expected: Rule{
pattern: mustBuildPattern(t, "pattern"),
Owners: nil,
Comment: "but no more",
},
},
{
name: "pattern with no owners with whitespace",
rule: "pattern ",
expected: Rule{
pattern: mustBuildPattern(t, "pattern"),
Owners: nil,
Comment: "",
},
},
{
name: "pattern with leading and trailing whitespace",
rule: " pattern @user ",
expected: Rule{
pattern: mustBuildPattern(t, "pattern"),
Owners: []Owner{{Value: "user", Type: "username"}},
Comment: "",
},
},
{
name: "pattern with leading and trailing whitespace and no owner",
rule: " pattern ",
expected: Rule{
pattern: mustBuildPattern(t, "pattern"),
Owners: nil,
Comment: "",
},
},
// Error cases
{
name: "empty rule",
rule: "",
err: "unexpected end of rule",
},
{
name: "malformed patterns",
rule: "file.{txt @user",
err: "unexpected character '{' at position 6",
},
{
name: "patterns with brackets",
rule: "file.[cC] @user",
err: "unexpected character '[' at position 6",
},
{
name: "malformed owners",
rule: "file.txt missing-at-sign",
err: "invalid owner format 'missing-at-sign' at position 10",
},
}
for _, e := range examples {
t.Run("parses "+e.name, func(t *testing.T) {
actual, err := parseRule(e.rule)
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
}