...

Text file src/cmd/go/testdata/script/test_flags.txt

Documentation: cmd/go/testdata/script

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

View as plain text