Skip to content

Commit 07970d9

Browse files
committed
feat: template file extensions are configurable. #60
1 parent 3eee25d commit 07970d9

File tree

5 files changed

+93
-6
lines changed

5 files changed

+93
-6
lines changed

HISTORY.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,18 @@ Unreleased
77

88
Drop support for Python 3.4 and 3.5.
99

10+
A setting is available: ``template_extensions`` lets you set the file
11+
extensions that will be considered when looking for unused templates
12+
(requested in `issue 60`_).
13+
1014
Fix an issue on Windows where file names were being compared
1115
case-sensitively, causing templates to be missed (`issue 46`_).
1216

1317
Fix an issue (`issue 63`_) where tag libraries can't be found if imported
1418
during test collection. Thanks to Daniel Izquierdo for the fix.
1519

1620
.. _issue 46: https://github.com/nedbat/django_coverage_plugin/issues/46
21+
.. _issue 60: https://github.com/nedbat/django_coverage_plugin/issues/60
1722
.. _issue 63: https://github.com/nedbat/django_coverage_plugin/issues/63
1823

1924
v1.8.0 --- 2020-01-23

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ The Django template plugin uses some existing settings from your
7575
.coveragerc file. The ``source=``, ``include=``, and ``omit=`` options
7676
control what template files are included in the report.
7777

78+
The plugin can find unused template and include them in your results. By
79+
default, it will look for files in your templates directory with an extension
80+
of .html, .htm, or .txt. You can configure it to look for a different set of
81+
extensions if you like::
82+
83+
[run]
84+
plugins = django_coverage_plugin
85+
86+
[django_coverage_plugin]
87+
template_extensions = html, txt, tex, email
88+
7889

7990
Caveats
8091
~~~~~~~

django_coverage_plugin/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88

99

1010
def coverage_init(reg, options):
11-
reg.add_file_tracer(DjangoTemplatePlugin())
11+
reg.add_file_tracer(DjangoTemplatePlugin(options))

django_coverage_plugin/plugin.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ class DjangoTemplatePlugin(
155155
coverage.plugin.FileTracer,
156156
):
157157

158-
def __init__(self):
158+
def __init__(self, options):
159+
extensions = options.get("template_extensions", "html,htm,txt")
160+
self.extensions = [e.strip() for e in extensions.split(",")]
161+
159162
self.debug_checked = False
160163

161164
self.django_template_dir = os.path.normcase(os.path.realpath(
@@ -190,12 +193,14 @@ def file_reporter(self, filename):
190193
return FileReporter(filename)
191194

192195
def find_executable_files(self, src_dir):
196+
# We're only interested in files that look like reasonable HTML
197+
# files: Must end with one of our extensions, and must not have
198+
# funny characters that probably mean they are editor junk.
199+
rx = r"^[^.#~!$@%^&*()+=,]+\.(" + "|".join(self.extensions) + r")$"
200+
193201
for (dirpath, dirnames, filenames) in os.walk(src_dir):
194202
for filename in filenames:
195-
# We're only interested in files that look like reasonable HTML
196-
# files: Must end with .htm or .html, and must not have certain
197-
# funny characters that probably mean they are editor junk.
198-
if re.match(r"^[^.#~!$@%^&*()+=,]+\.html?$", filename):
203+
if re.search(rx, filename):
199204
yield os.path.join(dirpath, filename)
200205

201206
# --- FileTracer methods

tests/test_source.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2+
# For details: https://github.com/nedbat/django_coverage_plugin/blob/master/NOTICE.txt
3+
4+
"""Tests of template inheritance for django_coverage_plugin."""
5+
6+
from .plugin_test import DjangoPluginTestCase
7+
8+
9+
class FindSourceTest(DjangoPluginTestCase):
10+
11+
def test_finding_source(self):
12+
# This is a template that is rendered.
13+
self.make_template(name="main.html", text="Hello")
14+
# These are templates that aren't rendered, but are considered renderable.
15+
self.make_template(name="unused.html", text="Not used")
16+
self.make_template(name="unused.htm", text="Not used")
17+
self.make_template(name="unused.txt", text="Not used")
18+
# These are things left behind by an editor.
19+
self.make_template(name="~unused.html", text="junk")
20+
self.make_template(name="unused=.html", text="junk")
21+
self.make_template(name="unused.html,", text="junk")
22+
# This is some other file format we don't recognize.
23+
self.make_template(name="phd.tex", text="Too complicated to read")
24+
25+
text = self.run_django_coverage(name="main.html")
26+
self.assertEqual(text, "Hello")
27+
28+
# The rendered file has data, and was measured.
29+
self.assert_analysis([1], name="main.html")
30+
# The unrendered files have data, and were not measured.
31+
self.assert_analysis([1], name="unused.html", missing=[1])
32+
self.assert_analysis([1], name="unused.htm", missing=[1])
33+
self.assert_analysis([1], name="unused.txt", missing=[1])
34+
# The editor leave-behinds are not in the measured files.
35+
self.assert_measured_files("main.html", "unused.html", "unused.htm", "unused.txt")
36+
37+
def test_customized_extensions(self):
38+
self.make_file(".coveragerc", """\
39+
[run]
40+
plugins = django_coverage_plugin
41+
[django_coverage_plugin]
42+
template_extensions = html, tex
43+
""")
44+
# This is a template that is rendered.
45+
self.make_template(name="main.html", text="Hello")
46+
# These are templates that aren't rendered, but are considered renderable.
47+
self.make_template(name="unused.html", text="Not used")
48+
self.make_template(name="phd.tex", text="Too complicated to read")
49+
# These are things left behind by an editor.
50+
self.make_template(name="~unused.html", text="junk")
51+
self.make_template(name="unused=.html", text="junk")
52+
self.make_template(name="unused.html,", text="junk")
53+
# This is some other file format we don't recognize.
54+
self.make_template(name="unused.htm", text="Not used")
55+
self.make_template(name="unused.txt", text="Not used")
56+
57+
text = self.run_django_coverage(name="main.html")
58+
self.assertEqual(text, "Hello")
59+
60+
# The rendered file has data, and was measured.
61+
self.assert_analysis([1], name="main.html")
62+
# The unrendered files have data, and were not measured.
63+
self.assert_analysis([1], name="unused.html", missing=[1])
64+
self.assert_analysis([1], name="phd.tex", missing=[1])
65+
# The editor leave-behinds are not in the measured files.
66+
self.assert_measured_files("main.html", "unused.html", "phd.tex")

0 commit comments

Comments
 (0)