|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/spf13/cobra" |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + |
| 9 | + "github.com/cloudposse/atmos/pkg/list/errors" |
| 10 | +) |
| 11 | + |
| 12 | +// setupTestCommand creates a test command with the necessary flags. |
| 13 | +func setupTestCommand(use string) *cobra.Command { |
| 14 | + cmd := &cobra.Command{ |
| 15 | + Use: use, |
| 16 | + } |
| 17 | + cmd.PersistentFlags().String("format", "", "Output format") |
| 18 | + cmd.PersistentFlags().String("delimiter", "", "Delimiter for CSV/TSV output") |
| 19 | + cmd.PersistentFlags().String("stack", "", "Stack pattern") |
| 20 | + cmd.PersistentFlags().String("query", "", "JQ query") |
| 21 | + cmd.PersistentFlags().Int("max-columns", 0, "Maximum columns") |
| 22 | + cmd.PersistentFlags().Bool("process-templates", true, "Enable/disable Go template processing") |
| 23 | + cmd.PersistentFlags().Bool("process-functions", true, "Enable/disable YAML functions processing") |
| 24 | + return cmd |
| 25 | +} |
| 26 | + |
| 27 | +// TestComponentDefinitionNotFoundError tests that the ComponentDefinitionNotFoundError. |
| 28 | +func TestComponentDefinitionNotFoundError(t *testing.T) { |
| 29 | + testCases := []struct { |
| 30 | + name string |
| 31 | + componentName string |
| 32 | + expectedOutput string |
| 33 | + runFunc func(cmd *cobra.Command, args []string) (string, error) |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "list values - component not found", |
| 37 | + componentName: "nonexistent-component", |
| 38 | + expectedOutput: "component 'nonexistent-component' does not exist", |
| 39 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 40 | + return "", &errors.ComponentDefinitionNotFoundError{Component: args[0]} |
| 41 | + }, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "list settings - component not found", |
| 45 | + componentName: "nonexistent-component", |
| 46 | + expectedOutput: "component 'nonexistent-component' does not exist", |
| 47 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 48 | + return "", &errors.ComponentDefinitionNotFoundError{Component: args[0]} |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "list metadata - component not found", |
| 53 | + componentName: "nonexistent-component", |
| 54 | + expectedOutput: "component 'nonexistent-component' does not exist", |
| 55 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 56 | + return "", &errors.ComponentDefinitionNotFoundError{Component: args[0]} |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tc := range testCases { |
| 62 | + t.Run(tc.name, func(t *testing.T) { |
| 63 | + // Create test command |
| 64 | + cmd := setupTestCommand(tc.name) |
| 65 | + args := []string{tc.componentName} |
| 66 | + |
| 67 | + // Mock the listValues/listSettings/listMetadata function |
| 68 | + mockRunFunc := tc.runFunc |
| 69 | + |
| 70 | + // Run the command with the mocked function |
| 71 | + output, err := mockRunFunc(cmd, args) |
| 72 | + assert.Equal(t, "", output) |
| 73 | + assert.Error(t, err) |
| 74 | + |
| 75 | + // Check that the error is of the expected type |
| 76 | + var componentNotFoundErr *errors.ComponentDefinitionNotFoundError |
| 77 | + assert.ErrorAs(t, err, &componentNotFoundErr) |
| 78 | + assert.Equal(t, tc.componentName, componentNotFoundErr.Component) |
| 79 | + assert.Contains(t, componentNotFoundErr.Error(), tc.expectedOutput) |
| 80 | + |
| 81 | + // Verify that the error would be properly returned by the RunE function |
| 82 | + // This simulates what would happen in the actual command execution |
| 83 | + // where errors are returned to main() instead of being logged |
| 84 | + assert.NotNil(t, err, "Error should be returned to be handled by main()") |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// TestNoValuesFoundError tests that the NoValuesFoundError is properly handled. |
| 90 | +func TestNoValuesFoundError(t *testing.T) { |
| 91 | + testCases := []struct { |
| 92 | + name string |
| 93 | + componentName string |
| 94 | + query string |
| 95 | + expectedOutput string |
| 96 | + runFunc func(cmd *cobra.Command, args []string) (string, error) |
| 97 | + shouldReturnNil bool |
| 98 | + }{ |
| 99 | + { |
| 100 | + name: "list values - no values found", |
| 101 | + componentName: "test-component", |
| 102 | + expectedOutput: "No values found for component 'test-component'", |
| 103 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 104 | + return "", &errors.NoValuesFoundError{Component: args[0]} |
| 105 | + }, |
| 106 | + shouldReturnNil: false, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "list vars - no vars found", |
| 110 | + componentName: "test-component", |
| 111 | + query: ".vars", |
| 112 | + expectedOutput: "No vars found for component 'test-component'", |
| 113 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 114 | + cmd.Flags().Set("query", ".vars") |
| 115 | + return "", &errors.NoValuesFoundError{Component: args[0]} |
| 116 | + }, |
| 117 | + shouldReturnNil: true, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "list settings - no settings found with component", |
| 121 | + componentName: "test-component", |
| 122 | + expectedOutput: "No settings found for component 'test-component'", |
| 123 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 124 | + return "", &errors.NoValuesFoundError{Component: args[0]} |
| 125 | + }, |
| 126 | + shouldReturnNil: false, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "list settings - no settings found without component", |
| 130 | + componentName: "", |
| 131 | + expectedOutput: "No settings found", |
| 132 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 133 | + return "", &errors.NoValuesFoundError{} |
| 134 | + }, |
| 135 | + shouldReturnNil: false, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "list metadata - no metadata found with component", |
| 139 | + componentName: "test-component", |
| 140 | + expectedOutput: "No metadata found for component 'test-component'", |
| 141 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 142 | + return "", &errors.NoValuesFoundError{Component: args[0]} |
| 143 | + }, |
| 144 | + shouldReturnNil: false, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "list metadata - no metadata found without component", |
| 148 | + componentName: "", |
| 149 | + expectedOutput: "No metadata found", |
| 150 | + runFunc: func(cmd *cobra.Command, args []string) (string, error) { |
| 151 | + return "", &errors.NoValuesFoundError{} |
| 152 | + }, |
| 153 | + shouldReturnNil: false, |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + for _, tc := range testCases { |
| 158 | + t.Run(tc.name, func(t *testing.T) { |
| 159 | + cmd := setupTestCommand(tc.name) |
| 160 | + args := []string{} |
| 161 | + if tc.componentName != "" { |
| 162 | + args = append(args, tc.componentName) |
| 163 | + } |
| 164 | + |
| 165 | + if tc.query != "" { |
| 166 | + cmd.Flags().Set("query", tc.query) |
| 167 | + } |
| 168 | + |
| 169 | + mockRunFunc := tc.runFunc |
| 170 | + |
| 171 | + output, err := mockRunFunc(cmd, args) |
| 172 | + assert.Equal(t, "", output) |
| 173 | + assert.Error(t, err) |
| 174 | + |
| 175 | + var noValuesErr *errors.NoValuesFoundError |
| 176 | + assert.ErrorAs(t, err, &noValuesErr) |
| 177 | + }) |
| 178 | + } |
| 179 | +} |
0 commit comments