-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(gnovm): fix TestPackages (#964)
`TestPackages` wasn't actually reporting test failures, due to a bug in the underlying function `machine.TestMemPackage()`. `TestMemPackage` was running the gno tests by just calling the test function with a new `testing.T`, but then it's not possible to detect if there's any failure, because `testing.T` has only private fields. The fix is to change that to a call to `testing.RunTest()` function, which is already used by `gno test`, and returns a `testing.Report` on which we can detect failures. A test has been added (see `TestMachineTestMemPackage()`) in gnovm/tests folder (because a `TestStore` is needed). On master, this test is failing. ``` go test ./gnovm/tests -v -run TestMachineTestMemPackage ``` The next step should be to merge the 2 ways that currently exists to run gno tests, i.e. `gno test` and `go test -run TestPackages`. Potentially this could fix #896. ## Contributors Checklist - [x] Added new tests, or not needed, or not feasible - [ ] Provided an example (e.g. screenshot) to aid review or the PR is self-explanatory - [ ] Updated the official documentation or not needed - [ ] No breaking changes were made, or a `BREAKING CHANGE: xxx` message was included in the description - [ ] Added references to related issues and PRs - [x] Provided any useful hints for running manual tests - [ ] Added new benchmarks to [generated graphs](https://gnoland.github.io/benchmarks), if any. More info [here](../.benchmarks/README.md). ## Maintainers Checklist - [x] Checked that the author followed the guidelines in `CONTRIBUTING.md` - [x] Checked the conventional-commit (especially PR title and verb, presence of `BREAKING CHANGE:` in the body) - [x] Ensured that this PR is not a significant change or confirmed that the review/consideration process was appropriate for the change </details> --------- Co-authored-by: Manfred Touron <94029+moul@users.noreply.github.com>
- Loading branch information
Showing
4 changed files
with
147 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package tests | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
gno "github.com/gnolang/gno/gnovm/pkg/gnolang" | ||
) | ||
|
||
func TestMachineTestMemPackage(t *testing.T) { | ||
matchFunc := func(pat, str string) (bool, error) { return true, nil } | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
shouldSucceed bool | ||
}{ | ||
{ | ||
name: "TestSuccess", | ||
path: "testdata/TestMemPackage/success", | ||
shouldSucceed: true, | ||
}, | ||
{ | ||
name: "TestFail", | ||
path: "testdata/TestMemPackage/fail", | ||
shouldSucceed: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
// NOTE: Because the purpose of this test is to ensure testing.T.Failed() | ||
// returns true if a gno test is failing, and because we don't want this | ||
// to affect the current testing.T, we are creating an other one thanks | ||
// to testing.RunTests() function. | ||
testing.RunTests(matchFunc, []testing.InternalTest{ | ||
{ | ||
Name: tt.name, | ||
F: func(t2 *testing.T) { //nolint:thelper | ||
rootDir := filepath.Join("..", "..") | ||
store := TestStore(rootDir, "test", os.Stdin, os.Stdout, os.Stderr, ImportModeStdlibsOnly) | ||
store.SetLogStoreOps(true) | ||
m := gno.NewMachineWithOptions(gno.MachineOptions{ | ||
PkgPath: "test", | ||
Output: os.Stdout, | ||
Store: store, | ||
Context: nil, | ||
}) | ||
memPkg := gno.ReadMemPackage(tt.path, "test") | ||
|
||
m.TestMemPackage(t2, memPkg) | ||
|
||
if tt.shouldSucceed { | ||
assert.False(t, t2.Failed(), "test %q should have succeed", tt.name) | ||
} else { | ||
assert.True(t, t2.Failed(), "test %q should have failed", tt.name) | ||
} | ||
}, | ||
}, | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package test | ||
|
||
import "testing" | ||
|
||
func TestFail(t *testing.T) { | ||
t.Errorf("OUPS") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package test | ||
|
||
import "testing" | ||
|
||
func TestSucess(t *testing.T) {} |