Home | History | Annotate | Download | only in testdeps
      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 	"io"
     15 	"regexp"
     16 	"runtime/pprof"
     17 )
     18 
     19 // TestDeps is an implementation of the testing.testDeps interface,
     20 // suitable for passing to testing.MainStart.
     21 type TestDeps struct{}
     22 
     23 var matchPat string
     24 var matchRe *regexp.Regexp
     25 
     26 func (TestDeps) MatchString(pat, str string) (result bool, err error) {
     27 	if matchRe == nil || matchPat != pat {
     28 		matchPat = pat
     29 		matchRe, err = regexp.Compile(matchPat)
     30 		if err != nil {
     31 			return
     32 		}
     33 	}
     34 	return matchRe.MatchString(str), nil
     35 }
     36 
     37 func (TestDeps) StartCPUProfile(w io.Writer) error {
     38 	return pprof.StartCPUProfile(w)
     39 }
     40 
     41 func (TestDeps) StopCPUProfile() {
     42 	pprof.StopCPUProfile()
     43 }
     44 
     45 func (TestDeps) WriteHeapProfile(w io.Writer) error {
     46 	return pprof.WriteHeapProfile(w)
     47 }
     48 
     49 func (TestDeps) WriteProfileTo(name string, w io.Writer, debug int) error {
     50 	return pprof.Lookup(name).WriteTo(w, debug)
     51 }
     52