-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_parser_test.go
390 lines (377 loc) · 10.3 KB
/
spec_parser_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
390
package gogh_test
import (
"reflect"
"testing"
testtarget "github.com/kyoh86/gogh/v3"
)
func TestSpecParser(t *testing.T) {
const (
owner1 = "kyoh86"
owner2 = "anonymous"
host1 = "example.com" // host a not default
host2 = "kyoh86.dev" // host a not default
name = "gogh"
)
t.Run("Empty", func(t *testing.T) {
var parser testtarget.SpecParser
for _, testcase := range []struct {
title string
source string
}{{
title: "valid-name",
source: name,
}, {
title: "default-owner,valid-name",
source: owner1 + "/" + name,
}} {
t.Run(testcase.title, func(t *testing.T) {
if _, err := parser.Parse(testcase.source); err == nil {
t.Fatal("expect error, but nil")
}
})
}
})
t.Run("DefaultHost", func(t *testing.T) {
parser := testtarget.NewSpecParser(testtarget.DefaultHost, owner1)
t.Run("ValidInput", func(t *testing.T) {
for _, testcase := range []struct {
title string
source string
expectHost string
expectUser string
expectName string
}{{
title: "valid-name",
source: name,
expectHost: testtarget.DefaultHost,
expectUser: owner1,
expectName: name,
}, {
title: "default-owner,valid-name",
source: owner1 + "/" + name,
expectHost: testtarget.DefaultHost,
expectUser: owner1,
expectName: name,
}, {
title: "default-host,default-owner,valid-name",
source: testtarget.DefaultHost + "/" + owner1 + "/" + name,
expectHost: testtarget.DefaultHost,
expectUser: owner1,
expectName: name,
}, {
title: "valid-host,valid-owner,valid-name",
source: host1 + "/" + owner2 + "/" + name,
expectHost: host1,
expectUser: owner2,
expectName: name,
}} {
t.Run(testcase.title, func(t *testing.T) {
spec, err := parser.Parse(testcase.source)
if err != nil {
t.Fatalf("failed to parse %q: %s", testcase.source, err)
}
if testcase.expectHost != spec.Host() {
t.Errorf("expect host %q but %q gotten", testcase.expectHost, spec.Host())
}
if testcase.expectUser != spec.Owner() {
t.Errorf("expect owner %q but %q gotten", testcase.expectUser, spec.Owner())
}
if testcase.expectName != spec.Name() {
t.Errorf("expect name %q but %q gotten", testcase.expectName, spec.Name())
}
})
}
})
t.Run("InvalidInput", func(t *testing.T) {
for _, testcase := range []struct {
title string
input string
expect error
}{
{
title: "empty",
input: "",
expect: testtarget.ErrEmptyName,
},
{
title: "empty-owner,empty-name",
input: "/",
expect: testtarget.ErrEmptyName,
},
{
title: "empty-owner,valid-name",
input: "/" + name,
expect: testtarget.ErrEmptyOwner,
},
{
title: "valid-owner,dot",
input: owner1 + "/.",
expect: testtarget.ErrInvalidName("'.' is reserved name"),
},
{
title: "valid-owner,dotdot",
input: owner1 + "/..",
expect: testtarget.ErrInvalidName("'..' is reserved name"),
},
{
title: "invalid-owner,valid-name",
input: "space in the owner/" + name,
expect: testtarget.ErrInvalidOwner("invalid owner: space in the owner"),
},
{
title: "valid-owner,empty-name",
input: owner1 + "/",
expect: testtarget.ErrEmptyName,
},
{
title: "valid-owner,invalid-name",
input: owner1 + "/space in the name",
expect: testtarget.ErrInvalidName("invalid name: space in the name"),
},
{
title: "empty-host,valid-owner,valid-name",
input: "/" + owner1 + "/" + name,
expect: testtarget.ErrEmptyHost,
},
{
title: "invalid-host,valid-owner,valid-name",
input: "space in the host/" + owner1 + "/" + name,
expect: testtarget.ErrInvalidHost("invalid host: space in the host"),
},
{
title: "valid-host,empty-owner,valid-name",
input: host1 + "//" + name,
expect: testtarget.ErrEmptyOwner,
},
{
title: "valid-host,invalid-owner,valid-name",
input: host1 + "/space in the owner/" + name,
expect: testtarget.ErrInvalidOwner("invalid owner: space in the owner"),
},
{
title: "valid-host,valid-owner,empty-name",
input: host1 + "/" + owner1 + "/",
expect: testtarget.ErrEmptyName,
},
{
title: "valid-host,valid-owner,invalid-name",
input: host1 + "/" + owner1 + "/space in the name",
expect: testtarget.ErrInvalidName("invalid name: space in the name"),
},
{
title: "valid-host,empty-owner,empty-name",
input: host1 + "//",
expect: testtarget.ErrEmptyName,
},
{
title: "empty-host,valid-owner,empty-name",
input: "/" + owner1 + "/",
expect: testtarget.ErrEmptyName,
},
{
title: "empty-host,empty-owner,valid-name",
input: "//" + name,
expect: testtarget.ErrEmptyOwner,
},
{
title: "empty-host,empty-owner,empty-name",
input: "//",
expect: testtarget.ErrEmptyName,
},
{
title: "unnecessary-following-slash",
input: host1 + "/" + owner1 + "/" + name + "/",
expect: testtarget.ErrTooManySlashes,
},
{
title: "unnecessary-heading-slash",
input: "/" + host1 + "/" + owner1 + "/" + name + "/",
expect: testtarget.ErrTooManySlashes,
},
} {
t.Run(testcase.title, func(t *testing.T) {
spec, _, err := parser.ParseWithAlias(testcase.input)
if err == nil {
t.Fatalf(
"expect failure to parse %q but parsed to %+v",
testcase.input,
spec,
)
}
if reflect.TypeOf(testcase.expect) != reflect.TypeOf(err) {
t.Fatalf(
"expect error %t to parse %q but %t gotten",
testcase.expect,
testcase.input,
err,
)
}
if testcase.expect.Error() != err.Error() {
t.Fatalf(
"expect error value %q to parse %q but %q gotten",
testcase.expect,
testcase.input,
err,
)
}
})
}
})
})
t.Run("WithHost", func(t *testing.T) {
parser := testtarget.NewSpecParser(host1, owner1)
t.Run("ValidInput", func(t *testing.T) {
for _, testcase := range []struct {
title string
source string
expectHost string
expectUser string
expectName string
}{{
title: "valid-name",
source: name,
expectHost: host1,
expectUser: owner1,
expectName: name,
}, {
title: "default-owner,valid-name",
source: owner1 + "/" + name,
expectHost: host1,
expectUser: owner1,
expectName: name,
}, {
title: "default-host,default-owner,valid-name",
source: host1 + "/" + owner1 + "/" + name,
expectHost: host1,
expectUser: owner1,
expectName: name,
}, {
title: "valid-host,valid-owner,valid-name",
source: host2 + "/" + owner2 + "/" + name,
expectHost: host2,
expectUser: owner2,
expectName: name,
}} {
t.Run(testcase.title, func(t *testing.T) {
spec, err := parser.Parse(testcase.source)
if err != nil {
t.Fatalf("failed to parse %q: %s", testcase.source, err)
}
if testcase.expectHost != spec.Host() {
t.Errorf("expect host %q but %q gotten", testcase.expectHost, spec.Host())
}
if testcase.expectUser != spec.Owner() {
t.Errorf("expect owner %q but %q gotten", testcase.expectUser, spec.Owner())
}
if testcase.expectName != spec.Name() {
t.Errorf("expect name %q but %q gotten", testcase.expectName, spec.Name())
}
})
}
})
})
t.Run("ParseWithAlias", func(t *testing.T) {
parser := testtarget.NewSpecParser("default-host", "default-owner")
t.Run("WithoutAlias", func(t *testing.T) {
for _, testcase := range []struct {
title string
source string
}{{
title: "without-alias",
source: name,
}, {
title: "same-name",
source: name + "=" + name,
}} {
_, alias, err := parser.ParseWithAlias(testcase.source)
if err != nil {
t.Fatalf("failed to parse %q: %s", testcase.source, err)
}
if alias != nil {
t.Errorf("want alias is nil but %#v gotten", *alias)
}
}
})
t.Run("WithValidAlias", func(t *testing.T) {
for _, testcase := range []struct {
title string
source string
wantHost string
wantUser string
wantName string
}{{
title: "with-alias-name",
source: host1 + "/" + owner1 + "/" + name + "=alias",
wantHost: host1,
wantUser: owner1,
wantName: "alias",
}, {
title: "with-not-default-repo-with-alias-name",
source: host2 + "/" + owner2 + "/" + name + "=alias",
wantHost: host2,
wantUser: owner2,
wantName: "alias",
}, {
title: "with-alias-owner",
source: host1 + "/" + owner1 + "/" + name + "=" + owner2 + "/alias",
wantHost: host1,
wantUser: owner2,
wantName: "alias",
}} {
t.Run(testcase.title, func(t *testing.T) {
_, alias, err := parser.ParseWithAlias(testcase.source)
if err != nil {
t.Fatalf("failed to parse %q: %s", testcase.source, err)
}
if alias == nil {
t.Fatal("want valid alisa but got nil")
}
if testcase.wantHost != alias.Host() {
t.Errorf("want host %q but %q gotten", testcase.wantHost, alias.Host())
}
if testcase.wantUser != alias.Owner() {
t.Errorf("want owner %q but %q gotten", testcase.wantUser, alias.Owner())
}
if testcase.wantName != alias.Name() {
t.Errorf("want name %q but %q gotten", testcase.wantName, alias.Name())
}
if testcase.wantHost != alias.Host() {
t.Errorf("want host %q but %q gotten", testcase.wantHost, alias.Host())
}
})
}
})
t.Run("WithInvalid", func(t *testing.T) {
for _, testcase := range []struct {
title string
source string
}{{
title: "invalid-name",
source: ".=",
}, {
title: "empty-alias",
source: name + "=",
}, {
title: "space-alias",
source: name + "= ",
}, {
title: "space-in-the-alias",
source: name + "=splitted name",
}, {
title: "double-alias",
source: name + "=alias1=alias2",
}, {
title: "too-many-shashes",
source: name + "=example.com/baz/many",
}} {
t.Run(testcase.title, func(t *testing.T) {
_, _, err := parser.ParseWithAlias(testcase.source)
if err == nil {
t.Fatal("want error, but got nil")
}
t.Log(err)
})
}
})
})
}