From 0ba877609b34707b567cbb6a93559b2b42e062c9 Mon Sep 17 00:00:00 2001 From: Alexej Disterhoft Date: Fri, 16 Feb 2024 22:10:30 +0100 Subject: [PATCH 1/3] test: add more tests for various code paths --- Taskfile.yaml | 6 ++++++ cmd/common/options_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/Taskfile.yaml b/Taskfile.yaml index 11d0c61..cbad0fb 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -39,6 +39,12 @@ tasks: cmds: - pre-commit run --all-files + pre-commit:fix: + desc: Run pre-commit checks and git add fixed files + cmds: + - task: pre-commit + - git add --update . + install:gotestsum: desc: Install gotestsum cmds: diff --git a/cmd/common/options_test.go b/cmd/common/options_test.go index 09bd0ab..6e1f3a9 100644 --- a/cmd/common/options_test.go +++ b/cmd/common/options_test.go @@ -4,3 +4,28 @@ // SPDX-License-Identifier: MIT package common_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + . "github.com/nobbs/kubectl-mapr-ticket/cmd/common" + + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/cli-runtime/pkg/genericiooptions" +) + +// nolint:paralleltest +func TestNewOptions(t *testing.T) { + // Create a new ConfigFlags and IOStreams + flags := &genericclioptions.ConfigFlags{} + streams := genericiooptions.IOStreams{} + + // Call the NewOptions function + options := NewOptions(flags, streams) + + // Verify that the returned options match the input values + assert.Equal(t, flags, options.KubernetesConfigFlags) + assert.Equal(t, streams, options.IOStreams) +} From 347adf2e2dafcde82f30b6945b73b34599c8a1b4 Mon Sep 17 00:00:00 2001 From: Alexej Disterhoft Date: Fri, 16 Feb 2024 22:10:30 +0100 Subject: [PATCH 2/3] test: add unit tests for version command --- cmd/version/version.go | 4 +- cmd/version/version_test.go | 132 ++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 2 deletions(-) diff --git a/cmd/version/version.go b/cmd/version/version.go index 98b9454..c783f89 100644 --- a/cmd/version/version.go +++ b/cmd/version/version.go @@ -46,7 +46,7 @@ func NewCmd(rootOpts *common.Options) *cobra.Command { return nil, cobra.ShellCompDirectiveNoFileComp }, Run: func(cmd *cobra.Command, args []string) { - o.PrintVersionInfo(cmd) + PrintVersionInfo(cmd) }, } @@ -58,7 +58,7 @@ func NewCmd(rootOpts *common.Options) *cobra.Command { return cmd } -func (o *options) PrintVersionInfo(cmd *cobra.Command) { +func PrintVersionInfo(cmd *cobra.Command) { versionInfo := version.NewVersion() cmd.Println(versionInfo) } diff --git a/cmd/version/version_test.go b/cmd/version/version_test.go index 8236ef3..e38d599 100644 --- a/cmd/version/version_test.go +++ b/cmd/version/version_test.go @@ -4,3 +4,135 @@ // SPDX-License-Identifier: MIT package version_test + +import ( + "bytes" + "strings" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + + "github.com/nobbs/kubectl-mapr-ticket/cmd/common" + . "github.com/nobbs/kubectl-mapr-ticket/cmd/version" + "github.com/nobbs/kubectl-mapr-ticket/pkg/version" + + "k8s.io/cli-runtime/pkg/genericiooptions" +) + +func TestNewCmd(t *testing.T) { + t.Parallel() + + t.Run("valid command call", func(t *testing.T) { + t.Parallel() + + // set up + ioStreams, _, out, errOut := genericiooptions.NewTestIOStreams() + opts := &common.Options{ + IOStreams: ioStreams, + } + + // run the command + cmd := NewCmd(opts) + cmd.SetArgs([]string{}) + err := cmd.Execute() + + // check the error + assert.NoError(t, err) + + // check the output + assert.Empty(t, errOut.String()) + assert.Equal(t, version.NewVersion().String(), strings.TrimSpace(out.String())) + }) + + t.Run("usage information", func(t *testing.T) { + t.Parallel() + + // set up + ioStreams, _, out, errOut := genericiooptions.NewTestIOStreams() + opts := &common.Options{ + IOStreams: ioStreams, + } + + // run the command + cmd := NewCmd(opts) + cmd.SetArgs([]string{"--help"}) + err := cmd.Execute() + + // check the error + assert.NoError(t, err) + + // check the output + assert.NotEmpty(t, cmd.Use) + assert.NotEmpty(t, cmd.Short) + assert.NotEmpty(t, cmd.Long) + + assert.Contains(t, out.String(), "Usage:") + assert.Contains(t, out.String(), cmd.Use) + assert.Contains(t, out.String(), cmd.Short) + assert.Contains(t, out.String(), cmd.Long) + + assert.Empty(t, errOut.String()) + }) + + t.Run("invalid command call with arguments", func(t *testing.T) { + t.Parallel() + + // set up + ioStreams, _, out, errOut := genericiooptions.NewTestIOStreams() + opts := &common.Options{ + IOStreams: ioStreams, + } + + // run the command + cmd := NewCmd(opts) + cmd.SetArgs([]string{"arg1"}) + err := cmd.Execute() + + // check the error + assert.Error(t, err) + + // check the output + assert.Contains(t, out.String(), "Usage:") + assert.Contains(t, errOut.String(), "unknown command") + }) + + t.Run("invalid command call with invalid flag", func(t *testing.T) { + t.Parallel() + + // set up + ioStreams, _, out, errOut := genericiooptions.NewTestIOStreams() + opts := &common.Options{ + IOStreams: ioStreams, + } + + // run the command + cmd := NewCmd(opts) + cmd.SetArgs([]string{"--invalid-flag"}) + err := cmd.Execute() + + // check the error + assert.Error(t, err) + + // check the output + assert.Contains(t, out.String(), "Usage:") + assert.Contains(t, errOut.String(), "unknown flag") + }) +} + +func TestPrintVersionInfo(t *testing.T) { + t.Parallel() + + // run the command + cmd := &cobra.Command{} + buf := new(bytes.Buffer) + cmd.SetOutput(buf) + + PrintVersionInfo(cmd) + + // get the expected output + expectedOutput := version.NewVersion().String() + + // compare the output + assert.Equal(t, strings.TrimSpace(expectedOutput), strings.TrimSpace(buf.String())) +} From 788b0cc68cfeb602c66cde647789854a52388e73 Mon Sep 17 00:00:00 2001 From: Alexej Disterhoft Date: Fri, 16 Feb 2024 22:10:30 +0100 Subject: [PATCH 3/3] test: add unit tests for root command --- cmd/root/root_test.go | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/cmd/root/root_test.go b/cmd/root/root_test.go index 687599e..475291b 100644 --- a/cmd/root/root_test.go +++ b/cmd/root/root_test.go @@ -4,3 +4,85 @@ // SPDX-License-Identifier: MIT package root_test + +import ( + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/nobbs/kubectl-mapr-ticket/cmd/claim" + "github.com/nobbs/kubectl-mapr-ticket/cmd/common" + "github.com/nobbs/kubectl-mapr-ticket/cmd/inspect" + . "github.com/nobbs/kubectl-mapr-ticket/cmd/root" + "github.com/nobbs/kubectl-mapr-ticket/cmd/secret" + "github.com/nobbs/kubectl-mapr-ticket/cmd/version" + "github.com/nobbs/kubectl-mapr-ticket/cmd/volume" + + "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/cli-runtime/pkg/genericiooptions" +) + +func TestNewCmd(t *testing.T) { + t.Parallel() + + t.Run("print usage information if no command is given", func(t *testing.T) { + t.Parallel() + + // set up + ioStreams, _, out, errOut := genericiooptions.NewTestIOStreams() + flags := genericclioptions.NewConfigFlags(false) + + // run the command + cmd := NewCmd(flags, ioStreams) + cmd.SetArgs([]string{}) + err := cmd.Execute() + + // check the error + assert.NoError(t, err) + + // check the output + assert.NotEmpty(t, cmd.Use) + assert.NotEmpty(t, cmd.Long) + + assert.Contains(t, out.String(), cmd.Use) + assert.Contains(t, out.String(), cmd.Long) + + assert.Empty(t, errOut.String()) + }) + + t.Run("check if all subcommands are registered", func(t *testing.T) { + t.Parallel() + + // set up + ioStreams, _, _, _ := genericiooptions.NewTestIOStreams() + flags := genericclioptions.NewConfigFlags(false) + + opts := common.NewOptions( + flags, + ioStreams, + ) + + // run the command + cmd := NewCmd(flags, ioStreams) + + // check the subcommands + assert.True(t, cmd.HasSubCommands()) + + // check if all subcommands are registered + assert.ElementsMatch(t, + []string{ + claim.NewCmd(opts).Use, + inspect.NewCmd(opts).Use, + secret.NewCmd(opts).Use, + version.NewCmd(opts).Use, + volume.NewCmd(opts).Use, + }, + func() (cmdCommandsUse []string) { + for _, c := range cmd.Commands() { + cmdCommandsUse = append(cmdCommandsUse, c.Use) + } + return + }(), + ) + }) +}