Skip to content

Commit

Permalink
test(cmd/gno): prevent nil deref in testMainCaseRun (#3071)
Browse files Browse the repository at this point in the history
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/[email protected]/src/runtime/panic.go:770
        	            				/Users/norman/go/pkg/mod/golang.org/[email protected]/src/runtime/panic.go:261
        	            				/Users/norman/go/pkg/mod/golang.org/[email protected]/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 <nil>
    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
```

<details><summary>Contributors' checklist...</summary>

- [ ] 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
</details>

Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz authored Nov 6, 2024
1 parent 538ebff commit f2928f1
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion gnovm/cmd/gno/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f2928f1

Please sign in to comment.