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

mbeliaev/patcher #500

Merged
merged 5 commits into from Feb 22, 2022
Merged

Conversation

beliaev-maksim
Copy link
Collaborator

Closes #481

that was a tricky one, after research I found the root cause. Since default decorator always uses mock = _default_mock, when we apply double patching, we override the value of the _patcher. When standard python mock is trying to stop the patcher, it fails, since we have another object in the memory.

Keeping only single mock for all nested functions is sufficient enough. Thus, prevent from override and do clean up on the __exit__.

@codecov
Copy link

codecov bot commented Feb 10, 2022

Codecov Report

Merging #500 (8377dc5) into master (4c4b46b) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #500   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            6         6           
  Lines         2162      2178   +16     
=========================================
+ Hits          2162      2178   +16     
Impacted Files Coverage Δ
responses/__init__.py 100.00% <100.00%> (ø)
responses/test_responses.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4c4b46b...8377dc5. Read the comment docs.

@@ -809,11 +810,16 @@ def start(self):
def unbound_on_send(adapter, request, *a, **kwargs):
return self._on_request(adapter, request, *a, **kwargs)

self._patcher = std_mock.patch(target=self.target, new=unbound_on_send)
self._patcher.start()
if not self._patcher:
Copy link
Member

Choose a reason for hiding this comment

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

Should the function on line 810 only be defined when we're creating a patcher as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

confirm, let's save some runtime on it and just return if patcher already exists

@markstory markstory merged commit a35896e into getsentry:master Feb 22, 2022
@crazyscientist
Copy link

Wow, thanks for fixing this 🥳

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.

Mocking is not completely undone when decorated tests are nested
3 participants