-
-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathroot_test.go
More file actions
1022 lines (928 loc) · 27.4 KB
/
root_test.go
File metadata and controls
1022 lines (928 loc) · 27.4 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 cmd
import (
"bytes"
"math"
"os"
"strings"
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
errUtils "github.com/cloudposse/atmos/errors"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/schema"
)
func TestNoColorLog(t *testing.T) {
// Skip in CI environments without TTY.
if _, err := os.Open("/dev/tty"); err != nil {
t.Skipf("Skipping test: TTY not available (/dev/tty): %v", err)
}
// Snapshot RootCmd state to prevent test pollution.
_ = NewTestKit(t)
// Save and restore working directory - previous tests may have changed it.
originalWd, err := os.Getwd()
if err != nil {
t.Fatalf("Failed to get current working directory: %v", err)
}
// Change back to original directory and automatically restore when test ends.
t.Chdir(originalWd)
// Ensure ATMOS_CHDIR is not set BEFORE anything else.
// Previous tests may have set it, and we need to clear it before RootCmd.Execute().
// We can't use t.Setenv here because previous tests may have set it,
// and t.Setenv only restores to the ORIGINAL value before the test package loaded.
os.Unsetenv("ATMOS_CHDIR")
defer os.Unsetenv("ATMOS_CHDIR") // Clean up after test.
stacksPath := "../tests/fixtures/scenarios/stack-templates"
t.Setenv("ATMOS_CLI_CONFIG_PATH", stacksPath)
t.Setenv("ATMOS_BASE_PATH", stacksPath)
t.Setenv("ATMOS_LOGS_LEVEL", "Warning")
// Set the environment variable to disable color
// t.Setenv("NO_COLOR", "1")
t.Setenv("ATMOS_LOGS_LEVEL", "Debug")
t.Setenv("NO_COLOR", "1")
// Create a buffer to capture the output
var buf bytes.Buffer
log.SetOutput(&buf)
// Use SetArgs - TestKit handles cleanup automatically.
RootCmd.SetArgs([]string{"about"})
// Reset buffer to ensure clean state (previous tests may have written to logger).
buf.Reset()
// Execute the command
if err := Execute(); err != nil {
t.Fatalf("Failed to execute command: %v", err)
}
// Check if the output is without color
output := buf.String()
if strings.Contains(output, "\033[") {
t.Errorf("Expected no color in output, but got: %s", output)
}
t.Cleanup(func() {
if t.Failed() {
t.Logf("Command output: %s", output)
}
})
}
func TestInitFunction(t *testing.T) {
// Test doesn't modify os.Args, so no need to save/restore.
// Test cases
tests := []struct {
name string
setup func()
validate func(t *testing.T)
expectedErrMsg string
}{
{
name: "verify command setup",
setup: func() {
// No special setup needed
},
validate: func(t *testing.T) {
// Verify subcommands are properly registered
assert.NotNil(t, RootCmd.Commands())
// Add specific command checks if needed
},
},
{
name: "verify version command setup",
setup: func() {
// No special setup needed
},
validate: func(t *testing.T) {
// Verify the version command is properly configured
versionCmd, _, _ := RootCmd.Find([]string{"version"})
assert.NotNil(t, versionCmd, "version command should be registered")
// Add more specific version command checks
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup
if tt.setup != nil {
tt.setup()
}
// The init() function runs automatically when the package is imported
// We can verify its effects through the RootCmd and other package-level variables
// Validate
if tt.validate != nil {
tt.validate(t)
}
})
}
}
func TestSetupLogger_TraceLevel(t *testing.T) {
// Save original state.
originalLevel := log.GetLevel()
defer func() {
log.SetLevel(originalLevel)
log.SetOutput(os.Stderr) // Reset to default
}()
tests := []struct {
name string
configLevel string
expectedLevel log.Level
}{
{"Trace", "Trace", log.TraceLevel},
{"Debug", "Debug", log.DebugLevel},
{"Info", "Info", log.InfoLevel},
{"Warning", "Warning", log.WarnLevel},
{"Off", "Off", log.Level(math.MaxInt32)},
{"Default", "", log.WarnLevel},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &schema.AtmosConfiguration{
Logs: schema.Logs{
Level: tt.configLevel,
File: "/dev/stderr", // Default log file
},
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{},
},
}
SetupLogger(cfg)
assert.Equal(t, tt.expectedLevel, log.GetLevel(),
"Expected level %v for config %q", tt.expectedLevel, tt.configLevel)
})
}
}
func TestSetupLogger_TraceVisibility(t *testing.T) {
// Save original state.
originalLevel := log.GetLevel()
defer func() {
log.SetLevel(originalLevel)
log.SetOutput(os.Stderr) // Reset to default
}()
var buf bytes.Buffer
log.SetOutput(&buf)
tests := []struct {
name string
configLevel string
traceVisible bool
debugVisible bool
infoVisible bool
}{
{
name: "Trace level shows all",
configLevel: "Trace",
traceVisible: true,
debugVisible: true,
infoVisible: true,
},
{
name: "Debug level hides trace",
configLevel: "Debug",
traceVisible: false,
debugVisible: true,
infoVisible: true,
},
{
name: "Info level hides trace and debug",
configLevel: "Info",
traceVisible: false,
debugVisible: false,
infoVisible: true,
},
{
name: "Warning level hides trace, debug, and info",
configLevel: "Warning",
traceVisible: false,
debugVisible: false,
infoVisible: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &schema.AtmosConfiguration{
Logs: schema.Logs{
Level: tt.configLevel,
File: "", // No file so it uses the pre-set buffer
},
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{},
},
}
SetupLogger(cfg)
// Test trace visibility.
buf.Reset()
log.Trace("trace test message")
hasTrace := strings.Contains(buf.String(), "trace test message")
assert.Equal(t, tt.traceVisible, hasTrace,
"Trace visibility incorrect for %q level", tt.configLevel)
// Test debug visibility.
buf.Reset()
log.Debug("debug test message")
hasDebug := strings.Contains(buf.String(), "debug test message")
assert.Equal(t, tt.debugVisible, hasDebug,
"Debug visibility incorrect for %q level", tt.configLevel)
// Test info visibility.
buf.Reset()
log.Info("info test message")
hasInfo := strings.Contains(buf.String(), "info test message")
assert.Equal(t, tt.infoVisible, hasInfo,
"Info visibility incorrect for %q level", tt.configLevel)
})
}
}
func TestSetupLogger_TraceLevelFromEnvironment(t *testing.T) {
// Save original state.
originalLevel := log.GetLevel()
originalEnv := os.Getenv("ATMOS_LOGS_LEVEL")
defer func() {
log.SetLevel(originalLevel)
log.SetOutput(os.Stderr) // Reset to default
if originalEnv == "" {
os.Unsetenv("ATMOS_LOGS_LEVEL")
} else {
os.Setenv("ATMOS_LOGS_LEVEL", originalEnv)
}
}()
// Test that ATMOS_LOGS_LEVEL=Trace works.
t.Setenv("ATMOS_LOGS_LEVEL", "Trace")
// Simulate loading config from environment.
cfg := &schema.AtmosConfiguration{
Logs: schema.Logs{
Level: os.Getenv("ATMOS_LOGS_LEVEL"),
File: "/dev/stderr", // Default log file
},
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{},
},
}
SetupLogger(cfg)
assert.Equal(t, log.TraceLevel, log.GetLevel(),
"Should set trace level from environment variable")
}
func TestSetupLogger_NoColorWithTraceLevel(t *testing.T) {
// Save original state.
originalLevel := log.GetLevel()
defer func() {
log.SetLevel(originalLevel)
log.SetOutput(os.Stderr) // Reset to default
}()
// Test that trace level works with no-color mode.
cfg := &schema.AtmosConfiguration{
Logs: schema.Logs{
Level: "Trace",
File: "/dev/stderr", // Default log file
},
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
// This simulates NO_COLOR environment.
},
},
}
// Mock the IsColorEnabled to return false.
// Since we can't easily mock it, we'll just test that SetupLogger doesn't panic.
assert.NotPanics(t, func() {
SetupLogger(cfg)
}, "SetupLogger should not panic with trace level and no color")
assert.Equal(t, log.TraceLevel, log.GetLevel(),
"Trace level should be set even with no color")
}
func TestVersionFlagParsing(t *testing.T) {
_ = NewTestKit(t)
tests := []struct {
name string
args []string
expectValue bool
}{
{
name: "--version flag is parsed correctly",
args: []string{"--version"},
expectValue: true,
},
{
name: "no --version flag defaults to false",
args: []string{},
expectValue: false,
},
{
name: "--version can be combined with other flags",
args: []string{"--version", "--no-color"},
expectValue: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = NewTestKit(t)
// Reset flag states before each test - need to reset both value and Changed state.
// Note: --version is a LOCAL flag (not persistent), so use Flags() not PersistentFlags().
versionFlag := RootCmd.Flags().Lookup("version")
if versionFlag != nil {
versionFlag.Value.Set("false")
versionFlag.Changed = false
}
// Use the global RootCmd; state isolation is handled by flag reset above.
RootCmd.SetArgs(tt.args)
// Check that the version flag is defined.
assert.NotNil(t, versionFlag, "version flag should be defined")
assert.Contains(t, versionFlag.Usage, "Atmos CLI version", "usage should mention Atmos CLI version")
// Parse flags.
err := RootCmd.ParseFlags(tt.args)
assert.NoError(t, err, "parsing flags should not error")
// Check if version flag was set to expected value.
versionSet, err := RootCmd.Flags().GetBool("version")
assert.NoError(t, err)
assert.Equal(t, tt.expectValue, versionSet, "version flag should be %v", tt.expectValue)
})
}
}
func TestVersionFlagExecutionPath(t *testing.T) {
tests := []struct {
name string
setup func()
cleanup func()
expectError bool
}{
{
name: "version subcommand works normally without deep exit",
setup: func() {
// Note: --version is a LOCAL flag (not persistent), so use Flags() not PersistentFlags().
versionFlag := RootCmd.Flags().Lookup("version")
if versionFlag != nil {
versionFlag.Value.Set("false")
versionFlag.Changed = false
}
RootCmd.SetArgs([]string{"version"})
},
cleanup: func() {},
expectError: false, // Should complete normally, no error
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Use NewTestKit to isolate RootCmd state.
_ = NewTestKit(t)
t.Cleanup(tt.cleanup)
// Setup test conditions.
tt.setup()
// Execute should complete normally without calling os.Exit.
// The --version flag is now handled in main.go for production,
// so tests only verify the version subcommand works without deep exit.
err := Execute()
if tt.expectError {
assert.Error(t, err, "Execute should return an error")
} else {
assert.NoError(t, err, "Execute should complete without error")
}
})
}
}
func TestPagerDoesNotRunWithoutTTY(t *testing.T) {
// This test verifies that the pager doesn't try to use the alternate screen buffer
// when there's no TTY available. This is important for scripted/CI environments
// where stdin/stdout/stderr are not connected to a terminal.
t.Run("help should not error when ATMOS_PAGER=false and no TTY", func(t *testing.T) {
// Use NewTestKit to isolate RootCmd state.
_ = NewTestKit(t)
// Save original os.Args and os.Exit.
originalArgs := os.Args
originalOsExit := errUtils.OsExit
defer func() {
os.Args = originalArgs
errUtils.OsExit = originalOsExit
}()
// Mock OsExit to prevent test framework panics from remaining deep exits.
// Note: Pager NO LONGER calls os.Exit() (eliminated in cmd/root.go:1239-1241).
// However, other code paths may still exit (e.g., version flag handler).
// This mock catches those until all deep exits are eliminated.
exitCalled := false
errUtils.OsExit = func(code int) {
exitCalled = true
// Don't actually exit in tests
}
// Set ATMOS_CLI_CONFIG_PATH to a test directory to avoid loading real config.
t.Setenv("ATMOS_CLI_CONFIG_PATH", "testdata/pager")
// Set ATMOS_PAGER=false to explicitly disable the pager.
t.Setenv("ATMOS_PAGER", "false")
// Set os.Args so our custom Execute() function can parse them.
// This is required because Execute() needs to initialize atmosConfig from environment variables.
os.Args = []string{"atmos", "--help"}
// Execute should not error even without a TTY.
// The pager should be disabled via ATMOS_PAGER=false, so no TTY error should occur.
// We're primarily checking that there's no "could not open a new TTY" panic/error from pager.
_ = Execute()
// Success: No TTY-related panic occurred.
// The test passing means pager handles missing TTY gracefully.
// Note: exitCalled may be true from other exit paths (version flag, etc.), but
// the important thing is that pager-specific errors don't cause exits anymore.
_ = exitCalled
})
t.Run("help should not error when ATMOS_PAGER=true but no TTY", func(t *testing.T) {
// Use NewTestKit to isolate RootCmd state.
_ = NewTestKit(t)
// Save original os.Args and os.Exit.
originalArgs := os.Args
originalOsExit := errUtils.OsExit
defer func() {
os.Args = originalArgs
errUtils.OsExit = originalOsExit
}()
// Mock OsExit to prevent test framework panics from remaining deep exits.
// Note: Pager NO LONGER calls os.Exit() (eliminated in cmd/root.go:1239-1241).
// The pager's own error handling (pkg/pager/pager.go:88-92) falls back to direct output.
// However, other code paths may still exit (e.g., version flag handler).
// This mock catches those until all deep exits are eliminated.
exitCalled := false
errUtils.OsExit = func(code int) {
exitCalled = true
// Don't actually exit in tests
}
// Set ATMOS_CLI_CONFIG_PATH to a test directory to avoid loading real config.
t.Setenv("ATMOS_CLI_CONFIG_PATH", "testdata/pager")
// Set ATMOS_PAGER=true to try to enable pager, but there's no TTY.
// The pager should detect no TTY and fall back to direct output.
t.Setenv("ATMOS_PAGER", "true")
// Set os.Args so our custom Execute() function can parse them.
os.Args = []string{"atmos", "--help"}
// Execute should not error even without a TTY.
// The pager should detect the lack of TTY and fall back to printing directly.
// We're primarily checking that there's no "could not open a new TTY" panic/error from pager.
_ = Execute()
// Success: No TTY-related panic occurred from pager.
// The test passing means pager handles missing TTY gracefully without exiting.
// Note: exitCalled may be true from other exit paths (version flag, etc.), but
// the important thing is that pager-specific errors don't cause exits anymore.
_ = exitCalled
})
}
// TestIsCompletionCommand tests the isCompletionCommand function.
func TestIsCompletionCommand(t *testing.T) {
tests := []struct {
name string
args []string
compLine string
argComp string
expected bool
}{
{
name: "regular completion command",
args: []string{"atmos", "completion"},
expected: true,
},
{
name: "__complete hidden command",
args: []string{"atmos", "__complete"},
expected: true,
},
{
name: "__completeNoDesc hidden command",
args: []string{"atmos", "__completeNoDesc"},
expected: true,
},
{
name: "COMP_LINE env var set",
args: []string{"atmos", "terraform"},
compLine: "atmos terraform ",
expected: true,
},
{
name: "_ARGCOMPLETE env var set",
args: []string{"atmos", "terraform"},
argComp: "1",
expected: true,
},
{
name: "regular command - not completion",
args: []string{"atmos", "version"},
expected: false,
},
{
name: "no args",
args: []string{"atmos"},
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Setup environment variables.
if tt.compLine != "" {
t.Setenv("COMP_LINE", tt.compLine)
}
if tt.argComp != "" {
t.Setenv("_ARGCOMPLETE", tt.argComp)
}
// Create a mock command with the appropriate name based on the test args.
var cmd *cobra.Command
if len(tt.args) > 1 {
cmd = &cobra.Command{
Use: tt.args[1],
}
}
// Test.
result := isCompletionCommand(cmd)
assert.Equal(t, tt.expected, result)
})
}
}
func TestFindAnsiCodeEnd(t *testing.T) {
tests := []struct {
name string
input string
expected int
}{
{
name: "simple code ending with m",
input: "0m",
expected: 1, // Returns index of 'm'
},
{
name: "color code ending with m",
input: "38;5;123mtext",
expected: 8, // Returns index of 'm'
},
{
name: "no ending letter",
input: "123;456;",
expected: -1,
},
{
name: "uppercase ending",
input: "1A",
expected: 1, // Returns index of 'A'
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := findAnsiCodeEnd(tt.input)
if result != tt.expected {
t.Errorf("findAnsiCodeEnd(%q) = %d, want %d", tt.input, result, tt.expected)
}
})
}
}
func TestIsBackgroundCode(t *testing.T) {
tests := []struct {
name string
ansiCode string
expected bool
}{
{
name: "foreground color code",
ansiCode: "38;5;123m",
expected: false,
},
{
name: "background code with prefix",
ansiCode: "48;5;123m",
expected: true,
},
{
name: "background code in middle",
ansiCode: "0;48;5;123m",
expected: true,
},
{
name: "reset code",
ansiCode: "0m",
expected: false,
},
{
name: "bold code",
ansiCode: "1m",
expected: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := isBackgroundCode(tt.ansiCode)
if result != tt.expected {
t.Errorf("isBackgroundCode(%q) = %v, want %v", tt.ansiCode, result, tt.expected)
}
})
}
}
func TestStripBackgroundCodes(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "plain text no codes",
input: "hello world",
expected: "hello world",
},
{
name: "foreground only",
input: "\x1b[38;5;123mcolored text\x1b[0m",
expected: "\x1b[38;5;123mcolored text\x1b[0m",
},
{
name: "background only",
input: "\x1b[48;5;123mbackground\x1b[0m",
expected: "background\x1b[0m",
},
{
name: "foreground and background mixed (separate sequences)",
input: "\x1b[38;5;123m\x1b[48;5;200mtext\x1b[0m",
expected: "\x1b[38;5;123mtext\x1b[0m",
},
{
name: "combined foreground and background in single sequence (TrueColor)",
input: "\x1b[38;2;255;0;0;48;2;0;0;255mred on blue\x1b[0m",
expected: "\x1b[38;2;255;0;0mred on blue\x1b[0m",
},
{
name: "combined foreground and background in single sequence (256 color)",
input: "\x1b[38;5;123;48;5;200mtext\x1b[0m",
expected: "\x1b[38;5;123mtext\x1b[0m",
},
{
name: "empty string",
input: "",
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := stripBackgroundCodes(tt.input)
if result != tt.expected {
t.Errorf("stripBackgroundCodes(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestNewFlagRenderLayout(t *testing.T) {
tests := []struct {
name string
termWidth int
maxFlagWidth int
wantDescWidth int
}{
{
name: "normal terminal width",
termWidth: 120,
maxFlagWidth: 20,
wantDescWidth: 94, // Calculated as: termWidth minus leftPad minus maxFlag minus spaceBetween minus rightMargin.
},
{
name: "narrow terminal forces minimum",
termWidth: 50,
maxFlagWidth: 20,
wantDescWidth: 40, // Minimum enforced
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
layout := newFlagRenderLayout(tt.termWidth, tt.maxFlagWidth)
if layout.descWidth != tt.wantDescWidth {
t.Errorf("newFlagRenderLayout() descWidth = %d, want %d", layout.descWidth, tt.wantDescWidth)
}
if layout.maxFlagWidth != tt.maxFlagWidth {
t.Errorf("newFlagRenderLayout() maxFlagWidth = %d, want %d", layout.maxFlagWidth, tt.maxFlagWidth)
}
})
}
}
// TestParseChdirFromArgs tests the parseChdirFromArgs function that manually parses --chdir or -C flags.
// This function is critical for commands with DisableFlagParsing=true (terraform, helmfile, packer).
func TestParseChdirFromArgs(t *testing.T) {
tests := []struct {
name string
args []string
expected string
}{
{
name: "--chdir with equals sign",
args: []string{"atmos", "--chdir=/tmp/foo", "terraform", "plan"},
expected: "/tmp/foo",
},
{
name: "--chdir with space",
args: []string{"atmos", "--chdir", "/tmp/bar", "terraform", "plan"},
expected: "/tmp/bar",
},
{
name: "-C with equals sign",
args: []string{"atmos", "-C=/tmp/baz", "terraform", "plan"},
expected: "/tmp/baz",
},
{
name: "-C with space",
args: []string{"atmos", "-C", "/tmp/qux", "terraform", "plan"},
expected: "/tmp/qux",
},
{
name: "-C concatenated (no space or equals)",
args: []string{"atmos", "-C/tmp/concat", "terraform", "plan"},
expected: "/tmp/concat",
},
{
name: "-C concatenated with relative path",
args: []string{"atmos", "-C../foo", "terraform", "plan"},
expected: "../foo",
},
{
name: "no chdir flag",
args: []string{"atmos", "terraform", "plan"},
expected: "",
},
{
name: "--chdir at end without value",
args: []string{"atmos", "terraform", "plan", "--chdir"},
expected: "",
},
{
name: "-C at end without value",
args: []string{"atmos", "terraform", "plan", "-C"},
expected: "",
},
{
name: "multiple --chdir flags (first wins)",
args: []string{"atmos", "--chdir=/first", "--chdir=/second", "terraform", "plan"},
expected: "/first",
},
{
name: "mixed -C and --chdir (first wins)",
args: []string{"atmos", "-C/first", "--chdir=/second", "terraform", "plan"},
expected: "/first",
},
{
name: "--chdir with tilde",
args: []string{"atmos", "--chdir=~/mydir", "terraform", "plan"},
expected: "~/mydir",
},
{
name: "empty args",
args: []string{},
expected: "",
},
{
name: "single arg",
args: []string{"atmos"},
expected: "",
},
{
name: "--chdir after bare -- is ignored",
args: []string{"atmos", "--", "--chdir=/mydir"},
expected: "",
},
{
name: "-C after bare -- is ignored",
args: []string{"atmos", "--", "-C/mydir"},
expected: "",
},
{
name: "--chdir before bare -- is found",
args: []string{"atmos", "--chdir=/mydir", "--", "terraform", "plan"},
expected: "/mydir",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Call the internal function directly with test args.
result := parseChdirFromArgsInternal(tt.args)
// Verify.
assert.Equal(t, tt.expected, result,
"parseChdirFromArgsInternal() with args %v should return %q, got %q",
tt.args, tt.expected, result)
})
}
}
// TestNormalizeOptionalValueFlags tests the normalizeOptionalValueFlags function
// that converts "--flag value" to "--flag=value" for flags with NoOptDefVal.
func TestNormalizeOptionalValueFlags(t *testing.T) {
tests := []struct {
name string
args []string
expected []string
}{
{
name: "identity with space-separated value",
args: []string{"terraform", "plan", "vpc", "-s", "dev", "--identity", "admin"},
expected: []string{"terraform", "plan", "vpc", "-s", "dev", "--identity=admin"},
},
{
name: "identity with equals value is unchanged",
args: []string{"terraform", "plan", "vpc", "--identity=admin", "-s", "dev"},
expected: []string{"terraform", "plan", "vpc", "--identity=admin", "-s", "dev"},
},
{
name: "identity without value (interactive selection)",
args: []string{"terraform", "plan", "vpc", "-s", "dev", "--identity"},
expected: []string{"terraform", "plan", "vpc", "-s", "dev", "--identity"},
},
{
name: "identity followed by another flag",
args: []string{"terraform", "plan", "vpc", "--identity", "--stack", "dev"},
expected: []string{"terraform", "plan", "vpc", "--identity", "--stack", "dev"},
},
{
name: "identity followed by short flag",
args: []string{"terraform", "plan", "vpc", "--identity", "-s", "dev"},
expected: []string{"terraform", "plan", "vpc", "--identity", "-s", "dev"},
},
{
name: "identity after end-of-options marker is not normalized",
args: []string{"terraform", "plan", "vpc", "--", "--identity", "admin"},
expected: []string{"terraform", "plan", "vpc", "--", "--identity", "admin"},
},
{
name: "no optional value flags are unchanged",
args: []string{"terraform", "plan", "vpc", "-s", "dev", "--dry-run"},
expected: []string{"terraform", "plan", "vpc", "-s", "dev", "--dry-run"},
},
{
name: "empty args",
args: []string{},
expected: []string{},
},
{
name: "identity at beginning of args",
args: []string{"--identity", "admin", "terraform", "plan", "vpc"},
expected: []string{"--identity=admin", "terraform", "plan", "vpc"},
},
{
name: "multiple identity flags (edge case)",
args: []string{"terraform", "plan", "--identity", "first", "--identity", "second"},
expected: []string{"terraform", "plan", "--identity=first", "--identity=second"},
},
{
name: "identity with value that looks like a component name",
args: []string{"terraform", "plan", "vpc", "-s", "core-usw2-root", "--identity", "vbpt-core-gbl-root-admin"},
expected: []string{"terraform", "plan", "vpc", "-s", "core-usw2-root", "--identity=vbpt-core-gbl-root-admin"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := normalizeOptionalValueFlags(tt.args)
assert.Equal(t, tt.expected, result)
})
}
}
func TestSetupColorProfile(t *testing.T) {
tests := []struct {
name string
forceColor bool
expectForce bool
}{
{
name: "force color enabled",
forceColor: true,
expectForce: true,
},
{
name: "force color disabled",
forceColor: false,
expectForce: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
atmosConfig := &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{
Terminal: schema.Terminal{
ForceColor: tt.forceColor,
},
},
}
// Should not panic.
setupColorProfile(atmosConfig)
// The function sets global color profile - difficult to test directly,
// but we can verify it doesn't crash.
})
}
}
func TestSetupColorProfileFromEnv(t *testing.T) {
tests := []struct {
name string
envVar string
envValue string
args []string
}{
{
name: "ATMOS_FORCE_COLOR set",
envVar: "ATMOS_FORCE_COLOR",
envValue: "true",
args: []string{},
},
{
name: "force-color flag",
envVar: "",
envValue: "",
args: []string{"atmos", "--force-color", "version"},
},
{
name: "no force color",
envVar: "",
envValue: "",
args: []string{"atmos", "version"},