Skip to content

Commit 3c46471

Browse files
authored
fail-on-template-vars: be more discreet with ignore_template_errors (#1121)
1 parent 5bf88be commit 3c46471

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

pytest_django/plugin.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -644,8 +644,9 @@ def _fail_for_invalid_template_variable():
644644
class InvalidVarException:
645645
"""Custom handler for invalid strings in templates."""
646646

647-
def __init__(self) -> None:
647+
def __init__(self, *, origin_value: str) -> None:
648648
self.fail = True
649+
self.origin_value = origin_value
649650

650651
def __contains__(self, key: str) -> bool:
651652
return key == "%s"
@@ -696,7 +697,7 @@ def __mod__(self, var: str) -> str:
696697
if self.fail:
697698
pytest.fail(msg)
698699
else:
699-
return msg
700+
return self.origin_value
700701

701702
with pytest.MonkeyPatch.context() as mp:
702703
if (
@@ -709,7 +710,11 @@ def __mod__(self, var: str) -> str:
709710
mp.setitem(
710711
dj_settings.TEMPLATES[0]["OPTIONS"],
711712
"string_if_invalid",
712-
InvalidVarException(),
713+
InvalidVarException(
714+
origin_value=dj_settings.TEMPLATES[0]["OPTIONS"].get(
715+
"string_if_invalid", ""
716+
)
717+
),
713718
)
714719
yield
715720

tests/test_environment.py

+44
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,50 @@ def test_for_invalid_template(client):
144144
)
145145

146146

147+
@pytest.mark.django_project(
148+
extra_settings="""
149+
TEMPLATE_LOADERS = (
150+
'django.template.loaders.filesystem.Loader',
151+
'django.template.loaders.app_directories.Loader',
152+
)
153+
TEMPLATES[0]["OPTIONS"]["string_if_invalid"] = "Something clever"
154+
"""
155+
)
156+
def test_invalid_template_variable_behaves_normally_when_ignored(
157+
django_pytester: DjangoPytester
158+
) -> None:
159+
django_pytester.create_app_file(
160+
"<div>{{ invalid_var }}</div>", "templates/invalid_template_base.html"
161+
)
162+
django_pytester.create_app_file(
163+
"{% include 'invalid_template_base.html' %}", "templates/invalid_template.html"
164+
)
165+
django_pytester.create_test_module(
166+
"""
167+
from django.template.loader import render_to_string
168+
169+
import pytest
170+
171+
@pytest.mark.ignore_template_errors
172+
def test_ignore(client):
173+
assert render_to_string('invalid_template.html') == "<div>Something clever</div>"
174+
175+
def test_for_invalid_template(client):
176+
render_to_string('invalid_template.html')
177+
178+
"""
179+
)
180+
result = django_pytester.runpytest_subprocess("-s", "--fail-on-template-vars")
181+
182+
origin = "'*/tpkg/app/templates/invalid_template_base.html'"
183+
result.stdout.fnmatch_lines_random(
184+
[
185+
"tpkg/test_the_test.py .F*",
186+
f"E * Failed: Undefined template variable 'invalid_var' in {origin}",
187+
]
188+
)
189+
190+
147191
@pytest.mark.django_project(
148192
extra_settings="""
149193
TEMPLATE_LOADERS = (

0 commit comments

Comments
 (0)