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