]> Cypherpunks.ru repositories - gostls13.git/commitdiff
testing: add Get method for -test.v option
authorIan Lance Taylor <iant@golang.org>
Thu, 3 Nov 2022 20:22:37 +0000 (13:22 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 3 Nov 2022 23:43:04 +0000 (23:43 +0000)
There is existing code that calls flag.Lookup("test.v") and inspects
the value. That stopped working as of CL 443596. Make code like that
continue to work at least for the case where we aren't using
-test.v=test2json.

Change-Id: Idb30b149b48ee3987a201e349cf4d9bfe9ddee56
Reviewed-on: https://go-review.googlesource.com/c/go/+/447796
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
src/testing/flag_test.go [new file with mode: 0644]
src/testing/testing.go

diff --git a/src/testing/flag_test.go b/src/testing/flag_test.go
new file mode 100644 (file)
index 0000000..483ae65
--- /dev/null
@@ -0,0 +1,86 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package testing_test
+
+import (
+       "flag"
+       "internal/testenv"
+       "os"
+       "os/exec"
+       "testing"
+)
+
+var testFlagArg = flag.String("test_flag_arg", "", "TestFlag: passing -v option")
+
+const flagTestEnv = "GO_WANT_FLAG_HELPER_PROCESS"
+
+func TestFlag(t *testing.T) {
+       if os.Getenv(flagTestEnv) == "1" {
+               testFlagHelper(t)
+               return
+       }
+
+       testenv.MustHaveExec(t)
+
+       for _, flag := range []string{"", "-test.v", "-test.v=test2json"} {
+               flag := flag
+               t.Run(flag, func(t *testing.T) {
+                       t.Parallel()
+                       exe, err := os.Executable()
+                       if err != nil {
+                               exe = os.Args[0]
+                       }
+                       cmd := exec.Command(exe, "-test.run=TestFlag", "-test_flag_arg="+flag)
+                       if flag != "" {
+                               cmd.Args = append(cmd.Args, flag)
+                       }
+                       cmd.Env = append(cmd.Environ(), flagTestEnv+"=1")
+                       b, err := cmd.CombinedOutput()
+                       if len(b) > 0 {
+                               t.Logf("%s", b)
+                       }
+                       if err != nil {
+                               t.Error(err)
+                       }
+               })
+       }
+}
+
+// testFlagHelper is called by the TestFlagHelper subprocess.
+func testFlagHelper(t *testing.T) {
+       f := flag.Lookup("test.v")
+       if f == nil {
+               t.Fatal(`flag.Lookup("test.v") failed`)
+       }
+
+       bf, ok := f.Value.(interface{ IsBoolFlag() bool })
+       if !ok {
+               t.Errorf("test.v flag (type %T) does not have IsBoolFlag method", f)
+       } else if !bf.IsBoolFlag() {
+               t.Error("test.v IsBoolFlag() returned false")
+       }
+
+       gf, ok := f.Value.(flag.Getter)
+       if !ok {
+               t.Fatalf("test.v flag (type %T) does not have Get method", f)
+       }
+       v := gf.Get()
+
+       var want any
+       switch *testFlagArg {
+       case "":
+               want = false
+       case "-test.v":
+               want = true
+       case "-test.v=test2json":
+               want = "test2json"
+       default:
+               t.Fatalf("unexpected test_flag_arg %q", *testFlagArg)
+       }
+
+       if v != want {
+               t.Errorf("test.v is %v want %v", v, want)
+       }
+}
index e694b6cb6b7a153dcde449da2dd3a0746a6a2e39..b2a65e95d3e9a2e8b9aff4b7c78d60382d374142 100644 (file)
@@ -513,6 +513,13 @@ func (f *chattyFlag) String() string {
        return "false"
 }
 
+func (f *chattyFlag) Get() any {
+       if f.json {
+               return "test2json"
+       }
+       return f.on
+}
+
 const marker = byte(0x16) // ^V for framing
 
 func (f *chattyFlag) prefix() string {