Skip to content

Commit

Permalink
Remove redundant ExitOnError output (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
cornfeedhobo authored Sep 9, 2020
1 parent ce4d9a3 commit ad91e2f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
1 change: 0 additions & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,6 @@ func (f *FlagSet) Parse(arguments []string) error {
case ContinueOnError:
return err
case ExitOnError:
fmt.Println(err)
os.Exit(2)
case PanicOnError:
panic(err)
Expand Down
29 changes: 29 additions & 0 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io/ioutil"
"net"
"os"
"os/exec"
"reflect"
"sort"
"strconv"
Expand Down Expand Up @@ -897,6 +898,34 @@ func TestOutput(t *testing.T) {
}
}

func TestOutputExitOnError(t *testing.T) {
if os.Getenv("PFLAG_CRASH_TEST") == "1" {
CommandLine = NewFlagSet(t.Name(), ExitOnError)
os.Args = []string{t.Name(), "--unknown"}
Parse()
t.Fatal("this error should not be triggered")
return
}
mockStdout := bytes.NewBufferString("")
mockStderr := bytes.NewBufferString("")
cmd := exec.Command(os.Args[0], "-test.run="+t.Name())
cmd.Env = append(os.Environ(), "PFLAG_CRASH_TEST=1")
cmd.Stdout = mockStdout
cmd.Stderr = mockStderr
err := cmd.Run()
if e, ok := err.(*exec.ExitError); ok && !e.Success() {
want := "unknown flag: --unknown\nUsage of " + t.Name() + ":\n"
if got := mockStderr.String(); got != want {
t.Errorf("got '%s', want '%s'", got, want)
}
if got := mockStdout.String(); len(got) != 0 {
t.Errorf("stdout should be empty, got: %s", got)
}
return
}
t.Fatal("this error should not be triggered")
}

// This tests that one can reset the flags. This still works but not well, and is
// superseded by FlagSet.
func TestChangingArgs(t *testing.T) {
Expand Down

0 comments on commit ad91e2f

Please sign in to comment.