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

test: fix failing tests after protocompat changes #22889

Merged
merged 4 commits into from
Dec 16, 2024
Merged

test: fix failing tests after protocompat changes #22889

merged 4 commits into from
Dec 16, 2024

Conversation

julienrbrt
Copy link
Member

@julienrbrt julienrbrt commented Dec 16, 2024

Description

Follow-up of #22866

Fixes:

https://github.com/cosmos/cosmos-sdk/actions/runs/12337602773/job/34431523563?pr=22864 https://github.com/cosmos/cosmos-sdk/actions/runs/12344400459/job/34447459757?pr=22873


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • New Features

    • Introduced a new BankKeeper interface for enhanced testing capabilities.
  • Bug Fixes

    • Improved error handling in hybrid handler functions for pointer manipulation.
    • Enhanced test suite for group keeper module to cover edge cases and error conditions.
  • Documentation

    • Updated method signatures in test files to reflect new expectations and functionality.
  • Chores

    • Removed obsolete mock implementation file to streamline testing utilities.

Copy link
Contributor

coderabbitai bot commented Dec 16, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

This pull request introduces modifications across multiple files in the Cosmos SDK, focusing on enhancing error handling, testing utilities, and mock implementations. The changes primarily affect the protocompat, authz, and group modules. Key modifications include updating the setPointer function to return errors, refactoring bank keeper mock implementations, and updating test suites to improve error checking and simulation capabilities.

Changes

File Change Summary
baseapp/internal/protocompat/protocompat.go Updated setPointer function to return errors and improve pointer handling
scripts/mockgen.sh Modified mock generation path for x/authz package
x/authz/keeper/keeper_test.go Added expectation for Send method in bank keeper mock
x/authz/testutil/bank_helpers.go Deleted file with mock bank server implementation
x/authz/testutil/expected_keepers.go New file introducing BankKeeper interface
x/authz/testutil/expected_keepers_mocks.go Updated mock methods and import paths
x/group/keeper/msg_server_test.go Enhanced test cases for group policies and proposals
x/group/testutil/expected_keepers.go Repositioned SpendableCoins method in BankKeeper interface

Possibly related PRs

Suggested labels

C:Simulations, C:server/v2

Suggested reviewers

  • tac0turtle
  • akhilkumarpilli
  • sontrinh16
  • facundomedica

📜 Recent review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 9f379ff and f552f81.

