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.
28 // TestDeps is an implementation of the testing.testDeps interface,
29 // suitable for passing to testing.MainStart.
30 type TestDeps struct{}
33 var matchRe *regexp.Regexp
35 func (TestDeps) MatchString(pat, str string) (result bool, err error) {
36 if matchRe == nil || matchPat != pat {
38 matchRe, err = regexp.Compile(matchPat)
43 return matchRe.MatchString(str), nil
46 func (TestDeps) StartCPUProfile(w io.Writer) error {
47 return pprof.StartCPUProfile(w)
50 func (TestDeps) StopCPUProfile() {
51 pprof.StopCPUProfile()
54 func (TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
55 return pprof.Lookup(name).WriteTo(w, debug)
58 // ImportPath is the import path of the testing binary, set by the generated main function.
61 func (TestDeps) ImportPath() string {
65 // testLog implements testlog.Interface, logging actions by package os.
72 func (l *testLog) Getenv(key string) {
76 func (l *testLog) Open(name string) {
80 func (l *testLog) Stat(name string) {
84 func (l *testLog) Chdir(name string) {
88 // add adds the (op, name) pair to the test log.
89 func (l *testLog) add(op, name string) {
90 if strings.Contains(name, "\n") || name == "" {
101 l.w.WriteString(name)
107 func (TestDeps) StartTestLog(w io.Writer) {
109 log.w = bufio.NewWriter(w)
111 // Tests that define TestMain and then run m.Run multiple times
112 // will call StartTestLog/StopTestLog multiple times.
113 // Checking log.set avoids calling testlog.SetLogger multiple times
114 // (which will panic) and also avoids writing the header multiple times.
116 testlog.SetLogger(&log)
117 log.w.WriteString("# test log\n") // known to cmd/go/internal/test/test.go
122 func (TestDeps) StopTestLog() error {
124 defer log.mu.Unlock()
130 // SetPanicOnExit0 tells the os package whether to panic on os.Exit(0).
131 func (TestDeps) SetPanicOnExit0(v bool) {
132 testlog.SetPanicOnExit0(v)
135 func (TestDeps) CoordinateFuzzing(timeout time.Duration, parallel int, seed []fuzz.CorpusEntry, corpusDir, cacheDir string) error {
136 // Fuzzing may be interrupted with a timeout or if the user presses ^C.
137 // In either case, we'll stop worker processes gracefully and save
138 // crashers and interesting values.
139 ctx, cancel := context.WithCancel(context.Background())
141 ctx, cancel = context.WithTimeout(ctx, timeout)
143 ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
146 err := fuzz.CoordinateFuzzing(ctx, parallel, seed, corpusDir, cacheDir)
147 if err == ctx.Err() {
153 func (TestDeps) RunFuzzWorker(fn func([]byte) error) error {
154 // Worker processes may or may not receive a signal when the user presses ^C
155 // On POSIX operating systems, a signal sent to a process group is delivered
156 // to all processes in that group. This is not the case on Windows.
157 // If the worker is interrupted, return quickly and without error.
158 // If only the coordinator process is interrupted, it tells each worker
159 // process to stop by closing its "fuzz_in" pipe.
160 ctx, cancel := context.WithCancel(context.Background())
161 ctx, stop := signal.NotifyContext(ctx, os.Interrupt)
164 err := fuzz.RunFuzzWorker(ctx, fn)
165 if err == ctx.Err() {
171 func (TestDeps) ReadCorpus(dir string) ([]fuzz.CorpusEntry, error) {
172 return fuzz.ReadCorpus(dir)