Skip to content

Commit

Permalink
test: add more tests for various code paths (#55)
Browse files Browse the repository at this point in the history
* test: add more tests for various code paths

* test: add unit tests for version command

* test: add unit tests for root command
  • Loading branch information
nobbs authored Feb 17, 2024
1 parent da53337 commit 4931b3d
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 2 deletions.
6 changes: 6 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
25 changes: 25 additions & 0 deletions cmd/common/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
82 changes: 82 additions & 0 deletions cmd/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}(),
)
})
}
4 changes: 2 additions & 2 deletions cmd/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand All @@ -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)
}
132 changes: 132 additions & 0 deletions cmd/version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}

0 comments on commit 4931b3d

Please sign in to comment.