|
| 1 | +# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0 |
| 2 | +# For details: https://github.com/coveragepy/django_coverage_plugin/blob/main/NOTICE.txt |
| 3 | + |
| 4 | +"""Tests for {# pragma: no cover #} support in Django templates.""" |
| 5 | + |
| 6 | +import coverage |
| 7 | +from django.template.loader import get_template |
| 8 | + |
| 9 | +from .plugin_test import DjangoPluginTestCase |
| 10 | + |
| 11 | + |
| 12 | +class PragmaTest(DjangoPluginTestCase): |
| 13 | + |
| 14 | + def test_pragma_on_block_tag_excludes_entire_block(self): |
| 15 | + for condition in (True, False): |
| 16 | + with self.subTest(condition=condition): |
| 17 | + self.make_template("""\ |
| 18 | + Before |
| 19 | + {% if condition %}{# pragma: no cover #} |
| 20 | + {{ content }} |
| 21 | + {% endif %} |
| 22 | + After |
| 23 | + """) |
| 24 | + self.run_django_coverage(context={'condition': condition, 'content': 'hi'}) |
| 25 | + self.assert_analysis([1, 5]) |
| 26 | + |
| 27 | + def test_pragma_on_plain_text_line(self): |
| 28 | + self.make_template("""\ |
| 29 | + Before |
| 30 | + excluded line {# pragma: no cover #} |
| 31 | + After |
| 32 | + """) |
| 33 | + self.run_django_coverage() |
| 34 | + self.assert_analysis([1, 3]) |
| 35 | + |
| 36 | + def test_pragma_on_non_closing_tag(self): |
| 37 | + """Test that pragma does not treat tags like {% cycle $} as blocks.""" |
| 38 | + self.make_template("""\ |
| 39 | + <div> |
| 40 | + {% cycle 'a' 'b' as values %}{# pragma: no cover #} |
| 41 | + Covered |
| 42 | + Not covered {{ values|last }}{# pragma: no cover #} |
| 43 | + </div> |
| 44 | + """) |
| 45 | + self.run_django_coverage() |
| 46 | + self.assert_analysis([1, 3, 5]) |
| 47 | + |
| 48 | + def test_pragma_with_nested_blocks(self): |
| 49 | + self.make_template("""\ |
| 50 | + Before |
| 51 | + {% if condition %}{# pragma: no cover #} |
| 52 | + {% for item in items %} |
| 53 | + {{ item }} |
| 54 | + {% endfor %} |
| 55 | + {% endif %} |
| 56 | + After |
| 57 | + """) |
| 58 | + self.run_django_coverage( |
| 59 | + context={'condition': True, 'items': ['a', 'b']}, |
| 60 | + ) |
| 61 | + self.assert_analysis([1, 7]) |
| 62 | + |
| 63 | + def test_custom_exclude_patterns(self): |
| 64 | + """Test that coverage.py config for report:exclude_lines is used.""" |
| 65 | + self.make_template("""\ |
| 66 | + Before |
| 67 | + {% if condition %}{# noqa: no-cover #} |
| 68 | + {{ content }} |
| 69 | + {% endif %} |
| 70 | + {% if not condition %} {# this block won't execute #} |
| 71 | + {% now "SHORT_DATETIME_FORMAT" %} {# !SKIP ME! #} |
| 72 | + {% lorem %}{# pragma: no cover #}{# I'm not covered because of custom exclude #} |
| 73 | + {% endif %} |
| 74 | + {% lorem %}{# I'm covered! #} |
| 75 | + After |
| 76 | + """) |
| 77 | + tem = get_template(self.template_file) |
| 78 | + self.cov = coverage.Coverage(source=['.']) |
| 79 | + self.append_config("run:plugins", "django_coverage_plugin") |
| 80 | + # Set the exclude_lines with own patterns. |
| 81 | + self.cov.config.set_option( |
| 82 | + "report:exclude_lines", ["noqa: no-cover", "!SKIP ME!"], |
| 83 | + ) |
| 84 | + self.cov.start() |
| 85 | + tem.render({'condition': True, 'content': 'hi'}) |
| 86 | + self.cov.stop() |
| 87 | + self.cov.save() |
| 88 | + self.assert_analysis([1, 5, 7, 9, 10], missing=[7]) # Expecting 1 missing line |
0 commit comments