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

In filetests, PrevRealm().Address is std.GetOrigPkgAddr(), instead of std.TestSetOrigCaller's #1704

Closed
grepsuzette opened this issue Feb 29, 2024 · 4 comments

Comments

@grepsuzette
Copy link
Contributor

grepsuzette commented Feb 29, 2024

A potential bug, based on what @tbruyelle said in #1664 and what I read about PrevRealm().

Description

Happens with a filetest:

*a.gno

package qwerty

import (
	"std"
)

func Address() std.Address {
	return std.PrevRealm().Addr()
}

a_filetest.gno

// PKGPATH: gno.land/r/demo/qwerty_test
package qwerty_test

import (
	"std"

	"gno.land/p/demo/testutils"
	qwerty "gno.land/r/demo/qwerty"
)

func main() {
	dude := testutils.TestAddress("dude")
	std.TestSetOrigCaller(dude)
	println(qwerty.Address())
	println(std.GetOrigPkgAddr())
	println(dude)
}

// Output:

Expected

So we print 3 lines:

	println(qwerty.Address())
	println(std.GetOrigPkgAddr())
	println(dude)

Would expect qwerty.Address() to be equal to dude.
But instead, qwerty.Address() is equal to std.GetOrigPkgAddr().


Note: calling std.TestSetOrigCaller(dude) does not help either. qwerty.Address() will still return the address of "the filetest realm address" before it got modified to dude's address.

@grepsuzette
Copy link
Contributor Author

grepsuzette commented Feb 29, 2024

I added a (passing) filetest for PrevRealm in #1705.

So it's likely something special in above filetest that makes the present issue fail.

Quite probably those lines:

// PKGPATH: gno.land/r/demo/qwerty_test
package qwerty_test

Are they wrong or something?

Edit:
The initial difference likely occurs at this line, as IsRealmPath depends on whether it begins with "gno.land/r/":

if !gno.IsRealmPath(pkgPath) {

Schematically we do in the case of an explicitely named package:

// save package using realm crawl procedure.
memPkg := &std.MemPackage{ Name: string(pkgName), Path: pkgPath, Files: []*std.MemFile{{Name: "main.gno", Body: string(bz) }}}
// run decls and init functions.
m.RunMemPackage(memPkg, true)
m.PreprocessAllFilesAndSaveBlockNodes() 
pv2 := store.GetPackage(pkgPath, false)
m.SetActivePackage(pv2)
m.RunMain()

theory 2

Or maybe it's because in this example we have import "gno.land/r/demo/qwerty" and m.PreprocessAllFilesAndSaveBlockNodes() creates a package node for that import:

func (m *Machine) PreprocessAllFilesAndSaveBlockNodes() {
ch := m.Store.IterMemPackage()
for memPkg := range ch {
fset := ParseMemPackage(memPkg)
pn := NewPackageNode(Name(memPkg.Name), memPkg.Path, fset)
m.Store.SetBlockNode(pn)
PredefineFileSet(m.Store, pn, fset)

@grepsuzette
Copy link
Contributor Author

grepsuzette commented Feb 29, 2024

Workaround

Get rid of line:

// PKGPATH: gno.land/r/demo/qwerty_test`

Use package main for your testfile, then it behaves normally.
A question then is, why did I use PKGPATH in the first place (I don't remember exactly. Edit: because I used gno.land/r/demo/mail, my old realm, as a starting point).

@tbruyelle
Copy link
Contributor

So yes, you got it. When you add a realm path (something that starts with gno.land/r/) in the // PKGPATH: directive, you basically turn your filetest into a realm. So logically, std.PrevRealm() invoked in qwerty won't return the user, but this filetest realm. That's the expected behavior.

I invite you to add a z3_filetest.gno in #1705, identical to z2_filetest.gno but with a // PKGPATH: directive that turns the filetest into a realm.

grepsuzette added a commit to grepsuzette/gno that referenced this issue Mar 3, 2024
@grepsuzette
Copy link
Contributor Author

I invite you to add a z3_filetest.gno in #1705, identical to z2_filetest.gno but with a // PKGPATH: directive that turns the filetest into a realm.

Done! Closing here. Thanks for your reply.

grepsuzette added a commit to grepsuzette/gno that referenced this issue Jun 6, 2024
thehowl added a commit that referenced this issue Oct 2, 2024
PrevRealm was tested from
`gno/examples/gno.land/r/demo/tests/tests_test.gnotest`.
As far as I can see in r/demo/tests however, it was not tested for
*filetests*.

Although `gno test .` passes, depending on whether #1704 is confirmed,
there may be more filetests to add in this folder (in a different PR I
guess).

---------

Co-authored-by: grepsuzette <[email protected]>
Co-authored-by: Morgan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants