diff --git a/gnovm/cmd/gno/test.go b/gnovm/cmd/gno/test.go index 5884463a552..9a3be4526b5 100644 --- a/gnovm/cmd/gno/test.go +++ b/gnovm/cmd/gno/test.go @@ -21,6 +21,8 @@ import ( gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/gnovm/pkg/gnomod" "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" "github.com/gnolang/gno/tm2/pkg/random" @@ -447,10 +449,25 @@ 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) - eval := m.Eval(gno.Call("runtest", testFuncStr)) + res := gno.Call("runtest", testFuncStr) + + eval := m.Eval(res) // NOTE: verbose prints get here + if verbose { + ctx := m.Context.(*teststd.TestExecContext) + + events := ctx.EventLogger.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)) + } + } 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..cc53bf099fa --- /dev/null +++ b/gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar @@ -0,0 +1,26 @@ +# test verbose flag to print event + +gno test -v=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 +}