-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmeta_test.go
More file actions
237 lines (216 loc) · 5.02 KB
/
meta_test.go
File metadata and controls
237 lines (216 loc) · 5.02 KB
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
package main
import (
"reflect"
"testing"
)
func TestParseMeta(t *testing.T) {
contents := `
#!/bin/bash
# on push branch=nope
## on push branch=main
## on push branch=wtflol
## on push branch~=gh-readonly-queue/main/.*
## on pull_request
on alalalalalaaaaa
`
want := &Meta{
Events: []MetaEvent{
{
Event: "push",
Conditions: []DirectiveCondition{{Key: "branch", Op: "=", Value: "main"}},
},
{
Event: "push",
Conditions: []DirectiveCondition{{Key: "branch", Op: "=", Value: "wtflol"}},
},
{
Event: "push",
Conditions: []DirectiveCondition{{Key: "branch", Op: "~=", Value: "gh-readonly-queue/main/.*"}},
},
{
Event: "pull_request",
Conditions: []DirectiveCondition{},
},
},
Priority: 0, // Default priority when not specified
Dedup: DedupNone, // Default dedup mode when not specified
Permissions: map[string]string{},
PermissionRepos: []string{},
}
got, err := parseMeta(contents)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("got %v, want %v", got, want)
}
}
func TestParseDirective(t *testing.T) {
tests := []struct {
in string
want *Directive
wantErr bool
}{
{
in: "on push branch=main",
want: &Directive{
Args: []string{"on", "push"},
Conditions: []DirectiveCondition{
{Key: "branch", Op: "=", Value: "main"},
},
},
},
{
in: "on push branch!=main branch!=foo",
want: &Directive{
Args: []string{"on", "push"},
Conditions: []DirectiveCondition{
{Key: "branch", Op: "!=", Value: "main"},
{Key: "branch", Op: "!=", Value: "foo"},
},
}},
{
in: "on push branch=main branch=foo bar",
wantErr: true,
},
{
in: "on push branch=main foo~=foo bar!~=baz",
want: &Directive{
Args: []string{"on", "push"},
Conditions: []DirectiveCondition{
{Key: "branch", Op: "=", Value: "main"},
{Key: "foo", Op: "~=", Value: "foo"},
{Key: "bar", Op: "!~=", Value: "baz"},
},
},
},
{
in: "on push branch=\"foo\"",
want: &Directive{
Args: []string{"on", "push"},
Conditions: []DirectiveCondition{
{Key: "branch", Op: "=", Value: "foo"},
},
},
},
{
in: "on push branch=\"\\\"\"",
want: &Directive{
Args: []string{"on", "push"},
Conditions: []DirectiveCondition{
{Key: "branch", Op: "=", Value: "\""},
},
},
},
{
in: "on push branch=\"\\\\\\n\" branch!=asdf",
want: &Directive{
Args: []string{"on", "push"},
Conditions: []DirectiveCondition{
{Key: "branch", Op: "=", Value: "\\\n"},
{Key: "branch", Op: "!=", Value: "asdf"},
},
},
},
}
for _, test := range tests {
got, err := parseDirective(test.in)
if test.wantErr {
if err == nil {
t.Fatalf("expected error, got nil")
}
continue
}
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, test.want) {
t.Fatalf("got %v, want %v", got, test.want)
}
}
}
func TestParsePriority(t *testing.T) {
content := `#!/bin/bash
## on push branch=main
## priority 10
echo "test"`
meta, err := parseMeta(content)
if err != nil {
t.Fatalf("Failed to parse meta: %v", err)
}
if meta.Priority != 10 {
t.Errorf("Expected priority 10, got %d", meta.Priority)
}
if len(meta.Events) != 1 {
t.Errorf("Expected 1 event, got %d", len(meta.Events))
}
if meta.Events[0].Event != "push" {
t.Errorf("Expected event 'push', got '%s'", meta.Events[0].Event)
}
}
func TestParseDedupMode(t *testing.T) {
tests := []struct {
input string
expected DedupMode
hasError bool
}{
{"none", DedupNone, false},
{"dequeue", DedupDequeue, false},
{"kill", DedupKill, false},
{"invalid", DedupNone, true},
{"", DedupNone, true},
}
for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
result, err := ParseDedupMode(test.input)
if test.hasError {
if err == nil {
t.Errorf("Expected error for input %q, but got none", test.input)
}
} else {
if err != nil {
t.Errorf("Unexpected error for input %q: %v", test.input, err)
}
if result != test.expected {
t.Errorf("Expected %v for input %q, got %v", test.expected, test.input, result)
}
}
})
}
}
func TestDedupModeString(t *testing.T) {
tests := []struct {
mode DedupMode
expected string
}{
{DedupNone, "none"},
{DedupDequeue, "dequeue"},
{DedupKill, "kill"},
{DedupMode(999), "none"}, // Invalid mode should default to "none"
}
for _, test := range tests {
t.Run(test.expected, func(t *testing.T) {
result := test.mode.String()
if result != test.expected {
t.Errorf("Expected %q for mode %v, got %q", test.expected, test.mode, result)
}
})
}
}
func TestParseDedupInMeta(t *testing.T) {
contents := `## priority 5
## dedup kill
## on push branch=main
## permission repo read
`
meta, err := parseMeta(contents)
if err != nil {
t.Fatal(err)
}
if meta.Dedup != DedupKill {
t.Errorf("Expected DedupKill, got %v", meta.Dedup)
}
if meta.Priority != 5 {
t.Errorf("Expected priority 5, got %d", meta.Priority)
}
}