Merge branch 'main' of git.warky.dev:wdevs/unitdore
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// TestStartArgCount verifies cobra.ExactArgs(1) rejects wrong arg counts for start.
|
||||
func TestStartArgCount(t *testing.T) {
|
||||
cmd := &cobra.Command{Use: "start", Short: "fake"}
|
||||
cmd.Args = exactArgsOne
|
||||
if err := cmd.ValidateArgs([]string{}); err == nil {
|
||||
t.Error("expected error when called with no args")
|
||||
}
|
||||
if err := cmd.ValidateArgs([]string{"a", "b"}); err == nil {
|
||||
t.Error("expected error when called with too many args")
|
||||
}
|
||||
if err := cmd.ValidateArgs([]string{"myunit"}); err != nil {
|
||||
t.Errorf("unexpected error for single arg: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStopArgCount verifies cobra.ExactArgs(1) rejects wrong arg counts for stop.
|
||||
func TestStopArgCount(t *testing.T) {
|
||||
cmd := &cobra.Command{Use: "stop", Short: "fake"}
|
||||
cmd.Args = exactArgsOne
|
||||
if err := cmd.ValidateArgs([]string{}); err == nil {
|
||||
t.Error("expected error when called with no args")
|
||||
}
|
||||
if err := cmd.ValidateArgs([]string{"a", "b"}); err == nil {
|
||||
t.Error("expected error when called with too many args")
|
||||
}
|
||||
if err := cmd.ValidateArgs([]string{"myunit"}); err != nil {
|
||||
t.Errorf("unexpected error for single arg: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// exactArgsOne requires exactly one positional argument. Mirrors cobra.ExactArgs(1).
|
||||
func exactArgsOne(cmd *cobra.Command, args []string) error {
|
||||
if len(args) != 1 {
|
||||
return fmt.Errorf("start/stop requires exactly 1 argument")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user