Skip to content

Commit b19a3f1

Browse files
committed
Detect and execute Fuzz targets with seed inputs.
1 parent 4845bda commit b19a3f1

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

internal/testmain/testmain.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type TestMain struct {
6969
Package *build.PackageData
7070
Tests []TestFunc
7171
Benchmarks []TestFunc
72+
Fuzz []TestFunc
7273
Examples []ExampleFunc
7374
TestMain *TestFunc
7475
}
@@ -133,6 +134,11 @@ func (tm *TestMain) scanFile(f *ast.File, loc FuncLocation) error {
133134
Location: loc,
134135
Name: name,
135136
})
137+
case isTest(name, "Fuzz"):
138+
tm.Fuzz = append(tm.Fuzz, TestFunc{
139+
Location: loc,
140+
Name: name,
141+
})
136142
}
137143
}
138144

@@ -275,8 +281,11 @@ var benchmarks = []testing.InternalBenchmark{
275281
{{- end}}
276282
}
277283
278-
// TODO(nevkontakte): Extract fuzz targets from the source.
279-
var fuzzTargets = []testing.InternalFuzzTarget{}
284+
var fuzzTargets = []testing.InternalFuzzTarget{
285+
{{- range .Fuzz}}
286+
{"{{.Name}}", {{.Location}}.{{.Name}}},
287+
{{- end}}
288+
}
280289
281290
var examples = []testing.InternalExample{
282291
{{- range .Examples }}

internal/testmain/testmain_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ func TestScan(t *testing.T) {
3636
{Location: LocInPackage, Name: "BenchmarkXxx"},
3737
{Location: LocExternal, Name: "BenchmarkYyy"},
3838
},
39+
Fuzz: []TestFunc{
40+
{Location: LocInPackage, Name: "FuzzXxx"},
41+
{Location: LocExternal, Name: "FuzzYyy"},
42+
},
3943
Examples: []ExampleFunc{
4044
{Location: LocInPackage, Name: "ExampleXxx"},
4145
{Location: LocExternal, Name: "ExampleYyy", Output: "hello\n"},
@@ -71,6 +75,10 @@ func TestSynthesize(t *testing.T) {
7175
{Location: LocInPackage, Name: "BenchmarkXxx"},
7276
{Location: LocExternal, Name: "BenchmarkYyy"},
7377
},
78+
Fuzz: []TestFunc{
79+
{Location: LocInPackage, Name: "FuzzXxx"},
80+
{Location: LocExternal, Name: "FuzzYyy"},
81+
},
7482
Examples: []ExampleFunc{
7583
{Location: LocInPackage, Name: "ExampleXxx", EmptyOutput: true},
7684
{Location: LocExternal, Name: "ExampleYyy", EmptyOutput: true},
@@ -134,7 +142,10 @@ var benchmarks = []testing.InternalBenchmark{
134142
{"BenchmarkYyy", _xtest.BenchmarkYyy},
135143
}
136144
137-
var fuzzTargets = []testing.InternalFuzzTarget{}
145+
var fuzzTargets = []testing.InternalFuzzTarget{
146+
{"FuzzXxx", _test.FuzzXxx},
147+
{"FuzzYyy", _xtest.FuzzYyy},
148+
}
138149
139150
var examples = []testing.InternalExample{
140151
{"ExampleXxx", _test.ExampleXxx, "", false},

0 commit comments

Comments
 (0)