mirror of
https://github.com/kyverno/kyverno.git
synced 2024-12-14 11:57:48 +00:00
dc71610df7
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package docs
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/spf13/cobra"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestCommandWithNilRoot(t *testing.T) {
|
|
cmd := Command(nil)
|
|
assert.NotNil(t, cmd)
|
|
cmd.SetArgs([]string{"-o", "foo"})
|
|
err := cmd.Execute()
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestCommandWithoutArgs(t *testing.T) {
|
|
cmd := Command(&cobra.Command{})
|
|
assert.NotNil(t, cmd)
|
|
err := cmd.Execute()
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestCommandWithInvalidArg(t *testing.T) {
|
|
cmd := Command(&cobra.Command{})
|
|
assert.NotNil(t, cmd)
|
|
b := bytes.NewBufferString("")
|
|
cmd.SetErr(b)
|
|
cmd.SetArgs([]string{"foo"})
|
|
err := cmd.Execute()
|
|
assert.Error(t, err)
|
|
out, err := io.ReadAll(b)
|
|
assert.NoError(t, err)
|
|
expected := `Error: unknown command "foo" for "docs"`
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
|
|
}
|
|
|
|
func TestCommandWithInvalidFlag(t *testing.T) {
|
|
cmd := Command(&cobra.Command{})
|
|
assert.NotNil(t, cmd)
|
|
b := bytes.NewBufferString("")
|
|
cmd.SetErr(b)
|
|
cmd.SetArgs([]string{"--xxx"})
|
|
err := cmd.Execute()
|
|
assert.Error(t, err)
|
|
out, err := io.ReadAll(b)
|
|
assert.NoError(t, err)
|
|
expected := `Error: unknown flag: --xxx`
|
|
assert.Equal(t, strings.TrimSpace(expected), strings.TrimSpace(string(out)))
|
|
}
|
|
|
|
func TestCommandHelp(t *testing.T) {
|
|
cmd := Command(&cobra.Command{})
|
|
assert.NotNil(t, cmd)
|
|
b := bytes.NewBufferString("")
|
|
cmd.SetOut(b)
|
|
cmd.SetArgs([]string{"--help"})
|
|
err := cmd.Execute()
|
|
assert.NoError(t, err)
|
|
out, err := io.ReadAll(b)
|
|
assert.NoError(t, err)
|
|
assert.True(t, strings.HasPrefix(string(out), cmd.Long))
|
|
}
|