From 921299aa664d97c98bf8d4c39fedc9d24ce01252 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Fri, 10 May 2024 20:03:07 +0900 Subject: [PATCH 1/6] feat: add `-print-events` flag to `gno test` to print emitted events --- gnovm/cmd/gno/test.go | 30 +++++++++++++++++-- .../testdata/gno_test/flag_print-events.txtar | 26 ++++++++++++++++ tm2/pkg/colors/colors.go | 12 ++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 5884463a552..f04e294725e 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -20,7 +20,9 @@ import ( "github.com/gnolang/gno/gnovm/pkg/gnoenv" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/gnovm/pkg/gnomod" + "github.com/gnolang/gno/gnovm/stdlibs" "github.com/gnolang/gno/gnovm/tests" + "github.com/gnolang/gno/tm2/pkg/colors" "github.com/gnolang/gno/tm2/pkg/commands" "github.com/gnolang/gno/tm2/pkg/errors" "github.com/gnolang/gno/tm2/pkg/random" @@ -36,6 +38,7 @@ type testCfg struct { updateGoldenTests bool printRuntimeMetrics bool withNativeFallback bool + printEvents bool } func newTestCmd(io commands.IO) *commands.Command { @@ -149,6 +152,13 @@ func (c *testCfg) RegisterFlags(fs *flag.FlagSet) { false, "print runtime metrics (gas, memory, cpu cycles)", ) + + fs.BoolVar( + &c.printEvents, + "print-events", + false, + "print emitted events", + ) } func execTest(cfg *testCfg, args []string, io commands.IO) error { @@ -228,6 +238,7 @@ func gnoTestPkg( rootDir = cfg.rootDir runFlag = cfg.run printRuntimeMetrics = cfg.printRuntimeMetrics + printEvents = cfg.printEvents stdin = io.In() stdout = io.Out() @@ -295,7 +306,7 @@ func gnoTestPkg( m.Alloc = gno.NewAllocator(maxAllocTx) } m.RunMemPackage(memPkg, true) - err := runTestFiles(m, tfiles, memPkg.Name, verbose, printRuntimeMetrics, runFlag, io) + err := runTestFiles(m, tfiles, memPkg.Name, verbose, printRuntimeMetrics, printEvents, runFlag, io) if err != nil { errs = multierr.Append(errs, err) } @@ -329,7 +340,7 @@ func gnoTestPkg( memPkg.Path = memPkg.Path + "_test" m.RunMemPackage(memPkg, true) - err := runTestFiles(m, ifiles, testPkgName, verbose, printRuntimeMetrics, runFlag, io) + err := runTestFiles(m, ifiles, testPkgName, verbose, printRuntimeMetrics, printEvents, runFlag, io) if err != nil { errs = multierr.Append(errs, err) } @@ -419,6 +430,7 @@ func runTestFiles( pkgName string, verbose bool, printRuntimeMetrics bool, + printEvents bool, runFlag string, io commands.IO, ) (errs error) { @@ -450,7 +462,19 @@ func runTestFiles( for _, test := range testFuncs.Tests { testFuncStr := fmt.Sprintf("%q", test.Name) - eval := m.Eval(gno.Call("runtest", testFuncStr)) + res := gno.Call("runtest", testFuncStr) + + eval := m.Eval(res) // NOTE: verbose prints get here + if printEvents { + ctx := m.Context.(stdlibs.ExecContext) + + events := ctx.EventLogger.Events() + for _, ev := range events { + // XXX: print events with better formatting (e.g. JSON) + strEv := fmt.Sprint(ev) + io.ErrPrintfln("--- event: %s", colors.ColoredBytesOnlyAscii([]byte(strEv), colors.Magenta)) + } + } ret := eval[0].GetString() if ret == "" { diff --git a/gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar b/gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar new file mode 100644 index 00000000000..08fd2f44470 --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar @@ -0,0 +1,26 @@ +# Test-print-events flag + +gno test -print-events=true . + +! stdout .+ +stderr '--- event: +' + +-- valid.gno -- +package valid + +import "std" + +func Hello() { + std.Emit("emit_in_pkg", "k1", "v1") +} + +-- valid_test.gno -- +package valid + +import "testing" + +func TestHello(t *testing.T) { + Hello() +} + + diff --git a/tm2/pkg/colors/colors.go b/tm2/pkg/colors/colors.go index 84a0353d880..e6dc0042c77 100644 --- a/tm2/pkg/colors/colors.go +++ b/tm2/pkg/colors/colors.go @@ -173,3 +173,15 @@ func ColoredBytes(data []byte, textColor, bytesColor func(...interface{}) string func DefaultColoredBytes(data []byte) string { return ColoredBytes(data, Blue, Green) } + +func ColoredBytesOnlyAscii(data []byte, textColor func(...interface{}) string) string { + s := "" + for _, b := range data { + if 0x21 <= b && b < 0x7F { + s += textColor(string(b)) + } else { + s += string(b) + } + } + return s +} From 2cf4dec42196d39547f54cba28a086fadfebc668 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Tue, 14 May 2024 14:57:40 +0900 Subject: [PATCH 2/6] feat: divide and print events for every test functions --- gnovm/cmd/gno/test.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index f04e294725e..79b73bdb317 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -459,6 +459,8 @@ func runTestFiles( n := gno.MustParseFile("main_test.gno", testmain) m.RunFiles(n) + printedEvents := 0 + for _, test := range testFuncs.Tests { testFuncStr := fmt.Sprintf("%q", test.Name) @@ -469,7 +471,8 @@ func runTestFiles( ctx := m.Context.(stdlibs.ExecContext) events := ctx.EventLogger.Events() - for _, ev := range events { + for _, ev := range events[printedEvents:] { + printedEvents++ // XXX: print events with better formatting (e.g. JSON) strEv := fmt.Sprint(ev) io.ErrPrintfln("--- event: %s", colors.ColoredBytesOnlyAscii([]byte(strEv), colors.Magenta)) From 446eee7d57bd852f13faac818a7b56dd1436e1ba Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 3 Oct 2024 13:41:08 +0900 Subject: [PATCH 3/6] refactor: use `verbose` flag to print event or not --- gnovm/cmd/gno/test.go | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 79b73bdb317..dba42ccb1fc 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -152,13 +152,6 @@ func (c *testCfg) RegisterFlags(fs *flag.FlagSet) { false, "print runtime metrics (gas, memory, cpu cycles)", ) - - fs.BoolVar( - &c.printEvents, - "print-events", - false, - "print emitted events", - ) } func execTest(cfg *testCfg, args []string, io commands.IO) error { @@ -238,7 +231,6 @@ func gnoTestPkg( rootDir = cfg.rootDir runFlag = cfg.run printRuntimeMetrics = cfg.printRuntimeMetrics - printEvents = cfg.printEvents stdin = io.In() stdout = io.Out() @@ -306,7 +298,7 @@ func gnoTestPkg( m.Alloc = gno.NewAllocator(maxAllocTx) } m.RunMemPackage(memPkg, true) - err := runTestFiles(m, tfiles, memPkg.Name, verbose, printRuntimeMetrics, printEvents, runFlag, io) + err := runTestFiles(m, tfiles, memPkg.Name, verbose, printRuntimeMetrics, runFlag, io) if err != nil { errs = multierr.Append(errs, err) } @@ -340,7 +332,7 @@ func gnoTestPkg( memPkg.Path = memPkg.Path + "_test" m.RunMemPackage(memPkg, true) - err := runTestFiles(m, ifiles, testPkgName, verbose, printRuntimeMetrics, printEvents, runFlag, io) + err := runTestFiles(m, ifiles, testPkgName, verbose, printRuntimeMetrics, runFlag, io) if err != nil { errs = multierr.Append(errs, err) } @@ -430,7 +422,6 @@ func runTestFiles( pkgName string, verbose bool, printRuntimeMetrics bool, - printEvents bool, runFlag string, io commands.IO, ) (errs error) { @@ -467,7 +458,7 @@ func runTestFiles( res := gno.Call("runtest", testFuncStr) eval := m.Eval(res) // NOTE: verbose prints get here - if printEvents { + if verbose { ctx := m.Context.(stdlibs.ExecContext) events := ctx.EventLogger.Events() From 68d336cbd7f9069f85a29f64b86f60f480ae3a67 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 3 Oct 2024 13:41:39 +0900 Subject: [PATCH 4/6] test --- gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar b/gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar index 08fd2f44470..cc53bf099fa 100644 --- a/gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar +++ b/gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar @@ -1,6 +1,6 @@ -# Test-print-events flag +# test verbose flag to print event -gno test -print-events=true . +gno test -v=true . ! stdout .+ stderr '--- event: +' From 4f46c48ed35fd92aeca1d587a667f1c37b8da909 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 3 Oct 2024 13:43:54 +0900 Subject: [PATCH 5/6] chore: fmt --- gnovm/cmd/gno/test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index dba42ccb1fc..88f3b0ab431 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -38,7 +38,6 @@ type testCfg struct { updateGoldenTests bool printRuntimeMetrics bool withNativeFallback bool - printEvents bool } func newTestCmd(io commands.IO) *commands.Command { From 984093bfa26d7a708e0810a4085bae704e08da78 Mon Sep 17 00:00:00 2001 From: n3wbie Date: Thu, 3 Oct 2024 14:14:05 +0900 Subject: [PATCH 6/6] fix: use test context --- gnovm/cmd/gno/test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 88f3b0ab431..9a3be4526b5 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -20,8 +20,8 @@ import ( "github.com/gnolang/gno/gnovm/pkg/gnoenv" gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/gnovm/pkg/gnomod" - "github.com/gnolang/gno/gnovm/stdlibs" "github.com/gnolang/gno/gnovm/tests" + teststd "github.com/gnolang/gno/gnovm/tests/stdlibs/std" "github.com/gnolang/gno/tm2/pkg/colors" "github.com/gnolang/gno/tm2/pkg/commands" "github.com/gnolang/gno/tm2/pkg/errors" @@ -458,7 +458,7 @@ func runTestFiles( eval := m.Eval(res) // NOTE: verbose prints get here if verbose { - ctx := m.Context.(stdlibs.ExecContext) + ctx := m.Context.(*teststd.TestExecContext) events := ctx.EventLogger.Events() for _, ev := range events[printedEvents:] {