Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add more tests for various code paths #55

Merged
merged 3 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()))
}
Loading