main: stuff test runner options into their own struct

Fixes #2406
Этот коммит содержится в:
Damian Gryski 2023-03-30 18:24:18 -07:00 коммит произвёл Ron Evans
родитель 698b1f19c6
коммит a2f95d6b87
4 изменённых файлов: 43 добавлений и 36 удалений

Просмотреть файл

@ -541,7 +541,14 @@ func (c *Config) Emulator(format, binary string) ([]string, error) {
type TestConfig struct { type TestConfig struct {
CompileTestBinary bool CompileTestBinary bool
// TODO: Filter the test functions to run, include verbose flag, etc CompileOnly bool
Verbose bool
Short bool
RunRegexp string
Count int
BenchRegexp string
BenchTime string
BenchMem bool
} }
// filterTags removes predefined build tags for a target if a conflicting option // filterTags removes predefined build tags for a target if a conflicting option

Просмотреть файл

@ -115,8 +115,9 @@ func TestCorpus(t *testing.T) {
var tags buildutil.TagsFlag var tags buildutil.TagsFlag
tags.Set(repo.Tags) tags.Set(repo.Tags)
opts.Tags = []string(tags) opts.Tags = []string(tags)
opts.TestConfig.Verbose = testing.Verbose()
passed, err := Test(path, out, out, &opts, false, testing.Verbose(), false, "", "", "", false, "") passed, err := Test(path, out, out, &opts, "")
if err != nil { if err != nil {
t.Errorf("test error: %v", err) t.Errorf("test error: %v", err)
} }

59
main.go
Просмотреть файл

@ -216,42 +216,44 @@ func Build(pkgName, outpath string, options *compileopts.Options) error {
// Test runs the tests in the given package. Returns whether the test passed and // Test runs the tests in the given package. Returns whether the test passed and
// possibly an error if the test failed to run. // possibly an error if the test failed to run.
func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options, testCompileOnly, testVerbose, testShort bool, testRunRegexp string, testCount int, testBenchRegexp string, testBenchTime string, testBenchMem bool, outpath string) (bool, error) { func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options, outpath string) (bool, error) {
options.TestConfig.CompileTestBinary = true options.TestConfig.CompileTestBinary = true
config, err := builder.NewConfig(options) config, err := builder.NewConfig(options)
if err != nil { if err != nil {
return false, err return false, err
} }
testConfig := &options.TestConfig
// Pass test flags to the test binary. // Pass test flags to the test binary.
var flags []string var flags []string
if testVerbose { if testConfig.Verbose {
flags = append(flags, "-test.v") flags = append(flags, "-test.v")
} }
if testShort { if testConfig.Short {
flags = append(flags, "-test.short") flags = append(flags, "-test.short")
} }
if testRunRegexp != "" { if testConfig.RunRegexp != "" {
flags = append(flags, "-test.run="+testRunRegexp) flags = append(flags, "-test.run="+testConfig.RunRegexp)
} }
if testBenchRegexp != "" { if testConfig.BenchRegexp != "" {
flags = append(flags, "-test.bench="+testBenchRegexp) flags = append(flags, "-test.bench="+testConfig.BenchRegexp)
} }
if testBenchTime != "" { if testConfig.BenchTime != "" {
flags = append(flags, "-test.benchtime="+testBenchTime) flags = append(flags, "-test.benchtime="+testConfig.BenchTime)
} }
if testBenchMem { if testConfig.BenchMem {
flags = append(flags, "-test.benchmem") flags = append(flags, "-test.benchmem")
} }
if testCount != 1 { if testConfig.Count != 1 {
flags = append(flags, "-test.count="+strconv.Itoa(testCount)) flags = append(flags, "-test.count="+strconv.Itoa(testConfig.Count))
} }
buf := bytes.Buffer{} buf := bytes.Buffer{}
passed := false passed := false
var duration time.Duration var duration time.Duration
result, err := buildAndRun(pkgName, config, &buf, flags, nil, 0, func(cmd *exec.Cmd, result builder.BuildResult) error { result, err := buildAndRun(pkgName, config, &buf, flags, nil, 0, func(cmd *exec.Cmd, result builder.BuildResult) error {
if testCompileOnly || outpath != "" { if testConfig.CompileOnly || outpath != "" {
// Write test binary to the specified file name. // Write test binary to the specified file name.
if outpath == "" { if outpath == "" {
// No -o path was given, so create one now. // No -o path was given, so create one now.
@ -260,7 +262,7 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
} }
copyFile(result.Binary, outpath) copyFile(result.Binary, outpath)
} }
if testCompileOnly { if testConfig.CompileOnly {
// Do not run the test. // Do not run the test.
passed = true passed = true
return nil return nil
@ -312,7 +314,7 @@ func Test(pkgName string, stdout, stderr io.Writer, options *compileopts.Options
// 1) the tests passed and in verbose mode // 1) the tests passed and in verbose mode
// 2) the tests failed // 2) the tests failed
// 3) running benchmarks // 3) running benchmarks
if (passed && testVerbose) || (!passed) || (testBenchRegexp != "") { if (passed && testConfig.Verbose) || (!passed) || (testConfig.BenchRegexp != "") {
buf.WriteTo(stdout) buf.WriteTo(stdout)
} }
@ -1407,21 +1409,17 @@ func main() {
if command == "help" || command == "build" || command == "build-library" || command == "test" { if command == "help" || command == "build" || command == "build-library" || command == "test" {
flag.StringVar(&outpath, "o", "", "output filename") flag.StringVar(&outpath, "o", "", "output filename")
} }
var testCompileOnlyFlag, testVerboseFlag, testShortFlag *bool
var testCount *int var testConfig compileopts.TestConfig
var testBenchRegexp *string
var testBenchTime *string
var testRunRegexp *string
var testBenchMem *bool
if command == "help" || command == "test" { if command == "help" || command == "test" {
testCompileOnlyFlag = flag.Bool("c", false, "compile the test binary but do not run it") flag.BoolVar(&testConfig.CompileOnly, "c", false, "compile the test binary but do not run it")
testVerboseFlag = flag.Bool("v", false, "verbose: print additional output") flag.BoolVar(&testConfig.Verbose, "v", false, "verbose: print additional output")
testShortFlag = flag.Bool("short", false, "short: run smaller test suite to save time") flag.BoolVar(&testConfig.Short, "short", false, "short: run smaller test suite to save time")
testRunRegexp = flag.String("run", "", "run: regexp of tests to run") flag.StringVar(&testConfig.RunRegexp, "run", "", "run: regexp of tests to run")
testCount = flag.Int("count", 1, "count: number of times to run tests/benchmarks `count` times") flag.IntVar(&testConfig.Count, "count", 1, "count: number of times to run tests/benchmarks `count` times")
testBenchRegexp = flag.String("bench", "", "run: regexp of benchmarks to run") flag.StringVar(&testConfig.BenchRegexp, "bench", "", "run: regexp of benchmarks to run")
testBenchTime = flag.String("benchtime", "", "run each benchmark for duration `d`") flag.StringVar(&testConfig.BenchTime, "benchtime", "", "run each benchmark for duration `d`")
testBenchMem = flag.Bool("benchmem", false, "show memory stats for benchmarks") flag.BoolVar(&testConfig.BenchMem, "benchmem", false, "show memory stats for benchmarks")
} }
// Early command processing, before commands are interpreted by the Go flag // Early command processing, before commands are interpreted by the Go flag
@ -1479,6 +1477,7 @@ func main() {
PrintStacks: *printStacks, PrintStacks: *printStacks,
PrintAllocs: printAllocs, PrintAllocs: printAllocs,
Tags: []string(tags), Tags: []string(tags),
TestConfig: testConfig,
GlobalValues: globalVarValues, GlobalValues: globalVarValues,
Programmer: *programmer, Programmer: *programmer,
OpenOCDCommands: ocdCommands, OpenOCDCommands: ocdCommands,
@ -1664,7 +1663,7 @@ func main() {
defer close(buf.done) defer close(buf.done)
stdout := (*testStdout)(buf) stdout := (*testStdout)(buf)
stderr := (*testStderr)(buf) stderr := (*testStderr)(buf)
passed, err := Test(pkgName, stdout, stderr, options, *testCompileOnlyFlag, *testVerboseFlag, *testShortFlag, *testRunRegexp, *testCount, *testBenchRegexp, *testBenchTime, *testBenchMem, outpath) passed, err := Test(pkgName, stdout, stderr, options, outpath)
if err != nil { if err != nil {
printCompilerError(func(args ...interface{}) { printCompilerError(func(args ...interface{}) {
fmt.Fprintln(stderr, args...) fmt.Fprintln(stderr, args...)

Просмотреть файл

@ -425,7 +425,7 @@ func TestTest(t *testing.T) {
defer out.Close() defer out.Close()
opts := targ.opts opts := targ.opts
passed, err := Test("github.com/tinygo-org/tinygo/tests/testing/pass", out, out, &opts, false, false, false, "", "", "", false, "") passed, err := Test("github.com/tinygo-org/tinygo/tests/testing/pass", out, out, &opts, "")
if err != nil { if err != nil {
t.Errorf("test error: %v", err) t.Errorf("test error: %v", err)
} }
@ -446,7 +446,7 @@ func TestTest(t *testing.T) {
defer out.Close() defer out.Close()
opts := targ.opts opts := targ.opts
passed, err := Test("github.com/tinygo-org/tinygo/tests/testing/fail", out, out, &opts, false, false, false, "", "", "", false, "") passed, err := Test("github.com/tinygo-org/tinygo/tests/testing/fail", out, out, &opts, "")
if err != nil { if err != nil {
t.Errorf("test error: %v", err) t.Errorf("test error: %v", err)
} }
@ -473,7 +473,7 @@ func TestTest(t *testing.T) {
var output bytes.Buffer var output bytes.Buffer
opts := targ.opts opts := targ.opts
passed, err := Test("github.com/tinygo-org/tinygo/tests/testing/nothing", io.MultiWriter(&output, out), out, &opts, false, false, false, "", "", "", false, "") passed, err := Test("github.com/tinygo-org/tinygo/tests/testing/nothing", io.MultiWriter(&output, out), out, &opts, "")
if err != nil { if err != nil {
t.Errorf("test error: %v", err) t.Errorf("test error: %v", err)
} }
@ -497,7 +497,7 @@ func TestTest(t *testing.T) {
defer out.Close() defer out.Close()
opts := targ.opts opts := targ.opts
passed, err := Test("github.com/tinygo-org/tinygo/tests/testing/builderr", out, out, &opts, false, false, false, "", "", "", false, "") passed, err := Test("github.com/tinygo-org/tinygo/tests/testing/builderr", out, out, &opts, "")
if err == nil { if err == nil {
t.Error("test did not error") t.Error("test did not error")
} }