]> Cypherpunks.ru repositories - gostls13.git/blob - src/testing/internal/testdeps/deps.go
[dev.fuzz] all: merge master into dev.fuzz
[gostls13.git] / src / testing / internal / testdeps / deps.go
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.
4
5 // Package testdeps provides access to dependencies needed by test execution.
6 //
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.
11 package testdeps
12
13 import (
14         "bufio"
15         "internal/fuzz"
16         "internal/testlog"
17         "io"
18         "regexp"
19         "runtime/pprof"
20         "strings"
21         "sync"
22 )
23
24 // TestDeps is an implementation of the testing.testDeps interface,
25 // suitable for passing to testing.MainStart.
26 type TestDeps struct{}
27
28 var matchPat string
29 var matchRe *regexp.Regexp
30
31 func (TestDeps) MatchString(pat, str string) (result bool, err error) {
32         if matchRe == nil || matchPat != pat {
33                 matchPat = pat
34                 matchRe, err = regexp.Compile(matchPat)
35                 if err != nil {
36                         return
37                 }
38         }
39         return matchRe.MatchString(str), nil
40 }
41
42 func (TestDeps) StartCPUProfile(w io.Writer) error {
43         return pprof.StartCPUProfile(w)
44 }
45
46 func (TestDeps) StopCPUProfile() {
47         pprof.StopCPUProfile()
48 }
49
50 func (TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
51         return pprof.Lookup(name).WriteTo(w, debug)
52 }
53
54 // ImportPath is the import path of the testing binary, set by the generated main function.
55 var ImportPath string
56
57 func (TestDeps) ImportPath() string {
58         return ImportPath
59 }
60
61 // testLog implements testlog.Interface, logging actions by package os.
62 type testLog struct {
63         mu  sync.Mutex
64         w   *bufio.Writer
65         set bool
66 }
67
68 func (l *testLog) Getenv(key string) {
69         l.add("getenv", key)
70 }
71
72 func (l *testLog) Open(name string) {
73         l.add("open", name)
74 }
75
76 func (l *testLog) Stat(name string) {
77         l.add("stat", name)
78 }
79
80 func (l *testLog) Chdir(name string) {
81         l.add("chdir", name)
82 }
83
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 == "" {
87                 return
88         }
89
90         l.mu.Lock()
91         defer l.mu.Unlock()
92         if l.w == nil {
93                 return
94         }
95         l.w.WriteString(op)
96         l.w.WriteByte(' ')
97         l.w.WriteString(name)
98         l.w.WriteByte('\n')
99 }
100
101 var log testLog
102
103 func (TestDeps) StartTestLog(w io.Writer) {
104         log.mu.Lock()
105         log.w = bufio.NewWriter(w)
106         if !log.set {
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.
111                 log.set = true
112                 testlog.SetLogger(&log)
113                 log.w.WriteString("# test log\n") // known to cmd/go/internal/test/test.go
114         }
115         log.mu.Unlock()
116 }
117
118 func (TestDeps) StopTestLog() error {
119         log.mu.Lock()
120         defer log.mu.Unlock()
121         err := log.w.Flush()
122         log.w = nil
123         return err
124 }
125
126 // SetPanicOnExit0 tells the os package whether to panic on os.Exit(0).
127 func (TestDeps) SetPanicOnExit0(v bool) {
128         testlog.SetPanicOnExit0(v)
129 }
130
131 func (TestDeps) CoordinateFuzzing(parallel int, seed [][]byte, crashDir string) error {
132         return fuzz.CoordinateFuzzing(parallel, seed, crashDir)
133 }
134
135 func (TestDeps) RunFuzzWorker(fn func([]byte) error) error {
136         return fuzz.RunFuzzWorker(fn)
137 }
138
139 func (TestDeps) ReadCorpus(dir string) ([][]byte, error) {
140         return fuzz.ReadCorpus(dir)
141 }