mirror of
https://github.com/spf13/cobra
synced 2025-05-05 21:07:24 +00:00
Avoid storing pointer to nil
Before this change, the new test fails with: --- FAIL: TestSetOutput (0.00s) command_test.go:198: expected setting output to nil to revert back to stdout, got <nil>
This commit is contained in:
parent
f2b7c790dd
commit
61a4023def
2 changed files with 11 additions and 3 deletions
|
@ -113,7 +113,7 @@ type Command struct {
|
||||||
flagErrorBuf *bytes.Buffer
|
flagErrorBuf *bytes.Buffer
|
||||||
|
|
||||||
args []string // actual args parsed from flags
|
args []string // actual args parsed from flags
|
||||||
output *io.Writer // out writer if set in SetOutput(w)
|
output io.Writer // out writer if set in SetOutput(w)
|
||||||
usageFunc func(*Command) error // Usage can be defined by application
|
usageFunc func(*Command) error // Usage can be defined by application
|
||||||
usageTemplate string // Can be defined by Application
|
usageTemplate string // Can be defined by Application
|
||||||
flagErrorFunc func(*Command, error) error
|
flagErrorFunc func(*Command, error) error
|
||||||
|
@ -141,7 +141,7 @@ func (c *Command) SetArgs(a []string) {
|
||||||
// SetOutput sets the destination for usage and error messages.
|
// SetOutput sets the destination for usage and error messages.
|
||||||
// If output is nil, os.Stderr is used.
|
// If output is nil, os.Stderr is used.
|
||||||
func (c *Command) SetOutput(output io.Writer) {
|
func (c *Command) SetOutput(output io.Writer) {
|
||||||
c.output = &output
|
c.output = output
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetUsageFunc sets usage function. Usage can be defined by application.
|
// SetUsageFunc sets usage function. Usage can be defined by application.
|
||||||
|
@ -199,7 +199,7 @@ func (c *Command) OutOrStderr() io.Writer {
|
||||||
|
|
||||||
func (c *Command) getOut(def io.Writer) io.Writer {
|
func (c *Command) getOut(def io.Writer) io.Writer {
|
||||||
if c.output != nil {
|
if c.output != nil {
|
||||||
return *c.output
|
return c.output
|
||||||
}
|
}
|
||||||
if c.HasParent() {
|
if c.HasParent() {
|
||||||
return c.parent.getOut(def)
|
return c.parent.getOut(def)
|
||||||
|
|
|
@ -191,6 +191,14 @@ func TestEnableCommandSortingIsDisabled(t *testing.T) {
|
||||||
EnableCommandSorting = true
|
EnableCommandSorting = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSetOutput(t *testing.T) {
|
||||||
|
cmd := &Command{}
|
||||||
|
cmd.SetOutput(nil)
|
||||||
|
if out := cmd.OutOrStdout(); out != os.Stdout {
|
||||||
|
t.Fatalf("expected setting output to nil to revert back to stdout, got %v", out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFlagErrorFunc(t *testing.T) {
|
func TestFlagErrorFunc(t *testing.T) {
|
||||||
|
|
||||||
cmd := &Command{
|
cmd := &Command{
|
||||||
|
|
Loading…
Add table
Reference in a new issue