diff --git a/examples/gno.land/p/demo/groups/gno.mod b/examples/gno.land/p/demo/groups/gno.mod index f0749e3f411..cf33d0ce74b 100644 --- a/examples/gno.land/p/demo/groups/gno.mod +++ b/examples/gno.land/p/demo/groups/gno.mod @@ -1,6 +1,3 @@ module gno.land/p/demo/groups -require ( - gno.land/p/demo/rat v0.0.0-latest - gno.land/r/demo/boards v0.0.0-latest -) +require gno.land/p/demo/rat v0.0.0-latest diff --git a/examples/gno.land/p/demo/groups/groups.gno b/examples/gno.land/p/demo/groups/groups.gno deleted file mode 100644 index fcf77dd2a74..00000000000 --- a/examples/gno.land/p/demo/groups/groups.gno +++ /dev/null @@ -1,8 +0,0 @@ -package groups - -import "gno.land/r/demo/boards" - -// TODO implement something and test. -type Group struct { - Board *boards.Board -} diff --git a/examples/gno.land/p/demo/tests/gno.mod b/examples/gno.land/p/demo/tests/gno.mod index d3d796f76f8..8a19acdbb18 100644 --- a/examples/gno.land/p/demo/tests/gno.mod +++ b/examples/gno.land/p/demo/tests/gno.mod @@ -3,5 +3,4 @@ module gno.land/p/demo/tests require ( gno.land/p/demo/tests/subtests v0.0.0-latest gno.land/p/demo/uassert v0.0.0-latest - gno.land/r/demo/tests v0.0.0-latest ) diff --git a/examples/gno.land/p/demo/tests/tests.gno b/examples/gno.land/p/demo/tests/tests.gno index 43732d82dac..ffad5b8c8cd 100644 --- a/examples/gno.land/p/demo/tests/tests.gno +++ b/examples/gno.land/p/demo/tests/tests.gno @@ -4,19 +4,10 @@ import ( "std" psubtests "gno.land/p/demo/tests/subtests" - "gno.land/r/demo/tests" - rtests "gno.land/r/demo/tests" ) const World = "world" -// IncCounter demonstrates that it's possible to call a realm function from -// a package. So a package can potentially write into the store, by calling -// an other realm. -func IncCounter() { - tests.IncCounter() -} - func CurrentRealmPath() string { return std.CurrentRealm().PkgPath() } @@ -64,10 +55,6 @@ func GetPSubtestsPrevRealm() std.Realm { return psubtests.GetPrevRealm() } -func GetRTestsGetPrevRealm() std.Realm { - return rtests.GetPrevRealm() -} - // Warning: unsafe pattern. func Exec(fn func()) { fn() diff --git a/examples/gno.land/p/demo/tests/z0_filetest.gno b/examples/gno.land/p/demo/tests/z0_filetest.gno deleted file mode 100644 index b788eaf398f..00000000000 --- a/examples/gno.land/p/demo/tests/z0_filetest.gno +++ /dev/null @@ -1,16 +0,0 @@ -package main - -import ( - ptests "gno.land/p/demo/tests" - rtests "gno.land/r/demo/tests" -) - -func main() { - println(rtests.Counter()) - ptests.IncCounter() - println(rtests.Counter()) -} - -// Output: -// 0 -// 1 diff --git a/gno.land/cmd/gnoland/testdata/assertorigincall.txtar b/gno.land/cmd/gnoland/testdata/assertorigincall.txtar index 1315f23cc95..dcaf7a78b6f 100644 --- a/gno.land/cmd/gnoland/testdata/assertorigincall.txtar +++ b/gno.land/cmd/gnoland/testdata/assertorigincall.txtar @@ -9,20 +9,16 @@ # | 4 | | through /r/foo | myrealm.A() | PANIC | # | 5 | | | myrealm.B() | pass | # | 6 | | | myrealm.C() | PANIC | -# | 7 | | through /p/demo/bar | myrealm.A() | PANIC | +# | 7 | MsgRun | wallet direct | myrealm.A() | PANIC | # | 8 | | | myrealm.B() | pass | # | 9 | | | myrealm.C() | PANIC | -# | 10 | MsgRun | wallet direct | myrealm.A() | PANIC | +# | 10 | | through /r/foo | myrealm.A() | PANIC | # | 11 | | | myrealm.B() | pass | # | 12 | | | myrealm.C() | PANIC | -# | 13 | | through /r/foo | myrealm.A() | PANIC | -# | 14 | | | myrealm.B() | pass | -# | 15 | | | myrealm.C() | PANIC | -# | 16 | | through /p/demo/bar | myrealm.A() | PANIC | -# | 17 | | | myrealm.B() | pass | -# | 18 | | | myrealm.C() | PANIC | -# | 19 | MsgCall | wallet direct | std.AssertOriginCall() | pass | -# | 20 | MsgRun | wallet direct | std.AssertOriginCall() | PANIC | +# | 13 | | through /p/demo/bar | bar.A() | PANIC | +# | 14 | | | bar.B() | pass | +# | 15 | | | bar.C() | PANIC | +# | 16 | MsgRun | wallet direct | std.AssertOriginCall() | PANIC | # Init ## set up and start a new node @@ -56,61 +52,43 @@ stdout 'OK!' ! gnokey maketx call -pkgpath gno.land/r/foo -func C -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 stderr 'invalid non-origin call' -## remove due to update to maketx call can only call realm (case 7,8,9) -## 7. MsgCall -> p/demo/bar.A -> myrlm.A: PANIC -## ! gnokey maketx call -pkgpath gno.land/p/demo/bar -func A -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 -## stderr 'invalid non-origin call' - -## 8. MsgCall -> p/demo/bar.B -> myrlm.B: PASS -## gnokey maketx call -pkgpath gno.land/p/demo/bar -func B -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 -## stdout 'OK!' - -## 9. MsgCall -> p/demo/bar.C -> myrlm.C: PANIC -## ! gnokey maketx call -pkgpath gno.land/p/demo/bar -func C -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 -## stderr 'invalid non-origin call' - -## 10. MsgRun -> run.main -> myrlm.A: PANIC +## 7. MsgRun -> run.main -> myrlm.A: PANIC ! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmA.gno stderr 'invalid non-origin call' -## 11. MsgRun -> run.main -> myrlm.B: PASS +## 8. MsgRun -> run.main -> myrlm.B: PASS gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmB.gno stdout 'OK!' -## 12. MsgRun -> run.main -> myrlm.C: PANIC +## 9. MsgRun -> run.main -> myrlm.C: PANIC ! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmC.gno stderr 'invalid non-origin call' -## 13. MsgRun -> run.main -> foo.A: PANIC +## 10. MsgRun -> run.main -> foo.A: PANIC ! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/fooA.gno stderr 'invalid non-origin call' -## 14. MsgRun -> run.main -> foo.B: PASS +## 11. MsgRun -> run.main -> foo.B: PASS gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/fooB.gno stdout 'OK!' -## 15. MsgRun -> run.main -> foo.C: PANIC +## 12. MsgRun -> run.main -> foo.C: PANIC ! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/fooC.gno stderr 'invalid non-origin call' -## 16. MsgRun -> run.main -> bar.A: PANIC +## 13. MsgRun -> run.main -> bar.A: PANIC ! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/barA.gno stderr 'invalid non-origin call' -## 17. MsgRun -> run.main -> bar.B: PASS +## 14. MsgRun -> run.main -> bar.B: PASS gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/barB.gno stdout 'OK!' -## 18. MsgRun -> run.main -> bar.C: PANIC +## 15. MsgRun -> run.main -> bar.C: PANIC ! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/barC.gno stderr 'invalid non-origin call' -## remove testcase 19 due to maketx call forced to call a realm -## 19. MsgCall -> std.AssertOriginCall: pass -## gnokey maketx call -pkgpath std -func AssertOriginCall -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 -## stdout 'OK!' - -## 20. MsgRun -> std.AssertOriginCall: PANIC +## 16. MsgRun -> std.AssertOriginCall: PANIC ! gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/baz.gno stderr 'invalid non-origin call' @@ -133,24 +111,25 @@ func B() { func C() { std.AssertOriginCall() } --- r/foo/foo.gno -- -package foo +-- p/demo/bar/bar.gno -- +package bar -import "gno.land/r/myrlm" +import "std" func A() { - myrlm.A() + C() } func B() { - myrlm.B() + if false { + C() + } } - func C() { - myrlm.C() + std.AssertOriginCall() } --- p/demo/bar/bar.gno -- -package bar +-- r/foo/foo.gno -- +package foo import "gno.land/r/myrlm" diff --git a/gno.land/cmd/gnoland/testdata/prevrealm.txtar b/gno.land/cmd/gnoland/testdata/prevrealm.txtar index 7a0d994a686..7c1e5fd7c72 100644 --- a/gno.land/cmd/gnoland/testdata/prevrealm.txtar +++ b/gno.land/cmd/gnoland/testdata/prevrealm.txtar @@ -8,16 +8,14 @@ # | 2 | | | myrlm.B() | user address | # | 3 | | through /r/foo | myrlm.A() | r/foo | # | 4 | | | myrlm.B() | r/foo | -# | 5 | | through /p/demo/bar | myrlm.A() | user address | +# | 5 | MsgRun | wallet direct | myrlm.A() | user address | # | 6 | | | myrlm.B() | user address | -# | 7 | MsgRun | wallet direct | myrlm.A() | user address | -# | 8 | | | myrlm.B() | user address | -# | 9 | | through /r/foo | myrlm.A() | r/foo | -# | 10 | | | myrlm.B() | r/foo | -# | 11 | | through /p/demo/bar | myrlm.A() | user address | -# | 12 | | | myrlm.B() | user address | -# | 13 | MsgCall | wallet direct | std.PrevRealm() | user address | -# | 14 | MsgRun | wallet direct | std.PrevRealm() | user address | +# | 7 | | through /r/foo | myrlm.A() | r/foo | +# | 8 | | | myrlm.B() | r/foo | +# | 9 | | through /p/demo/bar | bar.A() | user address | +# | 10 | | | bar.B() | user address | +# | 11 | MsgCall | wallet direct | std.PrevRealm() | user address | +# | 12 | MsgRun | wallet direct | std.PrevRealm() | user address | # Init ## deploy myrlm @@ -49,44 +47,35 @@ stdout ${RFOO_ADDR} gnokey maketx call -pkgpath gno.land/r/foo -func B -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 stdout ${RFOO_ADDR} -## remove due to update to maketx call can only call realm (case 5, 6, 13) -## 5. MsgCall -> p/demo/bar.A -> myrlm.A: user address -## gnokey maketx call -pkgpath gno.land/p/demo/bar -func A -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 -## stdout ${USER_ADDR_test1} - -## 6. MsgCall -> p/demo/bar.B -> myrlm.B -> r/foo.A: user address -## gnokey maketx call -pkgpath gno.land/p/demo/bar -func B -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 -## stdout ${USER_ADDR_test1} - -## 7. MsgRun -> myrlm.A: user address +## 5. MsgRun -> myrlm.A: user address gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmA.gno stdout ${USER_ADDR_test1} -## 8. MsgRun -> myrealm.B -> myrlm.A: user address +## 6. MsgRun -> myrealm.B -> myrlm.A: user address gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/myrlmB.gno stdout ${USER_ADDR_test1} -## 9. MsgRun -> r/foo.A -> myrlm.A: r/foo +## 7. MsgRun -> r/foo.A -> myrlm.A: r/foo gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/fooA.gno stdout ${RFOO_ADDR} -## 10. MsgRun -> r/foo.B -> myrlm.B -> r/foo.A: r/foo +## 8. MsgRun -> r/foo.B -> myrlm.B -> r/foo.A: r/foo gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/fooB.gno stdout ${RFOO_ADDR} -## 11. MsgRun -> p/demo/bar.A -> myrlm.A: user address +## 9. MsgRun -> p/demo/bar.A -> myrlm.A: user address gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/barA.gno stdout ${USER_ADDR_test1} -## 12. MsgRun -> p/demo/bar.B -> myrlm.B -> r/foo.A: user address +## 10. MsgRun -> p/demo/bar.B -> myrlm.B -> r/foo.A: user address gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/barB.gno stdout ${USER_ADDR_test1} -## 13. MsgCall -> std.PrevRealm(): user address +## 11. MsgCall -> std.PrevRealm(): user address ## gnokey maketx call -pkgpath std -func PrevRealm -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 ## stdout ${USER_ADDR_test1} -## 14. MsgRun -> std.PrevRealm(): user address +## 12. MsgRun -> std.PrevRealm(): user address gnokey maketx run -gas-fee 100000ugnot -gas-wanted 4000000 -broadcast -chainid tendermint_test test1 $WORK/run/baz.gno stdout ${USER_ADDR_test1} @@ -117,14 +106,14 @@ func B() string { -- p/demo/bar/bar.gno -- package bar -import "gno.land/r/myrlm" +import "std" func A() string { - return myrlm.A() + return std.PrevRealm().Addr().String() } func B() string { - return myrlm.B() + return A() } -- run/myrlmA.gno -- package main diff --git a/gnovm/memfile.go b/gnovm/memfile.go index a37bba6ef3d..25024804a24 100644 --- a/gnovm/memfile.go +++ b/gnovm/memfile.go @@ -2,6 +2,8 @@ package gnovm import ( "fmt" + "go/parser" + "go/token" "regexp" "sort" "strings" @@ -87,6 +89,28 @@ func (mempkg *MemPackage) Validate() error { prev = file.Name } + pIndex := strings.Index(mempkg.Path, "/p/") + if pIndex > 0 && !strings.ContainsRune(mempkg.Path[:pIndex], '/') { + for _, file := range mempkg.Files { + if !strings.HasSuffix(file.Name, ".gno") { + continue + } + fset := token.NewFileSet() + astFile, err := parser.ParseFile(fset, file.Name, file.Body, parser.ImportsOnly) + if err != nil { + return fmt.Errorf("failed to parse imports in file %q of package %q: %w", file.Name, mempkg.Path, err) + } + for _, imp := range astFile.Imports { + // ensure the pkg is a realm by checking if the path contains /r/ and no other / character before it (i.e protect from gno.land/p/demo/r/) + importPath := strings.TrimPrefix(strings.TrimSuffix(imp.Path.Value, `"`), `"`) + rIndex := strings.Index(importPath, "/r/") + if rIndex > 0 && !strings.ContainsRune(importPath[:rIndex], '/') { + return fmt.Errorf("package %q imports realm %q", mempkg.Path, importPath) + } + } + } + } + return nil } diff --git a/gnovm/memfile_test.go b/gnovm/memfile_test.go index c93c251b0e7..721d0550c97 100644 --- a/gnovm/memfile_test.go +++ b/gnovm/memfile_test.go @@ -7,6 +7,15 @@ import ( ) func TestMemPackage_Validate(t *testing.T) { + fileA := &MemFile{ + Name: "a.gno", + Body: "package test", + } + fileB := &MemFile{ + Name: "b.gno", + Body: "package test", + } + t.Parallel() tt := []struct { name string @@ -18,7 +27,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/demo/hey", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "", }, @@ -27,7 +36,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/demo/hey", - Files: []*MemFile{{Name: "b.gno"}, {Name: "a.gno"}}, + Files: []*MemFile{fileB, fileA}, }, "unsorted", }, @@ -36,7 +45,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/demo/hey", - Files: []*MemFile{{Name: "a.gno"}, {Name: "a.gno"}}, + Files: []*MemFile{fileA, fileA}, }, "duplicate", }, @@ -45,7 +54,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/very/long/path", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "path length", }, @@ -54,7 +63,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p/path/path", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "", }, @@ -63,7 +72,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/path/path", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "", }, @@ -72,7 +81,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/path/_path", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "", }, @@ -81,7 +90,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/path/path_", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "", }, @@ -90,7 +99,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/path/p_ath", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "", }, @@ -99,7 +108,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/path/_", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -108,7 +117,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/path/_/_", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -117,7 +126,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/path/__/path", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -126,7 +135,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/path/pa-th", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -135,7 +144,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/x/path/path", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -144,7 +153,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -153,7 +162,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p/", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -162,7 +171,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "github.com/p/path/path", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -171,7 +180,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p/p@th/abc/def", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -180,7 +189,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p/p&th/abc/def", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -189,7 +198,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p/1Path/abc/def", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -198,7 +207,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p/PaTh/abc/def", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -207,7 +216,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p/path//def", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -216,7 +225,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/p/path/abc/def/", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -225,7 +234,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/very/very/very/long/path", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "", }, @@ -234,7 +243,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/very/very/very/long/p@th", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -243,7 +252,7 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/very/very/very/long/path/", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, @@ -252,10 +261,84 @@ func TestMemPackage_Validate(t *testing.T) { &MemPackage{ Name: "hey", Path: "gno.land/r/very/very/very//long/path/", - Files: []*MemFile{{Name: "a.gno"}}, + Files: []*MemFile{fileA}, }, "invalid package/realm path", }, + { + "Invalid package imports realm", + &MemPackage{ + Name: "test", + Path: "gno.land/p/demo/test", + Files: []*MemFile{ + {Name: "a.gno", Body: ` + package test + + import "gno.land/r/demo/avl" + + func A() { + avl.A() + } + `}, + }, + }, + "package \"gno.land/p/demo/test\" imports realm \"gno.land/r/demo/avl\"", + }, + { + "Valid witr /r/ as a realm name", + &MemPackage{ + Name: "test", + Path: "gno.land/p/demo/test", + Files: []*MemFile{ + {Name: "a.gno", Body: ` + package test + + import "gno.land/p/r/r" + + func A() { + r.A() + } + `}, + }, + }, + "", + }, + { + "Valid package containing non gno file", + &MemPackage{ + Name: "test", + Path: "gno.land/p/demo/test", + Files: []*MemFile{ + { + Name: "README.md", + Body: ` + # Test + `, + }, + {Name: "a.gno", Body: ` + package test + + import "gno.land/p/r/r" + + func A() { + r.A() + } + `}, + }, + }, + "", + }, + { + "Invalid empty gno file", + &MemPackage{ + Name: "test", + Path: "gno.land/p/demo/test", + Files: []*MemFile{ + {Name: "a.gno"}, + }, + }, + "failed to parse imports in file \"a.gno\" of package \"gno.land/p/demo/test\"", + }, } for _, tc := range tt { t.Run(tc.name, func(t *testing.T) { diff --git a/gnovm/tests/files/zrealm_crossrealm11_stdlibs.gno b/gnovm/tests/files/zrealm_crossrealm11_stdlibs.gno index e6f33c50654..5936743ddc6 100644 --- a/gnovm/tests/files/zrealm_crossrealm11_stdlibs.gno +++ b/gnovm/tests/files/zrealm_crossrealm11_stdlibs.gno @@ -2,10 +2,11 @@ package crossrealm_test import ( + "std" + ptests "gno.land/p/demo/tests" "gno.land/p/demo/ufmt" rtests "gno.land/r/demo/tests" - "std" ) func getPrevRealm() std.Realm { @@ -64,10 +65,6 @@ func main() { callStackAdd: " -> r/demo/tests -> r/demo/tests/subtests", callerFn: rtests.GetRSubtestsPrevRealm, }, - { - callStackAdd: " -> p/demo/tests -> r/demo/tests", - callerFn: ptests.GetRTestsGetPrevRealm, - }, } println("---") // needed to have space prefixes @@ -140,7 +137,3 @@ func printColumns(left, right string) { // user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests // user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests // user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> r/demo/tests -> r/demo/tests/subtests = gno.land/r/demo/tests -// user1.gno -> r/crossrealm_test.main -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test -// user1.gno -> r/crossrealm_test.main -> r/crossrealm_test.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test -// user1.gno -> r/crossrealm_test.main -> r/demo/tests.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test -// user1.gno -> r/crossrealm_test.main -> p/demo/tests.Exec -> p/demo/tests -> r/demo/tests = gno.land/r/crossrealm_test