package cli import ( "fmt" "github.com/spf13/cobra" "testing" ) func Test_StopServiceCli_StopRequiresAServiceName(t *testing.T) { cli := GetCli() cli.SetArgs([]string{"stop"}) outcome := cli.Execute() if outcome == nil { t.Error("Expected error, got nil.") } } func Test_StopServiceCli_ErrorIfHostAndPortNotProvidedTogether(t *testing.T) { inputs := [][]string{ {"stop", "-H", "host"}, {"stop", "-p", "9999"}, } for _, input := range inputs { t.Run(fmt.Sprintf("%+v", input), func(t *testing.T) { cli := GetCli() cli.SetArgs(input) stopCommand, _, _ := cli.Find([]string{"stop"}) previousPreRun := stopCommand.PersistentPreRunE stopCommand.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { if err := previousPreRun(cmd, args); err == nil { t.Errorf("Expected error, got nil.") } return nil } stopCommand.RunE = func(cmd *cobra.Command, args []string) error { return nil } cli.Execute() }) } }