Skip to content
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
9 changes: 6 additions & 3 deletions django_coverage_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,16 @@ def lines(self):
if extends:
continue

if token.contents == "comment":
# Set this before the extends-skip below: in an inheriting
# template a comment outside the blocks must still suppress
# measurement of its contents (issue #70).
comment = True

if extends and not inblock:
# In an inheriting template, ignore all tags outside of
# blocks.
continue

if token.contents == "comment":
comment = True
if token.contents.startswith("end"):
continue
elif token.contents in ("else", "empty"):
Expand Down
25 changes: 25 additions & 0 deletions tests/test_extends.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,31 @@ def test_inheriting_with_unused_blocks(self):
self.assert_analysis([1, 2, 3], name="base.html")
self.assert_analysis([1, 4, 8], [8], name="specific.html")

def test_inheriting_with_comment_outside_blocks(self):
# https://github.com/coveragepy/django_coverage_plugin/issues/70
# A {% comment %} outside the blocks of an inheriting template was
# skipped before the comment flag was set, so {{ vars }} inside the
# comment were measured as executable lines that can never run.
self.make_template(name="base.html", text="""\
Hello
{% block second_line %}second{% endblock %}
""")

self.make_template(name="specific.html", text="""\
{% extends "base.html" %}
{% comment %}
{{ this.line.was.reported.missing }}
{% endcomment %}
{% block second_line %}
SECOND
{% endblock %}
""")

text = self.run_django_coverage(name="specific.html")
self.assertEqual(text, "Hello\n\nSECOND\n\n")
self.assert_analysis([1, 2], name="base.html")
self.assert_analysis([1, 6], name="specific.html")

def test_empty_parent_block_on_new_line_when_extended(self):
"""
When a block is empty and extended, endblock should not appear
Expand Down