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.
23 // TestDeps is an implementation of the testing.testDeps interface,
24 // suitable for passing to testing.MainStart.
25 type TestDeps struct{}
28 var matchRe *regexp.Regexp
30 func (TestDeps) MatchString(pat, str string) (result bool, err error) {
31 if matchRe == nil || matchPat != pat {
33 matchRe, err = regexp.Compile(matchPat)
38 return matchRe.MatchString(str), nil
41 func (TestDeps) StartCPUProfile(w io.Writer) error {
42 return pprof.StartCPUProfile(w)
45 func (TestDeps) StopCPUProfile() {
46 pprof.StopCPUProfile()
49 func (TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
50 return pprof.Lookup(name).WriteTo(w, debug)
53 // ImportPath is the import path of the testing binary, set by the generated main function.
56 func (TestDeps) ImportPath() string {
60 // testLog implements testlog.Interface, logging actions by package os.
67 func (l *testLog) Getenv(key string) {
71 func (l *testLog) Open(name string) {
75 func (l *testLog) Stat(name string) {
79 func (l *testLog) Chdir(name string) {
83 // add adds the (op, name) pair to the test log.
84 func (l *testLog) add(op, name string) {
85 if strings.Contains(name, "\n") || name == "" {
102 func (TestDeps) StartTestLog(w io.Writer) {
104 log.w = bufio.NewWriter(w)
106 // Tests that define TestMain and then run m.Run multiple times
107 // will call StartTestLog/StopTestLog multiple times.
108 // Checking log.set avoids calling testlog.SetLogger multiple times
109 // (which will panic) and also avoids writing the header multiple times.
111 testlog.SetLogger(&log)
112 log.w.WriteString("# test log\n") // known to cmd/go/internal/test/test.go
117 func (TestDeps) StopTestLog() error {
119 defer log.mu.Unlock()