📒 Files selected for processing (1)
  • x/authz/testutil/expected_keepers_mocks.go (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • x/authz/testutil/expected_keepers_mocks.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -235,6 +233,20 @@ func ResponseFullNameFromMethodDesc(sd *grpc.ServiceDesc, method grpc.MethodDesc

// since proto.Merge breaks due to the custom cosmos sdk any, we are forced to do this ugly setPointer hack.
// ref: https://github.com/cosmos/cosmos-sdk/issues/22779
func setPointer(dst, src any) {
reflect.ValueOf(dst).Elem().Set(reflect.ValueOf(src).Elem())
func setPointer(dst, src any) error {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

took this from the original PR coderabbit suggestion, as it best if we don't panic.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense!

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
x/authz/testutil/expected_keepers_mocks.go (1)

16-17: Use descriptive aliases for imported packages

The aliases types and types0 may cause confusion. Consider using more specific aliases like banktypes and sdktypes for clarity.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 234cdc4 and 08a1bb3.

📒 Files selected for processing (8)
  • baseapp/internal/protocompat/protocompat.go (3 hunks)
  • scripts/mockgen.sh (1 hunks)
  • x/authz/keeper/keeper_test.go (1 hunks)
  • x/authz/testutil/bank_helpers.go (0 hunks)
  • x/authz/testutil/expected_keepers.go (1 hunks)
  • x/authz/testutil/expected_keepers_mocks.go (4 hunks)
  • x/group/keeper/msg_server_test.go (14 hunks)
  • x/group/testutil/expected_keepers.go (1 hunks)
💤 Files with no reviewable changes (1)
  • x/authz/testutil/bank_helpers.go
🧰 Additional context used
📓 Path-based instructions (6)
x/group/testutil/expected_keepers.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

baseapp/internal/protocompat/protocompat.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/authz/testutil/expected_keepers.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/authz/keeper/keeper_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

x/authz/testutil/expected_keepers_mocks.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/group/keeper/msg_server_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

🔇 Additional comments (7)
x/authz/testutil/expected_keepers.go (1)

1-19: Code conforms to Go conventions

The BankKeeper interface is well-defined and follows Go best practices.

x/group/testutil/expected_keepers.go (1)

35-35: Placement of SpendableCoins method is appropriate

The SpendableCoins method is correctly positioned within the BankKeeper interface.

scripts/mockgen.sh (1)

18-18: LGTM: Mock generation path updated correctly

The path update for authz mock generation aligns with the new location of expected_keepers.go in the testutil directory.

baseapp/internal/protocompat/protocompat.go (3)

236-251: Improved error handling in setPointer function

The function now properly validates pointers and returns errors instead of potentially panicking. The validation includes:

  • Checking for valid values
  • Ensuring non-nil pointers
  • Verifying type compatibility

163-164: Consistent error handling pattern applied

The error handling pattern is consistently applied across both code paths in the hybrid handler.

Also applies to: 168-169


126-126: Verify error handling in makeGogoHybridHandler

The handler now properly propagates errors from setPointer, but we should verify that all callers handle these errors appropriately.

Also applies to: 131-132

✅ Verification successful

Error handling for setPointer is properly implemented

The verification shows that all calls to setPointer are properly error-checked:

  • In the first handler path, the error from setPointer is returned via the closure and checked with if err != nil
  • In the second handler path (gogo handler), errors are similarly propagated and checked
  • The final setPointer calls in both paths directly return the error to the caller
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for all callers of setPointer to verify error handling
ast-grep --pattern 'err := setPointer($_, $_)
  if err != nil {
    $$$
  }'

Length of output: 78


Script:

#!/bin/bash
# Let's first check all usages of setPointer function
rg "setPointer\(" -A 3

# Also check the implementation of setPointer to understand its error handling
ast-grep --pattern 'func setPointer($_, $_) error {
  $$$
}'

Length of output: 1475


Script:

#!/bin/bash
# Let's check the surrounding context of setPointer usages to verify error handling
rg "setPointer\(" -B 3 -A 5 baseapp/internal/protocompat/protocompat.go

# Also check the full implementation of makeGogoHybridHandler to understand the error flow
ast-grep --pattern 'func makeGogoHybridHandler($$$) {
  $$$
}'

Length of output: 1369

x/authz/keeper/keeper_test.go (1)

75-75: LGTM: Improved mock expectations

The mock now returns a proper MsgSendResponse instead of nil, which better reflects the actual behavior of the bank keeper's Send method.

@@ -2891,7 +2891,7 @@ func (s *TestSuite) TestExecPrunedProposalsAndVotes() {
}

msgs := []sdk.Msg{msgSend1, msgSend2}
s.bankKeeper.EXPECT().Send(gomock.Any(), msgSend1).Return(nil, errors.New("error"))
s.bankKeeper.EXPECT().Send(gomock.Any(), msgSend1).Return(&banktypes.MsgSendResponse{}, errors.New("error"))
Copy link
Contributor

@coderabbitai coderabbitai bot Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Correct the return values when mocking Send with an error

When mocking s.bankKeeper.EXPECT().Send to return an error, the response should be nil. Currently, it returns a non-nil MsgSendResponse alongside an error, which is inconsistent with Go conventions.

Apply this diff to fix the test:

-s.bankKeeper.EXPECT().Send(gomock.Any(), msgSend1).Return(&banktypes.MsgSendResponse{}, errors.New("error"))
+s.bankKeeper.EXPECT().Send(gomock.Any(), msgSend1).Return(nil, errors.New("error"))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
s.bankKeeper.EXPECT().Send(gomock.Any(), msgSend1).Return(&banktypes.MsgSendResponse{}, errors.New("error"))
s.bankKeeper.EXPECT().Send(gomock.Any(), msgSend1).Return(nil, errors.New("error"))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julienrbrt, understood. Let me know if there's anything else I can assist with.

(_/)
( •_•)
(/ )

scripts/mockgen.sh Outdated Show resolved Hide resolved
@julienrbrt julienrbrt enabled auto-merge December 16, 2024 14:04
@@ -235,6 +233,20 @@ func ResponseFullNameFromMethodDesc(sd *grpc.ServiceDesc, method grpc.MethodDesc

// since proto.Merge breaks due to the custom cosmos sdk any, we are forced to do this ugly setPointer hack.
// ref: https://github.com/cosmos/cosmos-sdk/issues/22779
func setPointer(dst, src any) {
reflect.ValueOf(dst).Elem().Set(reflect.ValueOf(src).Elem())
func setPointer(dst, src any) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worthwhile to test this function to ensure no regressions. Is there a function where it is already indirectly exercised?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the failing tests :D

@julienrbrt julienrbrt added this pull request to the merge queue Dec 16, 2024
Merged via the queue into main with commit cfe1940 Dec 16, 2024
76 of 77 checks passed
@julienrbrt julienrbrt deleted the julien/pc branch December 16, 2024 16:14
mergify bot pushed a commit that referenced this pull request Dec 16, 2024
Co-authored-by: Marko <[email protected]>
(cherry picked from commit cfe1940)

# Conflicts:
#	x/authz/testutil/expected_keepers_mocks.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:x/authz C:x/group Type: Build
Projects
None yet
Development

Successfully merging this pull request may close these issues.