mirror of
https://github.com/psviderski/uncloud.git
synced 2026-08-26 19:13:34 +00:00
fix: reject extra args in context commands (#376)
This commit is contained in:
@@ -0,0 +1,103 @@
|
|||||||
|
package context
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCommandArgsValidation(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
newCmd func() *cobra.Command
|
||||||
|
args []string
|
||||||
|
wantErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "root accepts no args",
|
||||||
|
newCmd: NewRootCommand,
|
||||||
|
args: nil,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "root rejects extra args",
|
||||||
|
newCmd: NewRootCommand,
|
||||||
|
args: []string{"extra"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "list accepts no args",
|
||||||
|
newCmd: NewListCommand,
|
||||||
|
args: nil,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "list rejects extra args",
|
||||||
|
newCmd: NewListCommand,
|
||||||
|
args: []string{"extra"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "show accepts no args",
|
||||||
|
newCmd: NewShowCommand,
|
||||||
|
args: nil,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "show rejects extra args",
|
||||||
|
newCmd: NewShowCommand,
|
||||||
|
args: []string{"extra"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "connection accepts no args",
|
||||||
|
newCmd: NewConnectionCommand,
|
||||||
|
args: nil,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "connection rejects extra args",
|
||||||
|
newCmd: NewConnectionCommand,
|
||||||
|
args: []string{"extra"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "use accepts no args",
|
||||||
|
newCmd: NewUseCommand,
|
||||||
|
args: nil,
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "use accepts one arg",
|
||||||
|
newCmd: NewUseCommand,
|
||||||
|
args: []string{"prod"},
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "use rejects extra args",
|
||||||
|
newCmd: NewUseCommand,
|
||||||
|
args: []string{"prod", "extra"},
|
||||||
|
wantErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
cmd := tt.newCmd()
|
||||||
|
require.NotNil(t, cmd.Args)
|
||||||
|
|
||||||
|
err := cmd.Args(cmd, tt.args)
|
||||||
|
if tt.wantErr {
|
||||||
|
require.Error(t, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ func NewConnectionCommand() *cobra.Command {
|
|||||||
Use: "connection",
|
Use: "connection",
|
||||||
Aliases: []string{"conn"},
|
Aliases: []string{"conn"},
|
||||||
Short: "Choose a new default connection for the current context.",
|
Short: "Choose a new default connection for the current context.",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||||
return selectConnection(uncli)
|
return selectConnection(uncli)
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ func NewListCommand() *cobra.Command {
|
|||||||
Use: "ls",
|
Use: "ls",
|
||||||
Aliases: []string{"list"},
|
Aliases: []string{"list"},
|
||||||
Short: "List available cluster contexts.",
|
Short: "List available cluster contexts.",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||||
return list(uncli)
|
return list(uncli)
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ func NewRootCommand() *cobra.Command {
|
|||||||
Use: "ctx",
|
Use: "ctx",
|
||||||
Aliases: []string{"context"},
|
Aliases: []string{"context"},
|
||||||
Short: "Switch between different cluster contexts. Contains subcommands to manage contexts.",
|
Short: "Switch between different cluster contexts. Contains subcommands to manage contexts.",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||||
return selectContext(uncli)
|
return selectContext(uncli)
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ func NewShowCommand() *cobra.Command {
|
|||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "show",
|
Use: "show",
|
||||||
Short: "Show current cluster context.",
|
Short: "Show current cluster context.",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||||
return show(uncli)
|
return show(uncli)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ func NewUseCommand() *cobra.Command {
|
|||||||
Short: "Switch to a different cluster context.",
|
Short: "Switch to a different cluster context.",
|
||||||
Long: "Switch to a different cluster context. If no context is provided, " +
|
Long: "Switch to a different cluster context. If no context is provided, " +
|
||||||
"a list of available contexts will be displayed for selection.",
|
"a list of available contexts will be displayed for selection.",
|
||||||
|
Args: cobra.MaximumNArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
uncli := cmd.Context().Value("cli").(*cli.CLI)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user