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

fix(cmd/gno): allow testing packages which contain test files with package x_test #1330

Merged
merged 19 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/gno.land/p/demo/tests/tests.gno
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
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.
Expand Down
12 changes: 12 additions & 0 deletions examples/gno.land/p/demo/tests/z1_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tests_test

import (
"testing"

"gno.land/p/demo/tests"
)

func TestGetHelloWorld(t *testing.T) {
s := "hello " + tests.World
println(s)
}
21 changes: 8 additions & 13 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,10 @@

// tfiles, ifiles := gno.ParseMemPackageTests(memPkg)
tfiles, ifiles := parseMemPackageTests(memPkg)
testPkgName := getPkgNameFromFileset(ifiles)

// run test files in pkg
{
if !strings.HasSuffix(testPkgName, "_test") {
albttx marked this conversation as resolved.
Show resolved Hide resolved
thehowl marked this conversation as resolved.
Show resolved Hide resolved
m := tests.TestMachine(testStore, stdout, gnoPkgPath)
if printRuntimeMetrics {
// from tm2/pkg/sdk/vm/keeper.go
Expand All @@ -344,18 +345,12 @@
if err != nil {
errs = multierr.Append(errs, err)
}
}

// run test files in xxx_test pkg
{
testPkgName := getPkgNameFromFileset(ifiles)
if testPkgName != "" {
m := tests.TestMachine(testStore, stdout, testPkgName)
m.RunMemPackage(memPkg, true)
err := runTestFiles(m, ifiles, testPkgName, verbose, printRuntimeMetrics, runFlag, io)
if err != nil {
errs = multierr.Append(errs, err)
}
} else { // run xxx_test
m := tests.TestMachine(testStore, stdout, testPkgName)
m.RunMemPackage(memPkg, true)
err := runTestFiles(m, ifiles, testPkgName, verbose, printRuntimeMetrics, runFlag, io)
if err != nil {
errs = multierr.Append(errs, err)

Check warning on line 353 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L348-L353

Added lines #L348 - L353 were not covered by tests
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,15 @@
// Files' package names must match the machine's active one.
// if there is one.
for _, fn := range fns {
if fn.PkgName != "" && fn.PkgName != m.Package.PkgName {
panic(fmt.Sprintf("expected package name [%s] but got [%s]",
m.Package.PkgName, fn.PkgName))
var (
notEmptyName = fn.PkgName != ""
albttx marked this conversation as resolved.
Show resolved Hide resolved
notBaseName = fn.PkgName != m.Package.PkgName
notTestName = m.Package.PkgName+"_test" != fn.PkgName
)

if notEmptyName && notBaseName && notTestName {
albttx marked this conversation as resolved.
Show resolved Hide resolved
panic(fmt.Sprintf("expected package name [%s] or [%s_test] but got [%s]",
m.Package.PkgName, m.Package.PkgName, fn.PkgName))

Check warning on line 413 in gnovm/pkg/gnolang/machine.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/machine.go#L412-L413

Added lines #L412 - L413 were not covered by tests
}
}
// Add files to *PackageNode.FileSet.
Expand Down
Loading