diff --git a/cli/cmd/capture/capture.go b/cli/cmd/capture/capture.go index be24da8f25..c7c6861b12 100644 --- a/cli/cmd/capture/capture.go +++ b/cli/cmd/capture/capture.go @@ -12,7 +12,9 @@ import ( var opts = struct { genericclioptions.ConfigFlags Name *string -}{} +}{ + Name: new(string), +} const defaultName = "retina-capture" diff --git a/cli/cmd_test.go b/cli/cmd_test.go new file mode 100644 index 0000000000..ef5802801f --- /dev/null +++ b/cli/cmd_test.go @@ -0,0 +1,33 @@ +package main + +import ( + "bytes" + "testing" + + "github.com/microsoft/retina/cli/cmd" + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func ExecuteInTest(cmd *cobra.Command, args []string) (stdout string, stderr string, err error) { + var stdoutBuf, stderrBuf bytes.Buffer + cmd.SetArgs(args) + cmd.SetOut(&stdoutBuf) + cmd.SetErr(&stderrBuf) + defer func() { + cmd.SetArgs(nil) + cmd.SetOut(nil) + cmd.SetErr(nil) + }() + + err = cmd.Execute() + return stdoutBuf.String(), stderrBuf.String(), err +} + +func TestCLICmd(t *testing.T) { + stdout, stderr, err := ExecuteInTest(cmd.Retina, []string{"-h"}) + require.NoError(t, err) + assert.Contains(t, stdout, "kubectl-retina") + assert.Equal(t, stderr, "") +}