-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathgenerate_test.go
More file actions
512 lines (430 loc) · 14.6 KB
/
generate_test.go
File metadata and controls
512 lines (430 loc) · 14.6 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
package generate
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"
)
func TestGenerateCmd(t *testing.T) {
t.Run("command structure", func(t *testing.T) {
assert.Equal(t, "generate", GenerateCmd.Use)
assert.Equal(t, "Generate Terraform configuration files for Atmos components and stacks", GenerateCmd.Short)
assert.Contains(t, GenerateCmd.Long, "terraform generate")
})
t.Run("has expected subcommands", func(t *testing.T) {
subcommands := GenerateCmd.Commands()
require.NotEmpty(t, subcommands)
// Get subcommand names.
names := make([]string, len(subcommands))
for i, cmd := range subcommands {
names[i] = cmd.Name()
}
assert.Contains(t, names, "backend")
assert.Contains(t, names, "backends")
assert.Contains(t, names, "files")
assert.Contains(t, names, "varfile")
assert.Contains(t, names, "varfiles")
assert.Contains(t, names, "planfile")
})
}
func TestBackendCmd(t *testing.T) {
t.Run("command structure", func(t *testing.T) {
assert.Equal(t, "backend [component]", backendCmd.Use)
assert.Equal(t, "Generate backend configuration for a Terraform component", backendCmd.Short)
})
t.Run("has expected flags", func(t *testing.T) {
flags := backendCmd.Flags()
stackFlag := flags.Lookup("stack")
require.NotNil(t, stackFlag)
assert.Equal(t, "s", stackFlag.Shorthand)
processTemplatesFlag := flags.Lookup("process-templates")
require.NotNil(t, processTemplatesFlag)
assert.Equal(t, "true", processTemplatesFlag.DefValue)
processFunctionsFlag := flags.Lookup("process-functions")
require.NotNil(t, processFunctionsFlag)
assert.Equal(t, "true", processFunctionsFlag.DefValue)
skipFlag := flags.Lookup("skip")
require.NotNil(t, skipFlag)
assert.Equal(t, "[]", skipFlag.DefValue)
})
}
func TestFilesCmd(t *testing.T) {
// Find the files command.
var filesCmdPtr *cobra.Command
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "files" {
filesCmdPtr = cmd
break
}
}
t.Run("command structure", func(t *testing.T) {
require.NotNil(t, filesCmdPtr)
assert.Equal(t, "files [component]", filesCmdPtr.Use)
assert.Contains(t, filesCmdPtr.Short, "Generate files")
})
t.Run("has expected flags", func(t *testing.T) {
require.NotNil(t, filesCmdPtr)
flags := filesCmdPtr.Flags()
stackFlag := flags.Lookup("stack")
require.NotNil(t, stackFlag)
assert.Equal(t, "s", stackFlag.Shorthand)
allFlag := flags.Lookup("all")
require.NotNil(t, allFlag)
stacksFlag := flags.Lookup("stacks")
require.NotNil(t, stacksFlag)
componentsFlag := flags.Lookup("components")
require.NotNil(t, componentsFlag)
dryRunFlag := flags.Lookup("dry-run")
require.NotNil(t, dryRunFlag)
cleanFlag := flags.Lookup("clean")
require.NotNil(t, cleanFlag)
})
}
func TestBackendsCmd(t *testing.T) {
// Find the backends command.
var backendsCmd *BackendsCmd
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "backends" {
backendsCmd = &BackendsCmd{cmd}
break
}
}
t.Run("command structure", func(t *testing.T) {
require.NotNil(t, backendsCmd)
assert.Equal(t, "backends", backendsCmd.Name())
})
}
// BackendsCmd wraps cobra.Command for type safety in tests.
type BackendsCmd struct {
*cobra.Command
}
func TestVarfileCmd(t *testing.T) {
// Find the varfile command.
var varfileCmd *VarfileCmd
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "varfile" {
varfileCmd = &VarfileCmd{cmd}
break
}
}
t.Run("command structure", func(t *testing.T) {
require.NotNil(t, varfileCmd)
assert.Equal(t, "varfile", varfileCmd.Name())
})
}
// VarfileCmd wraps cobra.Command for type safety in tests.
type VarfileCmd struct {
*cobra.Command
}
func TestVarfilesCmd(t *testing.T) {
// Find the varfiles command.
var varfilesCmd *VarfilesCmd
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "varfiles" {
varfilesCmd = &VarfilesCmd{cmd}
break
}
}
t.Run("command structure", func(t *testing.T) {
require.NotNil(t, varfilesCmd)
assert.Equal(t, "varfiles", varfilesCmd.Name())
})
}
// VarfilesCmd wraps cobra.Command for type safety in tests.
type VarfilesCmd struct {
*cobra.Command
}
func TestPlanfileCmd(t *testing.T) {
// Find the planfile command.
var planfileCmd *PlanfileCmd
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "planfile" {
planfileCmd = &PlanfileCmd{cmd}
break
}
}
t.Run("command structure", func(t *testing.T) {
require.NotNil(t, planfileCmd)
assert.Equal(t, "planfile", planfileCmd.Name())
})
}
// PlanfileCmd wraps cobra.Command for type safety in tests.
type PlanfileCmd struct {
*cobra.Command
}
// TestFilesParserSetup verifies that the files parser is properly configured.
func TestFilesParserSetup(t *testing.T) {
require.NotNil(t, filesParser, "filesParser should be initialized")
// Verify the parser has the files-specific flags.
registry := filesParser.Registry()
expectedFlags := []string{
"stack",
"all",
"stacks",
"components",
"dry-run",
"clean",
}
for _, flagName := range expectedFlags {
assert.True(t, registry.Has(flagName), "filesParser should have %s flag registered", flagName)
}
}
// TestFilesCommandArgs verifies that files command accepts the correct number of arguments.
func TestFilesCommandArgs(t *testing.T) {
// Find the files command.
var filesCmdPtr *cobra.Command
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "files" {
filesCmdPtr = cmd
break
}
}
require.NotNil(t, filesCmdPtr)
// The command should accept 0 or 1 argument (component name is optional).
require.NotNil(t, filesCmdPtr.Args)
// Verify with no args (should pass since --all is available).
err := filesCmdPtr.Args(filesCmdPtr, []string{})
assert.NoError(t, err, "files command should accept 0 arguments")
// Verify with one arg.
err = filesCmdPtr.Args(filesCmdPtr, []string{"my-component"})
assert.NoError(t, err, "files command should accept 1 argument")
// Verify with two args (should fail).
err = filesCmdPtr.Args(filesCmdPtr, []string{"arg1", "arg2"})
assert.Error(t, err, "files command should reject more than 1 argument")
}
// TestFilesFlagEnvVars verifies that files command flags have environment variable bindings.
func TestFilesFlagEnvVars(t *testing.T) {
registry := filesParser.Registry()
// Expected env var bindings.
expectedEnvVars := map[string]string{
"stack": "ATMOS_STACK",
"stacks": "ATMOS_STACKS",
"components": "ATMOS_COMPONENTS",
}
for flagName, expectedEnvVar := range expectedEnvVars {
require.True(t, registry.Has(flagName), "filesParser should have %s flag registered", flagName)
flag := registry.Get(flagName)
require.NotNil(t, flag, "filesParser should have info for %s flag", flagName)
envVars := flag.GetEnvVars()
assert.Contains(t, envVars, expectedEnvVar, "%s should be bound to %s", flagName, expectedEnvVar)
}
}
// TestFilesCommandDescription verifies the command description content.
func TestFilesCommandDescription(t *testing.T) {
// Find the files command.
var filesCmdPtr *cobra.Command
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "files" {
filesCmdPtr = cmd
break
}
}
require.NotNil(t, filesCmdPtr)
t.Run("short description mentions generate", func(t *testing.T) {
assert.Contains(t, filesCmdPtr.Short, "Generate files")
})
t.Run("long description explains usage", func(t *testing.T) {
assert.Contains(t, filesCmdPtr.Long, "generate section")
assert.Contains(t, filesCmdPtr.Long, "--all")
assert.Contains(t, filesCmdPtr.Long, "component")
})
}
// TestFilesCommandFlagTypes verifies that flags have correct types.
func TestFilesCommandFlagTypes(t *testing.T) {
// Find the files command.
var filesCmdPtr *cobra.Command
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "files" {
filesCmdPtr = cmd
break
}
}
require.NotNil(t, filesCmdPtr)
// String flags.
stringFlags := []string{"stack", "stacks", "components"}
for _, flagName := range stringFlags {
flag := filesCmdPtr.Flags().Lookup(flagName)
require.NotNil(t, flag, "%s flag should exist", flagName)
assert.Equal(t, "string", flag.Value.Type(), "%s should be a string flag", flagName)
}
// Bool flags.
boolFlags := []string{"all", "dry-run", "clean"}
for _, flagName := range boolFlags {
flag := filesCmdPtr.Flags().Lookup(flagName)
require.NotNil(t, flag, "%s flag should exist", flagName)
assert.Equal(t, "bool", flag.Value.Type(), "%s should be a bool flag", flagName)
}
}
// TestFilesCommandFlagDefaults verifies that flags have correct default values.
func TestFilesCommandFlagDefaults(t *testing.T) {
// Find the files command.
var filesCmdPtr *cobra.Command
for _, cmd := range GenerateCmd.Commands() {
if cmd.Name() == "files" {
filesCmdPtr = cmd
break
}
}
require.NotNil(t, filesCmdPtr)
tests := []struct {
name string
flag string
expectedDef string
expectedType string
}{
{"stack default is empty", "stack", "", "string"},
{"stacks default is empty", "stacks", "", "string"},
{"components default is empty", "components", "", "string"},
{"all default is false", "all", "false", "bool"},
{"dry-run default is false", "dry-run", "false", "bool"},
{"clean default is false", "clean", "false", "bool"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
flag := filesCmdPtr.Flags().Lookup(tt.flag)
require.NotNil(t, flag)
assert.Equal(t, tt.expectedDef, flag.DefValue)
})
}
}
// TestFilesParserFlags verifies all filesParser flags are properly configured.
func TestFilesParserFlags(t *testing.T) {
registry := filesParser.Registry()
tests := []struct {
name string
flag string
hasShort bool
shorthand string
hasEnvVars bool
envVarCount int
}{
{"stack flag has shorthand", "stack", true, "s", true, 1},
{"all flag has no shorthand", "all", false, "", false, 0},
{"stacks flag has env var", "stacks", false, "", true, 1},
{"components flag has env var", "components", false, "", true, 1},
{"dry-run flag exists", "dry-run", false, "", false, 0},
{"clean flag exists", "clean", false, "", false, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.True(t, registry.Has(tt.flag), "registry should have %s flag", tt.flag)
flagInfo := registry.Get(tt.flag)
require.NotNil(t, flagInfo)
if tt.hasEnvVars {
assert.Len(t, flagInfo.GetEnvVars(), tt.envVarCount)
}
})
}
}
// TestPlanfileValidation tests validation errors in the planfile command.
func TestPlanfileValidation(t *testing.T) {
t.Run("missing component returns error", func(t *testing.T) {
// Reset viper to avoid state pollution.
v := viper.New()
v.Set("stack", "test-stack")
// Bind the parser to the fresh viper instance.
err := planfileParser.BindToViper(v)
require.NoError(t, err)
// Create a test command to execute RunE directly.
cmd := &cobra.Command{Use: "planfile"}
planfileParser.RegisterFlags(cmd)
// Execute RunE with no component argument.
// In non-TTY environment, the prompt returns ErrInteractiveModeNotAvailable
// which is swallowed, leaving component empty and triggering ErrMissingComponent.
err = planfileCmd.RunE(cmd, []string{})
assert.ErrorIs(t, err, errUtils.ErrMissingComponent)
})
}
// TestBackendValidation tests validation errors in the backend command.
func TestBackendValidation(t *testing.T) {
t.Run("missing component returns error", func(t *testing.T) {
// Reset viper to avoid state pollution.
v := viper.New()
v.Set("stack", "test-stack")
// Bind the parser to the fresh viper instance.
err := backendParser.BindToViper(v)
require.NoError(t, err)
// Create a test command to execute RunE directly.
cmd := &cobra.Command{Use: "backend"}
backendParser.RegisterFlags(cmd)
// Execute RunE with no component argument.
err = backendCmd.RunE(cmd, []string{})
assert.ErrorIs(t, err, errUtils.ErrMissingComponent)
})
}
// TestVarfileValidation tests validation errors in the varfile command.
func TestVarfileValidation(t *testing.T) {
t.Run("missing component returns error", func(t *testing.T) {
// Reset viper to avoid state pollution.
v := viper.New()
v.Set("stack", "test-stack")
// Bind the parser to the fresh viper instance.
err := varfileParser.BindToViper(v)
require.NoError(t, err)
// Create a test command to execute RunE directly.
cmd := &cobra.Command{Use: "varfile"}
varfileParser.RegisterFlags(cmd)
// Execute RunE with no component argument.
err = varfileCmd.RunE(cmd, []string{})
assert.ErrorIs(t, err, errUtils.ErrMissingComponent)
})
}
// TestFilesValidation tests validation errors in the files command.
func TestFilesValidation(t *testing.T) {
t.Run("component with all flag returns error", func(t *testing.T) {
// Get the global viper instance and set up clean state.
v := viper.GetViper()
// Reset flags that might interfere.
v.Set("all", true)
v.Set("stack", "test-stack") // Provide stack to avoid that error.
defer func() {
// Clean up.
v.Set("all", false)
v.Set("stack", "")
}()
// Create a test command to execute RunE directly.
cmd := &cobra.Command{Use: "files"}
filesParser.RegisterFlags(cmd)
// Execute RunE with component argument and --all flag set in viper.
err := filesCmd.RunE(cmd, []string{"my-component"})
assert.ErrorIs(t, err, errUtils.ErrInvalidFlag)
})
t.Run("missing component without all flag returns error", func(t *testing.T) {
// Get the global viper instance and set up clean state.
v := viper.GetViper()
v.Set("all", false)
v.Set("stack", "")
defer func() {
// Clean up.
v.Set("all", false)
v.Set("stack", "")
}()
// Create a test command to execute RunE directly.
cmd := &cobra.Command{Use: "files"}
filesParser.RegisterFlags(cmd)
// Execute RunE with no component argument and no --all flag.
// In non-TTY environment, the prompt returns ErrInteractiveModeNotAvailable.
err := filesCmd.RunE(cmd, []string{})
assert.ErrorIs(t, err, errUtils.ErrMissingComponent)
})
t.Run("component without stack returns error", func(t *testing.T) {
// Get the global viper instance and set up clean state.
v := viper.GetViper()
v.Set("all", false)
v.Set("stack", "")
defer func() {
// Clean up.
v.Set("all", false)
v.Set("stack", "")
}()
// Create a test command to execute RunE directly.
cmd := &cobra.Command{Use: "files"}
filesParser.RegisterFlags(cmd)
// Execute RunE with component argument but no stack.
// In non-TTY environment, the prompt returns ErrInteractiveModeNotAvailable.
err := filesCmd.RunE(cmd, []string{"my-component"})
assert.ErrorIs(t, err, errUtils.ErrMissingStack)
})
}