Skip to content

Commit daeca9a

Browse files
committed
fix(go-runner): implement MatchString testdep
1 parent 4f8413e commit daeca9a

2 files changed

Lines changed: 108 additions & 100 deletions

File tree

example-codspeed/cli/runner.go

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,47 @@
44
package main
55

66
import (
7-
"fmt"
87
"io"
98
"reflect"
9+
"regexp"
1010
"time"
1111

12-
example "example"
13-
1412
codspeed_testing "github.com/CodSpeedHQ/codspeed-go/testing/testing"
13+
14+
// Import parent package containing the benchmarks
15+
example "example"
1516
)
1617

18+
// TestDeps is an implementation of the testing.testDeps interface,
19+
// suitable for passing to [testing.MainStart].
20+
//
21+
// It is partially copied from `testing/internal/testdeps/deps.go`, and
22+
// only implements the minimum required functionality.
23+
type TestDeps struct{}
24+
25+
func (d TestDeps) ImportPath() string { return "" }
26+
27+
var matchPat string
28+
var matchRe *regexp.Regexp
29+
30+
func (TestDeps) MatchString(pat, str string) (result bool, err error) {
31+
if matchRe == nil || matchPat != pat {
32+
matchPat = pat
33+
matchRe, err = regexp.Compile(matchPat)
34+
if err != nil {
35+
return
36+
}
37+
}
38+
return matchRe.MatchString(str), nil
39+
}
40+
41+
func (d TestDeps) SetPanicOnExit0(bool) {}
42+
func (d TestDeps) StartCPUProfile(io.Writer) error { return nil }
43+
func (d TestDeps) StopCPUProfile() {}
44+
func (d TestDeps) StartTestLog(io.Writer) {}
45+
func (d TestDeps) StopTestLog() error { return nil }
46+
func (d TestDeps) WriteProfileTo(string, io.Writer, int) error { return nil }
47+
1748
type corpusEntry = struct {
1849
Parent string
1950
Path string
@@ -23,42 +54,19 @@ type corpusEntry = struct {
2354
IsSeed bool
2455
}
2556

26-
type simpleDeps struct{}
27-
28-
func (d simpleDeps) ImportPath() string { return "" }
29-
func (d simpleDeps) MatchString(pat, str string) (bool, error) { return true, nil }
30-
func (d simpleDeps) SetPanicOnExit0(bool) {}
31-
func (d simpleDeps) StartCPUProfile(io.Writer) error { return nil }
32-
func (d simpleDeps) StopCPUProfile() {}
33-
func (d simpleDeps) StartTestLog(io.Writer) {}
34-
func (d simpleDeps) StopTestLog() error { return nil }
35-
func (d simpleDeps) WriteProfileTo(string, io.Writer, int) error { return nil }
36-
37-
func (d simpleDeps) CoordinateFuzzing(
38-
fuzzTime time.Duration,
39-
fuzzN int64,
40-
minimizeTime time.Duration,
41-
minimizeN int64,
42-
parallel int,
43-
corpus []corpusEntry,
44-
types []reflect.Type,
45-
corpusDir,
46-
cacheDir string,
47-
) error {
57+
func (d TestDeps) CoordinateFuzzing(fuzzTime time.Duration, fuzzN int64, minimizeTime time.Duration, minimizeN int64, parallel int, corpus []corpusEntry, types []reflect.Type, corpusDir, cacheDir string) error {
4858
return nil
4959
}
50-
func (d simpleDeps) RunFuzzWorker(fn func(corpusEntry) error) error {
51-
return nil
52-
}
53-
func (d simpleDeps) ReadCorpus(dir string, types []reflect.Type) ([]corpusEntry, error) {
60+
func (d TestDeps) RunFuzzWorker(fn func(corpusEntry) error) error { return nil }
61+
func (d TestDeps) ReadCorpus(dir string, types []reflect.Type) ([]corpusEntry, error) {
5462
return nil, nil
5563
}
56-
func (d simpleDeps) CheckCorpus(vals []any, types []reflect.Type) error {
64+
func (d TestDeps) CheckCorpus(vals []any, types []reflect.Type) error {
5765
return nil
5866
}
59-
func (d simpleDeps) ResetCoverage() {}
60-
func (d simpleDeps) SnapshotCoverage() {}
61-
func (d simpleDeps) InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) {
67+
func (d TestDeps) ResetCoverage() {}
68+
func (d TestDeps) SnapshotCoverage() {}
69+
func (d TestDeps) InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) {
6270
return "", nil, nil
6371
}
6472

@@ -77,10 +85,6 @@ func main() {
7785
},
7886
}
7987

80-
for i := 0; i < len(benchmarks); i++ {
81-
fmt.Printf("Benchmark %d: %s\n", i, benchmarks[i].Name)
82-
}
83-
84-
m := codspeed_testing.MainStart(simpleDeps{}, tests, benchmarks, fuzzTargets, examples)
88+
m := codspeed_testing.MainStart(TestDeps{}, tests, benchmarks, fuzzTargets, examples)
8589
m.Run()
8690
}

go-runner/src/builder/template.go

Lines changed: 66 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,85 @@
11
package main
22

33
import (
4-
"io"
5-
"time"
6-
"reflect"
7-
codspeed "github.com/CodSpeedHQ/codspeed-go/compat/testing"
8-
codspeed_testing "github.com/CodSpeedHQ/codspeed-go/testing/testing"
4+
"fmt"
5+
"io"
6+
"reflect"
7+
"regexp"
8+
"time"
99

10-
// Import parent package containing the benchmarks
11-
{{#each benchmarks}}
12-
{{import_alias}} "{{module_path}}"
13-
{{/each}}
10+
codspeed_testing "github.com/CodSpeedHQ/codspeed-go/testing/testing"
11+
12+
// Import parent package containing the benchmarks
13+
{{#each benchmarks}}
14+
{{import_alias}} "{{module_path}}"
15+
{{/each}}
1416
)
1517

16-
type BenchmarkEntry struct {
17-
Name string
18-
Func func(b *codspeed.B)
19-
}
18+
// TestDeps is an implementation of the testing.testDeps interface,
19+
// suitable for passing to [testing.MainStart].
20+
//
21+
// It is partially copied from `testing/internal/testdeps/deps.go`, and
22+
// only implements the minimum required functionality.
23+
type TestDeps struct {}
2024

21-
type corpusEntry = struct {
22-
Parent string
23-
Path string
24-
Data []byte
25-
Values []any
26-
Generation int
27-
IsSeed bool
28-
}
25+
func (d TestDeps) ImportPath() string { return "" }
2926

30-
type simpleDeps struct{}
27+
var matchPat string
28+
var matchRe *regexp.Regexp
3129

32-
func (d simpleDeps) ImportPath() string { return "" }
33-
func (d simpleDeps) MatchString(pat, str string) (bool, error) { return true, nil }
34-
func (d simpleDeps) SetPanicOnExit0(bool) {}
35-
func (d simpleDeps) StartCPUProfile(io.Writer) error { return nil }
36-
func (d simpleDeps) StopCPUProfile() {}
37-
func (d simpleDeps) StartTestLog(io.Writer) {}
38-
func (d simpleDeps) StopTestLog() error { return nil }
39-
func (d simpleDeps) WriteProfileTo(string, io.Writer, int) error { return nil }
30+
func (TestDeps) MatchString(pat, str string) (result bool, err error) {
31+
if matchRe == nil || matchPat != pat {
32+
matchPat = pat
33+
matchRe, err = regexp.Compile(matchPat)
34+
if err != nil {
35+
return
36+
}
37+
}
38+
return matchRe.MatchString(str), nil
39+
}
40+
41+
func (d TestDeps) SetPanicOnExit0(bool) {}
42+
func (d TestDeps) StartCPUProfile(io.Writer) error { return nil }
43+
func (d TestDeps) StopCPUProfile() {}
44+
func (d TestDeps) StartTestLog(io.Writer) {}
45+
func (d TestDeps) StopTestLog() error { return nil }
46+
func (d TestDeps) WriteProfileTo(string, io.Writer, int) error { return nil }
4047

41-
func (d simpleDeps) CoordinateFuzzing(
42-
fuzzTime time.Duration,
43-
fuzzN int64,
44-
minimizeTime time.Duration,
45-
minimizeN int64,
46-
parallel int,
47-
corpus []corpusEntry,
48-
types []reflect.Type,
49-
corpusDir,
50-
cacheDir string,
51-
) error {
52-
return nil
48+
type corpusEntry = struct {
49+
Parent string
50+
Path string
51+
Data []byte
52+
Values []any
53+
Generation int
54+
IsSeed bool
5355
}
54-
func (d simpleDeps) RunFuzzWorker(fn func(corpusEntry) error) error {
55-
return nil
56+
57+
func (d TestDeps) CoordinateFuzzing(fuzzTime time.Duration, fuzzN int64, minimizeTime time.Duration, minimizeN int64, parallel int, corpus []corpusEntry, types []reflect.Type, corpusDir, cacheDir string) error {
58+
return nil
5659
}
57-
func (d simpleDeps) ReadCorpus(dir string, types []reflect.Type) ([]corpusEntry, error) {
58-
return nil, nil
60+
func (d TestDeps) RunFuzzWorker(fn func(corpusEntry) error) error { return nil }
61+
func (d TestDeps) ReadCorpus(dir string, types []reflect.Type) ([]corpusEntry, error) {
62+
return nil, nil
5963
}
60-
func (d simpleDeps) CheckCorpus(vals []any, types []reflect.Type) error {
61-
return nil
64+
func (d TestDeps) CheckCorpus(vals []any, types []reflect.Type) error {
65+
return nil
6266
}
63-
func (d simpleDeps) ResetCoverage() {}
64-
func (d simpleDeps) SnapshotCoverage() {}
65-
func (d simpleDeps) InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) {
66-
return "", nil, nil
67+
func (d TestDeps) ResetCoverage() {}
68+
func (d TestDeps) SnapshotCoverage() {}
69+
func (d TestDeps) InitRuntimeCoverage() (mode string, tearDown func(coverprofile string, gocoverdir string) (string, error), snapcov func() float64) {
70+
return "", nil, nil
6771
}
6872

6973
func main() {
70-
var tests = []codspeed_testing.InternalTest{}
71-
var fuzzTargets = []codspeed_testing.InternalFuzzTarget{}
72-
var examples = []codspeed_testing.InternalExample{}
73-
var benchmarks = []codspeed_testing.InternalBenchmark{
74-
{{#each benchmarks}}
75-
{"{{name}}", {{qualified_name}}},
76-
{{/each}}
77-
}
74+
var tests = []codspeed_testing.InternalTest{}
75+
var fuzzTargets = []codspeed_testing.InternalFuzzTarget{}
76+
var examples = []codspeed_testing.InternalExample{}
77+
var benchmarks = []codspeed_testing.InternalBenchmark{
78+
{{#each benchmarks}}
79+
{"{{name}}", {{qualified_name}}},
80+
{{/each}}
81+
}
7882

79-
m := codspeed_testing.MainStart(simpleDeps{}, tests, benchmarks, fuzzTargets, examples)
80-
m.Run()
83+
m := codspeed_testing.MainStart(TestDeps{}, tests, benchmarks, fuzzTargets, examples)
84+
m.Run()
8185
}

0 commit comments

Comments
 (0)