From f5e3332108dd73166a1cbe35bfebe8bf99386019 Mon Sep 17 00:00:00 2001 From: Ethan Reesor Date: Sat, 16 Nov 2024 08:54:45 -0700 Subject: cmd/internal/test2json: generate and validate test artifacts Adds a mechanism for generating test2json test artifacts from and validating them against a real test. If a .test file has a corresponding .src file, TestGolden will now treat the .src file as a script test, executing it and verifying that the output matches the contents of the .test file. Running TestGolden with the -update flag will also regenerate .test files if they have a corresponding .src file. Capturing the output of the script test in this way required making minor changes to cmd/internal/script/scripttest. This was motivated by CL 601535 (golang/go#62728). Specifically, testing that CL required adding src/cmd/internal/test2json/testdata/frameescape.test which has a multitude of non-printing characters and thus must be generated by executing `go test`. Using a script test to generate the test file is more reliable than doing it by hand. Change-Id: I60456700e37e21a42d0514be2ce86dc6df2bccb0 Reviewed-on: https://go-review.googlesource.com/c/go/+/628615 Reviewed-by: Michael Matloob LUCI-TryBot-Result: Go LUCI Reviewed-by: Michael Matloob Reviewed-by: Damien Neil Auto-Submit: Damien Neil --- src/cmd/internal/script/scripttest/scripttest.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/cmd/internal/script/scripttest/scripttest.go') diff --git a/src/cmd/internal/script/scripttest/scripttest.go b/src/cmd/internal/script/scripttest/scripttest.go index 349201fd18..418b063b28 100644 --- a/src/cmd/internal/script/scripttest/scripttest.go +++ b/src/cmd/internal/script/scripttest/scripttest.go @@ -89,7 +89,7 @@ func Run(t testing.TB, e *script.Engine, s *script.State, filename string, testS return e.Execute(s, filename, bufio.NewReader(testScript), log) }() - if skip, ok := errors.AsType[skipError](err); ok { + if skip, ok := errors.AsType[SkipError](err); ok { if skip.msg == "" { t.Skip("SKIP") } else { @@ -113,17 +113,18 @@ func Skip() script.Cmd { return nil, script.ErrUsage } if len(args) == 0 { - return nil, skipError{""} + return nil, SkipError{""} } - return nil, skipError{args[0]} + return nil, SkipError{args[0]} }) } -type skipError struct { +// SkipError is returned by a script test that executes the [Skip] command. +type SkipError struct { msg string } -func (s skipError) Error() string { +func (s SkipError) Error() string { if s.msg == "" { return "skip" } -- cgit v1.3