Repository: spf13/cobra
Pull request #1053 (2020-03-08 08:32:49)

Replace deprecated SetOutput func with SetOut and SetErr in test
This PR replaced deprecated SetOutput function used in test to make it easier to remove the SetOutput in the future.

CLAassistant: [![CLA assistant check](https://cla-assistant.io/pull/badge/not_signed)](https://cla-assistant.io/spf13/cobra?pullRequest=1053) <br/>Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our [Contributor License Agreement](https://cla-assistant.io/spf13/cobra?pullRequest=1053) before we can accept your contribution.<br/><sub>You have signed the CLA already but the status is still pending? Let us [recheck](https://cla-assistant.io/check/spf13/cobra?pullRequest=1053) it.</sub>

Patch:
diff --git a/command.go b/command.go
index 55ddc50..fdac9d2 100644
--- a/command.go
+++ b/command.go
@@ -18,7 +18,6 @@ package cobra
 import (
 	"bytes"
 	"context"
-	"errors"
 	"fmt"
 	"io"
 	"os"
@@ -29,8 +28,6 @@ import (
 	flag "github.com/spf13/pflag"
 )
 
-var ErrSubCommandRequired = errors.New("subcommand is required")
-
 // FParseErrWhitelist configures Flag parse errors to be ignored
 type FParseErrWhitelist flag.ParseErrorsWhitelist
 
@@ -801,7 +798,7 @@ func (c *Command) execute(a []string) (err error) {
 	}
 
 	if !c.Runnable() {
-		return ErrSubCommandRequired
+		return flag.ErrHelp
 	}
 
 	c.preRun()
@@ -952,14 +949,6 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
 			return cmd, nil
 		}
 
-		// If command wasn't runnable, show full help, but do return the error.
-		// This will result in apps by default returning a non-success exit code, but also gives them the option to
-		// handle specially.
-		if err == ErrSubCommandRequired {
-			cmd.HelpFunc()(cmd, args)
-			return cmd, err
-		}
-
 		// If root command has SilentErrors flagged,
 		// all subcommands should respect it
 		if !cmd.SilenceErrors && !c.SilenceErrors {


Test patch:
diff --git a/cobra/cmd/golden_test.go b/cobra/cmd/golden_test.go
index 9010caa..e4055f3 100644
--- a/cobra/cmd/golden_test.go
+++ b/cobra/cmd/golden_test.go
@@ -13,8 +13,10 @@ var update = flag.Bool("update", false, "update .golden files")
 
 func init() {
 	// Mute commands.
-	addCmd.SetOutput(new(bytes.Buffer))
-	initCmd.SetOutput(new(bytes.Buffer))
+	addCmd.SetOut(new(bytes.Buffer))
+	addCmd.SetErr(new(bytes.Buffer))
+	initCmd.SetOut(new(bytes.Buffer))
+	initCmd.SetErr(new(bytes.Buffer))
 }
 
 // ensureLF converts any \r\n to \n
diff --git a/command_test.go b/command_test.go
index 1215593..8ec52f4 100644
--- a/command_test.go
+++ b/command_test.go
@@ -21,7 +21,8 @@ func executeCommand(root *Command, args ...string) (output string, err error) {
 
 func executeCommandWithContext(ctx context.Context, root *Command, args ...string) (output string, err error) {
 	buf := new(bytes.Buffer)
-	root.SetOutput(buf)
+	root.SetOut(buf)
+	root.SetErr(buf)
 	root.SetArgs(args)
 
 	err = root.ExecuteContext(ctx)
@@ -31,7 +32,8 @@ func executeCommandWithContext(ctx context.Context, root *Command, args ...strin
 
 func executeCommandC(root *Command, args ...string) (c *Command, output string, err error) {
 	buf := new(bytes.Buffer)
-	root.SetOutput(buf)
+	root.SetOut(buf)
+	root.SetErr(buf)
 	root.SetArgs(args)
 
 	c, err = root.ExecuteC()
@@ -903,8 +905,8 @@ func TestHelpExecutedOnNonRunnableChild(t *testing.T) {
 	rootCmd.AddCommand(childCmd)
 
 	output, err := executeCommand(rootCmd, "child")
-	if err != ErrSubCommandRequired {
-		t.Errorf("Expected error")
+	if err != nil {
+		t.Errorf("Unexpected error: %v", err)
 	}
 
 	checkStringContains(t, output, childCmd.Long)