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.
24 // TestDeps is an implementation of the testing.testDeps interface,
25 // suitable for passing to testing.MainStart.
26 type TestDeps struct{}
29 var matchRe *regexp.Regexp
31 func (TestDeps) MatchString(pat, str string) (result bool, err error) {
32 if matchRe == nil || matchPat != pat {
34 matchRe, err = regexp.Compile(matchPat)
39 return matchRe.MatchString(str), nil
42 func (TestDeps) StartCPUProfile(w io.Writer) error {
43 return pprof.StartCPUProfile(w)
46 func (TestDeps) StopCPUProfile() {
47 pprof.StopCPUProfile()
50 func (TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
51 return pprof.Lookup(name).WriteTo(w, debug)
54 // ImportPath is the import path of the testing binary, set by the generated main function.
57 func (TestDeps) ImportPath() string {
61 // testLog implements testlog.Interface, logging actions by package os.
68 func (l *testLog) Getenv(key string) {
72 func (l *testLog) Open(name string) {
76 func (l *testLog) Stat(name string) {
80 func (l *testLog) Chdir(name string) {
84 // add adds the (op, name) pair to the test log.
85 func (l *testLog) add(op, name string) {
86 if strings.Contains(name, "\n") || name == "" {
103 func (TestDeps) StartTestLog(w io.Writer) {
105 log.w = bufio.NewWriter(w)
107 // Tests that define TestMain and then run m.Run multiple times
108 // will call StartTestLog/StopTestLog multiple times.
109 // Checking log.set avoids calling testlog.SetLogger multiple times
110 // (which will panic) and also avoids writing the header multiple times.
112 testlog.SetLogger(&log)
113 log.w.WriteString("# test log\n") // known to cmd/go/internal/test/test.go
118 func (TestDeps) StopTestLog() error {
120 defer log.mu.Unlock()
126 // SetPanicOnExit0 tells the os package whether to panic on os.Exit(0).
127 func (TestDeps) SetPanicOnExit0(v bool) {
128 testlog.SetPanicOnExit0(v)
131 func (TestDeps) CoordinateFuzzing(parallel int, seed [][]byte, crashDir string) error {
132 return fuzz.CoordinateFuzzing(parallel, seed, crashDir)
135 func (TestDeps) RunFuzzWorker(fn func([]byte) error) error {
136 return fuzz.RunFuzzWorker(fn)
139 func (TestDeps) ReadCorpus(dir string) ([][]byte, error) {
140 return fuzz.ReadCorpus(dir)