From c25dd0a972a1d68b4d154831a6d196e28f17ddad Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Wed, 26 Jul 2023 13:23:44 +0200 Subject: [PATCH] testing: add Testing function This is new in Go 1.21. --- builder/build.go | 14 +++++++++++--- src/testing/testing.go | 9 +++++++++ src/testing/testing_test.go | 6 ++++++ testdata/testing.go | 3 +++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/builder/build.go b/builder/build.go index 2e8577e3..85114d9d 100644 --- a/builder/build.go +++ b/builder/build.go @@ -217,19 +217,27 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe var packageJobs []*compileJob packageActionIDJobs := make(map[string]*compileJob) + if config.Options.GlobalValues == nil { + config.Options.GlobalValues = make(map[string]map[string]string) + } if config.Options.GlobalValues["runtime"]["buildVersion"] == "" { version := goenv.Version if strings.HasSuffix(goenv.Version, "-dev") && goenv.GitSha1 != "" { version += "-" + goenv.GitSha1 } - if config.Options.GlobalValues == nil { - config.Options.GlobalValues = make(map[string]map[string]string) - } if config.Options.GlobalValues["runtime"] == nil { config.Options.GlobalValues["runtime"] = make(map[string]string) } config.Options.GlobalValues["runtime"]["buildVersion"] = version } + if config.TestConfig.CompileTestBinary { + // The testing.testBinary is set to "1" when in a test. + // This is needed for testing.Testing() to work correctly. + if config.Options.GlobalValues["testing"] == nil { + config.Options.GlobalValues["testing"] = make(map[string]string) + } + config.Options.GlobalValues["testing"]["testBinary"] = "1" + } var embedFileObjects []*compileJob for _, pkg := range lprogram.Sorted() { diff --git a/src/testing/testing.go b/src/testing/testing.go index fee9d40b..8429e922 100644 --- a/src/testing/testing.go +++ b/src/testing/testing.go @@ -120,6 +120,15 @@ func Verbose() bool { return flagVerbose } +// String constant that is being set when running a test. +var testBinary string + +// Testing returns whether the program was compiled as a test, using "tinygo +// test". It returns false when built using "tinygo build", "tinygo flash", etc. +func Testing() bool { + return testBinary == "1" +} + // flushToParent writes c.output to the parent after first writing the header // with the given format and arguments. func (c *common) flushToParent(testName, format string, args ...interface{}) { diff --git a/src/testing/testing_test.go b/src/testing/testing_test.go index 8a258653..631a3134 100644 --- a/src/testing/testing_test.go +++ b/src/testing/testing_test.go @@ -195,3 +195,9 @@ func TestSetenv(t *testing.T) { } } } + +func TestTesting(t *testing.T) { + if !testing.Testing() { + t.Error("Expected testing.Testing() to return true while in a test") + } +} diff --git a/testdata/testing.go b/testdata/testing.go index 4c1cf44e..ff378fea 100644 --- a/testdata/testing.go +++ b/testdata/testing.go @@ -73,6 +73,9 @@ func fakeMatchString(pat, str string) (bool, error) { } func main() { + if testing.Testing() { + println("not running a test at the moment, testing.Testing() should return false") + } testing.Init() flag.Set("test.run", ".*/B") m := testing.MainStart(matchStringOnly(fakeMatchString /*regexp.MatchString*/), tests, benchmarks, fuzzes, examples)