Skip to content

Commit 71a0d01

Browse files
committed
main_test.go: fork testdata/testing.go for go 1.18
1 parent 1dcdd5f commit 71a0d01

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

main_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ func TestBuild(t *testing.T) {
6666
"stdlib.go",
6767
"string.go",
6868
"structs.go",
69-
"testing.go",
7069
"zeroalloc.go",
7170
}
7271
_, minor, err := goenv.GetGorootVersion(goenv.Get("GOROOT"))
@@ -76,6 +75,11 @@ func TestBuild(t *testing.T) {
7675
if minor >= 17 {
7776
tests = append(tests, "go1.17.go")
7877
}
78+
if minor >= 18 {
79+
tests = append(tests, "testing_go118.go")
80+
} else {
81+
tests = append(tests, "testing.go")
82+
}
7983

8084
if *testTarget != "" {
8185
// This makes it possible to run one specific test (instead of all),

testdata/testing_go118.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package main
2+
3+
// TODO: also test the verbose version.
4+
5+
import (
6+
"errors"
7+
"flag"
8+
"io"
9+
"strings"
10+
"testing"
11+
)
12+
13+
func TestFoo(t *testing.T) {
14+
t.Log("log Foo.a")
15+
t.Log("log Foo.b")
16+
}
17+
18+
func TestBar(t *testing.T) {
19+
t.Log("log Bar")
20+
t.Log("log g\nh\ni\n")
21+
t.Run("Bar1", func(t *testing.T) {})
22+
t.Run("Bar2", func(t *testing.T) {
23+
t.Log("log Bar2\na\nb\nc")
24+
t.Error("failed")
25+
t.Log("after failed")
26+
})
27+
t.Run("Bar3", func(t *testing.T) {})
28+
t.Log("log Bar end")
29+
}
30+
31+
func TestAllLowercase(t *testing.T) {
32+
names := []string {
33+
"alpha",
34+
"BETA",
35+
"gamma",
36+
"BELTA",
37+
}
38+
39+
for _, name := range names {
40+
t.Run(name, func(t *testing.T) {
41+
if 'a' <= name[0] && name[0] <= 'a' {
42+
t.Logf("expected lowercase name, and got one, so I'm happy")
43+
} else {
44+
t.Errorf("expected lowercase name, got %s", name)
45+
}
46+
})
47+
}
48+
}
49+
50+
var tests = []testing.InternalTest{
51+
{"TestFoo", TestFoo},
52+
{"TestBar", TestBar},
53+
{"TestAllLowercase", TestAllLowercase},
54+
}
55+
56+
var benchmarks = []testing.InternalBenchmark{}
57+
58+
var fuzzes = []testing.InternalFuzzTarget{}
59+
60+
var examples = []testing.InternalExample{}
61+
62+
// A fake regexp matcher.
63+
// Inflexible, but saves 50KB of flash and 50KB of RAM per -size full,
64+
// and lets tests pass on cortex-m.
65+
// Must match the one in src/testing/match.go that is substituted on bare-metal platforms,
66+
// or "make test" will fail there.
67+
func fakeMatchString(pat, str string) (bool, error) {
68+
if pat == ".*" {
69+
return true, nil
70+
}
71+
matched := strings.Contains(str, pat)
72+
return matched, nil
73+
}
74+
75+
func main() {
76+
testing.Init()
77+
flag.Set("test.run", ".*/B")
78+
m := testing.MainStart(matchStringOnly(fakeMatchString /*regexp.MatchString*/), tests, benchmarks, fuzzes, examples)
79+
80+
exitcode := m.Run()
81+
if exitcode != 0 {
82+
println("exitcode:", exitcode)
83+
}
84+
}
85+
86+
var errMain = errors.New("testing: unexpected use of func Main")
87+
88+
// matchStringOnly is part of upstream, and is used below to provide a dummy deps to pass to MainStart
89+
// so it can be run with go (tested with go 1.16) to provide a baseline for the regression test.
90+
// See c56cc9b3b57276. Unfortunately, testdeps is internal, so we can't just use &testdeps.TestDeps{}.
91+
type matchStringOnly func(pat, str string) (bool, error)
92+
93+
func (f matchStringOnly) MatchString(pat, str string) (bool, error) { return f(pat, str) }
94+
func (f matchStringOnly) StartCPUProfile(w io.Writer) error { return errMain }
95+
func (f matchStringOnly) StopCPUProfile() {}
96+
func (f matchStringOnly) WriteProfileTo(string, io.Writer, int) error { return errMain }
97+
func (f matchStringOnly) ImportPath() string { return "" }
98+
func (f matchStringOnly) StartTestLog(io.Writer) {}
99+
func (f matchStringOnly) StopTestLog() error { return errMain }
100+
func (f matchStringOnly) SetPanicOnExit0(bool) {}

testdata/testing_go118.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--- FAIL: TestBar (0.00s)
2+
log Bar
3+
log g
4+
h
5+
i
6+
7+
--- FAIL: TestBar/Bar2 (0.00s)
8+
log Bar2
9+
a
10+
b
11+
c
12+
failed
13+
after failed
14+
log Bar end
15+
--- FAIL: TestAllLowercase (0.00s)
16+
--- FAIL: TestAllLowercase/BETA (0.00s)
17+
expected lowercase name, got BETA
18+
--- FAIL: TestAllLowercase/BELTA (0.00s)
19+
expected lowercase name, got BELTA
20+
FAIL
21+
exitcode: 1

0 commit comments

Comments
 (0)