gnolang / gno

Gno: An interpreted, stack-based Go virtual machine to build succinct and composable apps + Gno.land: a blockchain for timeless code and fair open-source.
https://gno.land/
Other
880 stars 364 forks source link

Allow *_test.gno file to run pkg with _test as suffix #768

Open albttx opened 1 year ago

albttx commented 1 year ago

Package test with suffix _test not working

✅ This work

package airdrop

import (
    "testing"
)

func TestABC(t *testing.T) {
    println("abc")
}

❌ This fail

package airdrop_test

import (
    "testing"
)

func TestABC(t *testing.T) {
    println("abc")
}

with panic output

panic: package ./examples/gno.land/p/demo/airdrop already exists in cache

goroutine 1 [running]:
github.com/gnolang/gno/gnovm/pkg/gnolang.(*defaultStore).SetCachePackage(0xc0000c2000, 0xc009d5f860)
        /home/albttx/go/src/github.com/gnolang/gno/gnovm/pkg/gnolang/store.go:207 +0x16e
github.com/gnolang/gno/gnovm/pkg/gnolang.(*Machine).RunMemPackage(0xc0092a59e0, 0xc0001fe180, 0x1)
        /home/albttx/go/src/github.com/gnolang/gno/gnovm/pkg/gnolang/machine.go:230 +0x1c6
main.gnoTestPkg({0xc00003ae40, 0x22}, {0xc0000d0210?, 0x1, 0x4?}, {0xc0000d0230, 0x1, 0x5066df?}, 0xc000112600, 0xc0000bc000)
        /home/albttx/go/src/github.com/gnolang/gno/gnovm/cmd/gno/test.go:266 +0x46f
main.execTest(0xc000112600, {0xc000112070, 0x1, 0x1}, 0xc000112060?)
        /home/albttx/go/src/github.com/gnolang/gno/gnovm/cmd/gno/test.go:189 +0xca7
main.newTestCmd.func1({0x0?, 0x0?}, {0xc000112070?, 0x0?, 0x0?})
        /home/albttx/go/src/github.com/gnolang/gno/gnovm/cmd/gno/test.go:46 +0x32
github.com/peterbourgon/ff/v3/ffcli.(*Command).Run(0x2?, {0xef9630?, 0xc000128000?})
        /home/albttx/go/pkg/mod/github.com/peterbourgon/ff/v3@v3.3.0/ffcli/command.go:153 +0x16c
github.com/peterbourgon/ff/v3/ffcli.(*Command).Run(0xc000016000?, {0xef9630?, 0xc000128000?})
        /home/albttx/go/pkg/mod/github.com/peterbourgon/ff/v3@v3.3.0/ffcli/command.go:157 +0x113
github.com/peterbourgon/ff/v3/ffcli.(*Command).ParseAndRun(0x4055fc?, {0xef9630, 0xc000128000}, {0xc000112050?, 0x401230?, 0x0?})
        /home/albttx/go/pkg/mod/github.com/peterbourgon/ff/v3@v3.3.0/ffcli/command.go:169 +0x4f
main.main()
        /home/albttx/go/src/github.com/gnolang/gno/gnovm/cmd/gno/main.go:14 +0x77
exit status 2

To fix this, we can do a test with a main function, but testing with TestFunctionXXX(t *testing.T) would be nice:

NOTE

The function TestFunctionXXX should be considered as a Realm

tbruyelle commented 1 year ago

Directives like // PKGPATH: are not read in _test files, only in _filetest files. Can you remove it from your example for better clarity?

moul commented 11 months ago

Related with https://github.com/gnolang/gno/issues/1215.