From 14d4d21c9ce70376078ca3ddc5e2ab7a68a6e407 Mon Sep 17 00:00:00 2001 From: Kirk Haines Date: Fri, 27 Sep 2024 10:06:03 +0200 Subject: [PATCH] fix(cmd/gno): pass an ExecContext to MachineOptions in `gno run` (#2856) This fixes: https://github.com/gnolang/gno/issues/2834 It tries to hew very closely to how the context is created under `gno test` so that there is consistency of results between code that is executed in the test and run contexts. Please let me know if there are any conventions for the codebase that I should have followed that I did not. I hooked into the portion of the `ExecRun()` that was calling `NewMachineWithOptions()`, and instead followed a similar pattern to what is defined in `test.go` to configure the machine with a context that has reasonable defaults. These, except for the Chain ID (which is `dev` when running under `gno test`), are identical to the test settings. This allows packages like `gno.land/p/demo/entropy` to work when code is executed with `gno run`, as well as any others which might try to access information only available from the context. A simple piece of code to demonstrate the issue is below. This will fail without this change. ``` package foo import ( "fmt" "std" ) func main() { fmt.Printf("GetHeight(): %d\n", std.GetHeight()) } ``` A test has been added to the tests for the run command, which tries to run code similar to the code above within the test.
Contributors' checklist... - [X] Added new tests, or not needed, or not feasible - [X] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [X] Updated the official documentation or not needed - [X] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [X] Added references to related issues and PRs - [X] Provided any useful hints for running manual tests
--- gnovm/cmd/gno/run.go | 9 +++++++-- gnovm/cmd/gno/run_test.go | 4 ++++ gnovm/tests/integ/context/context.gno | 11 +++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 gnovm/tests/integ/context/context.gno diff --git a/gnovm/cmd/gno/run.go b/gnovm/cmd/gno/run.go index cfbfe995a46..f174c2b4cc5 100644 --- a/gnovm/cmd/gno/run.go +++ b/gnovm/cmd/gno/run.go @@ -14,6 +14,7 @@ import ( gno "github.com/gnolang/gno/gnovm/pkg/gnolang" "github.com/gnolang/gno/gnovm/tests" "github.com/gnolang/gno/tm2/pkg/commands" + "github.com/gnolang/gno/tm2/pkg/std" ) type runCfg struct { @@ -112,11 +113,15 @@ func execRun(cfg *runCfg, args []string, io commands.IO) error { return errors.New("no files to run") } + var send std.Coins + pkgPath := string(files[0].PkgName) + ctx := tests.TestContext(pkgPath, send) m := gno.NewMachineWithOptions(gno.MachineOptions{ - PkgPath: string(files[0].PkgName), - Input: stdin, + PkgPath: pkgPath, Output: stdout, + Input: stdin, Store: testStore, + Context: ctx, Debug: cfg.debug || cfg.debugAddr != "", }) diff --git a/gnovm/cmd/gno/run_test.go b/gnovm/cmd/gno/run_test.go index 79a873cdfe5..975868b7daf 100644 --- a/gnovm/cmd/gno/run_test.go +++ b/gnovm/cmd/gno/run_test.go @@ -79,6 +79,10 @@ func TestRunApp(t *testing.T) { args: []string{"run", "../../tests/integ/invalid_assign/main.gno"}, recoverShouldContain: "cannot use bool as main.C without explicit conversion", }, + { + args: []string{"run", "-expr", "Context()", "../../tests/integ/context/context.gno"}, + stdoutShouldContain: "Context worked", + }, // TODO: a test file // TODO: args // TODO: nativeLibs VS stdlibs diff --git a/gnovm/tests/integ/context/context.gno b/gnovm/tests/integ/context/context.gno new file mode 100644 index 00000000000..92a9cc632b7 --- /dev/null +++ b/gnovm/tests/integ/context/context.gno @@ -0,0 +1,11 @@ +package runtests + +import ( + "fmt" + "std" +) + +func Context() { + // This requires a Context to work; it will fail ugly if the Context isn't available. + fmt.Printf("Context worked: %d\n", std.GetHeight()) +}