Skip to content

Commit 861d791

Browse files
Claude (via Conductor)claude
andcommitted
test: Add test files for migrated list commands
Add test coverage for newly migrated list subcommands following the same patterns as existing tests. All tests pass with the new flag handler pattern. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent f2914a4 commit 861d791

File tree

6 files changed

+925
-0
lines changed

6 files changed

+925
-0
lines changed

cmd/list/components_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//nolint:dupl // Test structure similarity is intentional for consistency
2+
package list
3+
4+
import (
5+
"testing"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
// TestListComponentsFlags tests that the list components command has the correct flags.
12+
func TestListComponentsFlags(t *testing.T) {
13+
cmd := &cobra.Command{
14+
Use: "components",
15+
Short: "List all Atmos components or filter by stack",
16+
Long: "List Atmos components, with options to filter results by specific stacks.",
17+
Args: cobra.NoArgs,
18+
}
19+
20+
cmd.PersistentFlags().StringP("stack", "s", "", "Filter by stack name or pattern")
21+
22+
stackFlag := cmd.PersistentFlags().Lookup("stack")
23+
assert.NotNil(t, stackFlag, "Expected stack flag to exist")
24+
assert.Equal(t, "", stackFlag.DefValue)
25+
assert.Equal(t, "s", stackFlag.Shorthand)
26+
}
27+
28+
// TestListComponentsValidatesArgs tests that the command validates arguments.
29+
func TestListComponentsValidatesArgs(t *testing.T) {
30+
cmd := &cobra.Command{
31+
Use: "components",
32+
Args: cobra.NoArgs,
33+
}
34+
35+
err := cmd.ValidateArgs([]string{})
36+
assert.NoError(t, err, "Validation should pass with no arguments")
37+
38+
err = cmd.ValidateArgs([]string{"extra"})
39+
assert.Error(t, err, "Validation should fail with arguments")
40+
}
41+
42+
// TestListComponentsCommand tests the components command structure.
43+
func TestListComponentsCommand(t *testing.T) {
44+
assert.Equal(t, "components", componentsCmd.Use)
45+
assert.Contains(t, componentsCmd.Short, "List all Atmos components")
46+
assert.NotNil(t, componentsCmd.RunE)
47+
48+
// Check that NoArgs validator is set
49+
err := componentsCmd.Args(componentsCmd, []string{"unexpected"})
50+
assert.Error(t, err, "Should reject extra arguments")
51+
52+
err = componentsCmd.Args(componentsCmd, []string{})
53+
assert.NoError(t, err, "Should accept no arguments")
54+
}
55+
56+
// TestListComponentsWithOptions_EmptyStack tests filtering with empty stack pattern.
57+
func TestListComponentsWithOptions_EmptyStack(t *testing.T) {
58+
opts := &ComponentsOptions{
59+
Stack: "",
60+
}
61+
62+
// Test that the options are properly structured
63+
assert.Equal(t, "", opts.Stack)
64+
}
65+
66+
// TestListComponentsWithOptions_StackPattern tests filtering with stack pattern.
67+
func TestListComponentsWithOptions_StackPattern(t *testing.T) {
68+
opts := &ComponentsOptions{
69+
Stack: "prod-*",
70+
}
71+
72+
// Test that the options are properly structured
73+
assert.Equal(t, "prod-*", opts.Stack)
74+
}

cmd/list/instances_test.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package list
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// TestListInstancesFlags tests that the list instances command has the correct flags.
11+
func TestListInstancesFlags(t *testing.T) {
12+
cmd := &cobra.Command{
13+
Use: "instances",
14+
Short: "List all Atmos instances",
15+
Long: "This command lists all Atmos instances or is used to upload instances to the pro API.",
16+
Args: cobra.NoArgs,
17+
}
18+
19+
cmd.PersistentFlags().String("format", "", "Output format")
20+
cmd.PersistentFlags().String("delimiter", "", "Delimiter for CSV/TSV output")
21+
cmd.PersistentFlags().String("stack", "", "Stack pattern")
22+
cmd.PersistentFlags().String("query", "", "JQ query")
23+
cmd.PersistentFlags().Int("max-columns", 0, "Maximum columns")
24+
cmd.PersistentFlags().Bool("upload", false, "Upload instances to pro API")
25+
26+
formatFlag := cmd.PersistentFlags().Lookup("format")
27+
assert.NotNil(t, formatFlag, "Expected format flag to exist")
28+
assert.Equal(t, "", formatFlag.DefValue)
29+
30+
delimiterFlag := cmd.PersistentFlags().Lookup("delimiter")
31+
assert.NotNil(t, delimiterFlag, "Expected delimiter flag to exist")
32+
assert.Equal(t, "", delimiterFlag.DefValue)
33+
34+
stackFlag := cmd.PersistentFlags().Lookup("stack")
35+
assert.NotNil(t, stackFlag, "Expected stack flag to exist")
36+
assert.Equal(t, "", stackFlag.DefValue)
37+
38+
queryFlag := cmd.PersistentFlags().Lookup("query")
39+
assert.NotNil(t, queryFlag, "Expected query flag to exist")
40+
assert.Equal(t, "", queryFlag.DefValue)
41+
42+
maxColumnsFlag := cmd.PersistentFlags().Lookup("max-columns")
43+
assert.NotNil(t, maxColumnsFlag, "Expected max-columns flag to exist")
44+
assert.Equal(t, "0", maxColumnsFlag.DefValue)
45+
46+
uploadFlag := cmd.PersistentFlags().Lookup("upload")
47+
assert.NotNil(t, uploadFlag, "Expected upload flag to exist")
48+
assert.Equal(t, "false", uploadFlag.DefValue)
49+
}
50+
51+
// TestListInstancesValidatesArgs tests that the command validates arguments.
52+
func TestListInstancesValidatesArgs(t *testing.T) {
53+
cmd := &cobra.Command{
54+
Use: "instances",
55+
Args: cobra.NoArgs,
56+
}
57+
58+
err := cmd.ValidateArgs([]string{})
59+
assert.NoError(t, err, "Validation should pass with no arguments")
60+
61+
err = cmd.ValidateArgs([]string{"extra"})
62+
assert.Error(t, err, "Validation should fail with arguments")
63+
}
64+
65+
// TestListInstancesCommand tests the instances command structure.
66+
func TestListInstancesCommand(t *testing.T) {
67+
assert.Equal(t, "instances", instancesCmd.Use)
68+
assert.Contains(t, instancesCmd.Short, "List all Atmos instances")
69+
assert.NotNil(t, instancesCmd.RunE)
70+
71+
// Check that NoArgs validator is set
72+
err := instancesCmd.Args(instancesCmd, []string{"unexpected"})
73+
assert.Error(t, err, "Should reject extra arguments")
74+
75+
err = instancesCmd.Args(instancesCmd, []string{})
76+
assert.NoError(t, err, "Should accept no arguments")
77+
}
78+
79+
// TestListInstancesOptions tests the InstancesOptions structure.
80+
func TestListInstancesOptions(t *testing.T) {
81+
opts := &InstancesOptions{
82+
Format: "json",
83+
MaxColumns: 10,
84+
Delimiter: ",",
85+
Stack: "prod-*",
86+
Query: ".component",
87+
Upload: false,
88+
}
89+
90+
assert.Equal(t, "json", opts.Format)
91+
assert.Equal(t, 10, opts.MaxColumns)
92+
assert.Equal(t, ",", opts.Delimiter)
93+
assert.Equal(t, "prod-*", opts.Stack)
94+
assert.Equal(t, ".component", opts.Query)
95+
assert.False(t, opts.Upload)
96+
}
97+
98+
// TestListInstancesOptions_Upload tests the upload flag behavior.
99+
func TestListInstancesOptions_Upload(t *testing.T) {
100+
opts := &InstancesOptions{
101+
Upload: true,
102+
}
103+
104+
assert.True(t, opts.Upload)
105+
}

cmd/list/metadata_test.go

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
package list
2+
3+
import (
4+
"testing"
5+
6+
"github.com/spf13/cobra"
7+
"github.com/stretchr/testify/assert"
8+
9+
l "github.com/cloudposse/atmos/pkg/list"
10+
)
11+
12+
// TestListMetadataFlags tests that the list metadata command has the correct flags.
13+
func TestListMetadataFlags(t *testing.T) {
14+
cmd := &cobra.Command{
15+
Use: "metadata [component]",
16+
Short: "List metadata across stacks",
17+
Long: "List metadata information across all stacks or for a specific component",
18+
}
19+
20+
cmd.PersistentFlags().String("format", "", "Output format")
21+
cmd.PersistentFlags().String("delimiter", "", "Delimiter for CSV/TSV output")
22+
cmd.PersistentFlags().String("stack", "", "Stack pattern")
23+
cmd.PersistentFlags().String("query", "", "JQ query")
24+
cmd.PersistentFlags().Int("max-columns", 0, "Maximum columns")
25+
cmd.PersistentFlags().Bool("process-templates", true, "Enable/disable Go template processing")
26+
cmd.PersistentFlags().Bool("process-functions", true, "Enable/disable YAML functions processing")
27+
28+
formatFlag := cmd.PersistentFlags().Lookup("format")
29+
assert.NotNil(t, formatFlag, "Expected format flag to exist")
30+
assert.Equal(t, "", formatFlag.DefValue)
31+
32+
delimiterFlag := cmd.PersistentFlags().Lookup("delimiter")
33+
assert.NotNil(t, delimiterFlag, "Expected delimiter flag to exist")
34+
assert.Equal(t, "", delimiterFlag.DefValue)
35+
36+
stackFlag := cmd.PersistentFlags().Lookup("stack")
37+
assert.NotNil(t, stackFlag, "Expected stack flag to exist")
38+
assert.Equal(t, "", stackFlag.DefValue)
39+
40+
queryFlag := cmd.PersistentFlags().Lookup("query")
41+
assert.NotNil(t, queryFlag, "Expected query flag to exist")
42+
assert.Equal(t, "", queryFlag.DefValue)
43+
44+
maxColumnsFlag := cmd.PersistentFlags().Lookup("max-columns")
45+
assert.NotNil(t, maxColumnsFlag, "Expected max-columns flag to exist")
46+
assert.Equal(t, "0", maxColumnsFlag.DefValue)
47+
48+
processTemplatesFlag := cmd.PersistentFlags().Lookup("process-templates")
49+
assert.NotNil(t, processTemplatesFlag, "Expected process-templates flag to exist")
50+
assert.Equal(t, "true", processTemplatesFlag.DefValue)
51+
52+
processFunctionsFlag := cmd.PersistentFlags().Lookup("process-functions")
53+
assert.NotNil(t, processFunctionsFlag, "Expected process-functions flag to exist")
54+
assert.Equal(t, "true", processFunctionsFlag.DefValue)
55+
}
56+
57+
// TestListMetadataCommand tests the metadata command structure.
58+
func TestListMetadataCommand(t *testing.T) {
59+
assert.Equal(t, "metadata [component]", metadataCmd.Use)
60+
assert.Contains(t, metadataCmd.Short, "List metadata across stacks")
61+
assert.NotNil(t, metadataCmd.RunE)
62+
assert.NotEmpty(t, metadataCmd.Example)
63+
}
64+
65+
// TestSetupMetadataOptions tests the setupMetadataOptions function.
66+
func TestSetupMetadataOptions(t *testing.T) {
67+
testCases := []struct {
68+
name string
69+
opts *MetadataOptions
70+
componentFilter string
71+
expectedQuery string
72+
expectedComp string
73+
}{
74+
{
75+
name: "with component and custom query",
76+
opts: &MetadataOptions{
77+
Query: ".metadata.component",
78+
MaxColumns: 10,
79+
Format: "json",
80+
Delimiter: ",",
81+
Stack: "prod-*",
82+
},
83+
componentFilter: "vpc",
84+
expectedQuery: ".metadata.component",
85+
expectedComp: l.KeyMetadata,
86+
},
87+
{
88+
name: "without component and default query",
89+
opts: &MetadataOptions{
90+
Query: "",
91+
MaxColumns: 5,
92+
Format: "yaml",
93+
Delimiter: "\t",
94+
Stack: "",
95+
},
96+
componentFilter: "",
97+
expectedQuery: ".metadata",
98+
expectedComp: l.KeyMetadata,
99+
},
100+
{
101+
name: "with component but no query",
102+
opts: &MetadataOptions{
103+
Query: "",
104+
MaxColumns: 0,
105+
Format: "",
106+
Delimiter: "",
107+
Stack: "*-dev-*",
108+
},
109+
componentFilter: "app",
110+
expectedQuery: ".metadata",
111+
expectedComp: l.KeyMetadata,
112+
},
113+
}
114+
115+
for _, tc := range testCases {
116+
t.Run(tc.name, func(t *testing.T) {
117+
filterOpts := setupMetadataOptions(tc.opts, tc.componentFilter)
118+
119+
assert.Equal(t, tc.expectedComp, filterOpts.Component)
120+
assert.Equal(t, tc.componentFilter, filterOpts.ComponentFilter)
121+
assert.Equal(t, tc.expectedQuery, filterOpts.Query)
122+
assert.False(t, filterOpts.IncludeAbstract)
123+
assert.Equal(t, tc.opts.MaxColumns, filterOpts.MaxColumns)
124+
assert.Equal(t, tc.opts.Format, filterOpts.FormatStr)
125+
assert.Equal(t, tc.opts.Delimiter, filterOpts.Delimiter)
126+
assert.Equal(t, tc.opts.Stack, filterOpts.StackPattern)
127+
})
128+
}
129+
}
130+
131+
// TestMetadataOptions tests the MetadataOptions structure.
132+
func TestMetadataOptions(t *testing.T) {
133+
opts := &MetadataOptions{
134+
Format: "json",
135+
MaxColumns: 10,
136+
Delimiter: ",",
137+
Stack: "prod-*",
138+
Query: ".metadata.component",
139+
ProcessTemplates: true,
140+
ProcessFunctions: false,
141+
}
142+
143+
assert.Equal(t, "json", opts.Format)
144+
assert.Equal(t, 10, opts.MaxColumns)
145+
assert.Equal(t, ",", opts.Delimiter)
146+
assert.Equal(t, "prod-*", opts.Stack)
147+
assert.Equal(t, ".metadata.component", opts.Query)
148+
assert.True(t, opts.ProcessTemplates)
149+
assert.False(t, opts.ProcessFunctions)
150+
}
151+
152+
// TestListMetadataWithOptions_DefaultQuery tests that default query is applied.
153+
func TestListMetadataWithOptions_DefaultQuery(t *testing.T) {
154+
opts := &MetadataOptions{
155+
Query: "",
156+
}
157+
158+
filterOpts := setupMetadataOptions(opts, "")
159+
assert.Equal(t, ".metadata", filterOpts.Query, "Should apply default .metadata query")
160+
}
161+
162+
// TestListMetadataWithOptions_CustomQuery tests that custom query is preserved.
163+
func TestListMetadataWithOptions_CustomQuery(t *testing.T) {
164+
opts := &MetadataOptions{
165+
Query: ".metadata.custom",
166+
}
167+
168+
filterOpts := setupMetadataOptions(opts, "")
169+
assert.Equal(t, ".metadata.custom", filterOpts.Query, "Should preserve custom query")
170+
}

0 commit comments

Comments
 (0)