aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorDamian Gryski <[email protected]>2023-03-30 18:24:18 -0700
committerRon Evans <[email protected]>2023-03-31 09:07:13 +0200
commita2f95d6b87418cc4a75892a43a6a405674aab876 (patch)
tree42cb0c8c2ee4f34a911692a897d80e312f9674e6
parent698b1f19c6167800af0683a625ce891f3037c867 (diff)
downloadtinygo-a2f95d6b87418cc4a75892a43a6a405674aab876.tar.gz
tinygo-a2f95d6b87418cc4a75892a43a6a405674aab876.zip
main: stuff test runner options into their own struct
Fixes #2406
-rw-r--r--compileopts/config.go9
-rw-r--r--corpus_test.go3
-rw-r--r--main.go59
-rw-r--r--main_test.go8
4 files changed, 43 insertions, 36 deletions
diff --git a/compileopts/config.go b/compileopts/config.go
index bfb02f1b7..86f35fa99 100644
--- a/compileopts/config.go
+++ b/compileopts/config.go
@@ -541,7 +541,14 @@ func (c *Config) Emulator(format, binary string) ([]string, error) {
type TestConfig struct {
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
diff --git a/corpus_test.go b/corpus_test.go
index ceb631693..1b27d6f6b 100644
--- a/corpus_test.go
+++ b/corpus_test.go
@@ -115,8 +115,9 @@ func TestCorpus(t *testing.T) {
var tags buildutil.TagsFlag
tags.Set(repo.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 {
t.Errorf("test error: %v", err)
}
diff --git a/main.go b/main.go
index 463c25d4d..74aa0f1fa 100644
--- a/main.go
+++ b/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
// 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
config, err := builder.NewConfig(options)
if err != nil {
return false, err
}
+ testConfig := &options.TestConfig
+
// Pass test flags to the test binary.
var flags []string
- if testVerbose {
+ if testConfig.Verbose {
flags = append(flags, "-test.v")
}
- if testShort {
+ if testConfig.Short {
flags = append(flags, "-test.short")
}
- if testRunRegexp != "" {
- flags = append(flags, "-test.run="+testRunRegexp)
+ if testConfig.RunRegexp != "" {
+ flags = append(flags, "-test.run="+testConfig.RunRegexp)
}
- if testBenchRegexp != "" {
- flags = append(flags, "-test.bench="+testBenchRegexp)
+ if testConfig.BenchRegexp != "" {
+ flags = append(flags, "-test.bench="+testConfig.BenchRegexp)
}
- if testBenchTime != "" {
- flags = append(flags, "-test.benchtime="+testBenchTime)
+ if testConfig.BenchTime != "" {
+ flags = append(flags, "-test.benchtime="+testConfig.BenchTime)
}
- if testBenchMem {
+ if testConfig.BenchMem {
flags = append(flags, "-test.benchmem")
}
- if testCount != 1 {
- flags = append(flags, "-test.count="+strconv.Itoa(testCount))
+ if testConfig.Count != 1 {
+ flags = append(flags, "-test.count="+strconv.Itoa(testConfig.Count))
}
buf := bytes.Buffer{}
passed := false
var duration time.Duration
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.
if outpath == "" {
// 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)
}
- if testCompileOnly {
+ if testConfig.CompileOnly {
// Do not run the test.
passed = true
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
// 2) the tests failed
// 3) running benchmarks
- if (passed && testVerbose) || (!passed) || (testBenchRegexp != "") {
+ if (passed && testConfig.Verbose) || (!passed) || (testConfig.BenchRegexp != "") {
buf.WriteTo(stdout)
}
@@ -1407,21 +1409,17 @@ func main() {
if command == "help" || command == "build" || command == "build-library" || command == "test" {
flag.StringVar(&outpath, "o", "", "output filename")
}
- var testCompileOnlyFlag, testVerboseFlag, testShortFlag *bool
- var testCount *int
- var testBenchRegexp *string
- var testBenchTime *string
- var testRunRegexp *string
- var testBenchMem *bool
+
+ var testConfig compileopts.TestConfig
if command == "help" || command == "test" {
- testCompileOnlyFlag = flag.Bool("c", false, "compile the test binary but do not run it")
- testVerboseFlag = flag.Bool("v", false, "verbose: print additional output")
- testShortFlag = flag.Bool("short", false, "short: run smaller test suite to save time")
- testRunRegexp = flag.String("run", "", "run: regexp of tests to run")
- testCount = flag.Int("count", 1, "count: number of times to run tests/benchmarks `count` times")
- testBenchRegexp = flag.String("bench", "", "run: regexp of benchmarks to run")
- testBenchTime = flag.String("benchtime", "", "run each benchmark for duration `d`")
- testBenchMem = flag.Bool("benchmem", false, "show memory stats for benchmarks")
+ flag.BoolVar(&testConfig.CompileOnly, "c", false, "compile the test binary but do not run it")
+ flag.BoolVar(&testConfig.Verbose, "v", false, "verbose: print additional output")
+ flag.BoolVar(&testConfig.Short, "short", false, "short: run smaller test suite to save time")
+ flag.StringVar(&testConfig.RunRegexp, "run", "", "run: regexp of tests to run")
+ flag.IntVar(&testConfig.Count, "count", 1, "count: number of times to run tests/benchmarks `count` times")
+ flag.StringVar(&testConfig.BenchRegexp, "bench", "", "run: regexp of benchmarks to run")
+ flag.StringVar(&testConfig.BenchTime, "benchtime", "", "run each benchmark for duration `d`")
+ flag.BoolVar(&testConfig.BenchMem, "benchmem", false, "show memory stats for benchmarks")
}
// Early command processing, before commands are interpreted by the Go flag
@@ -1479,6 +1477,7 @@ func main() {
PrintStacks: *printStacks,
PrintAllocs: printAllocs,
Tags: []string(tags),
+ TestConfig: testConfig,
GlobalValues: globalVarValues,
Programmer: *programmer,
OpenOCDCommands: ocdCommands,
@@ -1664,7 +1663,7 @@ func main() {
defer close(buf.done)
stdout := (*testStdout)(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 {
printCompilerError(func(args ...interface{}) {
fmt.Fprintln(stderr, args...)
diff --git a/main_test.go b/main_test.go
index 4de8fc09c..59254656e 100644
--- a/main_test.go
+++ b/main_test.go
@@ -425,7 +425,7 @@ func TestTest(t *testing.T) {
defer out.Close()
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 {
t.Errorf("test error: %v", err)
}
@@ -446,7 +446,7 @@ func TestTest(t *testing.T) {
defer out.Close()
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 {
t.Errorf("test error: %v", err)
}
@@ -473,7 +473,7 @@ func TestTest(t *testing.T) {
var output bytes.Buffer
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 {
t.Errorf("test error: %v", err)
}
@@ -497,7 +497,7 @@ func TestTest(t *testing.T) {
defer out.Close()
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 {
t.Error("test did not error")
}