Skip to content

Commit b0a833e

Browse files
committed
fix: skipped tags falsely reported as uncovered when not at start of line
When a tag ({% endif %}, {% endfor %}, {% else %}, etc.) is not at the start of a line, the preceding TEXT token ends with a whitespace-only fragment with no newline terminator. That partial line is not executable content but was incorrectly added to source_lines. Extends the endblock fix to apply universally — built-in and user-defined end tags are all covered by the same condition.
1 parent b9bc324 commit b0a833e

3 files changed

Lines changed: 53 additions & 6 deletions

File tree

django_coverage_plugin/plugin.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,10 +349,10 @@ def lines(self):
349349
if lines[0].isspace():
350350
lineno += 1
351351
num_lines -= 1
352-
# When {% endblock %} is not at the start of a line, the
353-
# preceding TEXT token ends with whitespace and no newline.
352+
# When a tag is not at the start of a line, the preceding
353+
# TEXT token ends with whitespace and no newline.
354354
# That partial line is not executable content.
355-
if inblock and num_lines > 0 and (
355+
if num_lines > 0 and (
356356
lines[-1].isspace() and not lines[-1].endswith(("\n", "\r"))
357357
):
358358
num_lines -= 1

tests/test_flow.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,31 @@ def test_if(self):
2525
self.assertEqual(text.strip(), '')
2626
self.assert_analysis([1, 2], [2])
2727

28+
def test_endif_not_at_start_of_line(self):
29+
self.make_template("""\
30+
<article>
31+
{% if foo %}
32+
Hello
33+
{% endif %}
34+
After
35+
</article>
36+
""")
37+
self.run_django_coverage(context={'foo': False})
38+
self.assert_analysis([1, 2, 3, 5, 6], missing=[3])
39+
40+
def test_else_not_at_start_of_line(self):
41+
self.make_template("""\
42+
<article>
43+
{% if foo %}
44+
Hello
45+
{% else %}
46+
Goodbye
47+
{% endif %}
48+
</article>
49+
""")
50+
self.run_django_coverage(context={'foo': True})
51+
self.assert_analysis([1, 2, 3, 5, 7], missing=[5])
52+
2853
def test_if_else(self):
2954
self.make_template("""\
3055
{% if foo %}
@@ -84,6 +109,17 @@ def test_loop(self):
84109
self.assertEqual(text, "Before\n\nAfter\n")
85110
self.assert_analysis([1, 2, 3, 5], [3])
86111

112+
def test_endfor_not_at_start_of_line(self):
113+
self.make_template("""\
114+
<ul>
115+
{% for item in items %}
116+
<li>{{ item }}</li>
117+
{% endfor %}
118+
</ul>
119+
""")
120+
self.run_django_coverage(context={'items': []})
121+
self.assert_analysis([1, 2, 3, 5], missing=[3])
122+
87123
def test_loop_with_empty_clause(self):
88124
self.make_template("""\
89125
Before
@@ -135,7 +171,7 @@ def test_regroup(self):
135171
<ul><li>New York: 20</li><li>Chicago: 7</li></ul></li><li>Japan
136172
<ul><li>Tokyo: 33</li></ul></li></ul>
137173
"""))
138-
self.assert_analysis([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13])
174+
self.assert_analysis([1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 13])
139175

140176

141177
class IfChangedTest(DjangoPluginTestCase):
@@ -154,7 +190,7 @@ def test_ifchanged(self):
154190
'items': [("A", "X"), ("A", "Y"), ("B", "Z"), ("B", "W")],
155191
})
156192
self.assertEqual(squashed(text), 'AXYBZW')
157-
self.assert_analysis([1, 2, 3, 4, 5])
193+
self.assert_analysis([1, 2, 3, 5])
158194

159195
def test_ifchanged_variable(self):
160196
self.make_template("""\
@@ -170,7 +206,7 @@ def test_ifchanged_variable(self):
170206
'items': [("A", "X"), ("A", "Y"), ("B", "Z"), ("B", "W")],
171207
})
172208
self.assertEqual(squashed(text), 'AXYBZW')
173-
self.assert_analysis([1, 2, 3, 4, 5])
209+
self.assert_analysis([1, 2, 3, 5])
174210

175211

176212
@django_stop_before(4, 0)

tests/test_simple.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,17 @@ def test_with(self):
238238
self.assertEqual(text, "\nalpha = 1, beta = 2.\n\n")
239239
self.assert_analysis([1, 2])
240240

241+
def test_endwith_not_at_start_of_line(self):
242+
self.make_template("""\
243+
<div>
244+
{% with alpha=1 %}
245+
{{ alpha }}
246+
{% endwith %}
247+
</div>
248+
""")
249+
self.run_django_coverage()
250+
self.assert_analysis([1, 2, 3, 5])
251+
241252

242253
class StringTemplateTest(DjangoPluginTestCase):
243254

0 commit comments

Comments
 (0)