-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
468 lines (409 loc) · 14.1 KB
/
main_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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package main
import (
"path/filepath"
"strings"
"testing"
)
func Test_parse_args(t *testing.T) {
t.Log("------------expects errors------------")
command :=[]string{"--root", "--series", "--movies"}
_, err := parse_args(command)
if err == nil {
t.Errorf("expected error 'missing root dir path'")
} else {
t.Log("--root", "--series", "--movies", "\n\t", err, "\n")
}
command =[]string{"-s", "--series", "-r", "--root", "-m", "--movies"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'missing series dir path'")
} else {
t.Log("-s", "--series", "-r", "--root", "-m", "--movies", "\n\t", err, "\n")
}
command =[]string{"-r", "-s", "-m"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'missing root dir path'")
} else {
t.Log("-r", "-s", "-m", "\n\t", err, "\n")
}
command =[]string{"-m", "--movies", "-r", "--root", "-s", "--series"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'missing movies dir path'")
} else {
t.Log("-m", "--movies", "-r", "--root", "-s", "--series", "\n\t", err, "\n")
}
command = []string{"--root", "./test_files", "./test_files"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'multiple values for one flag is not allowed'")
} else {
t.Log("--root", "./test_files", "./test_files", "\n\t", err, "\n")
}
command = []string{"-m", "./test_files", "./test_files"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'multiple values for one flag is not allowed'")
} else {
t.Log("-m", "./test_files", "./test_files", "\n\t", err, "\n")
}
command = []string{"-s", "./test_files", "./test_files"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'multiple values for one flag is not allowed'")
} else {
t.Log("-s", "./test_files", "./test_files", "\n\t", err, "\n")
}
command = []string{"./test_files"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'not valid flag'")
} else {
t.Log("./test_files", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "--season-0", "all", "1"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error '1 is not a valid arg. must be yes or no'")
} else {
t.Log("-r", "./test_files", "--season-0", "all", "1", "\n\t", err, "\n")
}
command = []string{"-s0", "all", "yes"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'missing root/series/movies dir path'")
} else {
t.Log("-s0", "all", "yes", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "--season-0", "yes"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'yes is not a valid arg. must be all or var'")
} else {
t.Log("-r", "./test_files", "--season-0", "yes", "\n\t", err, "\n")
}
command = []string{"-ken", "all", "yes"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'missing root/series/movies dir path'")
} else {
t.Log("-ken", "all", "yes", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "-ken", "all", "1"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error '1 is not a valid arg. must be yes or no'")
} else {
t.Log("-r", "./test_files", "-ken", "all", "1", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "--season-0", "-s0"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'only one of --season-0 and -s0 is allowed'")
} else {
t.Log("-r", "./test_files", "--season-0", "-s0", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "-ken", "yes"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'yes is not a valid arg. must be all or var'")
} else {
t.Log("-r", "./test_files", "-ken", "yes", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "--keep-ep-nums", "-ken"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'multiple keep-ep-nums flags'")
} else {
t.Log("-r", "./test_files", "--keep-ep-nums", "-ken", "\n\t", err, "\n")
}
command = []string{"-sen", "all", "yes"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'missing root/series/movies dir path'")
} else {
t.Log("-sen", "all", "yes", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "-sen", "all", "yes"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error '1 is not a valid arg. must be a valid positive integer'")
} else {
t.Log("-r", "./test_files", "-sen", "all", "yes", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "-sen", "1"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error '1 is not a valid arg. must be all or var'")
} else {
t.Log("-r", "./test_files", "-sen", "yes", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "--starting-ep-num", "-sen"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'multiple starting-ep-num flags'")
} else {
t.Log("-r", "./test_files", "--starting-ep-num", "-sen", "\n\t", err, "\n")
}
command = []string{"--root", "./test_files", "-r", "./test_files"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'root directory ./test_files is a duplicate'")
} else {
t.Log("--root", "./test_files", "-r", "./test_files", "\n\t", err, "\n")
}
command = []string{"--root", `.\test_files`, "-s", `.\test_files\Series`, "-m", "./test_files/Movies"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'series directory ./test_files/Series is a subdirectory of root directory ./test_files'")
} else {
t.Log("--root", "./test_files", "-s", "./test_files/Series", "-m", "./test_files/Movies", "\n\t", err, "\n")
}
command = []string{"-r", "./test_files", "--naming-scheme", "S01E01"}
_, err = parse_args(command)
if err == nil {
t.Errorf("expected error 'multiple starting-ep-num flags'")
} else {
t.Log("-r", "./test_files", "--naming-scheme", "S01E01", "\n\t", err, "\n")
}
t.Log("------------expects success------------")
command = []string{"--root", "./test_files", "-s0", "all", "yes"}
_, err = parse_args(command)
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
t.Log("--root", "./test_files", "-s0", "all", "yes")
}
command = []string{"--root", "./test_files"}
_, err = parse_args(command)
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
t.Log("--root", "./test_files")
}
command = []string{"--root", "./test_files", "-s0"}
_, err = parse_args(command)
if err != nil {
t.Errorf("--season-0 can have no value: %s", err)
} else {
t.Log("--root", "./test_files", "-s0")
}
}
func Test_naming_scheme_validation(t *testing.T) {
t.Log("------------expects errors------------")
err := validate_naming_scheme("S<season_num:>E<episode_num:>")
if err == nil {
t.Errorf("expected error 'missing value for token: <season_num:>'")
} else {
t.Log("S<season_num:>E<episode_num:>", "\n\t", err, "\n")
}
err = validate_naming_scheme(`S<season_num: 3l>`)
if err == nil {
t.Errorf("expected error '3l is not a valid arg. must be a valid positive integer'")
} else {
t.Log(`S<season_num: 3l>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`S<season_num: 3`)
if err == nil {
t.Errorf("expected error 'reached end of string but still in an unclosed api: <season_num: 3'")
} else {
t.Log(`S<season_num: 3`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<parent-parent:>`)
if err == nil {
t.Errorf("expected error 'missing value for token: <parent-parent:>'")
} else {
t.Log(`<parent-parent:>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`E<episode_num: -2>`)
if err == nil {
t.Errorf("expected error '-2 is not a valid arg. must be a valid positive integer'")
} else {
t.Log(`E<episode_num: -2>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<parent-parent:1,>`)
if err == nil {
t.Errorf("expected error '1, is not a valid arg. must be two valid positive integers separated by a comma'")
} else {
t.Log(`<parent-parent:1,>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<p-3:1,2,3>`)
if err == nil {
t.Errorf("expected error '1,2,3 is not a valid arg. must be two valid positive integers separated by a comma'")
} else {
t.Log(`<p:1,2,3>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<parent: '\d+(.*)-.*>`)
if err == nil {
t.Errorf(`expected error ' "'\d+(.*)-.*" is not a valid arg. must be a valid regex expression enclosed by two single quotes '`)
} else {
t.Log(`<parent: '\d+(.*)-.*'>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<self: '[]'>`)
if err == nil {
t.Errorf(`expected error ' "[]" is not a valid regex '`)
} else {
t.Log(`<self: [>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<self: '>`)
if err == nil {
t.Errorf(`expected error ' ' is unclosed '`)
} else {
t.Log(`<self: '>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<self: ' '>`)
if err == nil {
t.Errorf(`expected error ' ' ' is empty '`)
} else {
t.Log(`<self: ' '>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<self: ' ' '>`)
if err == nil {
t.Errorf(`expected error ' ' ' ' is empty '`)
} else {
t.Log(`<self: ' ' '>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<self: 2.0,-3>`)
if err == nil {
t.Errorf(`expected error ' -2,-3 is not a valid arg. must be two valid positive integers separated by a comma'`)
} else {
t.Log(`<self: 2.0,-3>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<self: 'adhd' '>`)
if err == nil {
t.Errorf(`expected error ' "" is not a valid regex '`)
} else {
t.Log(`<self: 'adhd' '>`, "\n\t", err, "\n")
}
err = validate_naming_scheme(`<self: 6,5>`)
if err == nil {
t.Errorf("expected error '6,5 is not a valid range. begin (6) must be less than or equal to end (5)'")
} else {
t.Log(`<self: 6,5>`, "\n\t", err, "\n")
}
t.Log("------------expects success------------")
err = validate_naming_scheme("S<season_num>E<episode_num> - <parent-parent> <parent> <p-3> <self>")
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
t.Log("S<season_num>E<episode_num> - <parent-parent> <parent> <p-3> <self>")
}
err = validate_naming_scheme(`S<season_num: 3>E<episode_num: 2> - <parent-parent: 2,3> <parent: '\d+(.*)-.*'> <p-3: '(\d+)'> <self>: 5,5`)
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
t.Log(`S<season_num: 3>E<episode_num: 2> - <parent-parent: 0,1> <parent: '\d+(.*)-.*'> <p-3: '(\d+)'> <self: 5,5>`)
}
err = validate_naming_scheme(`<p> <p-2>`)
if err != nil {
t.Errorf("unexpected error: %s", err)
} else {
t.Log(`<p>`)
}
}
func Test_split_regex_by_pipe(t *testing.T) {
t.Log("------------expects errors------------")
parts := split_regex_by_pipe(``)
if parts[0] != "" {
t.Errorf("expected empty string")
} else {
t.Log(`''`, "\n\t", parts, "\n")
}
parts = split_regex_by_pipe(`|`)
if len(parts) != 2 {
if parts[0] != "" && parts[1] != "" {
t.Errorf("expected empty string; got '%s', '%s'", parts[0], parts[1])
}
} else {
t.Log(`'|'`, "\n\t", parts)
for _, part := range parts {
t.Log("\t",part, "has only one match group?", has_only_one_match_group(part))
}
}
parts = split_regex_by_pipe(`a|b|c`)
if len(parts) != 3 {
if parts[0] != "a" {
t.Errorf("expected 'a'; got '%s'", parts[0])
}
if parts[1] != "b" {
t.Errorf("expected 'b'; got '%s'", parts[1])
}
if parts[2] != "c" {
t.Errorf("expected 'c'; got '%s'", parts[2])
}
} else {
t.Log(`'a|b|c'`, "\n\t", parts)
for _, part := range parts {
t.Log("\t",part, "has only one match group?", has_only_one_match_group(part))
}
}
parts = split_regex_by_pipe(`(a|b|c)`)
if len(parts) != 1 {
if parts[0] != "(a|b|c)" {
t.Errorf("expected '(a|b|c)'; got '%s'", parts[0])
}
} else {
t.Log(`'(a|b|c)'`, "\n\t", parts)
for _, part := range parts {
t.Log("\t",part, "has only one match group?", has_only_one_match_group(part))
}
}
parts = split_regex_by_pipe(`(a)b(c)|(d|f)e`)
if len(parts) != 2 {
if parts[0] != "(a)b(c)" {
t.Errorf("expected '(a)b(c)'; got '%s'", parts[0])
}
if parts[1] != "(d|f)e" {
t.Errorf("expected '(d|f)e'; got '%s'", parts[1])
}
} else {
t.Log(`'(a)b(c)|(d|f)e'`, "\n\t", parts)
for _, part := range parts {
t.Log("\t",part, "has only one match group?", has_only_one_match_group(part))
}
}
t.Log("------------expects success------------")
parts = split_regex_by_pipe(`(a)bc|(d|f)e`)
if len(parts) != 2 {
if parts[0] != "(a)bc" {
t.Errorf("expected '(a)bc'; got '%s'", parts[0])
}
if parts[1] != "(d|f)e" {
t.Errorf("expected '(d|f)e'; got '%s'", parts[1])
}
} else {
t.Log(`'(a)bc|(d|f)e'`, "\n\t", parts)
for _, part := range parts {
t.Log("\t",part, "has only one match group?", has_only_one_match_group(part))
}
}
}
func Test_generate_new_name(t *testing.T) {
path := filepath.Clean(`.test_files\Series\Series_seasonal\Season 1\1234567890.mp4`)
t.Log("------------expects success------------")
name, err := generate_new_name(some[string](`S<season_num: 3>E<episode_num: 2> - <parent-parent: '([^_]+)_.*$'> <parent: '([^ ]+) \d+'> <p-3: 'r(.*)$'> <self: 5,6>`),
2, 1, 3, 2,
"title", path)
if err != nil {
t.Error("expected no error; got", err)
} else {
if strings.ReplaceAll(name, `.test_files\Series\Series_seasonal\Season 1\S001E02 - Series Season ies 67.mp4`, "") != "" {
t.Errorf(`expected '.test_files\Series\Series_seasonal\Season 1\S001E02 - Series Season ies 67.mp4' got '%s'`, name)
} else {
t.Log("\n\told:\t\t", filepath.Base(path), "\n\tnaming scheme:\t", `S<season_num: 3>E<episode_num: 2> - <parent-parent: '([^_]+)_.*$'> <parent: '([^ ]+) \d+'> <p-3: 'r(.*)$'> <self: 5,6>`, "\n\tnew:\t\t", name)
}
}
name, err = generate_new_name(some[string](`<p>`),
2, 1, 3, 2,
"title", path)
if err != nil {
t.Error("expected no error; got", err)
} else {
if strings.ReplaceAll(name, `.test_files\Series\Series_seasonal\Season 1\Season 1.mp4`, "") != "" {
t.Errorf(`expected '.test_files\Series\Series_seasonal\Season 1\Season 1.mp4' got '%s'`, name)
} else {
t.Log("\n\told:\t\t", filepath.Base(path), "\n\tnaming scheme:\t", `<p> <p-2>`, "\n\tnew:\t\t", name)
}
}
}