Skip to content

Commit

Permalink
fix: init ctx of injected package when using gno test to support lazy…
Browse files Browse the repository at this point in the history
… imports containing an init() func

Signed-off-by: moul <94029+moul@users.noreply.github.com>
  • Loading branch information
moul committed Oct 20, 2023
1 parent daf34a0 commit 8fe571f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
21 changes: 13 additions & 8 deletions gnovm/tests/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ func TestMachine(store gno.Store, stdout io.Writer, pkgPath string) *gno.Machine
}

func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAlloc int64, send std.Coins) *gno.Machine {
ctx := testContext(pkgPath, send)
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "", // set later.
Output: stdout,
Store: store,
Context: ctx,
MaxAllocBytes: maxAlloc,
})
return m
}

func testContext(pkgPath string, send std.Coins) stdlibs.ExecContext {
// FIXME: create a better package to manage this, with custom constructors
pkgAddr := gno.DerivePkgAddr(pkgPath) // the addr of the pkgPath called.
caller := gno.DerivePkgAddr("user1.gno")
Expand All @@ -51,14 +63,7 @@ func testMachineCustom(store gno.Store, pkgPath string, stdout io.Writer, maxAll
OrigSendSpent: new(std.Coins),
Banker: banker,
}
m := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "", // set later.
Output: stdout,
Store: store,
Context: ctx,
MaxAllocBytes: maxAlloc,
})
return m
return ctx
}

type runFileTestOptions struct {
Expand Down
3 changes: 3 additions & 0 deletions gnovm/tests/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,10 +440,13 @@ func TestStore(rootDir, filesPath string, stdin io.Reader, stdout, stderr io.Wri
panic(fmt.Sprintf("found an empty package %q", pkgPath))
}

send := std.Coins{}
ctx := testContext(pkgPath, send)
m2 := gno.NewMachineWithOptions(gno.MachineOptions{
PkgPath: "test",
Output: stdout,
Store: store,
Context: ctx,
})
pn, pv = m2.RunMemPackage(memPkg, true)
return
Expand Down

0 comments on commit 8fe571f

Please sign in to comment.