-
-
Notifications
You must be signed in to change notification settings - Fork 707
test(tools): auto-patch release CLI helpers in conftest.py #3939
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import pytest | ||
|
|
||
| pytest_plugins = ["tests.tools.private.release.release_test_helper"] | ||
|
|
||
|
|
||
| @pytest.fixture(name="auto_patch_cmd_helpers", autouse=True) | ||
| def fixture_auto_patch_cmd_helpers(mocker): | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be out of scope, but why use |
||
| """Automatically patches run_cmd, Git, and GitHub CLI helpers. | ||
|
|
||
| This prevents tests from executing command line tools that could have | ||
| side-effects. | ||
| """ | ||
| mock_run_cmd = mocker.patch("tools.private.release.shell.run_cmd") | ||
| mocker.patch("tools.private.release.git.run_cmd", mock_run_cmd) | ||
| mocker.patch("tools.private.release.gh.run_cmd", mock_run_cmd) | ||
| mock_run_git = mocker.patch("tools.private.release.git.Git._run_git") | ||
| mock_run_gh = mocker.patch("tools.private.release.gh.GitHub._run_gh") | ||
| return { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend returning a dataclass instead of dict - helps with type checking (do we do type checking in this project? 🤣 )
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is a |
||
| "run_cmd": mock_run_cmd, | ||
| "run_git": mock_run_git, | ||
| "run_gh": mock_run_gh, | ||
| } | ||
|
rickeylev marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,3 +59,23 @@ def test_resolve_pr_number_invalid(mocker, gh): | |
| with pytest.raises(ValueError, match="Could not resolve PR reference"): | ||
| gh.resolve_pr_number("invalid-ref") | ||
| mock_run_cmd.assert_not_called() | ||
|
|
||
|
|
||
| def test_auto_patched_helpers_prevent_real_execution(auto_patch_cmd_helpers): | ||
| from tools.private.release.gh import GitHub | ||
| from tools.private.release.git import Git | ||
| from tools.private.release.shell import run_cmd | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
|
||
| # Calling run_cmd directly hits the mock | ||
| run_cmd("echo", "test") | ||
| auto_patch_cmd_helpers["run_cmd"].assert_called_with("echo", "test") | ||
|
|
||
| # Git._run_git hits the mock | ||
| git = Git(".") | ||
| git._run_git("status") | ||
| auto_patch_cmd_helpers["run_git"].assert_called_with("status") | ||
|
|
||
| # GitHub._run_gh hits the mock | ||
| gh_obj = GitHub("foo/bar") | ||
| gh_obj._run_gh("issue", "list") | ||
| auto_patch_cmd_helpers["run_gh"].assert_called_with("issue", "list") | ||
|
rickeylev marked this conversation as resolved.
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Optional Nit: More idiomatic
pytestwould be to have a single fixture for each item, and then a 4th fixture to collect them all.