Skip to content

Commit

Permalink
Hook into Go's testing library from regular code
Browse files Browse the repository at this point in the history
  • Loading branch information
Ted Behling committed Jun 27, 2020
1 parent f0ca41a commit 8aa5077
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test_boilerplate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//+build mage

package main

import (
"io"
"testing"
"regexp"
"errors"
)

func RunGoTests(tests []testing.InternalTest) error {
m := testing.MainStart(&nullTestDeps{}, tests, nil, nil)
if x := m.Run(); x > 0 {
return errors.New("testing.MainStart returned non-zero")
}
return nil
}

// nullTestDeps implements testing.testDeps to stub dependencies for testing.MainStart()
//
// See https://stackoverflow.com/questions/59064256/is-it-possible-to-call-a-test-func-from-another-file-to-start-the-testing
// https://golang.org/src/testing/testing.go?s=38015:38129#L1146
// https://golang.org/src/testing/internal/testdeps/deps.go
type nullTestDeps struct{}

func (d *nullTestDeps) MatchString(pat, str string) (bool, error) { return regexp.MatchString(pat, str) }
func (d *nullTestDeps) StartCPUProfile(_ io.Writer) error { return nil }
func (d *nullTestDeps) StopCPUProfile() {}
func (d *nullTestDeps) WriteHeapProfile(_ io.Writer) error { return nil }
func (d *nullTestDeps) WriteProfileTo(_ string, _ io.Writer, _ int) error { return nil }
func (d *nullTestDeps) ImportPath() string { return "" }
func (d *nullTestDeps) StartTestLog(io.Writer) { return }
func (d *nullTestDeps) StopTestLog() error { return nil}

0 comments on commit 8aa5077

Please sign in to comment.