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

单测:将gomonkey替换为xgo #305

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

xhd2015
Copy link

@xhd2015 xhd2015 commented Apr 3, 2024

在这个PR中, 我使用xgo替换了gomonkey。

xgo相对于gomonkey具有如下优点:

  • 不依赖asm, 纯go实现,兼容所有的OS和架构
  • 并发安全,多组用例同时mock,不会相互影响
  • API更加简单,不需要defer patch.Reset()等代码

xgo的项目地址: https://github.com/xhd2015/xgo

使用xgo进行单测的步骤:

  1. 安装
# macOS and Linux (and WSL)
curl -fsSL https://github.com/xhd2015/xgo/raw/master/install.sh | bash

# windows
powershell -c "irm github.com/xhd2015/xgo/raw/master/install.ps1|iex"
  1. 执行test
xgo test ./...

测试结果:

 xgo test ./...
?       github.com/apolloconfig/agollo/v4/agcache       [no test files]
?       github.com/apolloconfig/agollo/v4/cluster       [no test files]
?       github.com/apolloconfig/agollo/v4/constant      [no test files]
?       github.com/apolloconfig/agollo/v4/env/file      [no test files]
?       github.com/apolloconfig/agollo/v4/protocol/auth [no test files]
?       github.com/apolloconfig/agollo/v4/utils/parse   [no test files]
ok      github.com/apolloconfig/agollo/v4       23.068s
ok      github.com/apolloconfig/agollo/v4/agcache/memory        0.824s
ok      github.com/apolloconfig/agollo/v4/cluster/roundrobin    1.206s
ok      github.com/apolloconfig/agollo/v4/component     1.743s
ok      github.com/apolloconfig/agollo/v4/component/log 1.390s
ok      github.com/apolloconfig/agollo/v4/component/notify      2.106s
ok      github.com/apolloconfig/agollo/v4/component/remote      29.777s
ok      github.com/apolloconfig/agollo/v4/component/serverlist  2.570s
ok      github.com/apolloconfig/agollo/v4/env   3.793s
ok      github.com/apolloconfig/agollo/v4/env/config    2.712s
ok      github.com/apolloconfig/agollo/v4/env/config/json       2.413s
ok      github.com/apolloconfig/agollo/v4/env/file/json 2.229s
ok      github.com/apolloconfig/agollo/v4/env/server    2.342s
ok      github.com/apolloconfig/agollo/v4/extension     2.274s
ok      github.com/apolloconfig/agollo/v4/protocol/auth/sign    2.305s
ok      github.com/apolloconfig/agollo/v4/protocol/http 26.223s
ok      github.com/apolloconfig/agollo/v4/storage       4.719s
ok      github.com/apolloconfig/agollo/v4/utils 1.747s
ok      github.com/apolloconfig/agollo/v4/utils/parse/normal    1.769s
ok      github.com/apolloconfig/agollo/v4/utils/parse/properties        1.915s
ok      github.com/apolloconfig/agollo/v4/utils/parse/yaml      1.854s
ok      github.com/apolloconfig/agollo/v4/utils/parse/yml       1.905s

Summary by CodeRabbit

  • Tests
    • Updated mocking library in test files for improved testing reliability.

Copy link

coderabbitai bot commented Apr 3, 2024

Walkthrough

This update signifies a shift in the testing framework used within the codebase, transitioning from gomonkey to a more recent library, xgo/runtime/mock, for mocking dependencies in tests. The primary focus is on enhancing the testing strategy by adopting a new tool that potentially offers better functionality or compatibility with the project's needs. This change impacts how mocks are created and used, making tests more maintainable or aligning with updated best practices.

Changes

File(s) Summary of Changes
.../client_test.go Replaced gomonkey imports with xgo/runtime/mock; updated test functions to use mock.Patch.

🐇✨
In the realm of code where tests do play,
A rabbit hopped, finding a better way.
From gomonkey vines to xgo's bright moon,
Patching and mocking in harmonious tune.
With each leap and bound, bugs scatter and swoon.
🌟🐾

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

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.

Copy link
Contributor

mergify bot commented Apr 3, 2024

感谢您提出Pull Request,我会尽快Review。我会在1-2日内进行查看或者回复,如果遇到节假日可能会处理较慢,敬请谅解。

Copy link

@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: 0

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 9a87bea and e292702.
Files ignored due to path filters (2)
  • go.mod is excluded by !**/*.mod
  • go.sum is excluded by !**/*.sum
Files selected for processing (1)
  • client_test.go (4 hunks)
Additional comments not posted (3)
client_test.go (3)

28-28: The change in the import statement from gomonkey to xgo aligns with the PR's objectives and leverages xgo's benefits.


360-360: The modification to use mock.Patch for mocking in TestGetConfigAndInitValNotNil effectively leverages xgo's capabilities.


379-379: The modification to use mock.Patch for mocking in TestGetConfigAndInitValNil effectively leverages xgo's capabilities.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant