Skip to content

Commit 4e1906a

Browse files
authored
Make sure InvalidVarException.fail is reset (#1076)
after a test using ignore_template_errors marker
1 parent a4ea66d commit 4e1906a

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

pytest_django/plugin.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,18 +628,30 @@ def __mod__(self, var: str) -> str:
628628
else:
629629
return msg
630630

631+
# TODO: use pytest.MonkeyPatch once pytest<6.2 is not supported anymore
632+
NOT_SET = object()
633+
changed = False
634+
previous_value = NOT_SET
631635
if (
632636
os.environ.get(INVALID_TEMPLATE_VARS_ENV, "false") == "true"
633637
and django_settings_is_configured()
634638
):
635639
from django.conf import settings as dj_settings
636640

637641
if dj_settings.TEMPLATES:
642+
previous_value = dj_settings.TEMPLATES[0]["OPTIONS"].get("string_if_invalid", NOT_SET)
638643
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = InvalidVarException()
644+
changed = True
645+
yield
646+
if changed:
647+
if previous_value is NOT_SET:
648+
del dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"]
649+
else:
650+
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = previous_value
639651

640652

641653
@pytest.fixture(autouse=True)
642-
def _template_string_if_invalid_marker(request) -> None:
654+
def _template_string_if_invalid_marker(monkeypatch, request) -> None:
643655
"""Apply the @pytest.mark.ignore_template_errors marker,
644656
internal to pytest-django."""
645657
marker = request.keywords.get("ignore_template_errors", None)
@@ -648,7 +660,11 @@ def _template_string_if_invalid_marker(request) -> None:
648660
from django.conf import settings as dj_settings
649661

650662
if dj_settings.TEMPLATES:
651-
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"].fail = False
663+
monkeypatch.setattr(
664+
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"],
665+
"fail",
666+
False,
667+
)
652668

653669

654670
@pytest.fixture(autouse=True, scope="function")

tests/test_environment.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,47 @@ def test_ignore(client):
101101
)
102102

103103

104+
@pytest.mark.django_project(
105+
extra_settings="""
106+
TEMPLATE_LOADERS = (
107+
'django.template.loaders.filesystem.Loader',
108+
'django.template.loaders.app_directories.Loader',
109+
)
110+
"""
111+
)
112+
def test_invalid_template_variable_marker_cleanup(django_testdir) -> None:
113+
django_testdir.create_app_file(
114+
"<div>{{ invalid_var }}</div>", "templates/invalid_template_base.html"
115+
)
116+
django_testdir.create_app_file(
117+
"{% include 'invalid_template_base.html' %}", "templates/invalid_template.html"
118+
)
119+
django_testdir.create_test_module(
120+
"""
121+
from django.template.loader import render_to_string
122+
123+
import pytest
124+
125+
@pytest.mark.ignore_template_errors
126+
def test_ignore(client):
127+
render_to_string('invalid_template.html')
128+
129+
def test_for_invalid_template(client):
130+
render_to_string('invalid_template.html')
131+
132+
"""
133+
)
134+
result = django_testdir.runpytest_subprocess("-s", "--fail-on-template-vars")
135+
136+
origin = "'*/tpkg/app/templates/invalid_template_base.html'"
137+
result.stdout.fnmatch_lines_random(
138+
[
139+
"tpkg/test_the_test.py .F*",
140+
f"E * Failed: Undefined template variable 'invalid_var' in {origin}",
141+
]
142+
)
143+
144+
104145
@pytest.mark.django_project(
105146
extra_settings="""
106147
TEMPLATE_LOADERS = (

0 commit comments

Comments
 (0)