Skip to content

Commit

Permalink
Make sure InvalidVarException.fail is reset (#1076)
Browse files Browse the repository at this point in the history
after a test using ignore_template_errors marker
  • Loading branch information
xavfernandez committed Oct 26, 2023
1 parent a4ea66d commit 4e1906a
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,18 +628,30 @@ def __mod__(self, var: str) -> str:
else:
return msg

# 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:
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)
def _template_string_if_invalid_marker(request) -> None:
def _template_string_if_invalid_marker(monkeypatch, request) -> None:
"""Apply the @pytest.mark.ignore_template_errors marker,
internal to pytest-django."""
marker = request.keywords.get("ignore_template_errors", None)
Expand All @@ -648,7 +660,11 @@ def _template_string_if_invalid_marker(request) -> None:
from django.conf import settings as dj_settings

if dj_settings.TEMPLATES:
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"].fail = False
monkeypatch.setattr(
dj_settings.TEMPLATES[0]["OPTIONS"]["string_if_invalid"],
"fail",
False,
)


@pytest.fixture(autouse=True, scope="function")
Expand Down
41 changes: 41 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,47 @@ def test_ignore(client):
)


@pytest.mark.django_project(
extra_settings="""
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
"""
)
def test_invalid_template_variable_marker_cleanup(django_testdir) -> None:
django_testdir.create_app_file(
"<div>{{ invalid_var }}</div>", "templates/invalid_template_base.html"
)
django_testdir.create_app_file(
"{% include 'invalid_template_base.html' %}", "templates/invalid_template.html"
)
django_testdir.create_test_module(
"""
from django.template.loader import render_to_string
import pytest
@pytest.mark.ignore_template_errors
def test_ignore(client):
render_to_string('invalid_template.html')
def test_for_invalid_template(client):
render_to_string('invalid_template.html')
"""
)
result = django_testdir.runpytest_subprocess("-s", "--fail-on-template-vars")

origin = "'*/tpkg/app/templates/invalid_template_base.html'"
result.stdout.fnmatch_lines_random(
[
"tpkg/test_the_test.py .F*",
f"E * Failed: Undefined template variable 'invalid_var' in {origin}",
]
)


@pytest.mark.django_project(
extra_settings="""
TEMPLATE_LOADERS = (
Expand Down

0 comments on commit 4e1906a

Please sign in to comment.