-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
874 lines (741 loc) · 20.8 KB
/
cli_test.go
File metadata and controls
874 lines (741 loc) · 20.8 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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
package main
import (
"context"
"errors"
"fmt"
"testing"
"github.com/urfave/cli/v3"
)
// =============================================================================
// CLI Structure Tests
// =============================================================================
func TestCLI_HasCommands(t *testing.T) {
t.Parallel()
cmd := NewCLI()
if len(cmd.Commands) != 6 {
t.Errorf("expected 6 commands (login, logout, status, sync, watch, unmapped), got %d", len(cmd.Commands))
}
commandNames := make(map[string]bool)
for _, c := range cmd.Commands {
commandNames[c.Name] = true
}
expectedCommands := []string{"login", "logout", "status", "sync", "watch", "unmapped"}
for _, name := range expectedCommands {
if !commandNames[name] {
t.Errorf("missing command: %s", name)
}
}
}
func TestCLI_HasFlags(t *testing.T) {
t.Parallel()
cmd := NewCLI()
if len(cmd.Flags) != 13 {
t.Errorf("expected 13 flags on root command, got %d", len(cmd.Flags))
}
// Check that important flags exist
flagNames := make(map[string]bool)
for _, f := range cmd.Flags {
flagNames[f.Names()[0]] = true
}
expectedFlags := []string{
"config", "force", "dry-run", "manga", "all", "verbose", "reverse-direction",
"offline-db", "offline-db-force-refresh", "arm-api", "arm-api-url", "jikan-api",
}
for _, name := range expectedFlags {
if !flagNames[name] {
t.Errorf("missing flag: %s", name)
}
}
}
func TestCLI_SyncCommand_HasFlags(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var syncCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == "sync" {
syncCmd = c
break
}
}
if syncCmd == nil {
t.Fatal("sync command not found")
}
if len(syncCmd.Flags) != 12 {
t.Errorf("expected 12 flags on sync command, got %d", len(syncCmd.Flags))
}
// Check that sync has the right flags
flagNames := make(map[string]bool)
for _, f := range syncCmd.Flags {
flagNames[f.Names()[0]] = true
}
expectedFlags := []string{
"force", "dry-run", "manga", "all", "verbose", "reverse-direction",
"offline-db", "offline-db-force-refresh", "arm-api", "arm-api-url", "jikan-api",
}
for _, name := range expectedFlags {
if !flagNames[name] {
t.Errorf("sync command missing flag: %s", name)
}
}
}
func TestCLI_RootCommand_FlagAliases(t *testing.T) {
t.Parallel()
cmd := NewCLI()
aliases := make(map[string][]string)
for _, f := range cmd.Flags {
aliases[f.Names()[0]] = f.Names()
}
tests := []struct {
flag string
aliases []string
hasAlias bool
}{
{"config", []string{"config", "c"}, true},
{"force", []string{"force", "f"}, true},
{"dry-run", []string{"dry-run", "d"}, true},
{"manga", []string{"manga"}, false},
{"all", []string{"all"}, false},
{"verbose", []string{"verbose"}, false},
{"reverse-direction", []string{"reverse-direction"}, false},
}
for _, tt := range tests {
t.Run(tt.flag, func(t *testing.T) {
actual, ok := aliases[tt.flag]
if !ok {
t.Fatalf("flag %s not found", tt.flag)
}
if !equalSlices(actual, tt.aliases) {
t.Errorf("flag %s: expected aliases %v, got %v", tt.flag, tt.aliases, actual)
}
hasAlias := len(actual) > 1
if hasAlias != tt.hasAlias {
t.Errorf("flag %s: expected hasAlias=%v, got %v", tt.flag, tt.hasAlias, hasAlias)
}
})
}
}
func TestCLI_SyncCommand_FlagAliases(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var syncCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == "sync" {
syncCmd = c
break
}
}
if syncCmd == nil {
t.Fatal("sync command not found")
}
aliases := make(map[string][]string)
for _, f := range syncCmd.Flags {
aliases[f.Names()[0]] = f.Names()
}
tests := []struct {
flag string
aliases []string
hasAlias bool
}{
{"force", []string{"force", "f"}, true},
{"dry-run", []string{"dry-run", "d"}, true},
{"manga", []string{"manga"}, false},
{"all", []string{"all"}, false},
{"verbose", []string{"verbose"}, false},
{"reverse-direction", []string{"reverse-direction"}, false},
}
for _, tt := range tests {
t.Run(tt.flag, func(t *testing.T) {
actual, ok := aliases[tt.flag]
if !ok {
t.Fatalf("flag %s not found", tt.flag)
}
if !equalSlices(actual, tt.aliases) {
t.Errorf("flag %s: expected aliases %v, got %v", tt.flag, tt.aliases, actual)
}
hasAlias := len(actual) > 1
if hasAlias != tt.hasAlias {
t.Errorf("flag %s: expected hasAlias=%v, got %v", tt.flag, tt.hasAlias, hasAlias)
}
})
}
}
func TestCLI_VerboseFlag_NoShortAlias(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var verboseFlag cli.Flag
for _, f := range rootCmd.Flags {
if f.Names()[0] == "verbose" {
verboseFlag = f
break
}
}
if verboseFlag == nil {
t.Fatal("verbose flag not found on root command")
}
names := verboseFlag.Names()
if len(names) != 1 {
t.Errorf("verbose flag should have exactly 1 name (no aliases), got %d: %v", len(names), names)
}
if names[0] != "verbose" {
t.Errorf("verbose flag primary name should be 'verbose', got %s", names[0])
}
}
func equalSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func TestCLI_LoginCommand_HasServiceFlag(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var loginCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == "login" {
loginCmd = c
break
}
}
if loginCmd == nil {
t.Fatal("login command not found")
}
if len(loginCmd.Flags) != 1 {
t.Errorf("expected 1 flag on login command, got %d", len(loginCmd.Flags))
}
flag := loginCmd.Flags[0]
if flag.Names()[0] != "service" {
t.Errorf("expected 'service' flag, got %s", flag.Names()[0])
}
}
func TestCLI_StatusCommand_NoFlags(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var statusCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == "status" {
statusCmd = c
break
}
}
if statusCmd == nil {
t.Fatal("status command not found")
}
if len(statusCmd.Flags) != 0 {
t.Errorf("expected 0 flags on status command, got %d", len(statusCmd.Flags))
}
}
func TestCLI_WatchCommand_HasSyncFlags(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var watchCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == watchCommandName {
watchCmd = c
break
}
}
if watchCmd == nil {
t.Fatal("watch command not found")
}
// watch has 2 own flags (interval, once) + 12 sync flags = 14 total
if len(watchCmd.Flags) != 14 {
t.Errorf("expected 14 flags on watch command (2 watch + 12 sync), got %d", len(watchCmd.Flags))
}
// Check that sync flags are present
flagNames := make(map[string]bool)
for _, f := range watchCmd.Flags {
flagNames[f.Names()[0]] = true
}
syncFlagNames := []string{
"force", "dry-run", "manga", "all", "verbose", "reverse-direction",
"offline-db", "offline-db-force-refresh", "arm-api", "arm-api-url", "jikan-api",
}
for _, name := range syncFlagNames {
if !flagNames[name] {
t.Errorf("watch command missing sync flag: %s", name)
}
}
}
// =============================================================================
// Backward Compatibility Tests
// =============================================================================
func TestCLI_DefaultActionIsSync(t *testing.T) {
t.Parallel()
cmd := NewCLI()
if cmd.Action == nil {
t.Error("root command should have default action (sync)")
}
}
func TestGlobalFlagsAreSet(t *testing.T) {
t.Parallel()
// Verify that the global flag pointers are not nil
if forceSync == nil {
t.Error("forceSync should not be nil")
}
if dryRun == nil {
t.Error("dryRun should not be nil")
}
if mangaSync == nil {
t.Error("mangaSync should not be nil")
}
if allSync == nil {
t.Error("allSync should not be nil")
}
if verbose == nil {
t.Error("verbose should not be nil")
}
}
func TestGlobalFlagsHaveDefaultValues(t *testing.T) {
t.Parallel()
// Default values should be false for all flags
if *forceSync != false {
t.Errorf("expected forceSync default false, got %v", *forceSync)
}
if *dryRun != false {
t.Errorf("expected dryRun default false, got %v", *dryRun)
}
if *mangaSync != false {
t.Errorf("expected mangaSync default false, got %v", *mangaSync)
}
if *allSync != false {
t.Errorf("expected allSync default false, got %v", *allSync)
}
if *verbose != false {
t.Errorf("expected verbose default false, got %v", *verbose)
}
}
// =============================================================================
// Integration Tests
// =============================================================================
func TestCLI_RunWithHelp(t *testing.T) {
t.Parallel()
// Test that we can create CLI and it doesn't panic
cmd := NewCLI()
// Test version is set
if cmd.Version != "" {
// Version is set, which is good
t.Log("CLI has version:", cmd.Version)
}
}
func TestServiceConstants(t *testing.T) {
t.Parallel()
tests := []struct {
name string
constant string
expected string
}{
{"AniList constant", ServiceAnilist, "anilist"},
{"MyAnimeList constant", ServiceMyAnimeList, "myanimelist"},
{"All constant", ServiceAll, "all"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.constant != tt.expected {
t.Errorf("expected %s, got %s", tt.expected, tt.constant)
}
})
}
}
func TestRunCLI_ContextCancellation(t *testing.T) {
t.Parallel()
// Test that RunCLI returns without error when given empty args
// (It will show help/usage, which is not an error)
// We can't fully test without a real config file, but we can
// verify the structure is correct
cmd := NewCLI()
if cmd == nil {
t.Fatal("NewCLI() returned nil")
}
// Verify context handling is set up
ctx := t.Context()
// The Run method should accept context
// This is a compile-time check essentially
_ = ctx
_ = cmd
}
// =============================================================================
// Error Detection Tests
// =============================================================================
func TestIsCancellationError_ContextCanceled(t *testing.T) {
t.Parallel()
tests := []struct {
name string
err error
want bool
}{
{
name: "No error",
err: nil,
want: false,
},
{
name: "Random error",
err: errors.New("random error"),
want: false,
},
{
name: "Direct context.Canceled",
err: context.Canceled,
want: true,
},
{
name: "Wrapped context.Canceled",
err: fmt.Errorf("run app: %w", context.Canceled),
want: true,
},
{
name: "Double wrapped context.Canceled",
err: fmt.Errorf("command failed: %w", fmt.Errorf("run app: %w", context.Canceled)),
want: true,
},
{
name: "Context deadline exceeded (not cancellation)",
err: context.DeadlineExceeded,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := IsCancellationError(tt.err)
if got != tt.want {
t.Errorf("IsCancellationError() = %v, want %v", got, tt.want)
}
})
}
}
// =============================================================================
// Local Flag Tests (verify sync flags don't leak to other commands)
// =============================================================================
func TestCLI_SyncFlagsMarkedAsLocal(t *testing.T) {
t.Parallel()
cmd := NewCLI()
// Flags that should be marked as Local (not inherited by subcommands)
syncSpecificFlags := []string{
"force", "dry-run", "manga", "all", "verbose", "reverse-direction",
"offline-db", "offline-db-force-refresh", "arm-api", "arm-api-url",
}
for _, flagName := range syncSpecificFlags {
t.Run(flagName, func(t *testing.T) {
var foundFlag cli.Flag
for _, f := range cmd.Flags {
if f.Names()[0] == flagName {
foundFlag = f
break
}
}
if foundFlag == nil {
t.Fatalf("flag %s not found on root command", flagName)
}
// Check if flag implements LocalFlag interface
localFlag, ok := foundFlag.(cli.LocalFlag)
if !ok {
t.Errorf("flag %s does not implement LocalFlag interface", flagName)
return
}
if !localFlag.IsLocal() {
t.Errorf("flag %s should be marked as Local (true), got Local=false", flagName)
}
})
}
// config flag should NOT be local (it's truly global)
t.Run("config_is_not_local", func(t *testing.T) {
var configFlag cli.Flag
for _, f := range cmd.Flags {
if f.Names()[0] == "config" {
configFlag = f
break
}
}
if configFlag == nil {
t.Fatal("config flag not found on root command")
}
localFlag, ok := configFlag.(cli.LocalFlag)
if ok && localFlag.IsLocal() {
t.Errorf("config flag should NOT be marked as Local, got Local=true")
}
})
}
func TestCLI_NonSyncCommandsDontInheritSyncFlags(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
// Sync-specific flags that should NOT appear in non-sync subcommands
syncSpecificFlags := []string{
"force", "dry-run", "manga", "all", "verbose", "reverse-direction",
"offline-db", "offline-db-force-refresh", "arm-api", "arm-api-url",
}
nonSyncCommands := []string{"login", "logout", "status"}
for _, cmdName := range nonSyncCommands {
t.Run(cmdName, func(t *testing.T) {
var subCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == cmdName {
subCmd = c
break
}
}
if subCmd == nil {
t.Fatalf("%s command not found", cmdName)
}
// Get the flag names in this subcommand
flagNames := make(map[string]bool)
for _, f := range subCmd.Flags {
flagNames[f.Names()[0]] = true
}
// Verify sync-specific flags are NOT present
for _, syncFlag := range syncSpecificFlags {
if flagNames[syncFlag] {
t.Errorf("%s command should not have sync-specific flag '%s'", cmdName, syncFlag)
}
}
})
}
}
func TestCLI_ARMAPIFlagDescriptionContainsDefault(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var armAPIFlag cli.Flag
for _, f := range rootCmd.Flags {
if f.Names()[0] == "arm-api" {
armAPIFlag = f
break
}
}
if armAPIFlag == nil {
t.Fatal("arm-api flag not found on root command")
}
// Check if flag implements DocGenerationFlag interface to get usage
docFlag, ok := armAPIFlag.(cli.DocGenerationFlag)
if !ok {
t.Fatal("arm-api flag does not implement DocGenerationFlag interface")
}
usage := docFlag.GetUsage()
// Check for both "(default: false)" and that it mentions "anime ID mapping"
if !contains(usage, "(default: false)") {
t.Errorf("arm-api flag usage should contain '(default: false)', got %q", usage)
}
if !contains(usage, "anime ID mapping") {
t.Errorf("arm-api flag usage should contain 'anime ID mapping', got %q", usage)
}
if !contains(usage, "ignored for --manga") {
t.Errorf("arm-api flag usage should contain 'ignored for --manga', got %q", usage)
}
}
func TestCLI_OfflineDBFlagDescriptionContainsDefault(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var offlineDBFlag cli.Flag
for _, f := range rootCmd.Flags {
if f.Names()[0] == "offline-db" {
offlineDBFlag = f
break
}
}
if offlineDBFlag == nil {
t.Fatal("offline-db flag not found on root command")
}
docFlag, ok := offlineDBFlag.(cli.DocGenerationFlag)
if !ok {
t.Fatal("offline-db flag does not implement DocGenerationFlag interface")
}
usage := docFlag.GetUsage()
// Check for both "(default: true)" and that it mentions "anime ID mapping"
if !contains(usage, "(default: true)") {
t.Errorf("offline-db flag usage should contain '(default: true)', got %q", usage)
}
if !contains(usage, "anime ID mapping") {
t.Errorf("offline-db flag usage should contain 'anime ID mapping', got %q", usage)
}
if !contains(usage, "ignored for --manga") {
t.Errorf("offline-db flag usage should contain 'ignored for --manga', got %q", usage)
}
}
// =============================================================================
// Logout Command Tests (missing from original test suite)
// =============================================================================
func TestCLI_LogoutCommand_HasServiceFlag(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var logoutCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == "logout" {
logoutCmd = c
break
}
}
if logoutCmd == nil {
t.Fatal("logout command not found")
}
if len(logoutCmd.Flags) != 1 {
t.Errorf("expected 1 flag on logout command, got %d", len(logoutCmd.Flags))
}
flag := logoutCmd.Flags[0]
if flag.Names()[0] != "service" {
t.Errorf("expected 'service' flag, got %s", flag.Names()[0])
}
}
// =============================================================================
// Watch Command Specific Flags Tests
// =============================================================================
func TestCLI_WatchCommand_HasIntervalAndOnceFlags(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var watchCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == watchCommandName {
watchCmd = c
break
}
}
if watchCmd == nil {
t.Fatal("watch command not found")
}
flagNames := make(map[string]bool)
for _, f := range watchCmd.Flags {
flagNames[f.Names()[0]] = true
}
// Check for watch-specific flags
watchFlags := []string{"interval", "once"}
for _, flagName := range watchFlags {
if !flagNames[flagName] {
t.Errorf("watch command missing flag: %s", flagName)
}
}
}
func TestCLI_WatchCommand_IntervalHasShortAlias(t *testing.T) {
t.Parallel()
rootCmd := NewCLI()
var watchCmd *cli.Command
for _, c := range rootCmd.Commands {
if c.Name == watchCommandName {
watchCmd = c
break
}
}
if watchCmd == nil {
t.Fatal("watch command not found")
}
var intervalFlag cli.Flag
for _, f := range watchCmd.Flags {
if f.Names()[0] == "interval" {
intervalFlag = f
break
}
}
if intervalFlag == nil {
t.Fatal("interval flag not found on watch command")
}
names := intervalFlag.Names()
expectedAliases := []string{"interval", "i"}
if !equalSlices(names, expectedAliases) {
t.Errorf("interval flag: expected aliases %v, got %v", expectedAliases, names)
}
}
// =============================================================================
// getSyncFlagsFromCmd Regression Tests
// =============================================================================
// saveGlobalSyncFlags saves package-level sync flag globals and returns a
// restore function. Use with defer in tests that call getSyncFlagsFromCmd
// to prevent global state leakage between parallel tests.
func saveGlobalSyncFlags() func() {
savedVerbose := verbose
savedForce := forceSync
savedDry := dryRun
savedManga := mangaSync
savedAll := allSync
return func() {
verbose = savedVerbose
forceSync = savedForce
dryRun = savedDry
mangaSync = savedManga
allSync = savedAll
}
}
// TestGetSyncFlagsFromCmd_ReverseDirection ensures the --reverse-direction flag
// is correctly read by getSyncFlagsFromCmd. This is a regression test for a bug
// where cmd.Bool("reverse") was used instead of cmd.Bool("reverse-direction"),
// causing the flag to always return false.
func TestGetSyncFlagsFromCmd_ReverseDirection(t *testing.T) {
defer saveGlobalSyncFlags()()
var gotReverse bool
root := NewCLI()
for _, c := range root.Commands {
if c.Name == "sync" {
c.Action = func(_ context.Context, cmd *cli.Command) error {
_, gotReverse = getSyncFlagsFromCmd(cmd)
return nil
}
break
}
}
ctx := context.Background()
err := root.Run(ctx, []string{"app", "sync", "--reverse-direction"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !gotReverse {
t.Error("getSyncFlagsFromCmd: reverseOut should be true when --reverse-direction is passed")
}
}
func TestGetSyncFlagsFromCmd_NoReverse(t *testing.T) {
defer saveGlobalSyncFlags()()
var gotReverse bool
root := NewCLI()
for _, c := range root.Commands {
if c.Name == "sync" {
c.Action = func(_ context.Context, cmd *cli.Command) error {
_, gotReverse = getSyncFlagsFromCmd(cmd)
return nil
}
break
}
}
ctx := context.Background()
err := root.Run(ctx, []string{"app", "sync"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if gotReverse {
t.Error("getSyncFlagsFromCmd: reverseOut should be false when --reverse-direction is not passed")
}
}
func TestGetSyncFlagsFromCmd_VerboseFlag(t *testing.T) {
defer saveGlobalSyncFlags()()
var gotVerbose bool
root := NewCLI()
for _, c := range root.Commands {
if c.Name == "sync" {
c.Action = func(_ context.Context, cmd *cli.Command) error {
gotVerbose, _ = getSyncFlagsFromCmd(cmd)
return nil
}
break
}
}
ctx := context.Background()
err := root.Run(ctx, []string{"app", "sync", "--verbose"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !gotVerbose {
t.Error("getSyncFlagsFromCmd: verboseOut should be true when --verbose is passed")
}
}
// =============================================================================
// Helper Functions
// =============================================================================
func contains(s, substr string) bool {
return indexOf(s, substr) >= 0
}
func indexOf(s, substr string) int {
for i := 0; i <= len(s)-len(substr); i++ {
if s[i:i+len(substr)] == substr {
return i
}
}
return -1
}