Skip to content

Commit

Permalink
Add cleanup to _fail_for_invalid_template_variable fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
xavfernandez committed Oct 26, 2023
1 parent 787ceef commit e71523a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,14 +628,20 @@ def __mod__(self, var: str) -> str:
else:
return msg

if (
os.environ.get(INVALID_TEMPLATE_VARS_ENV, "false") == "true"
and django_settings_is_configured()
):
from django.conf import settings as dj_settings
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

if dj_settings.TEMPLATES:
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = InvalidVarException()
if dj_settings.TEMPLATES:
mp.setitem(
dj_settings.TEMPLATES[0]["OPTIONS"],
"string_if_invalid",
InvalidVarException(),
)
yield


@pytest.fixture(autouse=True)
Expand Down

0 comments on commit e71523a

Please sign in to comment.