-
-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathlist_test.go
More file actions
297 lines (259 loc) · 10.3 KB
/
list_test.go
File metadata and controls
297 lines (259 loc) · 10.3 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
package list
import (
"testing"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
l "github.com/cloudposse/atmos/pkg/list"
"github.com/cloudposse/atmos/pkg/list/errors"
)
// setupTestCommand creates a test command with the necessary flags.
func setupTestCommand(use string) *cobra.Command {
cmd := &cobra.Command{
Use: use,
}
cmd.PersistentFlags().String("format", "", "Output format")
cmd.PersistentFlags().String("delimiter", "", "Delimiter for CSV/TSV output")
cmd.PersistentFlags().String("stack", "", "Stack pattern")
cmd.PersistentFlags().String("query", "", "JQ query")
cmd.PersistentFlags().Int("max-columns", 0, "Maximum columns")
cmd.PersistentFlags().Bool("process-templates", true, "Enable/disable Go template processing")
cmd.PersistentFlags().Bool("process-functions", true, "Enable/disable YAML functions processing")
return cmd
}
// TestComponentDefinitionNotFoundError tests that the ComponentDefinitionNotFoundError.
func TestComponentDefinitionNotFoundError(t *testing.T) {
testCases := []struct {
name string
componentName string
expectedOutput string
runFunc func(cmd *cobra.Command, args []string) (string, error)
}{
{
name: "list values - component not found",
componentName: "nonexistent-component",
expectedOutput: "component 'nonexistent-component' does not exist",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
return "", &errors.ComponentDefinitionNotFoundError{Component: args[0]}
},
},
{
name: "list settings - component not found",
componentName: "nonexistent-component",
expectedOutput: "component 'nonexistent-component' does not exist",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
return "", &errors.ComponentDefinitionNotFoundError{Component: args[0]}
},
},
{
name: "list metadata - component not found",
componentName: "nonexistent-component",
expectedOutput: "component 'nonexistent-component' does not exist",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
return "", &errors.ComponentDefinitionNotFoundError{Component: args[0]}
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Create test command
cmd := setupTestCommand(tc.name)
args := []string{tc.componentName}
// Mock the listValues/listSettings/listMetadata function
mockRunFunc := tc.runFunc
// Run the command with the mocked function
output, err := mockRunFunc(cmd, args)
assert.Equal(t, "", output)
assert.Error(t, err)
// Check that the error is of the expected type
var componentNotFoundErr *errors.ComponentDefinitionNotFoundError
assert.ErrorAs(t, err, &componentNotFoundErr)
assert.Equal(t, tc.componentName, componentNotFoundErr.Component)
assert.Contains(t, componentNotFoundErr.Error(), tc.expectedOutput)
// Verify that the error would be properly returned by the RunE function
// This simulates what would happen in the actual command execution
// where errors are returned to main() instead of being logged
assert.NotNil(t, err, "Error should be returned to be handled by main()")
})
}
}
// TestNoValuesFoundError tests that the NoValuesFoundError is properly handled.
func TestNoValuesFoundError(t *testing.T) {
testCases := []struct {
name string
componentName string
query string
expectedOutput string
runFunc func(cmd *cobra.Command, args []string) (string, error)
shouldReturnNil bool
}{
{
name: "list values - no values found",
componentName: "test-component",
expectedOutput: "No values found for component 'test-component'",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
return "", &errors.NoValuesFoundError{Component: args[0]}
},
shouldReturnNil: false,
},
{
name: "list vars - no vars found",
componentName: "test-component",
query: ".vars",
expectedOutput: "No vars found for component 'test-component'",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
cmd.Flags().Set("query", ".vars")
return "", &errors.NoValuesFoundError{Component: args[0]}
},
shouldReturnNil: true,
},
{
name: "list settings - no settings found with component",
componentName: "test-component",
expectedOutput: "No settings found for component 'test-component'",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
return "", &errors.NoValuesFoundError{Component: args[0]}
},
shouldReturnNil: false,
},
{
name: "list settings - no settings found without component",
componentName: "",
expectedOutput: "No settings found",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
return "", &errors.NoValuesFoundError{}
},
shouldReturnNil: false,
},
{
name: "list metadata - no metadata found with component",
componentName: "test-component",
expectedOutput: "No metadata found for component 'test-component'",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
return "", &errors.NoValuesFoundError{Component: args[0]}
},
shouldReturnNil: false,
},
{
name: "list metadata - no metadata found without component",
componentName: "",
expectedOutput: "No metadata found",
runFunc: func(cmd *cobra.Command, args []string) (string, error) {
return "", &errors.NoValuesFoundError{}
},
shouldReturnNil: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cmd := setupTestCommand(tc.name)
args := []string{}
if tc.componentName != "" {
args = append(args, tc.componentName)
}
if tc.query != "" {
cmd.Flags().Set("query", tc.query)
}
mockRunFunc := tc.runFunc
output, err := mockRunFunc(cmd, args)
assert.Equal(t, "", output)
assert.Error(t, err)
var noValuesErr *errors.NoValuesFoundError
assert.ErrorAs(t, err, &noValuesErr)
})
}
}
func TestListCmds_Error(t *testing.T) {
stacksPath := "../tests/fixtures/scenarios/terraform-apply-affected"
t.Setenv("ATMOS_CLI_CONFIG_PATH", stacksPath)
t.Setenv("ATMOS_BASE_PATH", stacksPath)
err := componentsCmd.RunE(componentsCmd, []string{"--invalid-flag"})
assert.Error(t, err, "list components command should return an error when called with invalid flags")
err = metadataCmd.RunE(metadataCmd, []string{"--invalid-flag"})
assert.Error(t, err, "list metadata command should return an error when called with invalid flags")
err = settingsCmd.RunE(settingsCmd, []string{"--invalid-flag"})
assert.Error(t, err, "list settings command should return an error when called with invalid flags")
err = valuesCmd.RunE(valuesCmd, []string{"--invalid-flag"})
assert.Error(t, err, "list values command should return an error when called with invalid flags")
err = vendorCmd.RunE(vendorCmd, []string{"--invalid-flag"})
assert.Error(t, err, "list vendor command should return an error when called with invalid flags")
err = workflowsCmd.RunE(workflowsCmd, []string{"--invalid-flag"})
assert.Error(t, err, "list workflows command should return an error when called with invalid flags")
}
// TestListCmds_NoResults verifies that the list commands can handle empty results.
// This tests the condition that triggers "No components found" and "No stacks found" messages.
// Note: This test verifies the underlying filter functions return empty slices, which is the
// condition that causes the RunE functions to display the "No X found" UI error messages.
func TestListCmds_NoResults(t *testing.T) {
// Test that FilterAndListComponents can return empty results
t.Run("list components returns empty when no components exist", func(t *testing.T) {
emptyStacksMap := map[string]any{
"test-stack": map[string]any{
"components": map[string]any{
"terraform": map[string]any{},
"helmfile": map[string]any{},
},
},
}
output, err := l.FilterAndListComponents("", emptyStacksMap)
assert.NoError(t, err)
assert.Empty(t, output, "Expected empty output when no components exist")
})
// Test that FilterAndListStacks can return empty results
t.Run("list stacks returns empty when no matching stacks exist", func(t *testing.T) {
stacksMap := map[string]any{
"test-stack": map[string]any{
"components": map[string]any{
"terraform": map[string]any{
"existing-component": map[string]any{},
},
},
},
}
output, err := l.FilterAndListStacks(stacksMap, "nonexistent-component")
assert.NoError(t, err)
assert.Nil(t, output, "Expected nil output when no matching stacks exist")
})
}
// TestListCommandProvider tests the ListCommandProvider interface methods.
func TestListCommandProvider(t *testing.T) {
provider := &ListCommandProvider{}
t.Run("GetCommand returns listCmd", func(t *testing.T) {
cmd := provider.GetCommand()
require.NotNil(t, cmd)
assert.Equal(t, "list", cmd.Use)
})
t.Run("GetName returns list", func(t *testing.T) {
assert.Equal(t, "list", provider.GetName())
})
t.Run("GetGroup returns Stack Introspection", func(t *testing.T) {
assert.Equal(t, "Stack Introspection", provider.GetGroup())
})
t.Run("GetFlagsBuilder returns nil", func(t *testing.T) {
assert.Nil(t, provider.GetFlagsBuilder())
})
t.Run("GetPositionalArgsBuilder returns nil", func(t *testing.T) {
assert.Nil(t, provider.GetPositionalArgsBuilder())
})
t.Run("GetCompatibilityFlags returns nil", func(t *testing.T) {
assert.Nil(t, provider.GetCompatibilityFlags())
})
t.Run("GetAliases returns vendor and workflow aliases", func(t *testing.T) {
aliases := provider.GetAliases()
require.Len(t, aliases, 2)
// Verify vendor list alias.
vendorAlias := aliases[0]
assert.Equal(t, "vendor", vendorAlias.Subcommand)
assert.Equal(t, "vendor", vendorAlias.ParentCommand)
assert.Equal(t, "list", vendorAlias.Name)
assert.Contains(t, vendorAlias.Long, `alias for "atmos list vendor"`)
assert.Contains(t, vendorAlias.Example, "atmos vendor list")
// Verify workflow list alias.
workflowAlias := aliases[1]
assert.Equal(t, "workflows", workflowAlias.Subcommand)
assert.Equal(t, "workflow", workflowAlias.ParentCommand)
assert.Equal(t, "list", workflowAlias.Name)
assert.Contains(t, workflowAlias.Long, `alias for "atmos list workflows"`)
assert.Contains(t, workflowAlias.Example, "atmos workflow list")
})
}