From f2928f1c40776bda909f6e4a397c5e8fb409d66f Mon Sep 17 00:00:00 2001 From: n0izn0iz Date: Wed, 6 Nov 2024 17:17:26 +0100 Subject: [PATCH] test(cmd/gno): prevent nil deref in testMainCaseRun (#3071) In `gnovm/cmd/gno/main_test.go`, when an error is expected but none happen, the test helper tries to dereference nil, causing a confusing test result Before: ``` === RUN TestModApp/mod_tidy..~..~tests~integ~invalid_module_version1 main_test.go:92: recover runtime error: invalid memory address or nil pointer dereference main_test.go:93: Error Trace: /Users/norman/Code/gno/gnovm/cmd/gno/main_test.go:93 /Users/norman/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.4.darwin-arm64/src/runtime/panic.go:770 /Users/norman/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.4.darwin-arm64/src/runtime/panic.go:261 /Users/norman/go/pkg/mod/golang.org/toolchain@v0.0.1-go1.22.4.darwin-arm64/src/runtime/signal_unix.go:881 /Users/norman/Code/gno/gnovm/cmd/gno/main_test.go:131 Error: Should be false Test: TestModApp/mod_tidy..~..~tests~integ~invalid_module_version1 Messages: should not panic ``` After: ``` === RUN TestModApp/mod_tidy..~..~tests~integ~invalid_module_version1 main_test.go:131: err main_test.go:132: Error Trace: /Users/norman/Code/gno/gnovm/cmd/gno/main_test.go:132 Error: Expected value not to be nil. Test: TestModApp/mod_tidy..~..~tests~integ~invalid_module_version1 Messages: err shouldn't be nil ```
Contributors' checklist... - [ ] 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 - [ ] Provided any useful hints for running manual tests
Signed-off-by: Norman Meier --- gnovm/cmd/gno/main_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gnovm/cmd/gno/main_test.go b/gnovm/cmd/gno/main_test.go index 1797d0aede9..069c42db379 100644 --- a/gnovm/cmd/gno/main_test.go +++ b/gnovm/cmd/gno/main_test.go @@ -128,7 +128,7 @@ func testMainCaseRun(t *testing.T, tc []testMainCase) { if errShouldBeEmpty { require.Nil(t, err, "err should be nil") } else { - t.Log("err", err.Error()) + t.Log("err", fmt.Sprintf("%v", err)) require.NotNil(t, err, "err shouldn't be nil") if test.errShouldContain != "" { require.Contains(t, err.Error(), test.errShouldContain, "err should contain")