-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathtest_pragma.py
More file actions
147 lines (135 loc) · 4.83 KB
/
test_pragma.py
File metadata and controls
147 lines (135 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/coveragepy/django_coverage_plugin/blob/main/NOTICE.txt
"""Tests for {# pragma: no cover #} support in Django templates."""
import coverage
import pytest
from django.template.loader import get_template
from .plugin_test import DjangoPluginTestCase
class PragmaTest(DjangoPluginTestCase):
def test_pragma_on_block_tag_excludes_entire_block(self):
for condition in (True, False):
with self.subTest(condition=condition):
self.make_template("""\
Before
{% if condition %}{# pragma: no cover #}
{{ content }}
{% endif %}
After
""")
self.run_django_coverage(context={"condition": condition, "content": "hi"})
self.assert_analysis([1, 5])
def test_pragma_on_plain_text_line(self):
self.make_template("""\
Before
excluded line {# pragma: no cover #}
After
""")
self.run_django_coverage()
self.assert_analysis([1, 3])
def test_pragma_on_non_closing_tag(self):
"""Test that pragma does not treat tags like {% cycle %} as blocks."""
self.make_template("""\
<div>
{% cycle 'a' 'b' as values %}{# pragma: no cover #}
Covered
Not covered {{ values|last }}{# pragma: no cover #}
</div>
""")
self.run_django_coverage()
self.assert_analysis([1, 3, 5])
def test_pragma_with_nested_blocks(self):
self.make_template("""\
Before
{% if condition %}{# pragma: no cover #}
{% for item in items %}
{{ item }}
{% endfor %}
{% endif %}
After
""")
self.run_django_coverage(
context={"condition": True, "items": ["a", "b"]},
)
self.assert_analysis([1, 7])
def test_nested_if_blocks(self):
"""Test that nested block of same type are handled."""
self.make_template("""\
Before
{% if 1 %}{# pragma: no cover #}
{% if 0 %}
Not covered
{% endif %}
Also not covered, due to parent block.
{% endif %}
After
""")
self.run_django_coverage()
self.assert_analysis([1, 8])
def test_whitespace_around_pragma(self):
"""Test that whitespace characters are stripped."""
self.make_template("""\
Before
{% load static %}{# pragma: no cover #}
""")
self.run_django_coverage()
self.assert_analysis([1])
def test_custom_exclude_patterns(self):
"""Test that coverage.py config for report:exclude_lines is used."""
self.make_template("""\
Before
{% if condition %}{# noqa: no-cover #}
{{ content }}
{% endif %}
{% if not condition %} {# this block won't execute #}
{% now "SHORT_DATETIME_FORMAT" %} {# !SKIP ME! #}
{% lorem %}{# pragma: no cover #}{# I'm not covered because of custom exclude #}
{% endif %}
{% lorem %}{# I'm covered! #}
After
""")
self.make_file(
".coveragerc",
"""\
[run]
plugins = django_coverage_plugin
[report]
exclude_lines =
noqa: no-cover
!SKIP ME!
""",
)
tem = get_template(self.template_file)
self.cov = coverage.Coverage(source=["."])
self.cov.start()
tem.render({"condition": True, "content": "hi"})
self.cov.stop()
self.cov.save()
self.assert_analysis([1, 5, 7, 9, 10], missing=[7]) # Expecting 1 missing line
@pytest.mark.skipif(
coverage.version_info < (7, 2), reason="exclude_also requires coverage 7.2+"
)
def test_exclude_also(self):
"""Test that report:exclude_also patterns are picked up."""
self.make_template("""\
Before
{% if condition %}{# custom-exclude #}
{{ content }}
{% endif %}
After
""")
self.make_file(
".coveragerc",
"""\
[run]
plugins = django_coverage_plugin
[report]
exclude_also = custom-exclude
""",
)
tem = get_template(self.template_file)
self.cov = coverage.Coverage(source=["."])
self.cov.start()
tem.render({"condition": True, "content": "hi"})
self.cov.stop()
self.cov.save()
self.assert_analysis([1, 5])