From 22c1f4d693f8c47d204ecd1262aeae7f47086f0b Mon Sep 17 00:00:00 2001 From: "Schinckel, Matthew" Date: Thu, 30 May 2024 11:52:07 +0930 Subject: [PATCH 1/2] Allow ignoring script tags. --- README.rst | 12 ++++++ django_coverage_plugin/plugin.py | 42 ++++++++++++++++++++- tests/test_simple.py | 64 ++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 2 deletions(-) diff --git a/README.rst b/README.rst index 7ce3fd4..f6ce08e 100644 --- a/README.rst +++ b/README.rst @@ -101,6 +101,18 @@ If you use ``pyproject.toml`` for tool configuration use:: [tool.coverage.django_coverage_plugin] template_extensions = 'html, txt, tex, email' +By default, + {% endif %} +
+ yyy +
""" + self.make_template(template) + self.run_django_coverage() + report = self.cov.report() + lines = len(template.split('\n')) + self.assertEqual(report, 100.0 * (lines - 2) / lines) + + def test_script_tag_not_ignored(self): + self.make_file(".coveragerc", """\ + [run] + plugins = django_coverage_plugin + [django_coverage_plugin] + ignore_script_tags = false + """) + + template = """{% if 1 %} + {% url 'xxx' as yyy %} +
+ xxx + +
+ + {% endif %} +
+ yyy +
""" + self.make_template(template) + self.run_django_coverage() + report = self.cov.report() + lines = len(template.split('\n')) + self.assertEqual(report, 100.0) + class StringTemplateTest(DjangoPluginTestCase): From d445907121c0efc75d0490d4989081ac2120d207 Mon Sep 17 00:00:00 2001 From: "Schinckel, Matthew" Date: Fri, 31 May 2024 14:15:03 +0930 Subject: [PATCH 2/2] Use script_blocks as an interim for script_lines --- django_coverage_plugin/plugin.py | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/django_coverage_plugin/plugin.py b/django_coverage_plugin/plugin.py index b16c973..1f088bd 100644 --- a/django_coverage_plugin/plugin.py +++ b/django_coverage_plugin/plugin.py @@ -159,7 +159,7 @@ def __init__(self, options): self.source_map = {} - self.ignore_script_tags = options.get('ignore_script_tags', False) + self.ignore_script_tags = options.get('ignore_script_tags', False) in (True, 'True', 'true') # --- CoveragePlugin methods @@ -298,13 +298,16 @@ class ScriptParser(html.parser.HTMLParser): # We never have nested script tags, so we are all good. def __init__(self, *args, **kwargs): + self.script_checker = kwargs.pop('script_checker', lambda attrs: True) super().__init__(*args, **kwargs) - self.script_lines = [] + self.script_blocks = [] self.in_script = False + self.script_lines_cache = None def handle_starttag(self, tag, attrs): if tag == 'script': - self.in_script = True + attrs = dict(attrs) + self.in_script = self.script_checker(attrs) and attrs.get('type', 'text/javascript') == 'text/javascript' def handle_endtag(self, tag): if tag == 'script': @@ -313,9 +316,17 @@ def handle_endtag(self, tag): def handle_data(self, data): if self.in_script: start_line = self.getpos()[0] - self.script_lines.extend([ - line + start_line + 1 for line in range(data.rstrip().count('\n')) - ]) + self.script_blocks.append((start_line + 1, start_line + 1 + data.rstrip().count('\n'))) + + def _script_lines(self): + for block in self.script_blocks: + yield from range(block[0], block[1]) + + @property + def script_lines(self): + if self.script_lines_cache is None: + self.script_lines_cache = list(self._script_lines()) + return self.script_lines_cache class FileReporter(coverage.plugin.FileReporter):