Skip to content

Commit 3615eb1

Browse files
ianlancetaylorgopherbot
authored andcommitted
testing: add Get method for -test.v option
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 <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Russ Cox <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 69abfab commit 3615eb1

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/testing/flag_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package testing_test
6+
7+
import (
8+
"flag"
9+
"internal/testenv"
10+
"os"
11+
"os/exec"
12+
"testing"
13+
)
14+
15+
var testFlagArg = flag.String("test_flag_arg", "", "TestFlag: passing -v option")
16+
17+
const flagTestEnv = "GO_WANT_FLAG_HELPER_PROCESS"
18+
19+
func TestFlag(t *testing.T) {
20+
if os.Getenv(flagTestEnv) == "1" {
21+
testFlagHelper(t)
22+
return
23+
}
24+
25+
testenv.MustHaveExec(t)
26+
27+
for _, flag := range []string{"", "-test.v", "-test.v=test2json"} {
28+
flag := flag
29+
t.Run(flag, func(t *testing.T) {
30+
t.Parallel()
31+
exe, err := os.Executable()
32+
if err != nil {
33+
exe = os.Args[0]
34+
}
35+
cmd := exec.Command(exe, "-test.run=TestFlag", "-test_flag_arg="+flag)
36+
if flag != "" {
37+
cmd.Args = append(cmd.Args, flag)
38+
}
39+
cmd.Env = append(cmd.Environ(), flagTestEnv+"=1")
40+
b, err := cmd.CombinedOutput()
41+
if len(b) > 0 {
42+
t.Logf("%s", b)
43+
}
44+
if err != nil {
45+
t.Error(err)
46+
}
47+
})
48+
}
49+
}
50+
51+
// testFlagHelper is called by the TestFlagHelper subprocess.
52+
func testFlagHelper(t *testing.T) {
53+
f := flag.Lookup("test.v")
54+
if f == nil {
55+
t.Fatal(`flag.Lookup("test.v") failed`)
56+
}
57+
58+
bf, ok := f.Value.(interface{ IsBoolFlag() bool })
59+
if !ok {
60+
t.Errorf("test.v flag (type %T) does not have IsBoolFlag method", f)
61+
} else if !bf.IsBoolFlag() {
62+
t.Error("test.v IsBoolFlag() returned false")
63+
}
64+
65+
gf, ok := f.Value.(flag.Getter)
66+
if !ok {
67+
t.Fatalf("test.v flag (type %T) does not have Get method", f)
68+
}
69+
v := gf.Get()
70+
71+
var want any
72+
switch *testFlagArg {
73+
case "":
74+
want = false
75+
case "-test.v":
76+
want = true
77+
case "-test.v=test2json":
78+
want = "test2json"
79+
default:
80+
t.Fatalf("unexpected test_flag_arg %q", *testFlagArg)
81+
}
82+
83+
if v != want {
84+
t.Errorf("test.v is %v want %v", v, want)
85+
}
86+
}

src/testing/testing.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,13 @@ func (f *chattyFlag) String() string {
513513
return "false"
514514
}
515515

516+
func (f *chattyFlag) Get() any {
517+
if f.json {
518+
return "test2json"
519+
}
520+
return f.on
521+
}
522+
516523
const marker = byte(0x16) // ^V for framing
517524

518525
func (f *chattyFlag) prefix() string {

0 commit comments

Comments
 (0)