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