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 16 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
18 changes: 18 additions & 0 deletions examples/gno.land/p/demo/tests/tests_test.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tests_test

import (
"testing"

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

var World = "WORLD"

func TestGetHelloWorld(t *testing.T) {
// tests.World is 'world'
s := "hello " + tests.World + World
const want = "hello worldWORLD"
if s != want {
t.Errorf("got %q want %q", s, want)
}
}
59 changes: 40 additions & 19 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,6 @@
// XXX: display a warn?
mode = tests.ImportModeStdlibsPreferred
}
testStore := tests.TestStore(
rootDir, "",
stdin, stdout, stderr,
mode,
)
if verbose {
testStore.SetLogStoreOps(true)
}

if !verbose {
// TODO: speedup by ignoring if filter is file/*?
mockOut := bytes.NewBufferString("")
Expand All @@ -328,9 +319,19 @@

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

// run test files in pkg
{
if len(tfiles.Files) > 0 {
testStore := tests.TestStore(
rootDir, "",
stdin, stdout, stderr,
mode,
)
if verbose {
testStore.SetLogStoreOps(true)
}

m := tests.TestMachine(testStore, stdout, gnoPkgPath)
if printRuntimeMetrics {
// from tm2/pkg/sdk/vm/keeper.go
Expand All @@ -346,17 +347,37 @@
}
}

// 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)
// test xxx_test pkg
if len(ifiles.Files) > 0 {
testStore := tests.TestStore(
rootDir, "",
stdin, stdout, stderr,
mode,
)
if verbose {
testStore.SetLogStoreOps(true)
}

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

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L352-L359

Added lines #L352 - L359 were not covered by tests

m := tests.TestMachine(testStore, stdout, testPkgName)

memFiles := make([]*std.MemFile, 0, len(ifiles.FileNames())+1)
for _, f := range memPkg.Files {
for _, ifileName := range ifiles.FileNames() {
if f.Name == "gno.mod" || f.Name == ifileName {
memFiles = append(memFiles, f)
break

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

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L361-L368

Added lines #L361 - L368 were not covered by tests
}
}
}
memPkg.Files = memFiles
memPkg.Name = testPkgName
memPkg.Path = memPkg.Path + "_test"

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 380 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L372-L380

Added lines #L372 - L380 were not covered by tests
}
}

Expand Down
8 changes: 0 additions & 8 deletions gnovm/cmd/gno/testdata/gno_test/flag_run.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@

gno test .

stdout '=== RUN TestRun/hello' # show that t.Run doesn't care about -verbose
stdout '=== RUN TestRun/hi_you'
stdout '=== RUN TestRun/hi_me'

gno test ./run_test.gno

stdout '=== RUN TestRun/hello' # show that t.Run doesn't care about -verbose
stdout '=== RUN TestRun/hi_you'
stdout '=== RUN TestRun/hi_me'

thehowl marked this conversation as resolved.
Show resolved Hide resolved
gno test -verbose .

stdout '=== RUN TestRun/hello'
Expand Down
Loading