mirror of
https://github.com/kubescape/kubescape.git
synced 2026-04-15 06:58:11 +00:00
Wrote ne tsts for the following packages: - completion - config - download - fix - list Also addressed a potential crash in the compleition, download, and list subcommands when no arguement was provided to the Args, RunE or Run functions. Updated `DownloadSupportCommands`, 'ListSupportActions' function to return sorted slice of strings. Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
108 lines
2.3 KiB
Go
108 lines
2.3 KiB
Go
package completion
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// Generates autocompletion script for valid shell types
|
|
func TestGetCompletionCmd(t *testing.T) {
|
|
// Arrange
|
|
completionCmd := GetCompletionCmd()
|
|
assert.Equal(t, "completion [bash|zsh|fish|powershell]", completionCmd.Use)
|
|
assert.Equal(t, "Generate autocompletion script", completionCmd.Short)
|
|
assert.Equal(t, "To load completions", completionCmd.Long)
|
|
assert.Equal(t, completionCmdExamples, completionCmd.Example)
|
|
assert.Equal(t, true, completionCmd.DisableFlagsInUseLine)
|
|
assert.Equal(t, []string{"bash", "zsh", "fish", "powershell"}, completionCmd.ValidArgs)
|
|
}
|
|
|
|
func TestGetCompletionCmd_RunExpectedOutputs(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
want string
|
|
}{
|
|
{
|
|
name: "Unknown completion",
|
|
args: []string{"unknown"},
|
|
want: "Invalid arguement unknown",
|
|
},
|
|
{
|
|
name: "Empty arguements",
|
|
args: []string{},
|
|
want: "No arguements provided.\n",
|
|
},
|
|
}
|
|
|
|
completionCmd := GetCompletionCmd()
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Redirect stdout to a buffer
|
|
rescueStdout := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
completionCmd.Run(&cobra.Command{}, tt.args)
|
|
|
|
w.Close()
|
|
got, _ := io.ReadAll(r)
|
|
os.Stdout = rescueStdout
|
|
|
|
assert.Equal(t, tt.want, string(got))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestGetCompletionCmd_RunNotExpectedOutputs(t *testing.T) {
|
|
notExpectedOutput1 := "No arguments provided."
|
|
notExpectedOutput2 := "No arguments provided."
|
|
|
|
tests := []struct {
|
|
name string
|
|
args []string
|
|
}{
|
|
{
|
|
name: "Bash completion",
|
|
args: []string{"bash"},
|
|
},
|
|
{
|
|
name: "Zsh completion",
|
|
args: []string{"zsh"},
|
|
},
|
|
{
|
|
name: "Fish completion",
|
|
args: []string{"fish"},
|
|
},
|
|
{
|
|
name: "PowerShell completion",
|
|
args: []string{"powershell"},
|
|
},
|
|
}
|
|
|
|
completionCmd := GetCompletionCmd()
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Redirect stdout to a buffer
|
|
rescueStdout := os.Stdout
|
|
r, w, _ := os.Pipe()
|
|
os.Stdout = w
|
|
|
|
completionCmd.Run(&cobra.Command{}, tt.args)
|
|
|
|
w.Close()
|
|
got, _ := io.ReadAll(r)
|
|
os.Stdout = rescueStdout
|
|
|
|
assert.NotEqual(t, notExpectedOutput1, string(got))
|
|
assert.NotEqual(t, notExpectedOutput2, string(got))
|
|
})
|
|
}
|
|
}
|