This is new in Go 1.21.
Этот коммит содержится в:
Ayke van Laethem 2023-07-26 13:23:44 +02:00 коммит произвёл Ron Evans
родитель 215dd3f0be
коммит c25dd0a972
4 изменённых файлов: 29 добавлений и 3 удалений

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

@ -217,19 +217,27 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
var packageJobs []*compileJob var packageJobs []*compileJob
packageActionIDJobs := make(map[string]*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"] == "" { if config.Options.GlobalValues["runtime"]["buildVersion"] == "" {
version := goenv.Version version := goenv.Version
if strings.HasSuffix(goenv.Version, "-dev") && goenv.GitSha1 != "" { if strings.HasSuffix(goenv.Version, "-dev") && goenv.GitSha1 != "" {
version += "-" + 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 { if config.Options.GlobalValues["runtime"] == nil {
config.Options.GlobalValues["runtime"] = make(map[string]string) config.Options.GlobalValues["runtime"] = make(map[string]string)
} }
config.Options.GlobalValues["runtime"]["buildVersion"] = version 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 var embedFileObjects []*compileJob
for _, pkg := range lprogram.Sorted() { for _, pkg := range lprogram.Sorted() {

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

@ -120,6 +120,15 @@ func Verbose() bool {
return flagVerbose 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 // flushToParent writes c.output to the parent after first writing the header
// with the given format and arguments. // with the given format and arguments.
func (c *common) flushToParent(testName, format string, args ...interface{}) { func (c *common) flushToParent(testName, format string, args ...interface{}) {

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

@ -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")
}
}

3
testdata/testing.go предоставленный
Просмотреть файл

@ -73,6 +73,9 @@ func fakeMatchString(pat, str string) (bool, error) {
} }
func main() { func main() {
if testing.Testing() {
println("not running a test at the moment, testing.Testing() should return false")
}
testing.Init() testing.Init()
flag.Set("test.run", ".*/B") flag.Set("test.run", ".*/B")
m := testing.MainStart(matchStringOnly(fakeMatchString /*regexp.MatchString*/), tests, benchmarks, fuzzes, examples) m := testing.MainStart(matchStringOnly(fakeMatchString /*regexp.MatchString*/), tests, benchmarks, fuzzes, examples)