aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2023-07-26 13:23:44 +0200
committerRon Evans <[email protected]>2023-08-04 11:59:11 +0200
commitc25dd0a972a1d68b4d154831a6d196e28f17ddad (patch)
tree36bc4887c25c8a84cb525c64f3d4315b742ebd77
parent215dd3f0be095b05dcff7b718ff1ff42e8c1e026 (diff)
downloadtinygo-c25dd0a972a1d68b4d154831a6d196e28f17ddad.tar.gz
tinygo-c25dd0a972a1d68b4d154831a6d196e28f17ddad.zip
testing: add Testing function
This is new in Go 1.21.
-rw-r--r--builder/build.go14
-rw-r--r--src/testing/testing.go9
-rw-r--r--src/testing/testing_test.go6
-rw-r--r--testdata/testing.go3
4 files changed, 29 insertions, 3 deletions
diff --git a/builder/build.go b/builder/build.go
index 2e8577e37..85114d9d7 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 fee9d40b1..8429e9221 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 8a2586535..631a31341 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 4c1cf44ef..ff378fea9 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)