-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy paththeme_test.go
More file actions
1062 lines (894 loc) · 29.5 KB
/
theme_test.go
File metadata and controls
1062 lines (894 loc) · 29.5 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
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package theme
import (
"testing"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
errUtils "github.com/cloudposse/atmos/errors"
iolib "github.com/cloudposse/atmos/pkg/io"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/ui"
)
func TestThemeCommand(t *testing.T) {
t.Run("theme command exists", func(t *testing.T) {
assert.Equal(t, "theme", themeCmd.Use)
assert.NotEmpty(t, themeCmd.Short)
assert.NotEmpty(t, themeCmd.Long)
})
t.Run("has list subcommand", func(t *testing.T) {
hasListCmd := false
for _, subCmd := range themeCmd.Commands() {
if subCmd.Use == "list" {
hasListCmd = true
break
}
}
assert.True(t, hasListCmd, "theme command should have list subcommand")
})
t.Run("has show subcommand", func(t *testing.T) {
hasShowCmd := false
for _, subCmd := range themeCmd.Commands() {
if subCmd.Use == "show <theme-name>" {
hasShowCmd = true
break
}
}
assert.True(t, hasShowCmd, "theme command should have show subcommand")
})
}
func TestSetAtmosConfig(t *testing.T) {
t.Run("sets config successfully", func(t *testing.T) {
config := &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "dracula",
},
},
}
SetAtmosConfig(config)
assert.Equal(t, config, atmosConfigPtr)
})
t.Run("handles nil config", func(t *testing.T) {
SetAtmosConfig(nil)
assert.Nil(t, atmosConfigPtr)
})
}
func TestThemeCommandProvider(t *testing.T) {
provider := &ThemeCommandProvider{}
t.Run("GetCommand returns theme command", func(t *testing.T) {
cmd := provider.GetCommand()
assert.NotNil(t, cmd)
assert.Equal(t, "theme", cmd.Use)
})
t.Run("GetName returns correct name", func(t *testing.T) {
name := provider.GetName()
assert.Equal(t, "theme", name)
})
t.Run("GetGroup returns correct group", func(t *testing.T) {
group := provider.GetGroup()
assert.Equal(t, "Other Commands", group)
})
}
func TestThemeListCommand(t *testing.T) {
t.Run("list command exists", func(t *testing.T) {
assert.Equal(t, "list", themeListCmd.Use)
assert.NotEmpty(t, themeListCmd.Short)
})
t.Run("has recommended flag", func(t *testing.T) {
flag := themeListCmd.Flags().Lookup("recommended")
require.NotNil(t, flag, "list command should have --recommended flag")
assert.Equal(t, "bool", flag.Value.Type())
})
t.Run("parser is initialized", func(t *testing.T) {
assert.NotNil(t, themeListParser, "parser should be initialized")
})
t.Run("accepts no arguments", func(t *testing.T) {
err := themeListCmd.Args(themeListCmd, []string{})
assert.NoError(t, err, "list command should accept no arguments")
err = themeListCmd.Args(themeListCmd, []string{"extra"})
assert.Error(t, err, "list command should reject arguments")
})
}
func TestThemeShowCommand(t *testing.T) {
t.Run("show command exists", func(t *testing.T) {
assert.Equal(t, "show <theme-name>", themeShowCmd.Use)
assert.NotEmpty(t, themeShowCmd.Short)
assert.NotEmpty(t, themeShowCmd.Long)
})
t.Run("accepts zero or one argument (prompt-aware)", func(t *testing.T) {
// Validate Args is prompt-aware: allows 0 or 1 argument, rejects more than 1.
// Zero arguments allowed because interactive prompts will handle missing args.
err := themeShowCmd.Args(themeShowCmd, []string{})
assert.NoError(t, err, "show command should allow zero arguments (prompts will handle)")
err = themeShowCmd.Args(themeShowCmd, []string{"dracula"})
assert.NoError(t, err, "show command should accept one argument")
err = themeShowCmd.Args(themeShowCmd, []string{"dracula", "extra"})
assert.Error(t, err, "show command should reject more than one argument")
})
t.Run("parser is initialized", func(t *testing.T) {
assert.NotNil(t, themeShowParser, "parser should be initialized")
})
}
func TestThemeListOptions(t *testing.T) {
t.Run("creates options with default values", func(t *testing.T) {
opts := &ThemeListOptions{}
assert.False(t, opts.RecommendedOnly, "RecommendedOnly should default to false")
})
t.Run("creates options with recommended enabled", func(t *testing.T) {
opts := &ThemeListOptions{
RecommendedOnly: true,
}
assert.True(t, opts.RecommendedOnly)
})
}
func TestThemeShowOptions(t *testing.T) {
t.Run("creates options with theme name", func(t *testing.T) {
opts := &ThemeShowOptions{
ThemeName: "dracula",
}
assert.Equal(t, "dracula", opts.ThemeName)
})
t.Run("creates options with empty theme name", func(t *testing.T) {
opts := &ThemeShowOptions{}
assert.Empty(t, opts.ThemeName)
})
}
func TestThemeListFlagParser(t *testing.T) {
t.Run("flag parser registers recommended flag", func(t *testing.T) {
flag := themeListCmd.Flags().Lookup("recommended")
require.NotNil(t, flag)
assert.Equal(t, "false", flag.DefValue, "default should be false")
assert.Contains(t, flag.Usage, "recommended", "usage should mention recommended")
})
t.Run("flag parser has no shorthand for recommended", func(t *testing.T) {
flag := themeListCmd.Flags().Lookup("recommended")
require.NotNil(t, flag)
assert.Empty(t, flag.Shorthand, "recommended flag should have no shorthand")
})
}
func TestThemeListFlagHandling(t *testing.T) {
tests := []struct {
name string
setupViper func(v *viper.Viper)
setupCmd func(cmd *cobra.Command) error
getFinalValue func(cmd *cobra.Command, v *viper.Viper) bool
expectedValue bool
}{
{
name: "default value when no flag or env set",
setupViper: func(v *viper.Viper) {
// Clean slate.
},
setupCmd: func(cmd *cobra.Command) error {
return nil
},
getFinalValue: func(cmd *cobra.Command, v *viper.Viper) bool {
return v.GetBool("recommended")
},
expectedValue: false,
},
{
name: "env var sets value to true",
setupViper: func(v *viper.Viper) {
v.Set("recommended", true)
},
setupCmd: func(cmd *cobra.Command) error {
return nil
},
getFinalValue: func(cmd *cobra.Command, v *viper.Viper) bool {
return v.GetBool("recommended")
},
expectedValue: true,
},
{
name: "flag overrides env var (flag=true, env=false)",
setupViper: func(v *viper.Viper) {
v.Set("recommended", false)
},
setupCmd: func(cmd *cobra.Command) error {
return cmd.Flags().Set("recommended", "true")
},
getFinalValue: func(cmd *cobra.Command, v *viper.Viper) bool {
// When flag is set, check the flag directly since that's what Viper will return.
if cmd.Flags().Changed("recommended") {
val, _ := cmd.Flags().GetBool("recommended")
return val
}
return v.GetBool("recommended")
},
expectedValue: true,
},
{
name: "flag overrides env var (flag=false, env=true)",
setupViper: func(v *viper.Viper) {
v.Set("recommended", true)
},
setupCmd: func(cmd *cobra.Command) error {
return cmd.Flags().Set("recommended", "false")
},
getFinalValue: func(cmd *cobra.Command, v *viper.Viper) bool {
// When flag is set, check the flag directly since that's what Viper will return.
if cmd.Flags().Changed("recommended") {
val, _ := cmd.Flags().GetBool("recommended")
return val
}
return v.GetBool("recommended")
},
expectedValue: false,
},
{
name: "flag sets value to true",
setupViper: func(v *viper.Viper) {
// No env var.
},
setupCmd: func(cmd *cobra.Command) error {
return cmd.Flags().Set("recommended", "true")
},
getFinalValue: func(cmd *cobra.Command, v *viper.Viper) bool {
if cmd.Flags().Changed("recommended") {
val, _ := cmd.Flags().GetBool("recommended")
return val
}
return v.GetBool("recommended")
},
expectedValue: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create fresh Viper instance for isolation.
v := viper.New()
// Create a test command to avoid mutating the global themeListCmd.
testCmd := &cobra.Command{
Use: "test-list",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
// Register flags using the parser.
themeListParser.RegisterFlags(testCmd)
// Bind to viper.
err := themeListParser.BindFlagsToViper(testCmd, v)
require.NoError(t, err)
// Setup viper state.
tt.setupViper(v)
// Setup command flags.
err = tt.setupCmd(testCmd)
require.NoError(t, err)
// Get the final value using the test's strategy.
actualValue := tt.getFinalValue(testCmd, v)
// Verify.
assert.Equal(t, tt.expectedValue, actualValue,
"expected recommended=%v, got %v", tt.expectedValue, actualValue)
})
}
}
func TestThemeListParserBindToViper(t *testing.T) {
t.Run("binds flags to viper successfully", func(t *testing.T) {
v := viper.New()
testCmd := &cobra.Command{Use: "test"}
themeListParser.RegisterFlags(testCmd)
err := themeListParser.BindFlagsToViper(testCmd, v)
require.NoError(t, err)
})
t.Run("binds environment variables", func(t *testing.T) {
v := viper.New()
testCmd := &cobra.Command{Use: "test"}
themeListParser.RegisterFlags(testCmd)
err := themeListParser.BindFlagsToViper(testCmd, v)
require.NoError(t, err)
// Simulate environment variable by setting in Viper.
v.Set("recommended", true)
// Verify it's accessible.
assert.True(t, v.GetBool("recommended"))
})
}
func TestThemeShowParserBindToViper(t *testing.T) {
t.Run("binds successfully even with no flags", func(t *testing.T) {
v := viper.New()
testCmd := &cobra.Command{Use: "test"}
themeShowParser.RegisterFlags(testCmd)
err := themeShowParser.BindFlagsToViper(testCmd, v)
require.NoError(t, err)
})
}
func TestThemeListFlagPrecedence(t *testing.T) {
t.Run("flag precedence: CLI flag > env var > default", func(t *testing.T) {
v := viper.New()
testCmd := &cobra.Command{Use: "test"}
themeListParser.RegisterFlags(testCmd)
err := themeListParser.BindFlagsToViper(testCmd, v)
require.NoError(t, err)
// Test 1: Default value.
assert.False(t, v.GetBool("recommended"), "default should be false")
// Test 2: Env var overrides default.
v.Set("recommended", true)
assert.True(t, v.GetBool("recommended"), "env var should override default")
// Test 3: CLI flag overrides env var.
// After BindFlagsToViper, flags take precedence when Changed.
err = testCmd.Flags().Set("recommended", "false")
require.NoError(t, err)
// Check the flag directly since it was changed.
actualValue, err := testCmd.Flags().GetBool("recommended")
require.NoError(t, err)
assert.False(t, actualValue, "CLI flag should override env var")
})
}
func TestExecuteThemeListActiveThemeResolution(t *testing.T) {
tests := []struct {
name string
setupAtmosConfig func() *schema.AtmosConfiguration
setupViper func(v *viper.Viper)
expectedTheme string
}{
{
name: "defaults to atmos when no config",
setupAtmosConfig: func() *schema.AtmosConfiguration {
return nil
},
setupViper: func(v *viper.Viper) {
// No setup - clean state
},
expectedTheme: "atmos",
},
{
name: "defaults to atmos when config has empty theme",
setupAtmosConfig: func() *schema.AtmosConfiguration {
return &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "",
},
},
}
},
setupViper: func(v *viper.Viper) {
// No setup
},
expectedTheme: "atmos",
},
{
name: "uses config theme when set",
setupAtmosConfig: func() *schema.AtmosConfiguration {
return &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "dracula",
},
},
}
},
setupViper: func(v *viper.Viper) {
// No setup
},
expectedTheme: "dracula",
},
{
name: "ATMOS_THEME env var overrides empty config",
setupAtmosConfig: func() *schema.AtmosConfiguration {
return &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "",
},
},
}
},
setupViper: func(v *viper.Viper) {
v.Set("ATMOS_THEME", "monokai")
},
expectedTheme: "monokai",
},
{
name: "THEME env var overrides empty config and ATMOS_THEME not set",
setupAtmosConfig: func() *schema.AtmosConfiguration {
return &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "",
},
},
}
},
setupViper: func(v *viper.Viper) {
v.Set("THEME", "solarized-dark")
},
expectedTheme: "solarized-dark",
},
{
name: "config theme takes precedence over env vars",
setupAtmosConfig: func() *schema.AtmosConfiguration {
return &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "nord",
},
},
}
},
setupViper: func(v *viper.Viper) {
v.Set("ATMOS_THEME", "monokai")
v.Set("THEME", "solarized-dark")
},
expectedTheme: "nord",
},
{
name: "ATMOS_THEME takes precedence over THEME when config empty",
setupAtmosConfig: func() *schema.AtmosConfiguration {
return &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "",
},
},
}
},
setupViper: func(v *viper.Viper) {
v.Set("ATMOS_THEME", "monokai")
v.Set("THEME", "solarized-dark")
},
expectedTheme: "monokai",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup viper
originalViper := viper.GetViper()
defer func() {
// Restore original viper
viper.Reset()
for key, val := range originalViper.AllSettings() {
viper.Set(key, val)
}
}()
// Reset viper for clean state
viper.Reset()
tt.setupViper(viper.GetViper())
// Setup atmos config
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = tt.setupAtmosConfig()
// Create a test command
testCmd := &cobra.Command{
Use: "test-list",
RunE: func(cmd *cobra.Command, args []string) error {
return nil
},
}
// Register flags using the parser
themeListParser.RegisterFlags(testCmd)
err := themeListParser.BindFlagsToViper(testCmd, viper.GetViper())
require.NoError(t, err)
// Execute the theme resolution logic directly
activeTheme := ""
if atmosConfigPtr != nil && atmosConfigPtr.Settings.Terminal.Theme != "" {
activeTheme = atmosConfigPtr.Settings.Terminal.Theme
} else if envTheme := viper.GetString("ATMOS_THEME"); envTheme != "" {
activeTheme = envTheme
} else if envTheme := viper.GetString("THEME"); envTheme != "" {
activeTheme = envTheme
} else {
activeTheme = "atmos"
}
// Verify
assert.Equal(t, tt.expectedTheme, activeTheme,
"expected active theme=%s, got %s", tt.expectedTheme, activeTheme)
})
}
}
func TestExecuteThemeShowOptions(t *testing.T) {
t.Run("creates options with theme name from args", func(t *testing.T) {
// This test verifies the options struct creation logic
themeName := "dracula"
opts := &ThemeShowOptions{
ThemeName: themeName,
}
assert.Equal(t, "dracula", opts.ThemeName)
})
t.Run("creates options with different theme name", func(t *testing.T) {
themeName := "monokai"
opts := &ThemeShowOptions{
ThemeName: themeName,
}
assert.Equal(t, "monokai", opts.ThemeName)
})
}
func TestThemeListOptionsStruct(t *testing.T) {
t.Run("creates options with recommended only false", func(t *testing.T) {
opts := &ThemeListOptions{
RecommendedOnly: false,
}
assert.False(t, opts.RecommendedOnly)
})
t.Run("creates options with recommended only true", func(t *testing.T) {
opts := &ThemeListOptions{
RecommendedOnly: true,
}
assert.True(t, opts.RecommendedOnly)
})
}
func TestExecuteThemeList(t *testing.T) {
// Initialize I/O context and formatter for testing (required for ui.Write/Success/Info).
ioCtx, err := iolib.NewContext()
require.NoError(t, err, "Failed to create I/O context")
ui.InitFormatter(ioCtx)
t.Run("executes with default config defaults to atmos theme", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// Execute
err := executeThemeList(themeListCmd, []string{})
// Should not error - theme registry should have 'atmos' theme
require.NoError(t, err, "executeThemeList should not error when no config is present")
})
t.Run("executes with config containing theme", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "dracula",
},
},
}
// Reset viper
viper.Reset()
defer viper.Reset()
// Execute
err := executeThemeList(themeListCmd, []string{})
// Should not error
require.NoError(t, err, "executeThemeList should not error when config contains theme")
})
t.Run("executes with ATMOS_THEME env var", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "", // Empty config
},
},
}
// Reset viper
viper.Reset()
defer viper.Reset()
// Set ATMOS_THEME env var
t.Setenv("ATMOS_THEME", "monokai")
// Execute
err := executeThemeList(themeListCmd, []string{})
// Should not error
require.NoError(t, err, "executeThemeList should not error when ATMOS_THEME env var is set")
})
t.Run("executes with THEME env var", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "", // Empty config
},
},
}
// Reset viper
viper.Reset()
defer viper.Reset()
// Set THEME env var (ATMOS_THEME not set)
t.Setenv("THEME", "solarized-dark")
// Execute
err := executeThemeList(themeListCmd, []string{})
// Should not error
require.NoError(t, err, "executeThemeList should not error when THEME env var is set")
})
t.Run("executes with recommended flag enabled", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// Set recommended flag
viper.Set("recommended", true)
// Execute
err := executeThemeList(themeListCmd, []string{})
// Should not error
require.NoError(t, err, "executeThemeList should not error when recommended flag is enabled")
})
t.Run("executes with recommended flag disabled", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// Set recommended flag to false
viper.Set("recommended", false)
// Execute
err := executeThemeList(themeListCmd, []string{})
// Should not error
require.NoError(t, err, "executeThemeList should not error when recommended flag is disabled")
})
t.Run("executes with recommended flag via command line", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// Create a test command to simulate flag parsing
testCmd := &cobra.Command{Use: "test-list"}
themeListParser.RegisterFlags(testCmd)
err := themeListParser.BindFlagsToViper(testCmd, viper.GetViper())
require.NoError(t, err)
// Set the flag
err = testCmd.Flags().Set("recommended", "true")
require.NoError(t, err)
// Execute with the test command
err = executeThemeList(testCmd, []string{})
// Should not error
require.NoError(t, err, "executeThemeList should not error with CLI flag")
})
t.Run("verifies plural vs singular message formatting", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// Execute - this should list multiple themes and test the pluralization logic
err := executeThemeList(themeListCmd, []string{})
// Should not error
require.NoError(t, err, "executeThemeList should handle plural formatting")
})
t.Run("executes and displays active theme information", func(t *testing.T) {
// Setup with explicit theme config
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "dracula",
},
},
}
// Reset viper
viper.Reset()
defer viper.Reset()
// Execute - should show dracula as active theme
err := executeThemeList(themeListCmd, []string{})
// Should not error and should display active theme
require.NoError(t, err, "executeThemeList should display active theme information")
})
}
func TestThemeListEnvVarFallback(t *testing.T) {
// Initialize I/O context and formatter for testing.
ioCtx, err := iolib.NewContext()
require.NoError(t, err, "Failed to create I/O context")
ui.InitFormatter(ioCtx)
t.Run("falls back to ATMOS_THEME env var when config empty", func(t *testing.T) {
// Setup - nil config so atmosConfigPtr check fails
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// Set ATMOS_THEME via environment - this should hit line 81-82
t.Setenv("ATMOS_THEME", "dracula")
// Bind ATMOS_THEME env var to Viper and enable automatic env reading
_ = viper.BindEnv("ATMOS_THEME")
viper.AutomaticEnv()
// Execute
err := executeThemeList(themeListCmd, []string{})
require.NoError(t, err)
})
t.Run("falls back to THEME env var when ATMOS_THEME not set", func(t *testing.T) {
// Setup - nil config so atmosConfigPtr check fails
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// Set only THEME via environment (not ATMOS_THEME) - this should hit line 83-84
t.Setenv("THEME", "nord")
// Bind THEME env var to Viper (not ATMOS_THEME) and enable automatic env reading
_ = viper.BindEnv("THEME")
viper.AutomaticEnv()
// Execute
err := executeThemeList(themeListCmd, []string{})
require.NoError(t, err)
})
t.Run("uses default atmos theme when no config or env vars", func(t *testing.T) {
// Setup - nil config
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper - no env vars set
viper.Reset()
defer viper.Reset()
// Explicitly ensure no ATMOS_THEME or THEME env vars - this should hit the else path on line 85
_ = viper.BindEnv("ATMOS_THEME")
_ = viper.BindEnv("THEME")
// Execute - should default to "atmos"
err := executeThemeList(themeListCmd, []string{})
require.NoError(t, err)
})
t.Run("handles empty ATMOS_THEME and falls back to THEME", func(t *testing.T) {
// Setup - nil config
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// Set ATMOS_THEME to empty string (should skip to THEME check)
t.Setenv("ATMOS_THEME", "")
t.Setenv("THEME", "github")
// Bind both env vars and enable automatic env reading
_ = viper.BindEnv("ATMOS_THEME")
_ = viper.BindEnv("THEME")
viper.AutomaticEnv()
// Execute - should use THEME since ATMOS_THEME is empty
err := executeThemeList(themeListCmd, []string{})
require.NoError(t, err)
})
t.Run("prefers config theme over env vars", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
Theme: "github", // Set in config
},
},
}
// Reset viper
viper.Reset()
defer viper.Reset()
// Set both env vars (should be ignored)
viper.Set("ATMOS_THEME", "dracula")
viper.Set("THEME", "nord")
// Execute - should use "github" from config
err := executeThemeList(themeListCmd, []string{})
require.NoError(t, err)
})
}
func TestThemeListResultError(t *testing.T) {
// Initialize I/O context and formatter for testing.
ioCtx, err := iolib.NewContext()
require.NoError(t, err, "Failed to create I/O context")
ui.InitFormatter(ioCtx)
t.Run("handles result with no error", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Reset viper
viper.Reset()
defer viper.Reset()
// This should succeed and not return an error from the result
err := executeThemeList(themeListCmd, []string{})
require.NoError(t, err, "should handle successful result")
})
}
func TestThemesArgCompletion(t *testing.T) {
t.Run("returns list of theme names", func(t *testing.T) {
cmd := &cobra.Command{Use: "test"}
args := []string{}
themes, directive := ThemesArgCompletion(cmd, args, "")
// Should return some themes.
assert.NotEmpty(t, themes, "should return at least one theme")
// Should disable file completion.
assert.Equal(t, cobra.ShellCompDirectiveNoFileComp, directive)
})
t.Run("returns expected built-in themes", func(t *testing.T) {
cmd := &cobra.Command{Use: "test"}
args := []string{}
themes, _ := ThemesArgCompletion(cmd, args, "")
// Check for some expected built-in themes.
assert.Contains(t, themes, "atmos", "should contain 'atmos' theme")
assert.Contains(t, themes, "Dracula", "should contain 'Dracula' theme")
})
t.Run("ignores toComplete parameter", func(t *testing.T) {
cmd := &cobra.Command{Use: "test"}
args := []string{}
// The function returns all themes regardless of toComplete.
themes1, _ := ThemesArgCompletion(cmd, args, "")
themes2, _ := ThemesArgCompletion(cmd, args, "dra")
// Both should return the same list.
assert.Equal(t, themes1, themes2, "should return same themes regardless of toComplete")
})
t.Run("ignores args parameter", func(t *testing.T) {
cmd := &cobra.Command{Use: "test"}
themes1, _ := ThemesArgCompletion(cmd, []string{}, "")
themes2, _ := ThemesArgCompletion(cmd, []string{"arg1", "arg2"}, "")
// Both should return the same list.
assert.Equal(t, themes1, themes2, "should return same themes regardless of args")
})
}
func TestExecuteThemeShowMissingArgs(t *testing.T) {
// Initialize I/O context and formatter for testing.
ioCtx, err := iolib.NewContext()
require.NoError(t, err, "Failed to create I/O context")
ui.InitFormatter(ioCtx)
t.Run("returns error when no positional args provided", func(t *testing.T) {
// Setup
oldAtmosConfig := atmosConfigPtr
defer func() { atmosConfigPtr = oldAtmosConfig }()
atmosConfigPtr = nil
// Disable interactive mode to prevent prompting.
viper.Set("interactive", false)
defer viper.Reset()
// Execute with empty args.
err := executeThemeShow(themeShowCmd, []string{})
// Should return an error for missing theme name.
require.Error(t, err, "executeThemeShow should return an error when no args provided")
assert.ErrorIs(t, err, errUtils.ErrInvalidPositionalArgs, "should be invalid positional args error")
})
}
func TestExecuteThemeShow(t *testing.T) {
// Initialize I/O context and formatter for testing (required for ui.Markdown).
ioCtx, err := iolib.NewContext()