1// Copyright 2009 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 provides support for automated testing of Go packages.
6// It is intended to be used in concert with the "go test" command, which automates
7// execution of any function of the form
8//
9//	func TestXxx(*testing.T)
10//
11// where Xxx does not start with a lowercase letter. The function name
12// serves to identify the test routine.
13//
14// Within these functions, use the Error, Fail or related methods to signal failure.
15//
16// To write a new test suite, create a file that
17// contains the TestXxx functions as described here,
18// and give that file a name ending in "_test.go".
19// The file will be excluded from regular
20// package builds but will be included when the "go test" command is run.
21//
22// The test file can be in the same package as the one being tested,
23// or in a corresponding package with the suffix "_test".
24//
25// If the test file is in the same package, it may refer to unexported
26// identifiers within the package, as in this example:
27//
28//	package abs
29//
30//	import "testing"
31//
32//	func TestAbs(t *testing.T) {
33//	    got := Abs(-1)
34//	    if got != 1 {
35//	        t.Errorf("Abs(-1) = %d; want 1", got)
36//	    }
37//	}
38//
39// If the file is in a separate "_test" package, the package being tested
40// must be imported explicitly and only its exported identifiers may be used.
41// This is known as "black box" testing.
42//
43//	package abs_test
44//
45//	import (
46//		"testing"
47//
48//		"path_to_pkg/abs"
49//	)
50//
51//	func TestAbs(t *testing.T) {
52//	    got := abs.Abs(-1)
53//	    if got != 1 {
54//	        t.Errorf("Abs(-1) = %d; want 1", got)
55//	    }
56//	}
57//
58// For more detail, run "go help test" and "go help testflag".
59//
60// # Benchmarks
61//
62// Functions of the form
63//
64//	func BenchmarkXxx(*testing.B)
65//
66// are considered benchmarks, and are executed by the "go test" command when
67// its -bench flag is provided. Benchmarks are run sequentially.
68//
69// For a description of the testing flags, see
70// https://golang.org/cmd/go/#hdr-Testing_flags.
71//
72// A sample benchmark function looks like this:
73//
74//	func BenchmarkRandInt(b *testing.B) {
75//	    for range b.N {
76//	        rand.Int()
77//	    }
78//	}
79//
80// The benchmark function must run the target code b.N times.
81// It is called multiple times with b.N adjusted until the
82// benchmark function lasts long enough to be timed reliably.
83// The output
84//
85//	BenchmarkRandInt-8   	68453040	        17.8 ns/op
86//
87// means that the loop ran 68453040 times at a speed of 17.8 ns per loop.
88//
89// If a benchmark needs some expensive setup before running, the timer
90// may be reset:
91//
92//	func BenchmarkBigLen(b *testing.B) {
93//	    big := NewBig()
94//	    b.ResetTimer()
95//	    for range b.N {
96//	        big.Len()
97//	    }
98//	}
99//
100// If a benchmark needs to test performance in a parallel setting, it may use
101// the RunParallel helper function; such benchmarks are intended to be used with
102// the go test -cpu flag:
103//
104//	func BenchmarkTemplateParallel(b *testing.B) {
105//	    templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
106//	    b.RunParallel(func(pb *testing.PB) {
107//	        var buf bytes.Buffer
108//	        for pb.Next() {
109//	            buf.Reset()
110//	            templ.Execute(&buf, "World")
111//	        }
112//	    })
113//	}
114//
115// A detailed specification of the benchmark results format is given
116// in https://golang.org/design/14313-benchmark-format.
117//
118// There are standard tools for working with benchmark results at
119// https://golang.org/x/perf/cmd.
120// In particular, https://golang.org/x/perf/cmd/benchstat performs
121// statistically robust A/B comparisons.
122//
123// # Examples
124//
125// The package also runs and verifies example code. Example functions may
126// include a concluding line comment that begins with "Output:" and is compared with
127// the standard output of the function when the tests are run. (The comparison
128// ignores leading and trailing space.) These are examples of an example:
129//
130//	func ExampleHello() {
131//	    fmt.Println("hello")
132//	    // Output: hello
133//	}
134//
135//	func ExampleSalutations() {
136//	    fmt.Println("hello, and")
137//	    fmt.Println("goodbye")
138//	    // Output:
139//	    // hello, and
140//	    // goodbye
141//	}
142//
143// The comment prefix "Unordered output:" is like "Output:", but matches any
144// line order:
145//
146//	func ExamplePerm() {
147//	    for _, value := range Perm(5) {
148//	        fmt.Println(value)
149//	    }
150//	    // Unordered output: 4
151//	    // 2
152//	    // 1
153//	    // 3
154//	    // 0
155//	}
156//
157// Example functions without output comments are compiled but not executed.
158//
159// The naming convention to declare examples for the package, a function F, a type T and
160// method M on type T are:
161//
162//	func Example() { ... }
163//	func ExampleF() { ... }
164//	func ExampleT() { ... }
165//	func ExampleT_M() { ... }
166//
167// Multiple example functions for a package/type/function/method may be provided by
168// appending a distinct suffix to the name. The suffix must start with a
169// lower-case letter.
170//
171//	func Example_suffix() { ... }
172//	func ExampleF_suffix() { ... }
173//	func ExampleT_suffix() { ... }
174//	func ExampleT_M_suffix() { ... }
175//
176// The entire test file is presented as the example when it contains a single
177// example function, at least one other function, type, variable, or constant
178// declaration, and no test or benchmark functions.
179//
180// # Fuzzing
181//
182// 'go test' and the testing package support fuzzing, a testing technique where
183// a function is called with randomly generated inputs to find bugs not
184// anticipated by unit tests.
185//
186// Functions of the form
187//
188//	func FuzzXxx(*testing.F)
189//
190// are considered fuzz tests.
191//
192// For example:
193//
194//	func FuzzHex(f *testing.F) {
195//	  for _, seed := range [][]byte{{}, {0}, {9}, {0xa}, {0xf}, {1, 2, 3, 4}} {
196//	    f.Add(seed)
197//	  }
198//	  f.Fuzz(func(t *testing.T, in []byte) {
199//	    enc := hex.EncodeToString(in)
200//	    out, err := hex.DecodeString(enc)
201//	    if err != nil {
202//	      t.Fatalf("%v: decode: %v", in, err)
203//	    }
204//	    if !bytes.Equal(in, out) {
205//	      t.Fatalf("%v: not equal after round trip: %v", in, out)
206//	    }
207//	  })
208//	}
209//
210// A fuzz test maintains a seed corpus, or a set of inputs which are run by
211// default, and can seed input generation. Seed inputs may be registered by
212// calling (*F).Add or by storing files in the directory testdata/fuzz/<Name>
213// (where <Name> is the name of the fuzz test) within the package containing
214// the fuzz test. Seed inputs are optional, but the fuzzing engine may find
215// bugs more efficiently when provided with a set of small seed inputs with good
216// code coverage. These seed inputs can also serve as regression tests for bugs
217// identified through fuzzing.
218//
219// The function passed to (*F).Fuzz within the fuzz test is considered the fuzz
220// target. A fuzz target must accept a *T parameter, followed by one or more
221// parameters for random inputs. The types of arguments passed to (*F).Add must
222// be identical to the types of these parameters. The fuzz target may signal
223// that it's found a problem the same way tests do: by calling T.Fail (or any
224// method that calls it like T.Error or T.Fatal) or by panicking.
225//
226// When fuzzing is enabled (by setting the -fuzz flag to a regular expression
227// that matches a specific fuzz test), the fuzz target is called with arguments
228// generated by repeatedly making random changes to the seed inputs. On
229// supported platforms, 'go test' compiles the test executable with fuzzing
230// coverage instrumentation. The fuzzing engine uses that instrumentation to
231// find and cache inputs that expand coverage, increasing the likelihood of
232// finding bugs. If the fuzz target fails for a given input, the fuzzing engine
233// writes the inputs that caused the failure to a file in the directory
234// testdata/fuzz/<Name> within the package directory. This file later serves as
235// a seed input. If the file can't be written at that location (for example,
236// because the directory is read-only), the fuzzing engine writes the file to
237// the fuzz cache directory within the build cache instead.
238//
239// When fuzzing is disabled, the fuzz target is called with the seed inputs
240// registered with F.Add and seed inputs from testdata/fuzz/<Name>. In this
241// mode, the fuzz test acts much like a regular test, with subtests started
242// with F.Fuzz instead of T.Run.
243//
244// See https://go.dev/doc/fuzz for documentation about fuzzing.
245//
246// # Skipping
247//
248// Tests or benchmarks may be skipped at run time with a call to
249// the Skip method of *T or *B:
250//
251//	func TestTimeConsuming(t *testing.T) {
252//	    if testing.Short() {
253//	        t.Skip("skipping test in short mode.")
254//	    }
255//	    ...
256//	}
257//
258// The Skip method of *T can be used in a fuzz target if the input is invalid,
259// but should not be considered a failing input. For example:
260//
261//	func FuzzJSONMarshaling(f *testing.F) {
262//	    f.Fuzz(func(t *testing.T, b []byte) {
263//	        var v interface{}
264//	        if err := json.Unmarshal(b, &v); err != nil {
265//	            t.Skip()
266//	        }
267//	        if _, err := json.Marshal(v); err != nil {
268//	            t.Errorf("Marshal: %v", err)
269//	        }
270//	    })
271//	}
272//
273// # Subtests and Sub-benchmarks
274//
275// The Run methods of T and B allow defining subtests and sub-benchmarks,
276// without having to define separate functions for each. This enables uses
277// like table-driven benchmarks and creating hierarchical tests.
278// It also provides a way to share common setup and tear-down code:
279//
280//	func TestFoo(t *testing.T) {
281//	    // <setup code>
282//	    t.Run("A=1", func(t *testing.T) { ... })
283//	    t.Run("A=2", func(t *testing.T) { ... })
284//	    t.Run("B=1", func(t *testing.T) { ... })
285//	    // <tear-down code>
286//	}
287//
288// Each subtest and sub-benchmark has a unique name: the combination of the name
289// of the top-level test and the sequence of names passed to Run, separated by
290// slashes, with an optional trailing sequence number for disambiguation.
291//
292// The argument to the -run, -bench, and -fuzz command-line flags is an unanchored regular
293// expression that matches the test's name. For tests with multiple slash-separated
294// elements, such as subtests, the argument is itself slash-separated, with
295// expressions matching each name element in turn. Because it is unanchored, an
296// empty expression matches any string.
297// For example, using "matching" to mean "whose name contains":
298//
299//	go test -run ''        # Run all tests.
300//	go test -run Foo       # Run top-level tests matching "Foo", such as "TestFooBar".
301//	go test -run Foo/A=    # For top-level tests matching "Foo", run subtests matching "A=".
302//	go test -run /A=1      # For all top-level tests, run subtests matching "A=1".
303//	go test -fuzz FuzzFoo  # Fuzz the target matching "FuzzFoo"
304//
305// The -run argument can also be used to run a specific value in the seed
306// corpus, for debugging. For example:
307//
308//	go test -run=FuzzFoo/9ddb952d9814
309//
310// The -fuzz and -run flags can both be set, in order to fuzz a target but
311// skip the execution of all other tests.
312//
313// Subtests can also be used to control parallelism. A parent test will only
314// complete once all of its subtests complete. In this example, all tests are
315// run in parallel with each other, and only with each other, regardless of
316// other top-level tests that may be defined:
317//
318//	func TestGroupedParallel(t *testing.T) {
319//	    for _, tc := range tests {
320//	        tc := tc // capture range variable
321//	        t.Run(tc.Name, func(t *testing.T) {
322//	            t.Parallel()
323//	            ...
324//	        })
325//	    }
326//	}
327//
328// Run does not return until parallel subtests have completed, providing a way
329// to clean up after a group of parallel tests:
330//
331//	func TestTeardownParallel(t *testing.T) {
332//	    // This Run will not return until the parallel tests finish.
333//	    t.Run("group", func(t *testing.T) {
334//	        t.Run("Test1", parallelTest1)
335//	        t.Run("Test2", parallelTest2)
336//	        t.Run("Test3", parallelTest3)
337//	    })
338//	    // <tear-down code>
339//	}
340//
341// # Main
342//
343// It is sometimes necessary for a test or benchmark program to do extra setup or teardown
344// before or after it executes. It is also sometimes necessary to control
345// which code runs on the main thread. To support these and other cases,
346// if a test file contains a function:
347//
348//	func TestMain(m *testing.M)
349//
350// then the generated test will call TestMain(m) instead of running the tests or benchmarks
351// directly. TestMain runs in the main goroutine and can do whatever setup
352// and teardown is necessary around a call to m.Run. m.Run will return an exit
353// code that may be passed to os.Exit. If TestMain returns, the test wrapper
354// will pass the result of m.Run to os.Exit itself.
355//
356// When TestMain is called, flag.Parse has not been run. If TestMain depends on
357// command-line flags, including those of the testing package, it should call
358// flag.Parse explicitly. Command line flags are always parsed by the time test
359// or benchmark functions run.
360//
361// A simple implementation of TestMain is:
362//
363//	func TestMain(m *testing.M) {
364//		// call flag.Parse() here if TestMain uses flags
365//		m.Run()
366//	}
367//
368// TestMain is a low-level primitive and should not be necessary for casual
369// testing needs, where ordinary test functions suffice.
370package testing
371
372import (
373	"bytes"
374	"errors"
375	"flag"
376	"fmt"
377	"internal/goexperiment"
378	"internal/race"
379	"io"
380	"math/rand"
381	"os"
382	"reflect"
383	"runtime"
384	"runtime/debug"
385	"runtime/trace"
386	"slices"
387	"strconv"
388	"strings"
389	"sync"
390	"sync/atomic"
391	"time"
392	"unicode"
393	"unicode/utf8"
394)
395
396var initRan bool
397
398// Init registers testing flags. These flags are automatically registered by
399// the "go test" command before running test functions, so Init is only needed
400// when calling functions such as Benchmark without using "go test".
401//
402// Init is not safe to call concurrently. It has no effect if it was already called.
403func Init() {
404	if initRan {
405		return
406	}
407	initRan = true
408	// The short flag requests that tests run more quickly, but its functionality
409	// is provided by test writers themselves. The testing package is just its
410	// home. The all.bash installation script sets it to make installation more
411	// efficient, but by default the flag is off so a plain "go test" will do a
412	// full test of the package.
413	short = flag.Bool("test.short", false, "run smaller test suite to save time")
414
415	// The failfast flag requests that test execution stop after the first test failure.
416	failFast = flag.Bool("test.failfast", false, "do not start new tests after the first test failure")
417
418	// The directory in which to create profile files and the like. When run from
419	// "go test", the binary always runs in the source directory for the package;
420	// this flag lets "go test" tell the binary to write the files in the directory where
421	// the "go test" command is run.
422	outputDir = flag.String("test.outputdir", "", "write profiles to `dir`")
423	// Report as tests are run; default is silent for success.
424	flag.Var(&chatty, "test.v", "verbose: print additional output")
425	count = flag.Uint("test.count", 1, "run tests and benchmarks `n` times")
426	coverProfile = flag.String("test.coverprofile", "", "write a coverage profile to `file`")
427	gocoverdir = flag.String("test.gocoverdir", "", "write coverage intermediate files to this directory")
428	matchList = flag.String("test.list", "", "list tests, examples, and benchmarks matching `regexp` then exit")
429	match = flag.String("test.run", "", "run only tests and examples matching `regexp`")
430	skip = flag.String("test.skip", "", "do not list or run tests matching `regexp`")
431	memProfile = flag.String("test.memprofile", "", "write an allocation profile to `file`")
432	memProfileRate = flag.Int("test.memprofilerate", 0, "set memory allocation profiling `rate` (see runtime.MemProfileRate)")
433	cpuProfile = flag.String("test.cpuprofile", "", "write a cpu profile to `file`")
434	blockProfile = flag.String("test.blockprofile", "", "write a goroutine blocking profile to `file`")
435	blockProfileRate = flag.Int("test.blockprofilerate", 1, "set blocking profile `rate` (see runtime.SetBlockProfileRate)")
436	mutexProfile = flag.String("test.mutexprofile", "", "write a mutex contention profile to the named file after execution")
437	mutexProfileFraction = flag.Int("test.mutexprofilefraction", 1, "if >= 0, calls runtime.SetMutexProfileFraction()")
438	panicOnExit0 = flag.Bool("test.paniconexit0", false, "panic on call to os.Exit(0)")
439	traceFile = flag.String("test.trace", "", "write an execution trace to `file`")
440	timeout = flag.Duration("test.timeout", 0, "panic test binary after duration `d` (default 0, timeout disabled)")
441	cpuListStr = flag.String("test.cpu", "", "comma-separated `list` of cpu counts to run each test with")
442	parallel = flag.Int("test.parallel", runtime.GOMAXPROCS(0), "run at most `n` tests in parallel")
443	testlog = flag.String("test.testlogfile", "", "write test action log to `file` (for use only by cmd/go)")
444	shuffle = flag.String("test.shuffle", "off", "randomize the execution order of tests and benchmarks")
445	fullPath = flag.Bool("test.fullpath", false, "show full file names in error messages")
446
447	initBenchmarkFlags()
448	initFuzzFlags()
449}
450
451var (
452	// Flags, registered during Init.
453	short                *bool
454	failFast             *bool
455	outputDir            *string
456	chatty               chattyFlag
457	count                *uint
458	coverProfile         *string
459	gocoverdir           *string
460	matchList            *string
461	match                *string
462	skip                 *string
463	memProfile           *string
464	memProfileRate       *int
465	cpuProfile           *string
466	blockProfile         *string
467	blockProfileRate     *int
468	mutexProfile         *string
469	mutexProfileFraction *int
470	panicOnExit0         *bool
471	traceFile            *string
472	timeout              *time.Duration
473	cpuListStr           *string
474	parallel             *int
475	shuffle              *string
476	testlog              *string
477	fullPath             *bool
478
479	haveExamples bool // are there examples?
480
481	cpuList     []int
482	testlogFile *os.File
483
484	numFailed atomic.Uint32 // number of test failures
485
486	running sync.Map // map[string]time.Time of running, unpaused tests
487)
488
489type chattyFlag struct {
490	on   bool // -v is set in some form
491	json bool // -v=test2json is set, to make output better for test2json
492}
493
494func (*chattyFlag) IsBoolFlag() bool { return true }
495
496func (f *chattyFlag) Set(arg string) error {
497	switch arg {
498	default:
499		return fmt.Errorf("invalid flag -test.v=%s", arg)
500	case "true", "test2json":
501		f.on = true
502		f.json = arg == "test2json"
503	case "false":
504		f.on = false
505		f.json = false
506	}
507	return nil
508}
509
510func (f *chattyFlag) String() string {
511	if f.json {
512		return "test2json"
513	}
514	if f.on {
515		return "true"
516	}
517	return "false"
518}
519
520func (f *chattyFlag) Get() any {
521	if f.json {
522		return "test2json"
523	}
524	return f.on
525}
526
527const marker = byte(0x16) // ^V for framing
528
529func (f *chattyFlag) prefix() string {
530	if f.json {
531		return string(marker)
532	}
533	return ""
534}
535
536type chattyPrinter struct {
537	w          io.Writer
538	lastNameMu sync.Mutex // guards lastName
539	lastName   string     // last printed test name in chatty mode
540	json       bool       // -v=json output mode
541}
542
543func newChattyPrinter(w io.Writer) *chattyPrinter {
544	return &chattyPrinter{w: w, json: chatty.json}
545}
546
547// prefix is like chatty.prefix but using p.json instead of chatty.json.
548// Using p.json allows tests to check the json behavior without modifying
549// the global variable. For convenience, we allow p == nil and treat
550// that as not in json mode (because it's not chatty at all).
551func (p *chattyPrinter) prefix() string {
552	if p != nil && p.json {
553		return string(marker)
554	}
555	return ""
556}
557
558// Updatef prints a message about the status of the named test to w.
559//
560// The formatted message must include the test name itself.
561func (p *chattyPrinter) Updatef(testName, format string, args ...any) {
562	p.lastNameMu.Lock()
563	defer p.lastNameMu.Unlock()
564
565	// Since the message already implies an association with a specific new test,
566	// we don't need to check what the old test name was or log an extra NAME line
567	// for it. (We're updating it anyway, and the current message already includes
568	// the test name.)
569	p.lastName = testName
570	fmt.Fprintf(p.w, p.prefix()+format, args...)
571}
572
573// Printf prints a message, generated by the named test, that does not
574// necessarily mention that tests's name itself.
575func (p *chattyPrinter) Printf(testName, format string, args ...any) {
576	p.lastNameMu.Lock()
577	defer p.lastNameMu.Unlock()
578
579	if p.lastName == "" {
580		p.lastName = testName
581	} else if p.lastName != testName {
582		fmt.Fprintf(p.w, "%s=== NAME  %s\n", p.prefix(), testName)
583		p.lastName = testName
584	}
585
586	fmt.Fprintf(p.w, format, args...)
587}
588
589// The maximum number of stack frames to go through when skipping helper functions for
590// the purpose of decorating log messages.
591const maxStackLen = 50
592
593// common holds the elements common between T and B and
594// captures common methods such as Errorf.
595type common struct {
596	mu          sync.RWMutex         // guards this group of fields
597	output      []byte               // Output generated by test or benchmark.
598	w           io.Writer            // For flushToParent.
599	ran         bool                 // Test or benchmark (or one of its subtests) was executed.
600	failed      bool                 // Test or benchmark has failed.
601	skipped     bool                 // Test or benchmark has been skipped.
602	done        bool                 // Test is finished and all subtests have completed.
603	helperPCs   map[uintptr]struct{} // functions to be skipped when writing file/line info
604	helperNames map[string]struct{}  // helperPCs converted to function names
605	cleanups    []func()             // optional functions to be called at the end of the test
606	cleanupName string               // Name of the cleanup function.
607	cleanupPc   []uintptr            // The stack trace at the point where Cleanup was called.
608	finished    bool                 // Test function has completed.
609	inFuzzFn    bool                 // Whether the fuzz target, if this is one, is running.
610
611	chatty         *chattyPrinter // A copy of chattyPrinter, if the chatty flag is set.
612	bench          bool           // Whether the current test is a benchmark.
613	hasSub         atomic.Bool    // whether there are sub-benchmarks.
614	cleanupStarted atomic.Bool    // Registered cleanup callbacks have started to execute
615	runner         string         // Function name of tRunner running the test.
616	isParallel     bool           // Whether the test is parallel.
617
618	parent   *common
619	level    int               // Nesting depth of test or benchmark.
620	creator  []uintptr         // If level > 0, the stack trace at the point where the parent called t.Run.
621	name     string            // Name of test or benchmark.
622	start    highPrecisionTime // Time test or benchmark started
623	duration time.Duration
624	barrier  chan bool // To signal parallel subtests they may start. Nil when T.Parallel is not present (B) or not usable (when fuzzing).
625	signal   chan bool // To signal a test is done.
626	sub      []*T      // Queue of subtests to be run in parallel.
627
628	lastRaceErrors  atomic.Int64 // Max value of race.Errors seen during the test or its subtests.
629	raceErrorLogged atomic.Bool
630
631	tempDirMu  sync.Mutex
632	tempDir    string
633	tempDirErr error
634	tempDirSeq int32
635}
636
637// Short reports whether the -test.short flag is set.
638func Short() bool {
639	if short == nil {
640		panic("testing: Short called before Init")
641	}
642	// Catch code that calls this from TestMain without first calling flag.Parse.
643	if !flag.Parsed() {
644		panic("testing: Short called before Parse")
645	}
646
647	return *short
648}
649
650// testBinary is set by cmd/go to "1" if this is a binary built by "go test".
651// The value is set to "1" by a -X option to cmd/link. We assume that
652// because this is possible, the compiler will not optimize testBinary
653// into a constant on the basis that it is an unexported package-scope
654// variable that is never changed. If the compiler ever starts implementing
655// such an optimization, we will need some technique to mark this variable
656// as "changed by a cmd/link -X option".
657var testBinary = "0"
658
659// Testing reports whether the current code is being run in a test.
660// This will report true in programs created by "go test",
661// false in programs created by "go build".
662func Testing() bool {
663	return testBinary == "1"
664}
665
666// CoverMode reports what the test coverage mode is set to. The
667// values are "set", "count", or "atomic". The return value will be
668// empty if test coverage is not enabled.
669func CoverMode() string {
670	if goexperiment.CoverageRedesign {
671		return cover2.mode
672	}
673	return cover.Mode
674}
675
676// Verbose reports whether the -test.v flag is set.
677func Verbose() bool {
678	// Same as in Short.
679	if !flag.Parsed() {
680		panic("testing: Verbose called before Parse")
681	}
682	return chatty.on
683}
684
685func (c *common) checkFuzzFn(name string) {
686	if c.inFuzzFn {
687		panic(fmt.Sprintf("testing: f.%s was called inside the fuzz target, use t.%s instead", name, name))
688	}
689}
690
691// frameSkip searches, starting after skip frames, for the first caller frame
692// in a function not marked as a helper and returns that frame.
693// The search stops if it finds a tRunner function that
694// was the entry point into the test and the test is not a subtest.
695// This function must be called with c.mu held.
696func (c *common) frameSkip(skip int) runtime.Frame {
697	// If the search continues into the parent test, we'll have to hold
698	// its mu temporarily. If we then return, we need to unlock it.
699	shouldUnlock := false
700	defer func() {
701		if shouldUnlock {
702			c.mu.Unlock()
703		}
704	}()
705	var pc [maxStackLen]uintptr
706	// Skip two extra frames to account for this function
707	// and runtime.Callers itself.
708	n := runtime.Callers(skip+2, pc[:])
709	if n == 0 {
710		panic("testing: zero callers found")
711	}
712	frames := runtime.CallersFrames(pc[:n])
713	var firstFrame, prevFrame, frame runtime.Frame
714	for more := true; more; prevFrame = frame {
715		frame, more = frames.Next()
716		if frame.Function == "runtime.gopanic" {
717			continue
718		}
719		if frame.Function == c.cleanupName {
720			frames = runtime.CallersFrames(c.cleanupPc)
721			continue
722		}
723		if firstFrame.PC == 0 {
724			firstFrame = frame
725		}
726		if frame.Function == c.runner {
727			// We've gone up all the way to the tRunner calling
728			// the test function (so the user must have
729			// called tb.Helper from inside that test function).
730			// If this is a top-level test, only skip up to the test function itself.
731			// If we're in a subtest, continue searching in the parent test,
732			// starting from the point of the call to Run which created this subtest.
733			if c.level > 1 {
734				frames = runtime.CallersFrames(c.creator)
735				parent := c.parent
736				// We're no longer looking at the current c after this point,
737				// so we should unlock its mu, unless it's the original receiver,
738				// in which case our caller doesn't expect us to do that.
739				if shouldUnlock {
740					c.mu.Unlock()
741				}
742				c = parent
743				// Remember to unlock c.mu when we no longer need it, either
744				// because we went up another nesting level, or because we
745				// returned.
746				shouldUnlock = true
747				c.mu.Lock()
748				continue
749			}
750			return prevFrame
751		}
752		// If more helper PCs have been added since we last did the conversion
753		if c.helperNames == nil {
754			c.helperNames = make(map[string]struct{})
755			for pc := range c.helperPCs {
756				c.helperNames[pcToName(pc)] = struct{}{}
757			}
758		}
759		if _, ok := c.helperNames[frame.Function]; !ok {
760			// Found a frame that wasn't inside a helper function.
761			return frame
762		}
763	}
764	return firstFrame
765}
766
767// decorate prefixes the string with the file and line of the call site
768// and inserts the final newline if needed and indentation spaces for formatting.
769// This function must be called with c.mu held.
770func (c *common) decorate(s string, skip int) string {
771	frame := c.frameSkip(skip)
772	file := frame.File
773	line := frame.Line
774	if file != "" {
775		if *fullPath {
776			// If relative path, truncate file name at last file name separator.
777		} else if index := strings.LastIndexAny(file, `/\`); index >= 0 {
778			file = file[index+1:]
779		}
780	} else {
781		file = "???"
782	}
783	if line == 0 {
784		line = 1
785	}
786	buf := new(strings.Builder)
787	// Every line is indented at least 4 spaces.
788	buf.WriteString("    ")
789	fmt.Fprintf(buf, "%s:%d: ", file, line)
790	lines := strings.Split(s, "\n")
791	if l := len(lines); l > 1 && lines[l-1] == "" {
792		lines = lines[:l-1]
793	}
794	for i, line := range lines {
795		if i > 0 {
796			// Second and subsequent lines are indented an additional 4 spaces.
797			buf.WriteString("\n        ")
798		}
799		buf.WriteString(line)
800	}
801	buf.WriteByte('\n')
802	return buf.String()
803}
804
805// flushToParent writes c.output to the parent after first writing the header
806// with the given format and arguments.
807func (c *common) flushToParent(testName, format string, args ...any) {
808	p := c.parent
809	p.mu.Lock()
810	defer p.mu.Unlock()
811
812	c.mu.Lock()
813	defer c.mu.Unlock()
814
815	if len(c.output) > 0 {
816		// Add the current c.output to the print,
817		// and then arrange for the print to replace c.output.
818		// (This displays the logged output after the --- FAIL line.)
819		format += "%s"
820		args = append(args[:len(args):len(args)], c.output)
821		c.output = c.output[:0]
822	}
823
824	if c.chatty != nil && (p.w == c.chatty.w || c.chatty.json) {
825		// We're flushing to the actual output, so track that this output is
826		// associated with a specific test (and, specifically, that the next output
827		// is *not* associated with that test).
828		//
829		// Moreover, if c.output is non-empty it is important that this write be
830		// atomic with respect to the output of other tests, so that we don't end up
831		// with confusing '=== NAME' lines in the middle of our '--- PASS' block.
832		// Neither humans nor cmd/test2json can parse those easily.
833		// (See https://go.dev/issue/40771.)
834		//
835		// If test2json is used, we never flush to parent tests,
836		// so that the json stream shows subtests as they finish.
837		// (See https://go.dev/issue/29811.)
838		c.chatty.Updatef(testName, format, args...)
839	} else {
840		// We're flushing to the output buffer of the parent test, which will
841		// itself follow a test-name header when it is finally flushed to stdout.
842		fmt.Fprintf(p.w, c.chatty.prefix()+format, args...)
843	}
844}
845
846type indenter struct {
847	c *common
848}
849
850func (w indenter) Write(b []byte) (n int, err error) {
851	n = len(b)
852	for len(b) > 0 {
853		end := bytes.IndexByte(b, '\n')
854		if end == -1 {
855			end = len(b)
856		} else {
857			end++
858		}
859		// An indent of 4 spaces will neatly align the dashes with the status
860		// indicator of the parent.
861		line := b[:end]
862		if line[0] == marker {
863			w.c.output = append(w.c.output, marker)
864			line = line[1:]
865		}
866		const indent = "    "
867		w.c.output = append(w.c.output, indent...)
868		w.c.output = append(w.c.output, line...)
869		b = b[end:]
870	}
871	return
872}
873
874// fmtDuration returns a string representing d in the form "87.00s".
875func fmtDuration(d time.Duration) string {
876	return fmt.Sprintf("%.2fs", d.Seconds())
877}
878
879// TB is the interface common to T, B, and F.
880type TB interface {
881	Cleanup(func())
882	Error(args ...any)
883	Errorf(format string, args ...any)
884	Fail()
885	FailNow()
886	Failed() bool
887	Fatal(args ...any)
888	Fatalf(format string, args ...any)
889	Helper()
890	Log(args ...any)
891	Logf(format string, args ...any)
892	Name() string
893	Setenv(key, value string)
894	Skip(args ...any)
895	SkipNow()
896	Skipf(format string, args ...any)
897	Skipped() bool
898	TempDir() string
899
900	// A private method to prevent users implementing the
901	// interface and so future additions to it will not
902	// violate Go 1 compatibility.
903	private()
904}
905
906var _ TB = (*T)(nil)
907var _ TB = (*B)(nil)
908
909// T is a type passed to Test functions to manage test state and support formatted test logs.
910//
911// A test ends when its Test function returns or calls any of the methods
912// FailNow, Fatal, Fatalf, SkipNow, Skip, or Skipf. Those methods, as well as
913// the Parallel method, must be called only from the goroutine running the
914// Test function.
915//
916// The other reporting methods, such as the variations of Log and Error,
917// may be called simultaneously from multiple goroutines.
918type T struct {
919	common
920	isEnvSet bool
921	context  *testContext // For running tests and subtests.
922}
923
924func (c *common) private() {}
925
926// Name returns the name of the running (sub-) test or benchmark.
927//
928// The name will include the name of the test along with the names of
929// any nested sub-tests. If two sibling sub-tests have the same name,
930// Name will append a suffix to guarantee the returned name is unique.
931func (c *common) Name() string {
932	return c.name
933}
934
935func (c *common) setRan() {
936	if c.parent != nil {
937		c.parent.setRan()
938	}
939	c.mu.Lock()
940	defer c.mu.Unlock()
941	c.ran = true
942}
943
944// Fail marks the function as having failed but continues execution.
945func (c *common) Fail() {
946	if c.parent != nil {
947		c.parent.Fail()
948	}
949	c.mu.Lock()
950	defer c.mu.Unlock()
951	// c.done needs to be locked to synchronize checks to c.done in parent tests.
952	if c.done {
953		panic("Fail in goroutine after " + c.name + " has completed")
954	}
955	c.failed = true
956}
957
958// Failed reports whether the function has failed.
959func (c *common) Failed() bool {
960	c.mu.RLock()
961	defer c.mu.RUnlock()
962
963	if !c.done && int64(race.Errors()) > c.lastRaceErrors.Load() {
964		c.mu.RUnlock()
965		c.checkRaces()
966		c.mu.RLock()
967	}
968
969	return c.failed
970}
971
972// FailNow marks the function as having failed and stops its execution
973// by calling runtime.Goexit (which then runs all deferred calls in the
974// current goroutine).
975// Execution will continue at the next test or benchmark.
976// FailNow must be called from the goroutine running the
977// test or benchmark function, not from other goroutines
978// created during the test. Calling FailNow does not stop
979// those other goroutines.
980func (c *common) FailNow() {
981	c.checkFuzzFn("FailNow")
982	c.Fail()
983
984	// Calling runtime.Goexit will exit the goroutine, which
985	// will run the deferred functions in this goroutine,
986	// which will eventually run the deferred lines in tRunner,
987	// which will signal to the test loop that this test is done.
988	//
989	// A previous version of this code said:
990	//
991	//	c.duration = ...
992	//	c.signal <- c.self
993	//	runtime.Goexit()
994	//
995	// This previous version duplicated code (those lines are in
996	// tRunner no matter what), but worse the goroutine teardown
997	// implicit in runtime.Goexit was not guaranteed to complete
998	// before the test exited. If a test deferred an important cleanup
999	// function (like removing temporary files), there was no guarantee
1000	// it would run on a test failure. Because we send on c.signal during
1001	// a top-of-stack deferred function now, we know that the send
1002	// only happens after any other stacked defers have completed.
1003	c.mu.Lock()
1004	c.finished = true
1005	c.mu.Unlock()
1006	runtime.Goexit()
1007}
1008
1009// log generates the output. It's always at the same stack depth.
1010func (c *common) log(s string) {
1011	c.logDepth(s, 3) // logDepth + log + public function
1012}
1013
1014// logDepth generates the output at an arbitrary stack depth.
1015func (c *common) logDepth(s string, depth int) {
1016	c.mu.Lock()
1017	defer c.mu.Unlock()
1018	if c.done {
1019		// This test has already finished. Try and log this message
1020		// with our parent. If we don't have a parent, panic.
1021		for parent := c.parent; parent != nil; parent = parent.parent {
1022			parent.mu.Lock()
1023			defer parent.mu.Unlock()
1024			if !parent.done {
1025				parent.output = append(parent.output, parent.decorate(s, depth+1)...)
1026				return
1027			}
1028		}
1029		panic("Log in goroutine after " + c.name + " has completed: " + s)
1030	} else {
1031		if c.chatty != nil {
1032			if c.bench {
1033				// Benchmarks don't print === CONT, so we should skip the test
1034				// printer and just print straight to stdout.
1035				fmt.Print(c.decorate(s, depth+1))
1036			} else {
1037				c.chatty.Printf(c.name, "%s", c.decorate(s, depth+1))
1038			}
1039
1040			return
1041		}
1042		c.output = append(c.output, c.decorate(s, depth+1)...)
1043	}
1044}
1045
1046// Log formats its arguments using default formatting, analogous to Println,
1047// and records the text in the error log. For tests, the text will be printed only if
1048// the test fails or the -test.v flag is set. For benchmarks, the text is always
1049// printed to avoid having performance depend on the value of the -test.v flag.
1050func (c *common) Log(args ...any) {
1051	c.checkFuzzFn("Log")
1052	c.log(fmt.Sprintln(args...))
1053}
1054
1055// Logf formats its arguments according to the format, analogous to Printf, and
1056// records the text in the error log. A final newline is added if not provided. For
1057// tests, the text will be printed only if the test fails or the -test.v flag is
1058// set. For benchmarks, the text is always printed to avoid having performance
1059// depend on the value of the -test.v flag.
1060func (c *common) Logf(format string, args ...any) {
1061	c.checkFuzzFn("Logf")
1062	c.log(fmt.Sprintf(format, args...))
1063}
1064
1065// Error is equivalent to Log followed by Fail.
1066func (c *common) Error(args ...any) {
1067	c.checkFuzzFn("Error")
1068	c.log(fmt.Sprintln(args...))
1069	c.Fail()
1070}
1071
1072// Errorf is equivalent to Logf followed by Fail.
1073func (c *common) Errorf(format string, args ...any) {
1074	c.checkFuzzFn("Errorf")
1075	c.log(fmt.Sprintf(format, args...))
1076	c.Fail()
1077}
1078
1079// Fatal is equivalent to Log followed by FailNow.
1080func (c *common) Fatal(args ...any) {
1081	c.checkFuzzFn("Fatal")
1082	c.log(fmt.Sprintln(args...))
1083	c.FailNow()
1084}
1085
1086// Fatalf is equivalent to Logf followed by FailNow.
1087func (c *common) Fatalf(format string, args ...any) {
1088	c.checkFuzzFn("Fatalf")
1089	c.log(fmt.Sprintf(format, args...))
1090	c.FailNow()
1091}
1092
1093// Skip is equivalent to Log followed by SkipNow.
1094func (c *common) Skip(args ...any) {
1095	c.checkFuzzFn("Skip")
1096	c.log(fmt.Sprintln(args...))
1097	c.SkipNow()
1098}
1099
1100// Skipf is equivalent to Logf followed by SkipNow.
1101func (c *common) Skipf(format string, args ...any) {
1102	c.checkFuzzFn("Skipf")
1103	c.log(fmt.Sprintf(format, args...))
1104	c.SkipNow()
1105}
1106
1107// SkipNow marks the test as having been skipped and stops its execution
1108// by calling [runtime.Goexit].
1109// If a test fails (see Error, Errorf, Fail) and is then skipped,
1110// it is still considered to have failed.
1111// Execution will continue at the next test or benchmark. See also FailNow.
1112// SkipNow must be called from the goroutine running the test, not from
1113// other goroutines created during the test. Calling SkipNow does not stop
1114// those other goroutines.
1115func (c *common) SkipNow() {
1116	c.checkFuzzFn("SkipNow")
1117	c.mu.Lock()
1118	c.skipped = true
1119	c.finished = true
1120	c.mu.Unlock()
1121	runtime.Goexit()
1122}
1123
1124// Skipped reports whether the test was skipped.
1125func (c *common) Skipped() bool {
1126	c.mu.RLock()
1127	defer c.mu.RUnlock()
1128	return c.skipped
1129}
1130
1131// Helper marks the calling function as a test helper function.
1132// When printing file and line information, that function will be skipped.
1133// Helper may be called simultaneously from multiple goroutines.
1134func (c *common) Helper() {
1135	c.mu.Lock()
1136	defer c.mu.Unlock()
1137	if c.helperPCs == nil {
1138		c.helperPCs = make(map[uintptr]struct{})
1139	}
1140	// repeating code from callerName here to save walking a stack frame
1141	var pc [1]uintptr
1142	n := runtime.Callers(2, pc[:]) // skip runtime.Callers + Helper
1143	if n == 0 {
1144		panic("testing: zero callers found")
1145	}
1146	if _, found := c.helperPCs[pc[0]]; !found {
1147		c.helperPCs[pc[0]] = struct{}{}
1148		c.helperNames = nil // map will be recreated next time it is needed
1149	}
1150}
1151
1152// Cleanup registers a function to be called when the test (or subtest) and all its
1153// subtests complete. Cleanup functions will be called in last added,
1154// first called order.
1155func (c *common) Cleanup(f func()) {
1156	c.checkFuzzFn("Cleanup")
1157	var pc [maxStackLen]uintptr
1158	// Skip two extra frames to account for this function and runtime.Callers itself.
1159	n := runtime.Callers(2, pc[:])
1160	cleanupPc := pc[:n]
1161
1162	fn := func() {
1163		defer func() {
1164			c.mu.Lock()
1165			defer c.mu.Unlock()
1166			c.cleanupName = ""
1167			c.cleanupPc = nil
1168		}()
1169
1170		name := callerName(0)
1171		c.mu.Lock()
1172		c.cleanupName = name
1173		c.cleanupPc = cleanupPc
1174		c.mu.Unlock()
1175
1176		f()
1177	}
1178
1179	c.mu.Lock()
1180	defer c.mu.Unlock()
1181	c.cleanups = append(c.cleanups, fn)
1182}
1183
1184// TempDir returns a temporary directory for the test to use.
1185// The directory is automatically removed when the test and
1186// all its subtests complete.
1187// Each subsequent call to t.TempDir returns a unique directory;
1188// if the directory creation fails, TempDir terminates the test by calling Fatal.
1189func (c *common) TempDir() string {
1190	c.checkFuzzFn("TempDir")
1191	// Use a single parent directory for all the temporary directories
1192	// created by a test, each numbered sequentially.
1193	c.tempDirMu.Lock()
1194	var nonExistent bool
1195	if c.tempDir == "" { // Usually the case with js/wasm
1196		nonExistent = true
1197	} else {
1198		_, err := os.Stat(c.tempDir)
1199		nonExistent = os.IsNotExist(err)
1200		if err != nil && !nonExistent {
1201			c.Fatalf("TempDir: %v", err)
1202		}
1203	}
1204
1205	if nonExistent {
1206		c.Helper()
1207
1208		// Drop unusual characters (such as path separators or
1209		// characters interacting with globs) from the directory name to
1210		// avoid surprising os.MkdirTemp behavior.
1211		mapper := func(r rune) rune {
1212			if r < utf8.RuneSelf {
1213				const allowed = "!#$%&()+,-.=@^_{}~ "
1214				if '0' <= r && r <= '9' ||
1215					'a' <= r && r <= 'z' ||
1216					'A' <= r && r <= 'Z' {
1217					return r
1218				}
1219				if strings.ContainsRune(allowed, r) {
1220					return r
1221				}
1222			} else if unicode.IsLetter(r) || unicode.IsNumber(r) {
1223				return r
1224			}
1225			return -1
1226		}
1227		pattern := strings.Map(mapper, c.Name())
1228		c.tempDir, c.tempDirErr = os.MkdirTemp("", pattern)
1229		if c.tempDirErr == nil {
1230			c.Cleanup(func() {
1231				if err := removeAll(c.tempDir); err != nil {
1232					c.Errorf("TempDir RemoveAll cleanup: %v", err)
1233				}
1234			})
1235		}
1236	}
1237
1238	if c.tempDirErr == nil {
1239		c.tempDirSeq++
1240	}
1241	seq := c.tempDirSeq
1242	c.tempDirMu.Unlock()
1243
1244	if c.tempDirErr != nil {
1245		c.Fatalf("TempDir: %v", c.tempDirErr)
1246	}
1247
1248	dir := fmt.Sprintf("%s%c%03d", c.tempDir, os.PathSeparator, seq)
1249	if err := os.Mkdir(dir, 0777); err != nil {
1250		c.Fatalf("TempDir: %v", err)
1251	}
1252	return dir
1253}
1254
1255// removeAll is like os.RemoveAll, but retries Windows "Access is denied."
1256// errors up to an arbitrary timeout.
1257//
1258// Those errors have been known to occur spuriously on at least the
1259// windows-amd64-2012 builder (https://go.dev/issue/50051), and can only occur
1260// legitimately if the test leaves behind a temp file that either is still open
1261// or the test otherwise lacks permission to delete. In the case of legitimate
1262// failures, a failing test may take a bit longer to fail, but once the test is
1263// fixed the extra latency will go away.
1264func removeAll(path string) error {
1265	const arbitraryTimeout = 2 * time.Second
1266	var (
1267		start     time.Time
1268		nextSleep = 1 * time.Millisecond
1269	)
1270	for {
1271		err := os.RemoveAll(path)
1272		if !isWindowsRetryable(err) {
1273			return err
1274		}
1275		if start.IsZero() {
1276			start = time.Now()
1277		} else if d := time.Since(start) + nextSleep; d >= arbitraryTimeout {
1278			return err
1279		}
1280		time.Sleep(nextSleep)
1281		nextSleep += time.Duration(rand.Int63n(int64(nextSleep)))
1282	}
1283}
1284
1285// Setenv calls os.Setenv(key, value) and uses Cleanup to
1286// restore the environment variable to its original value
1287// after the test.
1288//
1289// Because Setenv affects the whole process, it cannot be used
1290// in parallel tests or tests with parallel ancestors.
1291func (c *common) Setenv(key, value string) {
1292	c.checkFuzzFn("Setenv")
1293	prevValue, ok := os.LookupEnv(key)
1294
1295	if err := os.Setenv(key, value); err != nil {
1296		c.Fatalf("cannot set environment variable: %v", err)
1297	}
1298
1299	if ok {
1300		c.Cleanup(func() {
1301			os.Setenv(key, prevValue)
1302		})
1303	} else {
1304		c.Cleanup(func() {
1305			os.Unsetenv(key)
1306		})
1307	}
1308}
1309
1310// panicHandling controls the panic handling used by runCleanup.
1311type panicHandling int
1312
1313const (
1314	normalPanic panicHandling = iota
1315	recoverAndReturnPanic
1316)
1317
1318// runCleanup is called at the end of the test.
1319// If ph is recoverAndReturnPanic, it will catch panics, and return the
1320// recovered value if any.
1321func (c *common) runCleanup(ph panicHandling) (panicVal any) {
1322	c.cleanupStarted.Store(true)
1323	defer c.cleanupStarted.Store(false)
1324
1325	if ph == recoverAndReturnPanic {
1326		defer func() {
1327			panicVal = recover()
1328		}()
1329	}
1330
1331	// Make sure that if a cleanup function panics,
1332	// we still run the remaining cleanup functions.
1333	defer func() {
1334		c.mu.Lock()
1335		recur := len(c.cleanups) > 0
1336		c.mu.Unlock()
1337		if recur {
1338			c.runCleanup(normalPanic)
1339		}
1340	}()
1341
1342	for {
1343		var cleanup func()
1344		c.mu.Lock()
1345		if len(c.cleanups) > 0 {
1346			last := len(c.cleanups) - 1
1347			cleanup = c.cleanups[last]
1348			c.cleanups = c.cleanups[:last]
1349		}
1350		c.mu.Unlock()
1351		if cleanup == nil {
1352			return nil
1353		}
1354		cleanup()
1355	}
1356}
1357
1358// resetRaces updates c.parent's count of data race errors (or the global count,
1359// if c has no parent), and updates c.lastRaceErrors to match.
1360//
1361// Any races that occurred prior to this call to resetRaces will
1362// not be attributed to c.
1363func (c *common) resetRaces() {
1364	if c.parent == nil {
1365		c.lastRaceErrors.Store(int64(race.Errors()))
1366	} else {
1367		c.lastRaceErrors.Store(c.parent.checkRaces())
1368	}
1369}
1370
1371// checkRaces checks whether the global count of data race errors has increased
1372// since c's count was last reset.
1373//
1374// If so, it marks c as having failed due to those races (logging an error for
1375// the first such race), and updates the race counts for the parents of c so
1376// that if they are currently suspended (such as in a call to T.Run) they will
1377// not log separate errors for the race(s).
1378//
1379// Note that multiple tests may be marked as failed due to the same race if they
1380// are executing in parallel.
1381func (c *common) checkRaces() (raceErrors int64) {
1382	raceErrors = int64(race.Errors())
1383	for {
1384		last := c.lastRaceErrors.Load()
1385		if raceErrors <= last {
1386			// All races have already been reported.
1387			return raceErrors
1388		}
1389		if c.lastRaceErrors.CompareAndSwap(last, raceErrors) {
1390			break
1391		}
1392	}
1393
1394	if c.raceErrorLogged.CompareAndSwap(false, true) {
1395		// This is the first race we've encountered for this test.
1396		// Mark the test as failed, and log the reason why only once.
1397		// (Note that the race detector itself will still write a goroutine
1398		// dump for any further races it detects.)
1399		c.Errorf("race detected during execution of test")
1400	}
1401
1402	// Update the parent(s) of this test so that they don't re-report the race.
1403	parent := c.parent
1404	for parent != nil {
1405		for {
1406			last := parent.lastRaceErrors.Load()
1407			if raceErrors <= last {
1408				// This race was already reported by another (likely parallel) subtest.
1409				return raceErrors
1410			}
1411			if parent.lastRaceErrors.CompareAndSwap(last, raceErrors) {
1412				break
1413			}
1414		}
1415		parent = parent.parent
1416	}
1417
1418	return raceErrors
1419}
1420
1421// callerName gives the function name (qualified with a package path)
1422// for the caller after skip frames (where 0 means the current function).
1423func callerName(skip int) string {
1424	var pc [1]uintptr
1425	n := runtime.Callers(skip+2, pc[:]) // skip + runtime.Callers + callerName
1426	if n == 0 {
1427		panic("testing: zero callers found")
1428	}
1429	return pcToName(pc[0])
1430}
1431
1432func pcToName(pc uintptr) string {
1433	pcs := []uintptr{pc}
1434	frames := runtime.CallersFrames(pcs)
1435	frame, _ := frames.Next()
1436	return frame.Function
1437}
1438
1439// Parallel signals that this test is to be run in parallel with (and only with)
1440// other parallel tests. When a test is run multiple times due to use of
1441// -test.count or -test.cpu, multiple instances of a single test never run in
1442// parallel with each other.
1443func (t *T) Parallel() {
1444	if t.isParallel {
1445		panic("testing: t.Parallel called multiple times")
1446	}
1447	if t.isEnvSet {
1448		panic("testing: t.Parallel called after t.Setenv; cannot set environment variables in parallel tests")
1449	}
1450	t.isParallel = true
1451	if t.parent.barrier == nil {
1452		// T.Parallel has no effect when fuzzing.
1453		// Multiple processes may run in parallel, but only one input can run at a
1454		// time per process so we can attribute crashes to specific inputs.
1455		return
1456	}
1457
1458	// We don't want to include the time we spend waiting for serial tests
1459	// in the test duration. Record the elapsed time thus far and reset the
1460	// timer afterwards.
1461	t.duration += highPrecisionTimeSince(t.start)
1462
1463	// Add to the list of tests to be released by the parent.
1464	t.parent.sub = append(t.parent.sub, t)
1465
1466	// Report any races during execution of this test up to this point.
1467	//
1468	// We will assume that any races that occur between here and the point where
1469	// we unblock are not caused by this subtest. That assumption usually holds,
1470	// although it can be wrong if the test spawns a goroutine that races in the
1471	// background while the rest of the test is blocked on the call to Parallel.
1472	// If that happens, we will misattribute the background race to some other
1473	// test, or to no test at all — but that false-negative is so unlikely that it
1474	// is not worth adding race-report noise for the common case where the test is
1475	// completely suspended during the call to Parallel.
1476	t.checkRaces()
1477
1478	if t.chatty != nil {
1479		t.chatty.Updatef(t.name, "=== PAUSE %s\n", t.name)
1480	}
1481	running.Delete(t.name)
1482
1483	t.signal <- true   // Release calling test.
1484	<-t.parent.barrier // Wait for the parent test to complete.
1485	t.context.waitParallel()
1486
1487	if t.chatty != nil {
1488		t.chatty.Updatef(t.name, "=== CONT  %s\n", t.name)
1489	}
1490	running.Store(t.name, highPrecisionTimeNow())
1491	t.start = highPrecisionTimeNow()
1492
1493	// Reset the local race counter to ignore any races that happened while this
1494	// goroutine was blocked, such as in the parent test or in other parallel
1495	// subtests.
1496	//
1497	// (Note that we don't call parent.checkRaces here:
1498	// if other parallel subtests have already introduced races, we want to
1499	// let them report those races instead of attributing them to the parent.)
1500	t.lastRaceErrors.Store(int64(race.Errors()))
1501}
1502
1503// Setenv calls os.Setenv(key, value) and uses Cleanup to
1504// restore the environment variable to its original value
1505// after the test.
1506//
1507// Because Setenv affects the whole process, it cannot be used
1508// in parallel tests or tests with parallel ancestors.
1509func (t *T) Setenv(key, value string) {
1510	// Non-parallel subtests that have parallel ancestors may still
1511	// run in parallel with other tests: they are only non-parallel
1512	// with respect to the other subtests of the same parent.
1513	// Since SetEnv affects the whole process, we need to disallow it
1514	// if the current test or any parent is parallel.
1515	isParallel := false
1516	for c := &t.common; c != nil; c = c.parent {
1517		if c.isParallel {
1518			isParallel = true
1519			break
1520		}
1521	}
1522	if isParallel {
1523		panic("testing: t.Setenv called after t.Parallel; cannot set environment variables in parallel tests")
1524	}
1525
1526	t.isEnvSet = true
1527
1528	t.common.Setenv(key, value)
1529}
1530
1531// InternalTest is an internal type but exported because it is cross-package;
1532// it is part of the implementation of the "go test" command.
1533type InternalTest struct {
1534	Name string
1535	F    func(*T)
1536}
1537
1538var errNilPanicOrGoexit = errors.New("test executed panic(nil) or runtime.Goexit")
1539
1540func tRunner(t *T, fn func(t *T)) {
1541	t.runner = callerName(0)
1542
1543	// When this goroutine is done, either because fn(t)
1544	// returned normally or because a test failure triggered
1545	// a call to runtime.Goexit, record the duration and send
1546	// a signal saying that the test is done.
1547	defer func() {
1548		t.checkRaces()
1549
1550		// TODO(#61034): This is the wrong place for this check.
1551		if t.Failed() {
1552			numFailed.Add(1)
1553		}
1554
1555		// Check if the test panicked or Goexited inappropriately.
1556		//
1557		// If this happens in a normal test, print output but continue panicking.
1558		// tRunner is called in its own goroutine, so this terminates the process.
1559		//
1560		// If this happens while fuzzing, recover from the panic and treat it like a
1561		// normal failure. It's important that the process keeps running in order to
1562		// find short inputs that cause panics.
1563		err := recover()
1564		signal := true
1565
1566		t.mu.RLock()
1567		finished := t.finished
1568		t.mu.RUnlock()
1569		if !finished && err == nil {
1570			err = errNilPanicOrGoexit
1571			for p := t.parent; p != nil; p = p.parent {
1572				p.mu.RLock()
1573				finished = p.finished
1574				p.mu.RUnlock()
1575				if finished {
1576					if !t.isParallel {
1577						t.Errorf("%v: subtest may have called FailNow on a parent test", err)
1578						err = nil
1579					}
1580					signal = false
1581					break
1582				}
1583			}
1584		}
1585
1586		if err != nil && t.context.isFuzzing {
1587			prefix := "panic: "
1588			if err == errNilPanicOrGoexit {
1589				prefix = ""
1590			}
1591			t.Errorf("%s%s\n%s\n", prefix, err, string(debug.Stack()))
1592			t.mu.Lock()
1593			t.finished = true
1594			t.mu.Unlock()
1595			err = nil
1596		}
1597
1598		// Use a deferred call to ensure that we report that the test is
1599		// complete even if a cleanup function calls t.FailNow. See issue 41355.
1600		didPanic := false
1601		defer func() {
1602			// Only report that the test is complete if it doesn't panic,
1603			// as otherwise the test binary can exit before the panic is
1604			// reported to the user. See issue 41479.
1605			if didPanic {
1606				return
1607			}
1608			if err != nil {
1609				panic(err)
1610			}
1611			running.Delete(t.name)
1612			t.signal <- signal
1613		}()
1614
1615		doPanic := func(err any) {
1616			t.Fail()
1617			if r := t.runCleanup(recoverAndReturnPanic); r != nil {
1618				t.Logf("cleanup panicked with %v", r)
1619			}
1620			// Flush the output log up to the root before dying.
1621			for root := &t.common; root.parent != nil; root = root.parent {
1622				root.mu.Lock()
1623				root.duration += highPrecisionTimeSince(root.start)
1624				d := root.duration
1625				root.mu.Unlock()
1626				root.flushToParent(root.name, "--- FAIL: %s (%s)\n", root.name, fmtDuration(d))
1627				if r := root.parent.runCleanup(recoverAndReturnPanic); r != nil {
1628					fmt.Fprintf(root.parent.w, "cleanup panicked with %v", r)
1629				}
1630			}
1631			didPanic = true
1632			panic(err)
1633		}
1634		if err != nil {
1635			doPanic(err)
1636		}
1637
1638		t.duration += highPrecisionTimeSince(t.start)
1639
1640		if len(t.sub) > 0 {
1641			// Run parallel subtests.
1642
1643			// Decrease the running count for this test and mark it as no longer running.
1644			t.context.release()
1645			running.Delete(t.name)
1646
1647			// Release the parallel subtests.
1648			close(t.barrier)
1649			// Wait for subtests to complete.
1650			for _, sub := range t.sub {
1651				<-sub.signal
1652			}
1653
1654			// Run any cleanup callbacks, marking the test as running
1655			// in case the cleanup hangs.
1656			cleanupStart := highPrecisionTimeNow()
1657			running.Store(t.name, cleanupStart)
1658			err := t.runCleanup(recoverAndReturnPanic)
1659			t.duration += highPrecisionTimeSince(cleanupStart)
1660			if err != nil {
1661				doPanic(err)
1662			}
1663			t.checkRaces()
1664			if !t.isParallel {
1665				// Reacquire the count for sequential tests. See comment in Run.
1666				t.context.waitParallel()
1667			}
1668		} else if t.isParallel {
1669			// Only release the count for this test if it was run as a parallel
1670			// test. See comment in Run method.
1671			t.context.release()
1672		}
1673		t.report() // Report after all subtests have finished.
1674
1675		// Do not lock t.done to allow race detector to detect race in case
1676		// the user does not appropriately synchronize a goroutine.
1677		t.done = true
1678		if t.parent != nil && !t.hasSub.Load() {
1679			t.setRan()
1680		}
1681	}()
1682	defer func() {
1683		if len(t.sub) == 0 {
1684			t.runCleanup(normalPanic)
1685		}
1686	}()
1687
1688	t.start = highPrecisionTimeNow()
1689	t.resetRaces()
1690	fn(t)
1691
1692	// code beyond here will not be executed when FailNow is invoked
1693	t.mu.Lock()
1694	t.finished = true
1695	t.mu.Unlock()
1696}
1697
1698// Run runs f as a subtest of t called name. It runs f in a separate goroutine
1699// and blocks until f returns or calls t.Parallel to become a parallel test.
1700// Run reports whether f succeeded (or at least did not fail before calling t.Parallel).
1701//
1702// Run may be called simultaneously from multiple goroutines, but all such calls
1703// must return before the outer test function for t returns.
1704func (t *T) Run(name string, f func(t *T)) bool {
1705	if t.cleanupStarted.Load() {
1706		panic("testing: t.Run called during t.Cleanup")
1707	}
1708
1709	t.hasSub.Store(true)
1710	testName, ok, _ := t.context.match.fullName(&t.common, name)
1711	if !ok || shouldFailFast() {
1712		return true
1713	}
1714	// Record the stack trace at the point of this call so that if the subtest
1715	// function - which runs in a separate stack - is marked as a helper, we can
1716	// continue walking the stack into the parent test.
1717	var pc [maxStackLen]uintptr
1718	n := runtime.Callers(2, pc[:])
1719	t = &T{
1720		common: common{
1721			barrier: make(chan bool),
1722			signal:  make(chan bool, 1),
1723			name:    testName,
1724			parent:  &t.common,
1725			level:   t.level + 1,
1726			creator: pc[:n],
1727			chatty:  t.chatty,
1728		},
1729		context: t.context,
1730	}
1731	t.w = indenter{&t.common}
1732
1733	if t.chatty != nil {
1734		t.chatty.Updatef(t.name, "=== RUN   %s\n", t.name)
1735	}
1736	running.Store(t.name, highPrecisionTimeNow())
1737
1738	// Instead of reducing the running count of this test before calling the
1739	// tRunner and increasing it afterwards, we rely on tRunner keeping the
1740	// count correct. This ensures that a sequence of sequential tests runs
1741	// without being preempted, even when their parent is a parallel test. This
1742	// may especially reduce surprises if *parallel == 1.
1743	go tRunner(t, f)
1744
1745	// The parent goroutine will block until the subtest either finishes or calls
1746	// Parallel, but in general we don't know whether the parent goroutine is the
1747	// top-level test function or some other goroutine it has spawned.
1748	// To avoid confusing false-negatives, we leave the parent in the running map
1749	// even though in the typical case it is blocked.
1750
1751	if !<-t.signal {
1752		// At this point, it is likely that FailNow was called on one of the
1753		// parent tests by one of the subtests. Continue aborting up the chain.
1754		runtime.Goexit()
1755	}
1756
1757	if t.chatty != nil && t.chatty.json {
1758		t.chatty.Updatef(t.parent.name, "=== NAME  %s\n", t.parent.name)
1759	}
1760	return !t.failed
1761}
1762
1763// Deadline reports the time at which the test binary will have
1764// exceeded the timeout specified by the -timeout flag.
1765//
1766// The ok result is false if the -timeout flag indicates “no timeout” (0).
1767func (t *T) Deadline() (deadline time.Time, ok bool) {
1768	deadline = t.context.deadline
1769	return deadline, !deadline.IsZero()
1770}
1771
1772// testContext holds all fields that are common to all tests. This includes
1773// synchronization primitives to run at most *parallel tests.
1774type testContext struct {
1775	match    *matcher
1776	deadline time.Time
1777
1778	// isFuzzing is true in the context used when generating random inputs
1779	// for fuzz targets. isFuzzing is false when running normal tests and
1780	// when running fuzz tests as unit tests (without -fuzz or when -fuzz
1781	// does not match).
1782	isFuzzing bool
1783
1784	mu sync.Mutex
1785
1786	// Channel used to signal tests that are ready to be run in parallel.
1787	startParallel chan bool
1788
1789	// running is the number of tests currently running in parallel.
1790	// This does not include tests that are waiting for subtests to complete.
1791	running int
1792
1793	// numWaiting is the number tests waiting to be run in parallel.
1794	numWaiting int
1795
1796	// maxParallel is a copy of the parallel flag.
1797	maxParallel int
1798}
1799
1800func newTestContext(maxParallel int, m *matcher) *testContext {
1801	return &testContext{
1802		match:         m,
1803		startParallel: make(chan bool),
1804		maxParallel:   maxParallel,
1805		running:       1, // Set the count to 1 for the main (sequential) test.
1806	}
1807}
1808
1809func (c *testContext) waitParallel() {
1810	c.mu.Lock()
1811	if c.running < c.maxParallel {
1812		c.running++
1813		c.mu.Unlock()
1814		return
1815	}
1816	c.numWaiting++
1817	c.mu.Unlock()
1818	<-c.startParallel
1819}
1820
1821func (c *testContext) release() {
1822	c.mu.Lock()
1823	if c.numWaiting == 0 {
1824		c.running--
1825		c.mu.Unlock()
1826		return
1827	}
1828	c.numWaiting--
1829	c.mu.Unlock()
1830	c.startParallel <- true // Pick a waiting test to be run.
1831}
1832
1833// No one should be using func Main anymore.
1834// See the doc comment on func Main and use MainStart instead.
1835var errMain = errors.New("testing: unexpected use of func Main")
1836
1837type matchStringOnly func(pat, str string) (bool, error)
1838
1839func (f matchStringOnly) MatchString(pat, str string) (bool, error)   { return f(pat, str) }
1840func (f matchStringOnly) StartCPUProfile(w io.Writer) error           { return errMain }
1841func (f matchStringOnly) StopCPUProfile()                             {}
1842func (f matchStringOnly) WriteProfileTo(string, io.Writer, int) error { return errMain }
1843func (f matchStringOnly) ImportPath() string                          { return "" }
1844func (f matchStringOnly) StartTestLog(io.Writer)                      {}
1845func (f matchStringOnly) StopTestLog() error                          { return errMain }
1846func (f matchStringOnly) SetPanicOnExit0(bool)                        {}
1847func (f matchStringOnly) CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error {
1848	return errMain
1849}
1850func (f matchStringOnly) RunFuzzWorker(func(corpusEntry) error) error { return errMain }
1851func (f matchStringOnly) ReadCorpus(string, []reflect.Type) ([]corpusEntry, error) {
1852	return nil, errMain
1853}
1854func (f matchStringOnly) CheckCorpus([]any, []reflect.Type) error { return nil }
1855func (f matchStringOnly) ResetCoverage()                          {}
1856func (f matchStringOnly) SnapshotCoverage()                       {}
1857
1858func (f matchStringOnly) InitRuntimeCoverage() (mode string, tearDown func(string, string) (string, error), snapcov func() float64) {
1859	return
1860}
1861
1862// Main is an internal function, part of the implementation of the "go test" command.
1863// It was exported because it is cross-package and predates "internal" packages.
1864// It is no longer used by "go test" but preserved, as much as possible, for other
1865// systems that simulate "go test" using Main, but Main sometimes cannot be updated as
1866// new functionality is added to the testing package.
1867// Systems simulating "go test" should be updated to use MainStart.
1868func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) {
1869	os.Exit(MainStart(matchStringOnly(matchString), tests, benchmarks, nil, examples).Run())
1870}
1871
1872// M is a type passed to a TestMain function to run the actual tests.
1873type M struct {
1874	deps        testDeps
1875	tests       []InternalTest
1876	benchmarks  []InternalBenchmark
1877	fuzzTargets []InternalFuzzTarget
1878	examples    []InternalExample
1879
1880	timer     *time.Timer
1881	afterOnce sync.Once
1882
1883	numRun int
1884
1885	// value to pass to os.Exit, the outer test func main
1886	// harness calls os.Exit with this code. See #34129.
1887	exitCode int
1888}
1889
1890// testDeps is an internal interface of functionality that is
1891// passed into this package by a test's generated main package.
1892// The canonical implementation of this interface is
1893// testing/internal/testdeps's TestDeps.
1894type testDeps interface {
1895	ImportPath() string
1896	MatchString(pat, str string) (bool, error)
1897	SetPanicOnExit0(bool)
1898	StartCPUProfile(io.Writer) error
1899	StopCPUProfile()
1900	StartTestLog(io.Writer)
1901	StopTestLog() error
1902	WriteProfileTo(string, io.Writer, int) error
1903	CoordinateFuzzing(time.Duration, int64, time.Duration, int64, int, []corpusEntry, []reflect.Type, string, string) error
1904	RunFuzzWorker(func(corpusEntry) error) error
1905	ReadCorpus(string, []reflect.Type) ([]corpusEntry, error)
1906	CheckCorpus([]any, []reflect.Type) error
1907	ResetCoverage()
1908	SnapshotCoverage()
1909	InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64)
1910}
1911
1912// MainStart is meant for use by tests generated by 'go test'.
1913// It is not meant to be called directly and is not subject to the Go 1 compatibility document.
1914// It may change signature from release to release.
1915func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) *M {
1916	registerCover2(deps.InitRuntimeCoverage())
1917	Init()
1918	return &M{
1919		deps:        deps,
1920		tests:       tests,
1921		benchmarks:  benchmarks,
1922		fuzzTargets: fuzzTargets,
1923		examples:    examples,
1924	}
1925}
1926
1927var testingTesting bool
1928var realStderr *os.File
1929
1930// Run runs the tests. It returns an exit code to pass to os.Exit.
1931func (m *M) Run() (code int) {
1932	defer func() {
1933		code = m.exitCode
1934	}()
1935
1936	// Count the number of calls to m.Run.
1937	// We only ever expected 1, but we didn't enforce that,
1938	// and now there are tests in the wild that call m.Run multiple times.
1939	// Sigh. go.dev/issue/23129.
1940	m.numRun++
1941
1942	// TestMain may have already called flag.Parse.
1943	if !flag.Parsed() {
1944		flag.Parse()
1945	}
1946
1947	if chatty.json {
1948		// With -v=json, stdout and stderr are pointing to the same pipe,
1949		// which is leading into test2json. In general, operating systems
1950		// do a good job of ensuring that writes to the same pipe through
1951		// different file descriptors are delivered whole, so that writing
1952		// AAA to stdout and BBB to stderr simultaneously produces
1953		// AAABBB or BBBAAA on the pipe, not something like AABBBA.
1954		// However, the exception to this is when the pipe fills: in that
1955		// case, Go's use of non-blocking I/O means that writing AAA
1956		// or BBB might be split across multiple system calls, making it
1957		// entirely possible to get output like AABBBA. The same problem
1958		// happens inside the operating system kernel if we switch to
1959		// blocking I/O on the pipe. This interleaved output can do things
1960		// like print unrelated messages in the middle of a TestFoo line,
1961		// which confuses test2json. Setting os.Stderr = os.Stdout will make
1962		// them share a single pfd, which will hold a lock for each program
1963		// write, preventing any interleaving.
1964		//
1965		// It might be nice to set Stderr = Stdout always, or perhaps if
1966		// we can tell they are the same file, but for now -v=json is
1967		// a very clear signal. Making the two files the same may cause
1968		// surprises if programs close os.Stdout but expect to be able
1969		// to continue to write to os.Stderr, but it's hard to see why a
1970		// test would think it could take over global state that way.
1971		//
1972		// This fix only helps programs where the output is coming directly
1973		// from Go code. It does not help programs in which a subprocess is
1974		// writing to stderr or stdout at the same time that a Go test is writing output.
1975		// It also does not help when the output is coming from the runtime,
1976		// such as when using the print/println functions, since that code writes
1977		// directly to fd 2 without any locking.
1978		// We keep realStderr around to prevent fd 2 from being closed.
1979		//
1980		// See go.dev/issue/33419.
1981		realStderr = os.Stderr
1982		os.Stderr = os.Stdout
1983	}
1984
1985	if *parallel < 1 {
1986		fmt.Fprintln(os.Stderr, "testing: -parallel can only be given a positive integer")
1987		flag.Usage()
1988		m.exitCode = 2
1989		return
1990	}
1991	if *matchFuzz != "" && *fuzzCacheDir == "" {
1992		fmt.Fprintln(os.Stderr, "testing: -test.fuzzcachedir must be set if -test.fuzz is set")
1993		flag.Usage()
1994		m.exitCode = 2
1995		return
1996	}
1997
1998	if *matchList != "" {
1999		listTests(m.deps.MatchString, m.tests, m.benchmarks, m.fuzzTargets, m.examples)
2000		m.exitCode = 0
2001		return
2002	}
2003
2004	if *shuffle != "off" {
2005		var n int64
2006		var err error
2007		if *shuffle == "on" {
2008			n = time.Now().UnixNano()
2009		} else {
2010			n, err = strconv.ParseInt(*shuffle, 10, 64)
2011			if err != nil {
2012				fmt.Fprintln(os.Stderr, `testing: -shuffle should be "off", "on", or a valid integer:`, err)
2013				m.exitCode = 2
2014				return
2015			}
2016		}
2017		fmt.Println("-test.shuffle", n)
2018		rng := rand.New(rand.NewSource(n))
2019		rng.Shuffle(len(m.tests), func(i, j int) { m.tests[i], m.tests[j] = m.tests[j], m.tests[i] })
2020		rng.Shuffle(len(m.benchmarks), func(i, j int) { m.benchmarks[i], m.benchmarks[j] = m.benchmarks[j], m.benchmarks[i] })
2021	}
2022
2023	parseCpuList()
2024
2025	m.before()
2026	defer m.after()
2027
2028	// Run tests, examples, and benchmarks unless this is a fuzz worker process.
2029	// Workers start after this is done by their parent process, and they should
2030	// not repeat this work.
2031	if !*isFuzzWorker {
2032		deadline := m.startAlarm()
2033		haveExamples = len(m.examples) > 0
2034		testRan, testOk := runTests(m.deps.MatchString, m.tests, deadline)
2035		fuzzTargetsRan, fuzzTargetsOk := runFuzzTests(m.deps, m.fuzzTargets, deadline)
2036		exampleRan, exampleOk := runExamples(m.deps.MatchString, m.examples)
2037		m.stopAlarm()
2038		if !testRan && !exampleRan && !fuzzTargetsRan && *matchBenchmarks == "" && *matchFuzz == "" {
2039			fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
2040			if testingTesting && *match != "^$" {
2041				// If this happens during testing of package testing it could be that
2042				// package testing's own logic for when to run a test is broken,
2043				// in which case every test will run nothing and succeed,
2044				// with no obvious way to detect this problem (since no tests are running).
2045				// So make 'no tests to run' a hard failure when testing package testing itself.
2046				fmt.Print(chatty.prefix(), "FAIL: package testing must run tests\n")
2047				testOk = false
2048			}
2049		}
2050		anyFailed := !testOk || !exampleOk || !fuzzTargetsOk || !runBenchmarks(m.deps.ImportPath(), m.deps.MatchString, m.benchmarks)
2051		if !anyFailed && race.Errors() > 0 {
2052			fmt.Print(chatty.prefix(), "testing: race detected outside of test execution\n")
2053			anyFailed = true
2054		}
2055		if anyFailed {
2056			fmt.Print(chatty.prefix(), "FAIL\n")
2057			m.exitCode = 1
2058			return
2059		}
2060	}
2061
2062	fuzzingOk := runFuzzing(m.deps, m.fuzzTargets)
2063	if !fuzzingOk {
2064		fmt.Print(chatty.prefix(), "FAIL\n")
2065		if *isFuzzWorker {
2066			m.exitCode = fuzzWorkerExitCode
2067		} else {
2068			m.exitCode = 1
2069		}
2070		return
2071	}
2072
2073	m.exitCode = 0
2074	if !*isFuzzWorker {
2075		fmt.Print(chatty.prefix(), "PASS\n")
2076	}
2077	return
2078}
2079
2080func (t *T) report() {
2081	if t.parent == nil {
2082		return
2083	}
2084	dstr := fmtDuration(t.duration)
2085	format := "--- %s: %s (%s)\n"
2086	if t.Failed() {
2087		t.flushToParent(t.name, format, "FAIL", t.name, dstr)
2088	} else if t.chatty != nil {
2089		if t.Skipped() {
2090			t.flushToParent(t.name, format, "SKIP", t.name, dstr)
2091		} else {
2092			t.flushToParent(t.name, format, "PASS", t.name, dstr)
2093		}
2094	}
2095}
2096
2097func listTests(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, fuzzTargets []InternalFuzzTarget, examples []InternalExample) {
2098	if _, err := matchString(*matchList, "non-empty"); err != nil {
2099		fmt.Fprintf(os.Stderr, "testing: invalid regexp in -test.list (%q): %s\n", *matchList, err)
2100		os.Exit(1)
2101	}
2102
2103	for _, test := range tests {
2104		if ok, _ := matchString(*matchList, test.Name); ok {
2105			fmt.Println(test.Name)
2106		}
2107	}
2108	for _, bench := range benchmarks {
2109		if ok, _ := matchString(*matchList, bench.Name); ok {
2110			fmt.Println(bench.Name)
2111		}
2112	}
2113	for _, fuzzTarget := range fuzzTargets {
2114		if ok, _ := matchString(*matchList, fuzzTarget.Name); ok {
2115			fmt.Println(fuzzTarget.Name)
2116		}
2117	}
2118	for _, example := range examples {
2119		if ok, _ := matchString(*matchList, example.Name); ok {
2120			fmt.Println(example.Name)
2121		}
2122	}
2123}
2124
2125// RunTests is an internal function but exported because it is cross-package;
2126// it is part of the implementation of the "go test" command.
2127func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool) {
2128	var deadline time.Time
2129	if *timeout > 0 {
2130		deadline = time.Now().Add(*timeout)
2131	}
2132	ran, ok := runTests(matchString, tests, deadline)
2133	if !ran && !haveExamples {
2134		fmt.Fprintln(os.Stderr, "testing: warning: no tests to run")
2135	}
2136	return ok
2137}
2138
2139func runTests(matchString func(pat, str string) (bool, error), tests []InternalTest, deadline time.Time) (ran, ok bool) {
2140	ok = true
2141	for _, procs := range cpuList {
2142		runtime.GOMAXPROCS(procs)
2143		for i := uint(0); i < *count; i++ {
2144			if shouldFailFast() {
2145				break
2146			}
2147			if i > 0 && !ran {
2148				// There were no tests to run on the first
2149				// iteration. This won't change, so no reason
2150				// to keep trying.
2151				break
2152			}
2153			ctx := newTestContext(*parallel, newMatcher(matchString, *match, "-test.run", *skip))
2154			ctx.deadline = deadline
2155			t := &T{
2156				common: common{
2157					signal:  make(chan bool, 1),
2158					barrier: make(chan bool),
2159					w:       os.Stdout,
2160				},
2161				context: ctx,
2162			}
2163			if Verbose() {
2164				t.chatty = newChattyPrinter(t.w)
2165			}
2166			tRunner(t, func(t *T) {
2167				for _, test := range tests {
2168					t.Run(test.Name, test.F)
2169				}
2170			})
2171			select {
2172			case <-t.signal:
2173			default:
2174				panic("internal error: tRunner exited without sending on t.signal")
2175			}
2176			ok = ok && !t.Failed()
2177			ran = ran || t.ran
2178		}
2179	}
2180	return ran, ok
2181}
2182
2183// before runs before all testing.
2184func (m *M) before() {
2185	if *memProfileRate > 0 {
2186		runtime.MemProfileRate = *memProfileRate
2187	}
2188	if *cpuProfile != "" {
2189		f, err := os.Create(toOutputDir(*cpuProfile))
2190		if err != nil {
2191			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
2192			return
2193		}
2194		if err := m.deps.StartCPUProfile(f); err != nil {
2195			fmt.Fprintf(os.Stderr, "testing: can't start cpu profile: %s\n", err)
2196			f.Close()
2197			return
2198		}
2199		// Could save f so after can call f.Close; not worth the effort.
2200	}
2201	if *traceFile != "" {
2202		f, err := os.Create(toOutputDir(*traceFile))
2203		if err != nil {
2204			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
2205			return
2206		}
2207		if err := trace.Start(f); err != nil {
2208			fmt.Fprintf(os.Stderr, "testing: can't start tracing: %s\n", err)
2209			f.Close()
2210			return
2211		}
2212		// Could save f so after can call f.Close; not worth the effort.
2213	}
2214	if *blockProfile != "" && *blockProfileRate >= 0 {
2215		runtime.SetBlockProfileRate(*blockProfileRate)
2216	}
2217	if *mutexProfile != "" && *mutexProfileFraction >= 0 {
2218		runtime.SetMutexProfileFraction(*mutexProfileFraction)
2219	}
2220	if *coverProfile != "" && CoverMode() == "" {
2221		fmt.Fprintf(os.Stderr, "testing: cannot use -test.coverprofile because test binary was not built with coverage enabled\n")
2222		os.Exit(2)
2223	}
2224	if *gocoverdir != "" && CoverMode() == "" {
2225		fmt.Fprintf(os.Stderr, "testing: cannot use -test.gocoverdir because test binary was not built with coverage enabled\n")
2226		os.Exit(2)
2227	}
2228	if *testlog != "" {
2229		// Note: Not using toOutputDir.
2230		// This file is for use by cmd/go, not users.
2231		var f *os.File
2232		var err error
2233		if m.numRun == 1 {
2234			f, err = os.Create(*testlog)
2235		} else {
2236			f, err = os.OpenFile(*testlog, os.O_WRONLY, 0)
2237			if err == nil {
2238				f.Seek(0, io.SeekEnd)
2239			}
2240		}
2241		if err != nil {
2242			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
2243			os.Exit(2)
2244		}
2245		m.deps.StartTestLog(f)
2246		testlogFile = f
2247	}
2248	if *panicOnExit0 {
2249		m.deps.SetPanicOnExit0(true)
2250	}
2251}
2252
2253// after runs after all testing.
2254func (m *M) after() {
2255	m.afterOnce.Do(func() {
2256		m.writeProfiles()
2257	})
2258
2259	// Restore PanicOnExit0 after every run, because we set it to true before
2260	// every run. Otherwise, if m.Run is called multiple times the behavior of
2261	// os.Exit(0) will not be restored after the second run.
2262	if *panicOnExit0 {
2263		m.deps.SetPanicOnExit0(false)
2264	}
2265}
2266
2267func (m *M) writeProfiles() {
2268	if *testlog != "" {
2269		if err := m.deps.StopTestLog(); err != nil {
2270			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *testlog, err)
2271			os.Exit(2)
2272		}
2273		if err := testlogFile.Close(); err != nil {
2274			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *testlog, err)
2275			os.Exit(2)
2276		}
2277	}
2278	if *cpuProfile != "" {
2279		m.deps.StopCPUProfile() // flushes profile to disk
2280	}
2281	if *traceFile != "" {
2282		trace.Stop() // flushes trace to disk
2283	}
2284	if *memProfile != "" {
2285		f, err := os.Create(toOutputDir(*memProfile))
2286		if err != nil {
2287			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
2288			os.Exit(2)
2289		}
2290		runtime.GC() // materialize all statistics
2291		if err = m.deps.WriteProfileTo("allocs", f, 0); err != nil {
2292			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *memProfile, err)
2293			os.Exit(2)
2294		}
2295		f.Close()
2296	}
2297	if *blockProfile != "" && *blockProfileRate >= 0 {
2298		f, err := os.Create(toOutputDir(*blockProfile))
2299		if err != nil {
2300			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
2301			os.Exit(2)
2302		}
2303		if err = m.deps.WriteProfileTo("block", f, 0); err != nil {
2304			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *blockProfile, err)
2305			os.Exit(2)
2306		}
2307		f.Close()
2308	}
2309	if *mutexProfile != "" && *mutexProfileFraction >= 0 {
2310		f, err := os.Create(toOutputDir(*mutexProfile))
2311		if err != nil {
2312			fmt.Fprintf(os.Stderr, "testing: %s\n", err)
2313			os.Exit(2)
2314		}
2315		if err = m.deps.WriteProfileTo("mutex", f, 0); err != nil {
2316			fmt.Fprintf(os.Stderr, "testing: can't write %s: %s\n", *mutexProfile, err)
2317			os.Exit(2)
2318		}
2319		f.Close()
2320	}
2321	if CoverMode() != "" {
2322		coverReport()
2323	}
2324}
2325
2326// toOutputDir returns the file name relocated, if required, to outputDir.
2327// Simple implementation to avoid pulling in path/filepath.
2328func toOutputDir(path string) string {
2329	if *outputDir == "" || path == "" {
2330		return path
2331	}
2332	// On Windows, it's clumsy, but we can be almost always correct
2333	// by just looking for a drive letter and a colon.
2334	// Absolute paths always have a drive letter (ignoring UNC).
2335	// Problem: if path == "C:A" and outputdir == "C:\Go" it's unclear
2336	// what to do, but even then path/filepath doesn't help.
2337	// TODO: Worth doing better? Probably not, because we're here only
2338	// under the management of go test.
2339	if runtime.GOOS == "windows" && len(path) >= 2 {
2340		letter, colon := path[0], path[1]
2341		if ('a' <= letter && letter <= 'z' || 'A' <= letter && letter <= 'Z') && colon == ':' {
2342			// If path starts with a drive letter we're stuck with it regardless.
2343			return path
2344		}
2345	}
2346	if os.IsPathSeparator(path[0]) {
2347		return path
2348	}
2349	return fmt.Sprintf("%s%c%s", *outputDir, os.PathSeparator, path)
2350}
2351
2352// startAlarm starts an alarm if requested.
2353func (m *M) startAlarm() time.Time {
2354	if *timeout <= 0 {
2355		return time.Time{}
2356	}
2357
2358	deadline := time.Now().Add(*timeout)
2359	m.timer = time.AfterFunc(*timeout, func() {
2360		m.after()
2361		debug.SetTraceback("all")
2362		extra := ""
2363
2364		if list := runningList(); len(list) > 0 {
2365			var b strings.Builder
2366			b.WriteString("\nrunning tests:")
2367			for _, name := range list {
2368				b.WriteString("\n\t")
2369				b.WriteString(name)
2370			}
2371			extra = b.String()
2372		}
2373		panic(fmt.Sprintf("test timed out after %v%s", *timeout, extra))
2374	})
2375	return deadline
2376}
2377
2378// runningList returns the list of running tests.
2379func runningList() []string {
2380	var list []string
2381	running.Range(func(k, v any) bool {
2382		list = append(list, fmt.Sprintf("%s (%v)", k.(string), highPrecisionTimeSince(v.(highPrecisionTime)).Round(time.Second)))
2383		return true
2384	})
2385	slices.Sort(list)
2386	return list
2387}
2388
2389// stopAlarm turns off the alarm.
2390func (m *M) stopAlarm() {
2391	if *timeout > 0 {
2392		m.timer.Stop()
2393	}
2394}
2395
2396func parseCpuList() {
2397	for _, val := range strings.Split(*cpuListStr, ",") {
2398		val = strings.TrimSpace(val)
2399		if val == "" {
2400			continue
2401		}
2402		cpu, err := strconv.Atoi(val)
2403		if err != nil || cpu <= 0 {
2404			fmt.Fprintf(os.Stderr, "testing: invalid value %q for -test.cpu\n", val)
2405			os.Exit(1)
2406		}
2407		cpuList = append(cpuList, cpu)
2408	}
2409	if cpuList == nil {
2410		cpuList = append(cpuList, runtime.GOMAXPROCS(-1))
2411	}
2412}
2413
2414func shouldFailFast() bool {
2415	return *failFast && numFailed.Load() > 0
2416}
2417