Skip to content

Commit

Permalink
Adapt patch for pytest < 6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
xavfernandez committed Oct 26, 2023
1 parent 86a93e7 commit 714dd8f
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,20 +628,26 @@ def __mod__(self, var: str) -> str:
else:
return msg

with pytest.MonkeyPatch.context() as mp:
if (
os.environ.get(INVALID_TEMPLATE_VARS_ENV, "false") == "true"
and django_settings_is_configured()
):
from django.conf import settings as dj_settings
# TODO: use pytest.MonkeyPatch once pytest<6.2 is not supported anymore
NOT_SET = object()
changed = False
previous_value = NOT_SET
if (
os.environ.get(INVALID_TEMPLATE_VARS_ENV, "false") == "true"
and django_settings_is_configured()
):
from django.conf import settings as dj_settings

if dj_settings.TEMPLATES:
mp.setitem(
dj_settings.TEMPLATES[0]["OPTIONS"],
"string_if_invalid",
InvalidVarException(),
)
yield
if dj_settings.TEMPLATES:
previous_value = dj_settings.TEMPLATES[0]["OPTIONS"].get("string_if_invalid", NOT_SET)
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = InvalidVarException()
changed = True
yield
if changed:
if previous_value is NOT_SET:
del dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"]
else:
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = previous_value


@pytest.fixture(autouse=True)
Expand Down

0 comments on commit 714dd8f

Please sign in to comment.