|
1 | 1 | package cmd |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "sync" |
4 | 5 | "testing" |
5 | 6 |
|
| 7 | + "github.com/spf13/cobra" |
6 | 8 | "github.com/stretchr/testify/assert" |
7 | 9 | ) |
8 | 10 |
|
| 11 | +var testStateMutex sync.RWMutex |
| 12 | + |
| 13 | +func withTestState(t *testing.T, skipDetection bool, plugins map[string]struct{}, fn func()) { |
| 14 | + t.Helper() |
| 15 | + |
| 16 | + testStateMutex.Lock() |
| 17 | + defer testStateMutex.Unlock() |
| 18 | + |
| 19 | + originalSkip := skipPluginDetection |
| 20 | + originalPlugins := pluginsFound |
| 21 | + |
| 22 | + defer func() { |
| 23 | + skipPluginDetection = originalSkip |
| 24 | + pluginsFound = originalPlugins |
| 25 | + }() |
| 26 | + |
| 27 | + skipPluginDetection = skipDetection |
| 28 | + pluginsFound = plugins |
| 29 | + |
| 30 | + fn() |
| 31 | +} |
| 32 | + |
| 33 | +func setupCompletionCommand(t *testing.T, cmdType string, runE func(*cobra.Command, []string) error) *cobra.Command { |
| 34 | + t.Helper() |
| 35 | + |
| 36 | + rootCmd := &cobra.Command{Use: "tridentctl"} |
| 37 | + completionCmd := &cobra.Command{Use: "completion"} |
| 38 | + targetCmd := &cobra.Command{ |
| 39 | + Use: cmdType, |
| 40 | + Short: "Generate the autocompletion script for " + cmdType, |
| 41 | + RunE: runE, |
| 42 | + } |
| 43 | + |
| 44 | + rootCmd.AddCommand(completionCmd) |
| 45 | + completionCmd.AddCommand(targetCmd) |
| 46 | + |
| 47 | + return targetCmd |
| 48 | +} |
| 49 | + |
9 | 50 | func TestGenPatchedBashCompletionScript(t *testing.T) { |
10 | 51 | dummyOriginalScript := ` args=("${words[@]:1}") |
11 | 52 | requestComp="${words[0]} __complete ${args[*]}" |
@@ -55,3 +96,131 @@ func TestGenPatchedZshCompletionScript(t *testing.T) { |
55 | 96 |
|
56 | 97 | assert.Equal(t, expectedResult, result) |
57 | 98 | } |
| 99 | + |
| 100 | +type completionTestCase struct { |
| 101 | + name string |
| 102 | + skipPluginDetection bool |
| 103 | + pluginsFound map[string]struct{} |
| 104 | + description string |
| 105 | +} |
| 106 | + |
| 107 | +func TestCompletionBashCmd(t *testing.T) { |
| 108 | + testCases := []completionTestCase{ |
| 109 | + { |
| 110 | + name: "basic_bash_completion", |
| 111 | + skipPluginDetection: true, |
| 112 | + pluginsFound: map[string]struct{}{}, |
| 113 | + description: "Should generate bash completion successfully", |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + for _, tc := range testCases { |
| 118 | + t.Run(tc.name, func(t *testing.T) { |
| 119 | + withTestState(t, tc.skipPluginDetection, tc.pluginsFound, func() { |
| 120 | + cmd := setupCompletionCommand(t, "bash", completionBashCmd.RunE) |
| 121 | + |
| 122 | + output := captureOutput(t, func() { |
| 123 | + err := cmd.RunE(cmd, []string{}) |
| 124 | + assert.NoError(t, err, tc.description) |
| 125 | + }) |
| 126 | + |
| 127 | + assert.NotEmpty(t, output, "Should generate some completion output") |
| 128 | + }) |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestCompletionZshCmd(t *testing.T) { |
| 134 | + testCases := []completionTestCase{ |
| 135 | + { |
| 136 | + name: "basic_zsh_completion", |
| 137 | + skipPluginDetection: true, |
| 138 | + pluginsFound: map[string]struct{}{}, |
| 139 | + description: "Should generate zsh completion successfully", |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + for _, tc := range testCases { |
| 144 | + t.Run(tc.name, func(t *testing.T) { |
| 145 | + withTestState(t, tc.skipPluginDetection, tc.pluginsFound, func() { |
| 146 | + cmd := setupCompletionCommand(t, "zsh", completionZshCmd.RunE) |
| 147 | + |
| 148 | + output := captureOutput(t, func() { |
| 149 | + err := cmd.RunE(cmd, []string{}) |
| 150 | + assert.NoError(t, err, tc.description) |
| 151 | + }) |
| 152 | + |
| 153 | + assert.NotEmpty(t, output, "Should generate completion output") |
| 154 | + }) |
| 155 | + }) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func TestCompletionFishCmd(t *testing.T) { |
| 160 | + cmd := setupCompletionCommand(t, "fish", completionFishCmd.RunE) |
| 161 | + |
| 162 | + output := captureOutput(t, func() { |
| 163 | + err := cmd.RunE(cmd, []string{}) |
| 164 | + assert.NoError(t, err, "Should call cmd.Root().GenFishCompletion successfully") |
| 165 | + }) |
| 166 | + |
| 167 | + assert.NotEmpty(t, output, "Should generate fish completion output") |
| 168 | +} |
| 169 | + |
| 170 | +func TestCompletionPowershellCmd(t *testing.T) { |
| 171 | + cmd := setupCompletionCommand(t, "powershell", completionPowershellCmd.RunE) |
| 172 | + |
| 173 | + output := captureOutput(t, func() { |
| 174 | + err := cmd.RunE(cmd, []string{}) |
| 175 | + assert.NoError(t, err, "Should call cmd.Root().GenPowerShellCompletionWithDesc successfully") |
| 176 | + }) |
| 177 | + |
| 178 | + assert.NotEmpty(t, output, "Should generate powershell completion output") |
| 179 | +} |
| 180 | + |
| 181 | +func TestGetCobraPoweredPlugins(t *testing.T) { |
| 182 | + testCases := []struct { |
| 183 | + name string |
| 184 | + skipPluginDetection bool |
| 185 | + pluginsFound map[string]struct{} |
| 186 | + expectNil bool |
| 187 | + description string |
| 188 | + }{ |
| 189 | + { |
| 190 | + name: "skip_detection_returns_nil", |
| 191 | + skipPluginDetection: true, |
| 192 | + pluginsFound: map[string]struct{}{"plugin1": {}, "plugin2": {}}, |
| 193 | + expectNil: true, |
| 194 | + description: "Should return nil when skipPluginDetection is true", |
| 195 | + }, |
| 196 | + { |
| 197 | + name: "no_plugins_found_empty_slice", |
| 198 | + skipPluginDetection: false, |
| 199 | + pluginsFound: map[string]struct{}{}, |
| 200 | + expectNil: false, |
| 201 | + description: "Should return empty slice when no plugins in pluginsFound map", |
| 202 | + }, |
| 203 | + { |
| 204 | + name: "plugins_found_cobra_detection_process", |
| 205 | + skipPluginDetection: false, |
| 206 | + pluginsFound: map[string]struct{}{"plugin1": {}, "plugin2": {}}, |
| 207 | + expectNil: true, |
| 208 | + description: "Should process plugins and return slice (may be empty if plugins fail cobra detection)", |
| 209 | + }, |
| 210 | + } |
| 211 | + |
| 212 | + for _, tc := range testCases { |
| 213 | + t.Run(tc.name, func(t *testing.T) { |
| 214 | + withTestState(t, tc.skipPluginDetection, tc.pluginsFound, func() { |
| 215 | + result := GetCobraPoweredPlugins() |
| 216 | + |
| 217 | + if tc.expectNil { |
| 218 | + assert.Nil(t, result, tc.description) |
| 219 | + } else { |
| 220 | + assert.NotNil(t, result, tc.description) |
| 221 | + assert.IsType(t, []string{}, result, "Should return string slice type") |
| 222 | + } |
| 223 | + }) |
| 224 | + }) |
| 225 | + } |
| 226 | +} |
0 commit comments