Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure InvalidVarException.fail is reset #1076

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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

Check warning on line 650 in pytest_django/plugin.py

View check run for this annotation

Codecov / codecov/patch

pytest_django/plugin.py#L650

Added line #L650 was not covered by tests


@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 @@
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