From b9545da71c2f5e93355d82a1f9b5ead02f2bc617 Mon Sep 17 00:00:00 2001 From: Ethan Reesor Date: Thu, 5 Mar 2026 14:20:45 -0600 Subject: testing: escapes framing markers Uses `^[` to escape the framing marker `^V` used to delimit test output. A test that itself executes a go test binary, or otherwise emits that control character, previously would corrupt the test2json parse of the enclosing run. Updates #62728. Change-Id: I0e8790a05fd7af469cd7ee2e8ccc13786cc372dc Reviewed-on: https://go-review.googlesource.com/c/go/+/751940 LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Matloob Reviewed-by: Damien Neil --- src/testing/sub_test.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/testing/sub_test.go') diff --git a/src/testing/sub_test.go b/src/testing/sub_test.go index 5d5573ccec..fedb0a052d 100644 --- a/src/testing/sub_test.go +++ b/src/testing/sub_test.go @@ -222,6 +222,10 @@ func TestTRun(t *T) { sub_test.go:NNN: fail ^V--- FAIL: chatty with recursion and json/#00/#02 (N.NNs) ^V=== NAME chatty with recursion and json/#00 +^V=== RUN chatty with recursion and json/#00/#03 + sub_test.go:NNN: ^[^V^[^[ +^V--- PASS: chatty with recursion and json/#00/#03 (N.NNs) +^V=== NAME chatty with recursion and json/#00 ^V--- FAIL: chatty with recursion and json/#00 (N.NNs) ^V=== NAME chatty with recursion and json ^V--- FAIL: chatty with recursion and json (N.NNs) @@ -231,6 +235,7 @@ func TestTRun(t *T) { t.Run("", func(t *T) {}) t.Run("", func(t *T) { t.Skip("skip") }) t.Run("", func(t *T) { t.Fatal("fail") }) + t.Run("", func(t *T) { t.Log(string(markFraming) + string(markEscape)) }) }) }, }, { @@ -629,7 +634,7 @@ func TestTRun(t *T) { want := strings.TrimSpace(tc.output) re := makeRegexp(want) if ok, err := regexp.MatchString(re, got); !ok || err != nil { - t.Errorf("%s:output:\ngot:\n%s\nwant:\n%s", tc.desc, got, want) + t.Errorf("%s:output:\ngot:\n%s\nwant:\n%s", tc.desc, notateOutput(got), want) } }) } @@ -823,14 +828,24 @@ func TestBRun(t *T) { } } +// makeRegexp transforms a line in the text notation to a pattern. func makeRegexp(s string) string { s = regexp.QuoteMeta(s) - s = strings.ReplaceAll(s, "^V", "\x16") + s = strings.ReplaceAll(s, "^V", string(markFraming)) + s = strings.ReplaceAll(s, "^\\[", string(markEscape)) s = strings.ReplaceAll(s, ":NNN:", `:\d\d\d\d?:`) s = strings.ReplaceAll(s, "N\\.NNs", `\d*\.\d*s`) return s } +// notateOutput transforms an output line into something more easily comparable +// to text notation. +func notateOutput(s string) string { + s = strings.ReplaceAll(s, string(markFraming), "^V") + s = strings.ReplaceAll(s, string(markEscape), "^[") + return s +} + func TestBenchmarkOutput(t *T) { // Ensure Benchmark initialized common.w by invoking it with an error and // normal case. -- cgit v1.3-6-g1900