From 10e4357dbb7b0b4e50fe595771a8cb8175d23150 Mon Sep 17 00:00:00 2001 From: Dan Willemsen Date: Wed, 18 Jan 2017 14:37:13 -0800 Subject: [PATCH] Fix -test.run test selection with gotestmain matchString was implemented separately on older go versions since they didn't want a build dependency on the regex package from the testing package. Starting in Go 1.8, this is implemented using a internal package to break the dependency, but until we require that everywhere, just copy the regex implementation like the old `go test` did. I'm not sure we'll be able to use the internal package in the future anyways. Change-Id: Ief37542640026f9d73a75fd802f2691af50e2511 --- gotestmain/gotestmain.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/gotestmain/gotestmain.go b/gotestmain/gotestmain.go index 20af754..ff4a5ba 100644 --- a/gotestmain/gotestmain.go +++ b/gotestmain/gotestmain.go @@ -85,6 +85,7 @@ var testMainTmpl = template.Must(template.New("testMain").Parse(` package main import ( + "regexp" "testing" pkg "{{.Package}}" @@ -96,8 +97,18 @@ var t = []testing.InternalTest{ {{end}} } -func matchString(pat, str string) (bool, error) { - return true, nil +var matchPat string +var matchRe *regexp.Regexp + +func matchString(pat, str string) (result bool, err error) { + if matchRe == nil || matchPat != pat { + matchPat = pat + matchRe, err = regexp.Compile(matchPat) + if err != nil { + return + } + } + return matchRe.MatchString(str), nil } func main() {