Skip to content

Commit

Permalink
Avoid _blocking_manager mutable global
Browse files Browse the repository at this point in the history
Scope it to the pytest config at least.
  • Loading branch information
bluetech committed Nov 7, 2023
1 parent d93631f commit 2414995
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ def find_django_path(args) -> pathlib.Path | None:
return PROJECT_NOT_FOUND


def _setup_django() -> None:
def _setup_django(config: pytest.Config) -> None:
if "django" not in sys.modules:
return

Expand All @@ -235,7 +235,8 @@ def _setup_django() -> None:
if not django.apps.apps.ready:
django.setup()

_blocking_manager.block()
blocking_manager = config.stash[blocking_manager_key]
blocking_manager.block()


def _get_boolean_value(
Expand Down Expand Up @@ -354,14 +355,16 @@ def _get_option_with_source(
with _handle_import_error(_django_project_scan_outcome):
dj_settings.DATABASES # noqa: B018

_setup_django()
early_config.stash[blocking_manager_key] = DjangoDbBlocker(_ispytest=True)

_setup_django(early_config)


@pytest.hookimpl(trylast=True)
def pytest_configure() -> None:
def pytest_configure(config: pytest.Config) -> None:
# Allow Django settings to be configured in a user pytest_configure call,
# but make sure we call django.setup()
_setup_django()
_setup_django(config)


@pytest.hookimpl()
Expand Down Expand Up @@ -478,7 +481,7 @@ def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, N
we need to follow this model.
"""
if django_settings_is_configured():
_setup_django()
_setup_django(request.config)
from django.test.utils import setup_test_environment, teardown_test_environment

debug_ini = request.config.getini("django_debug_mode")
Expand All @@ -496,7 +499,7 @@ def django_test_environment(request: pytest.FixtureRequest) -> Generator[None, N


@pytest.fixture(scope="session")
def django_db_blocker() -> DjangoDbBlocker | None:
def django_db_blocker(request: pytest.FixtureRequest) -> DjangoDbBlocker | None:
"""Wrapper around Django's database access.
This object can be used to re-enable database access. This fixture is used
Expand All @@ -512,7 +515,8 @@ def django_db_blocker() -> DjangoDbBlocker | None:
if not django_settings_is_configured():
return None

return _blocking_manager
blocking_manager = request.config.stash[blocking_manager_key]
return blocking_manager


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -813,7 +817,8 @@ def restore(self) -> None:
self._dj_db_wrapper.ensure_connection = self._history.pop()


_blocking_manager = DjangoDbBlocker(_ispytest=True)
# On Config.stash.
blocking_manager_key = pytest.StashKey[DjangoDbBlocker]()


def validate_urls(marker) -> list[str]:
Expand Down

0 comments on commit 2414995

Please sign in to comment.