Skip to content

Commit 1ffc323

Browse files
committed
plugin: unconfigure should restore not unblock
`restore` pops the `block` in `_setup_django`, while `unblock` pushes an unblock. We want to leave things clean, so should `restore`.
1 parent 3fff7fc commit 1ffc323

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

pytest_django/plugin.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,13 @@ def get_order_number(test: pytest.Item) -> int:
477477

478478

479479
def pytest_unconfigure(config: pytest.Config) -> None:
480-
if blocking_manager_key not in config.stash:
481-
return
482-
blocking_manager = config.stash[blocking_manager_key]
483-
blocking_manager.unblock()
480+
# Undo the block() in _setup_django(), if it happenned.
481+
# It's also possible the user forgot to call restore().
482+
# We can warn about it, but let's just clean it up.
483+
if blocking_manager_key in config.stash:
484+
blocking_manager = config.stash[blocking_manager_key]
485+
while blocking_manager.is_active:
486+
blocking_manager.restore()
484487

485488

486489
@pytest.fixture(autouse=True, scope="session")
@@ -852,6 +855,11 @@ def restore(self) -> None:
852855
"""
853856
self._dj_db_wrapper.ensure_connection = self._history.pop()
854857

858+
@property
859+
def is_active(self) -> bool:
860+
"""Whether a block() or unblock() is currently active."""
861+
return bool(self._history)
862+
855863

856864
# On Config.stash.
857865
blocking_manager_key = pytest.StashKey[DjangoDbBlocker]()

0 commit comments

Comments
 (0)