Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gno): use verbose flag to print emitted events #2071

Closed
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -36,6 +38,7 @@ type testCfg struct {
updateGoldenTests bool
printRuntimeMetrics bool
withNativeFallback bool
printEvents bool
}

func newTestCmd(io commands.IO) *commands.Command {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -228,6 +238,7 @@ func gnoTestPkg(
rootDir = cfg.rootDir
runFlag = cfg.run
printRuntimeMetrics = cfg.printRuntimeMetrics
printEvents = cfg.printEvents

stdin = io.In()
stdout = io.Out()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -419,6 +430,7 @@ func runTestFiles(
pkgName string,
verbose bool,
printRuntimeMetrics bool,
printEvents bool,
runFlag string,
io commands.IO,
) (errs error) {
Expand Down Expand Up @@ -447,10 +459,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 printEvents {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @moul, I think we should print these events if the verbose flag is enabled, and not hide this functionality behind an additional flag we need to now maintain

The reasoning is that events are now an integral part of the Gno development lifecycle, and not a "feature" we can disable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning is that events are now an integral part of the Gno development lifecycle

I totally understand, however for easier(or at least more readable) debugging flags needs to be separated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ctx := m.Context.(stdlibs.ExecContext)

events := ctx.EventLogger.Events()
for _, ev := range events[printedEvents:] {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
printedEvents++
// XXX: print events with better formatting (e.g. JSON)
strEv := fmt.Sprint(ev)
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
io.ErrPrintfln("--- event: %s", colors.ColoredBytesOnlyAscii([]byte(strEv), colors.Magenta))
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
}
}

ret := eval[0].GetString()
if ret == "" {
Expand Down
26 changes: 26 additions & 0 deletions gnovm/cmd/gno/testdata/gno_test/flag_print-events.txtar
Original file line number Diff line number Diff line change
@@ -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()
}


12 changes: 12 additions & 0 deletions tm2/pkg/colors/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,15 @@
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 {
zivkovicmilos marked this conversation as resolved.
Show resolved Hide resolved
s += textColor(string(b))
} else {
s += string(b)

Check warning on line 183 in tm2/pkg/colors/colors.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/colors/colors.go#L177-L183

Added lines #L177 - L183 were not covered by tests
}
}
r3v4s marked this conversation as resolved.
Show resolved Hide resolved
return s

Check warning on line 186 in tm2/pkg/colors/colors.go

View check run for this annotation

Codecov / codecov/patch

tm2/pkg/colors/colors.go#L186

Added line #L186 was not covered by tests
}
Loading