Skip to content

Commit

Permalink
Merge branch 'master' into mbeliaev/mypy
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGES
  • Loading branch information
beliaev-maksim committed Jun 28, 2022
2 parents ecb170b + 72c67ba commit ed1a39c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
0.22.0
------

* Add `passthrough` argument to `BaseResponse` object. See #557
* Fix type for the `mock`'s patcher object. See #556
* Add `passthrough` argument to `BaseResponse` object. See #557
* Fix `registries` leak. See #563

0.21.0
------
Expand Down
9 changes: 6 additions & 3 deletions responses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,6 @@ def get_wrapped(
Wrapped function
"""
if registry is not None:
responses._set_registry(registry)

assert_mock = std_mock.patch.object(
target=responses,
attribute="assert_all_requests_are_fired",
Expand All @@ -196,6 +193,9 @@ def get_wrapped(
@wraps(func)
async def wrapper(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]

if registry is not None:
responses._set_registry(registry)

with assert_mock, responses:
return await func(*args, **kwargs)

Expand All @@ -204,6 +204,9 @@ async def wrapper(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any: # type: ignore[misc]

if registry is not None:
responses._set_registry(registry)

with assert_mock, responses:
# set 'assert_all_requests_are_fired' temporarily for a single run.
# Mock automatically unsets to avoid leakage to another decorated
Expand Down
38 changes: 38 additions & 0 deletions responses/tests/test_registries.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,44 @@ def run():
assert_reset()


def test_set_registry_reversed():
"""See https://github.com/getsentry/responses/issues/563"""

class CustomRegistry(registries.FirstMatchRegistry):
pass

@responses.activate
def run():
# test that registry does not leak to another test
assert type(responses.mock.get_registry()) == registries.FirstMatchRegistry

@responses.activate(registry=CustomRegistry)
def run_with_registry():
assert type(responses.mock.get_registry()) == CustomRegistry

run()
run_with_registry()
assert_reset()


async def test_registry_async():
class CustomRegistry(registries.FirstMatchRegistry):
pass

@responses.activate
async def run():
# test that registry does not leak to another test
assert type(responses.mock.get_registry()) == registries.FirstMatchRegistry

@responses.activate(registry=CustomRegistry)
async def run_with_registry():
assert type(responses.mock.get_registry()) == CustomRegistry

await run()
await run_with_registry()
assert_reset()


def test_set_registry_context_manager():
def run():
class CustomRegistry(registries.FirstMatchRegistry):
Expand Down

0 comments on commit ed1a39c

Please sign in to comment.