Skip to content

Commit df37273

Browse files
Improve code coverage for cli/cmd package
Signed-off-by: Meghna Singh <[email protected]>
1 parent 0ee9d28 commit df37273

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+20114
-664
lines changed

cli/cmd/check_operator_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package cmd
44

55
import (
6-
"io"
76
"os"
87
"testing"
98

@@ -32,7 +31,6 @@ func TestCheckOperatorStatusRunE(t *testing.T) {
3231

3332
command = mockCommand
3433

35-
// Test for ModeDirect where pod namespace is not set.
3634
OperatingMode = ModeDirect
3735
_ = os.Unsetenv(podNamespace)
3836
err := checkOperatorStatusRunE(&cobra.Command{}, nil)
@@ -174,14 +172,12 @@ func TestWriteStatus(t *testing.T) {
174172
t.Run(test.name, func(t *testing.T) {
175173
OutputFormat = test.outputFormat
176174

177-
changeSTDOUT()
178-
writeStatus(test.status)
179-
restoreSTDOUT()
180-
181-
o, _ := io.ReadAll(r)
175+
output := captureOutput(t, func() {
176+
writeStatus(test.status)
177+
})
182178

183179
for _, c := range test.contains {
184-
assert.Contains(t, string(o), c, "unexpected output")
180+
assert.Contains(t, output, c, "unexpected output")
185181
}
186182
})
187183
}

cli/cmd/check_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2024 NetApp, Inc. All Rights Reserved.
2+
3+
package cmd
4+
5+
import (
6+
"testing"
7+
8+
"github.com/spf13/cobra"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestCheckCmd_PersistentPreRunE(t *testing.T) {
13+
cmd := &cobra.Command{}
14+
args := []string{}
15+
16+
assert.NotPanics(t, func() {
17+
_ = checkCmd.PersistentPreRunE(cmd, args)
18+
})
19+
}

cli/cmd/completion_test.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,52 @@
11
package cmd
22

33
import (
4+
"sync"
45
"testing"
56

7+
"github.com/spf13/cobra"
68
"github.com/stretchr/testify/assert"
79
)
810

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+
950
func TestGenPatchedBashCompletionScript(t *testing.T) {
1051
dummyOriginalScript := ` args=("${words[@]:1}")
1152
requestComp="${words[0]} __complete ${args[*]}"
@@ -55,3 +96,131 @@ func TestGenPatchedZshCompletionScript(t *testing.T) {
5596

5697
assert.Equal(t, expectedResult, result)
5798
}
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

Comments
 (0)