From c534fa1b6f935d8176528131488853c4f67f6cc8 Mon Sep 17 00:00:00 2001 From: Dan Kegel Date: Sun, 13 Mar 2022 12:27:44 -0700 Subject: [PATCH] On baremetal platforms, use simpler test matcher. Fixes #2666. Uses same matcher in testdata (because now we can't replace it in testdata). --- src/testing/is_baremetal.go | 10 ++++++++++ src/testing/is_not_baremetal.go | 10 ++++++++++ src/testing/match.go | 16 ++++++++++++++++ testdata/testing.go | 18 +++++++++--------- testdata/testing.txt | 4 ++-- 5 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 src/testing/is_baremetal.go create mode 100644 src/testing/is_not_baremetal.go diff --git a/src/testing/is_baremetal.go b/src/testing/is_baremetal.go new file mode 100644 index 00000000..95eefe0f --- /dev/null +++ b/src/testing/is_baremetal.go @@ -0,0 +1,10 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// +//go:build baremetal +// +build baremetal + +package testing + +const isBaremetal = true diff --git a/src/testing/is_not_baremetal.go b/src/testing/is_not_baremetal.go new file mode 100644 index 00000000..4035c93b --- /dev/null +++ b/src/testing/is_not_baremetal.go @@ -0,0 +1,10 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. +// +//go:build !baremetal +// +build !baremetal + +package testing + +const isBaremetal = false diff --git a/src/testing/match.go b/src/testing/match.go index b18c6e7f..2ba8c5fe 100644 --- a/src/testing/match.go +++ b/src/testing/match.go @@ -26,6 +26,11 @@ type matcher struct { var matchMutex sync.Mutex func newMatcher(matchString func(pat, str string) (bool, error), patterns, name string) *matcher { + if isBaremetal { + // Probably not enough ram to load regexp, substitute something simpler. + matchString = fakeMatchString + } + var filter []string if patterns != "" { filter = splitRegexp(patterns) @@ -166,3 +171,14 @@ func isSpace(r rune) bool { } return false } + +// A fake regexp matcher. +// Inflexible, but saves 50KB of flash and 50KB of RAM per -size full, +// and lets tests pass on cortex-m. +func fakeMatchString(pat, str string) (bool, error) { + if pat == ".*" { + return true, nil + } + matched := strings.Contains(str, pat) + return matched, nil +} diff --git a/testdata/testing.go b/testdata/testing.go index 4a1f67c4..2c090da1 100644 --- a/testdata/testing.go +++ b/testdata/testing.go @@ -6,6 +6,7 @@ import ( "errors" "flag" "io" + "strings" "testing" ) @@ -32,7 +33,7 @@ func TestAllLowercase(t *testing.T) { "alpha", "BETA", "gamma", - "DELTA", + "BELTA", } for _, name := range names { @@ -56,23 +57,22 @@ var benchmarks = []testing.InternalBenchmark{} var examples = []testing.InternalExample{} -// A fake regexp matcher that can only handle two patterns. +// A fake regexp matcher. // Inflexible, but saves 50KB of flash and 50KB of RAM per -size full, -// and lets tests pass on cortex-m3. +// and lets tests pass on cortex-m. +// Must match the one in src/testing/match.go that is substituted on bare-metal platforms, +// or "make test" will fail there. func fakeMatchString(pat, str string) (bool, error) { if pat == ".*" { return true, nil } - if pat == "[BD]" { - return (str[0] == 'B' || str[0] == 'D'), nil - } - println("BUG: fakeMatchString does not grok", pat) - return false, nil + matched := strings.Contains(str, pat) + return matched, nil } func main() { testing.Init() - flag.Set("test.run", ".*/[BD]") + flag.Set("test.run", ".*/B") m := testing.MainStart(matchStringOnly(fakeMatchString /*regexp.MatchString*/), tests, benchmarks, examples) exitcode := m.Run() diff --git a/testdata/testing.txt b/testdata/testing.txt index ff124de1..6a8ee059 100644 --- a/testdata/testing.txt +++ b/testdata/testing.txt @@ -15,7 +15,7 @@ --- FAIL: TestAllLowercase (0.00s) --- FAIL: TestAllLowercase/BETA (0.00s) expected lowercase name, got BETA - --- FAIL: TestAllLowercase/DELTA (0.00s) - expected lowercase name, got DELTA + --- FAIL: TestAllLowercase/BELTA (0.00s) + expected lowercase name, got BELTA FAIL exitcode: 1