aboutsummaryrefslogtreecommitdiff
path: root/src/flag/flag_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/flag/flag_test.go')
-rw-r--r--src/flag/flag_test.go61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/flag/flag_test.go b/src/flag/flag_test.go
index 0d9491c020..a7450f3f48 100644
--- a/src/flag/flag_test.go
+++ b/src/flag/flag_test.go
@@ -8,9 +8,11 @@ import (
"bytes"
. "flag"
"fmt"
+ "internal/testenv"
"io"
"io/ioutil"
"os"
+ "os/exec"
"sort"
"strconv"
"strings"
@@ -544,3 +546,62 @@ func TestRangeError(t *testing.T) {
}
}
}
+
+func TestExitCode(t *testing.T) {
+ testenv.MustHaveExec(t)
+
+ magic := 123
+ if os.Getenv("GO_CHILD_FLAG") != "" {
+ fs := NewFlagSet("test", ExitOnError)
+ if os.Getenv("GO_CHILD_FLAG_HANDLE") != "" {
+ var b bool
+ fs.BoolVar(&b, os.Getenv("GO_CHILD_FLAG_HANDLE"), false, "")
+ }
+ fs.Parse([]string{os.Getenv("GO_CHILD_FLAG")})
+ os.Exit(magic)
+ }
+
+ tests := []struct {
+ flag string
+ flagHandle string
+ expectExit int
+ }{
+ {
+ flag: "-h",
+ expectExit: 0,
+ },
+ {
+ flag: "-help",
+ expectExit: 0,
+ },
+ {
+ flag: "-undefined",
+ expectExit: 2,
+ },
+ {
+ flag: "-h",
+ flagHandle: "h",
+ expectExit: magic,
+ },
+ {
+ flag: "-help",
+ flagHandle: "help",
+ expectExit: magic,
+ },
+ }
+
+ for _, test := range tests {
+ cmd := exec.Command(os.Args[0], "-test.run=TestExitCode")
+ cmd.Env = append(
+ os.Environ(),
+ "GO_CHILD_FLAG="+test.flag,
+ "GO_CHILD_FLAG_HANDLE="+test.flagHandle,
+ )
+ cmd.Run()
+ got := cmd.ProcessState.ExitCode()
+ if got != test.expectExit {
+ t.Errorf("unexpected exit code for test case %+v \n: got %d, expect %d",
+ test, got, test.expectExit)
+ }
+ }
+}