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

[fact] Refactor git module #983

Merged
merged 6 commits into from
Jan 16, 2022
Merged

[fact] Refactor git module #983

merged 6 commits into from
Jan 16, 2022

Conversation

algomaster99
Copy link
Contributor

Fixes #982

The changes refactor the git module. While restructuring it, I also made the following changes

  • Renamed Push to PushSpec to avoid confusion between push and Push.
  • Removed underscore from all methods and prepended them in packages. We know which methods are "public" because they are exposed via __init__.py.
  • Shift global variables CONCURRENT_TASKS and _EMPTY_REPO_ERROR inside the only function they were used.

- Remove underscore from all methods in git if existed
- Push -> PushSpec
@algomaster99
Copy link
Contributor Author

algomaster99 commented Jan 5, 2022

@slarse I am unsure why the tests are failing. I tried to debug test_tries_all_calls_despite_exceptions and it tries cloning for each spec however it throws an exception in the process. It was somehow suppressing it before. Maybe I would have missed something while refactoring.

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

I think I found the problem, you ran into one of the gnarlier pitfalls of mocking in Python. Try my fixes and see if it works.

@@ -200,13 +200,13 @@ async def raise_(pt):
"Push failed", 128, b"some error", pt.repo_url
)

mocker.patch("_repobee.git._push_async", side_effect=raise_)
mocker.patch("_repobee.git.push_async", side_effect=raise_)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mocks work by monkeypatching the reference to the object. This is the wrong reference to mock, you need to mock the reference that's used in the module you're testing. It took me a long time to debug the first time I ran into this :)

Suggested change
mocker.patch("_repobee.git.push_async", side_effect=raise_)
mocker.patch("_repobee.git._push.push_async", side_effect=raise_)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the wrong reference to mock, you need to mock the reference that's used in the module you're testing.

I looked up what is monkey patching and I think I get the gist and use of it. But I don't think I get why this was the wrong reference and we need to provide an exact reference to the method used. Could you please explain "the reference that's used in the module you're testing" again?


successful_pts, failed_pts = git.push(push_tuples, tries=tries)

assert not successful_pts
assert failed_pts == push_tuples
git._push_async.assert_has_calls(expected_calls, any_order=True)
git.push_async.assert_has_calls(expected_calls, any_order=True)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
git.push_async.assert_has_calls(expected_calls, any_order=True)
git._push.push_async.assert_has_calls(expected_calls, any_order=True)

@@ -230,7 +230,7 @@ async def raise_(pt):
)

async_push_mock = mocker.patch(
"_repobee.git._push_async", side_effect=raise_once
"_repobee.git.push_async", side_effect=raise_once
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"_repobee.git.push_async", side_effect=raise_once
"_repobee.git._push.push_async", side_effect=raise_once

@@ -307,7 +307,7 @@ async def raise_(spec, *args, **kwargs):
)

clone_mock = mocker.patch(
"_repobee.git._clone_async", autospec=True, side_effect=raise_
"_repobee.git.clone_async", autospec=True, side_effect=raise_
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
"_repobee.git.clone_async", autospec=True, side_effect=raise_
"_repobee.git._fetch.clone_async", autospec=True, side_effect=raise_

CloneSpec,
CloneStatus,
clone,
clone_async,
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should not be exposed in the API of this package, as it was private in the module.

Suggested change
clone_async,

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I exposed it because it was directly used in the tests. I can make it private.

.. moduleauthor:: Simon Larsén
"""

from _repobee.git._push import PushSpec, push, push_async # NOQA
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should not be exposed in the API of this package, as it was private in the module.

Suggested change
from _repobee.git._push import PushSpec, push, push_async # NOQA
from _repobee.git._push import PushSpec, push # NOQA

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Nice!

Now I'd just suggest removing the moduleauthor directive from the module docstrings. It's super inconvenient trying to keep those up-to-date with multiple contributors.

Comment on lines 6 to 7

.. moduleauthor:: Simon Larsén
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.. moduleauthor:: Simon Larsén

Comment on lines 6 to 7

.. moduleauthor:: Simon Larsén
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.. moduleauthor:: Simon Larsén

Comment on lines 6 to 7

.. moduleauthor:: Simon Larsén
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.. moduleauthor:: Simon Larsén

Comment on lines 6 to 7

.. moduleauthor:: Simon Larsén
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
.. moduleauthor:: Simon Larsén

@algomaster99
Copy link
Contributor Author

@slarse I think we can keep the directive for now since the author has not changed. I would count myself as an author if I had made a fix or added a feature. If there were any authors before, then it makes sense to remove them.

@slarse
Copy link
Collaborator

slarse commented Jan 16, 2022

@algomaster99 I'm iteratively removing the moduleauthor directive from all modules, so please remove them. They invariably go out of date, for example you didn't add yourself here

Copy link
Collaborator

@slarse slarse left a comment

Choose a reason for hiding this comment

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

Thanks @algomaster99 , very nice refactoring!

@algomaster99
Copy link
Contributor Author

algomaster99 commented Jan 16, 2022

I'm iteratively removing the moduleauthor directive from all modules

Okay. I removed the directive from all files suggested above and also git.init.

for example you didn't add yourself here

#980 was also a pure refactoring so I didn't count myself as an author. :)

@slarse
Copy link
Collaborator

slarse commented Jan 16, 2022

I'm iteratively removing the moduleauthor directive from all modules

Okay. I removed the directive from all files suggested above and also git.init.

for example you didn't add yourself here

#980 was also a pure refactoring so I didn't count myself as an author. :)

I accidentally edited your comment instead of posting a reply...

Anyway, the way I see it, if you edit a file you're an author of that file. I think few devs disagree with that.

@slarse slarse merged commit f317eb9 into repobee:master Jan 16, 2022
@algomaster99 algomaster99 deleted the refactor-git-module branch January 16, 2022 11:31
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.

Convert git module into a package
2 participants