1 // Copyright 2016 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.
5 // Package testdeps provides access to dependencies needed by test execution.
7 // This package is imported by the generated main package, which passes
8 // TestDeps into testing.Main. This allows tests to use packages at run time
9 // without making those packages direct dependencies of package testing.
10 // Direct dependencies of package testing are harder to write tests for.
29 // TestDeps is an implementation of the testing.testDeps interface,
30 // suitable for passing to testing.MainStart.
31 type TestDeps struct{}
34 var matchRe *regexp.Regexp
36 func (TestDeps) MatchString(pat, str string) (result bool, err error) {
37 if matchRe == nil || matchPat != pat {
39 matchRe, err = regexp.Compile(matchPat)
44 return matchRe.MatchString(str), nil
47 func (TestDeps) StartCPUProfile(w io.Writer) error {
48 return pprof.StartCPUProfile(w)
51 func (TestDeps) StopCPUProfile() {
52 pprof.StopCPUProfile()
55 func (TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
56 return pprof.Lookup(name).WriteTo(w, debug)
59 // ImportPath is the import path of the testing binary, set by the generated main function.
62 func (TestDeps) ImportPath() string {
66 // testLog implements testlog.Interface, logging actions by package os.
73 func (l *testLog) Getenv(key string) {
77 func (l *testLog) Open(name string) {
81 func (l *testLog) Stat(name string) {
85 func (l *testLog) Chdir(name string) {
89 // add adds the (op, name) pair to the test log.
90 func (l *testLog) add(op, name string) {
91 if strings.Contains(name, "\n") || name == "" {
102 l.w.WriteString(name)
108 func (TestDeps) StartTestLog(w io.Writer) {
110 log.w = bufio.NewWriter(w)
112 // Tests that define TestMain and then run m.Run multiple times
113 // will call StartTestLog/StopTestLog multiple times.
114 // Checking log.set avoids calling testlog.SetLogger multiple times
115 // (which will panic) and also avoids writing the header multiple times.
117 testlog.SetLogger(&log)
118 log.w.WriteString("# test log\n") // known to cmd/go/internal/test/test.go
123 func (TestDeps) StopTestLog() error {
125 defer log.mu.Unlock()
131 // SetPanicOnExit0 tells the os package whether to panic on os.Exit(0).
132 func (TestDeps) SetPanicOnExit0(v bool) {
133 testlog.SetPanicOnExit0(v)
136 func (TestDeps) CoordinateFuzzing(timeout time.Duration, parallel int, seed []fuzz.CorpusEntry, types []reflect.Type, corpusDir, cacheDir string) (err error) {
137 // Fuzzing may be interrupted with a timeout or if the user presses ^C.
138 // In either case, we'll stop worker processes gracefully and save
139 // crashers and interesting values.
140 ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
142 err = fuzz.CoordinateFuzzing(ctx, timeout, parallel, seed, types, corpusDir, cacheDir)
143 if err == ctx.Err() {
149 func (TestDeps) RunFuzzWorker(fn func(fuzz.CorpusEntry) error) error {
150 // Worker processes may or may not receive a signal when the user presses ^C
151 // On POSIX operating systems, a signal sent to a process group is delivered
152 // to all processes in that group. This is not the case on Windows.
153 // If the worker is interrupted, return quickly and without error.
154 // If only the coordinator process is interrupted, it tells each worker
155 // process to stop by closing its "fuzz_in" pipe.
156 ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
158 err := fuzz.RunFuzzWorker(ctx, fn)
159 if err == ctx.Err() {
165 func (TestDeps) ReadCorpus(dir string, types []reflect.Type) ([]fuzz.CorpusEntry, error) {
166 return fuzz.ReadCorpus(dir, types)