1 env GO111MODULE=on
2
3 [short] skip
4
5 # Arguments after the flag terminator should be ignored.
6 # If we pass '-- -test.v', we should not get verbose output
7 # *and* output from the test should not be echoed.
8 go test ./x -- -test.v
9 stdout '\Aok\s+example.com/x\s+[0-9.s]+\n\z'
10 ! stderr .
11
12 # For backward-compatibility with previous releases of the 'go' command,
13 # arguments that appear after unrecognized flags should not be treated
14 # as packages, even if they are unambiguously not arguments to flags.
15 # Even though ./x looks like a package path, the real package should be
16 # the implicit '.'.
17 ! go test --answer=42 ./x
18 stderr '^no Go files in .+$'
19 ! stderr '/x'
20
21 # However, *flags* that appear after unrecognized flags should still be
22 # interpreted as flags, under the (possibly-erroneous) assumption that
23 # unrecognized flags are non-boolean.
24
25 go test -v -x ./x -timeout 24h -boolflag=true foo -timeout 25h
26 stdout 'args: foo -timeout 25h'
27 stdout 'timeout: 24h0m0s$' # -timeout is unambiguously not a flag, so the real flag wins.
28
29 go test -v -x ./x -timeout 24h -boolflag foo -timeout 25h
30 stdout 'args: foo -test\.timeout=25h0m0s' # For legacy reasons, '-timeout ' is erroneously rewritten to -test.timeout; see https://golang.org/issue/40763.
31 stdout 'timeout: 24h0m0s$' # Actual flag wins.
32
33 go test -v -x ./x -timeout 24h -stringflag foo -timeout 25h
34 stdout 'args: $'
35 stdout 'timeout: 25h0m0s$' # Later flag wins.
36
37 # An explicit '-outputdir=' argument should set test.outputdir
38 # to the 'go' command's working directory, not zero it out
39 # for the test binary.
40 go test -x -coverprofile=cover.out '-outputdir=' ./x
41 stderr '-test.outputdir=[^ ]'
42 exists ./cover.out
43 ! exists ./x/cover.out
44
45 # Test flags from GOFLAGS should be forwarded to the test binary,
46 # with the 'test.' prefix in the GOFLAGS entry...
47 env GOFLAGS='-test.timeout=24h0m0s -count=1'
48 go test -v -x ./x
49 stdout 'timeout: 24h0m0s$'
50 stderr '-test.count=1'
51
52 # ...or without.
53 env GOFLAGS='-timeout=24h0m0s -count=1'
54 go test -v -x ./x
55 stdout 'timeout: 24h0m0s$'
56 stderr '-test.count=1'
57
58 # Arguments from the command line should override GOFLAGS...
59 go test -v -x -timeout=25h0m0s ./x
60 stdout 'timeout: 25h0m0s$'
61 stderr '-test.count=1'
62
63 # ...even if they use a different flag name.
64 go test -v -x -test.timeout=26h0m0s ./x
65 stdout 'timeout: 26h0m0s$'
66 stderr '-test\.timeout=26h0m0s'
67 ! stderr 'timeout=24h0m0s'
68 stderr '-test.count=1'
69
70 # Invalid flags should be reported exactly once.
71 ! go test -covermode=walrus ./x
72 stderr -count=1 'invalid value "walrus" for flag -covermode: valid modes are .*$'
73 stderr '^usage: go test .*$'
74 stderr '^Run ''go help test'' and ''go help testflag'' for details.$'
75
76 # Passing -help to the test binary should show flag help.
77 go test ./x -args -help
78 stdout 'usage_message'
79
80 # -covermode, -coverpkg, and -coverprofile should imply -cover
81 go test -covermode=set ./x
82 stdout '\s+coverage:\s+'
83
84 go test -coverpkg=encoding/binary ./x
85 stdout '\s+coverage:\s+'
86
87 go test -coverprofile=cover.out ./x
88 stdout '\s+coverage:\s+'
89 exists ./cover.out
90 rm ./cover.out
91
92 # -*profile and -trace flags should force output to the current working directory
93 # or -outputdir, not the directory containing the test.
94
95 go test -memprofile=mem.out ./x
96 exists ./mem.out
97 rm ./mem.out
98
99 go test -trace=trace.out ./x
100 exists ./trace.out
101 rm ./trace.out
102
103 # Relative paths with -outputdir should be relative to the go command's working
104 # directory, not the directory containing the test.
105 mkdir profiles
106 go test -memprofile=mem.out -outputdir=./profiles ./x
107 exists ./profiles/mem.out
108 rm profiles
109
110 -- go.mod --
111 module example.com
112 go 1.14
113 -- x/x_test.go --
114 package x
115
116 import (
117 "flag"
118 "strings"
119 "testing"
120 )
121
122 var _ = flag.String("usage_message", "", "dummy flag to check usage message")
123 var boolflag = flag.Bool("boolflag", false, "ignored boolean flag")
124 var stringflag = flag.String("stringflag", "", "ignored string flag")
125
126 func TestLogTimeout(t *testing.T) {
127 t.Logf("timeout: %v", flag.Lookup("test.timeout").Value)
128 }
129
130 func TestLogArgs(t *testing.T) {
131 t.Logf("args: %s", strings.Join(flag.Args(), " "))
132 }
133
View as plain text