Skip to content

Commit

Permalink
feat(gnovm): add 'gno test -print-events' + cleanup machine between t…
Browse files Browse the repository at this point in the history
…ests

Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Oct 17, 2024
1 parent 05cd4f5 commit dd4fb02
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
29 changes: 27 additions & 2 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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/commands"
"github.com/gnolang/gno/tm2/pkg/errors"
"github.com/gnolang/gno/tm2/pkg/random"
Expand All @@ -35,6 +36,7 @@ type testCfg struct {
timeout time.Duration
updateGoldenTests bool
printRuntimeMetrics bool
printEvents bool
withNativeFallback bool
}

Expand Down Expand Up @@ -149,6 +151,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 +237,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 +305,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 +339,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 +429,7 @@ func runTestFiles(
pkgName string,
verbose bool,
printRuntimeMetrics bool,
printEvents bool,
runFlag string,
io commands.IO,
) (errs error) {
Expand All @@ -428,6 +439,9 @@ func runTestFiles(
}
}()

// cleanup machine between tests
tests.CleanupMachine(m)

testFuncs := &testFuncs{
PackageName: pkgName,
Verbose: verbose,
Expand All @@ -452,6 +466,17 @@ func runTestFiles(

eval := m.Eval(gno.Call("runtest", testFuncStr))

if printEvents {
events := m.Context.(*teststd.TestExecContext).EventLogger.Events()
if events != nil {
res, err := json.Marshal(events)
if err != nil {
panic(err)

Check warning on line 474 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L470-L474

Added lines #L470 - L474 were not covered by tests
}
io.ErrPrintfln("EVENTS: %s", string(res))

Check warning on line 476 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L476

Added line #L476 was not covered by tests
}
}

ret := eval[0].GetString()
if ret == "" {
err := errors.New("failed to execute unit test: %q", test.Name)
Expand Down
15 changes: 14 additions & 1 deletion gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestContext(pkgPath string, send std.Coins) *teststd.TestExecContext {
pkgAddr := gno.DerivePkgAddr(pkgPath) // the addr of the pkgPath called.
caller := gno.DerivePkgAddr("user1.gno")

pkgCoins := std.MustParseCoins(ugnot.ValueString(200000000)).Add(send) // >= send.
pkgCoins := std.MustParseCoins(ugnot.ValueString(200_000_000)).Add(send) // >= send.
banker := newTestBanker(pkgAddr.Bech32(), pkgCoins)
ctx := stdlibs.ExecContext{
ChainID: "dev",
Expand All @@ -74,6 +74,19 @@ func TestContext(pkgPath string, send std.Coins) *teststd.TestExecContext {
}
}

// CleanupMachine can be called during two tests while reusing the same Machine instance.
func CleanupMachine(m *gno.Machine) {
prevCtx := m.Context.(*teststd.TestExecContext)
prevSend := prevCtx.OrigSend

newCtx := TestContext("", prevCtx.OrigSend)
pkgCoins := std.MustParseCoins(ugnot.ValueString(200_000_000)).Add(prevSend) // >= send.
banker := newTestBanker(prevCtx.OrigPkgAddr, pkgCoins)
newCtx.OrigPkgAddr = prevCtx.OrigPkgAddr
newCtx.Banker = banker
m.Context = newCtx
}

type runFileTestOptions struct {
nativeLibs bool
logger loggerFunc
Expand Down

0 comments on commit dd4fb02

Please sign in to comment.