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

fail-on-template-vars: improve compatibility with Django behavior #1130

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,14 @@ def _get_origin() -> str | None:
return name
return None

def __bool__(self) -> bool:
for frame_info in inspect.stack():
if frame_info.function == "resolve" and frame_info.filename.endswith("base.py"):
# To go through this guard:
# https://github.com/django/django/blob/5.0.7/django/template/base.py#L716
return True
return bool(self.origin_value)

def __mod__(self, var: str) -> str:
origin = self._get_origin()
if origin:
Expand Down
42 changes: 42 additions & 0 deletions tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,48 @@ def test_for_invalid_template(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_object_does_not_exists_behaves_like_an_empty_string(
django_pytester: DjangoPytester,
) -> None:
django_pytester.create_app_file(
"<div>{% if object_exists %}This should not appear{% endif %}</div>",
"templates/invalid_template_base.html",
)
django_pytester.create_app_file(
"{% include 'invalid_template_base.html' %}",
"templates/invalid_template.html",
)
django_pytester.create_test_module(
"""
from django.core.exceptions import ObjectDoesNotExist
from django.template.loader import render_to_string

import pytest

def fake_one_to_one_relation_missing():
raise ObjectDoesNotExist()

def test_ignore():
assert render_to_string(
'invalid_template.html',
{"object_exists": fake_one_to_one_relation_missing},
) == "<div></div>"
"""
)

result = django_pytester.runpytest_subprocess("-s", "--fail-on-template-vars")

result.assert_outcomes(passed=1)


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