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(
137 timeout time.Duration,
139 minimizeTimeout time.Duration,
142 seed []fuzz.CorpusEntry,
143 types []reflect.Type,
145 cacheDir string) (err error) {
146 // Fuzzing may be interrupted with a timeout or if the user presses ^C.
147 // In either case, we'll stop worker processes gracefully and save
148 // crashers and interesting values.
149 ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
151 err = fuzz.CoordinateFuzzing(ctx, fuzz.CoordinateFuzzingOpts{
155 MinimizeTimeout: minimizeTimeout,
156 MinimizeLimit: minimizeLimit,
160 CorpusDir: corpusDir,
163 if err == ctx.Err() {
169 func (TestDeps) RunFuzzWorker(fn func(fuzz.CorpusEntry) error) error {
170 // Worker processes may or may not receive a signal when the user presses ^C
171 // On POSIX operating systems, a signal sent to a process group is delivered
172 // to all processes in that group. This is not the case on Windows.
173 // If the worker is interrupted, return quickly and without error.
174 // If only the coordinator process is interrupted, it tells each worker
175 // process to stop by closing its "fuzz_in" pipe.
176 ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
178 err := fuzz.RunFuzzWorker(ctx, fn)
179 if err == ctx.Err() {
185 func (TestDeps) ReadCorpus(dir string, types []reflect.Type) ([]fuzz.CorpusEntry, error) {
186 return fuzz.ReadCorpus(dir, types)
189 func (TestDeps) CheckCorpus(vals []interface{}, types []reflect.Type) error {
190 return fuzz.CheckCorpus(vals, types)
193 func (TestDeps) ResetCoverage() {
197 func (TestDeps) SnapshotCoverage() {
198 fuzz.SnapshotCoverage()