Skip to content

Commit bb2bc1a

Browse files
committed
refactor: no more need for a search path for static files
We used to search an OS-specific directory in addition to our own, specifically so that Debian could use an OS-installed copy of jQuery and its plugins. But we no longer have jQuery or any third-party JavaScript code, so we don't need to search the Debian directories.
1 parent b5a5374 commit bb2bc1a

File tree

2 files changed

+14
-106
lines changed

2 files changed

+14
-106
lines changed

coverage/html.py

Lines changed: 13 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,12 @@
2323
os = isolate_module(os)
2424

2525

26-
# Static files are looked for in a list of places.
27-
STATIC_PATH = [
28-
# The place Debian puts system Javascript libraries.
29-
"/usr/share/javascript",
30-
31-
# Our htmlfiles directory.
32-
os.path.join(os.path.dirname(__file__), "htmlfiles"),
33-
]
34-
35-
36-
def data_filename(fname, pkgdir=""):
37-
"""Return the path to a data file of ours.
38-
39-
The file is searched for on `STATIC_PATH`, and the first place it's found,
40-
is returned.
41-
42-
Each directory in `STATIC_PATH` is searched as-is, and also, if `pkgdir`
43-
is provided, at that sub-directory.
44-
26+
def data_filename(fname):
27+
"""Return the path to an "htmlfiles" data file of ours.
4528
"""
46-
tried = []
47-
for static_dir in STATIC_PATH:
48-
static_filename = os.path.join(static_dir, fname)
49-
if os.path.exists(static_filename):
50-
return static_filename
51-
else:
52-
tried.append(static_filename)
53-
if pkgdir:
54-
static_filename = os.path.join(static_dir, pkgdir, fname)
55-
if os.path.exists(static_filename):
56-
return static_filename
57-
else:
58-
tried.append(static_filename)
59-
raise CoverageException(
60-
f"Couldn't find static file {fname!r} from {os.getcwd()!r}, tried: {tried!r}"
61-
)
29+
static_dir = os.path.join(os.path.dirname(__file__), "htmlfiles")
30+
static_filename = os.path.join(static_dir, fname)
31+
return static_filename
6232

6333

6434
def read_data(fname):
@@ -158,11 +128,11 @@ class HtmlReporter:
158128
# These files will be copied from the htmlfiles directory to the output
159129
# directory.
160130
STATIC_FILES = [
161-
("style.css", ""),
162-
("coverage_html.js", ""),
163-
("keybd_closed.png", ""),
164-
("keybd_open.png", ""),
165-
("favicon_32.png", ""),
131+
"style.css",
132+
"coverage_html.js",
133+
"keybd_closed.png",
134+
"keybd_open.png",
135+
"favicon_32.png",
166136
]
167137

168138
def __init__(self, cov):
@@ -251,18 +221,12 @@ def report(self, morfs):
251221
def make_local_static_report_files(self):
252222
"""Make local instances of static files for HTML report."""
253223
# The files we provide must always be copied.
254-
for static, pkgdir in self.STATIC_FILES:
255-
shutil.copyfile(
256-
data_filename(static, pkgdir),
257-
os.path.join(self.directory, static)
258-
)
224+
for static in self.STATIC_FILES:
225+
shutil.copyfile(data_filename(static), os.path.join(self.directory, static))
259226

260227
# The user may have extra CSS they want copied.
261228
if self.extra_css:
262-
shutil.copyfile(
263-
self.config.extra_css,
264-
os.path.join(self.directory, self.extra_css)
265-
)
229+
shutil.copyfile(self.config.extra_css, os.path.join(self.directory, self.extra_css))
266230

267231
def html_file(self, fr, analysis):
268232
"""Generate an HTML file for one source file."""

tests/test_html.py

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import coverage
1818
from coverage import env
19-
from coverage.exceptions import CoverageException, NotPython, NoSource
19+
from coverage.exceptions import NotPython, NoSource
2020
from coverage.files import abs_file, flat_rootname
2121
import coverage.html
2222
from coverage.report import get_analysis_to_report
@@ -555,62 +555,6 @@ def test_html_skip_empty(self):
555555
self.assert_doesnt_exist("htmlcov/submodule___init___py.html")
556556

557557

558-
class HtmlStaticFileTest(CoverageTest):
559-
"""Tests of the static file copying for the HTML report."""
560-
561-
def setup_test(self):
562-
super().setup_test()
563-
original_path = list(coverage.html.STATIC_PATH)
564-
self.addCleanup(setattr, coverage.html, 'STATIC_PATH', original_path)
565-
566-
def test_copying_static_files_from_system(self):
567-
# Make a new place for static files.
568-
self.make_file("static_here/jquery.min.js", "Not Really JQuery!")
569-
coverage.html.STATIC_PATH.insert(0, "static_here")
570-
571-
self.make_file("main.py", "print(17)")
572-
cov = coverage.Coverage()
573-
self.start_import_stop(cov, "main")
574-
cov.html_report()
575-
576-
with open("htmlcov/jquery.min.js") as f:
577-
jquery = f.read()
578-
assert jquery == "Not Really JQuery!"
579-
580-
def test_copying_static_files_from_system_in_dir(self):
581-
# Make a new place for static files.
582-
INSTALLED = [
583-
"jquery/jquery.min.js",
584-
"jquery-hotkeys/jquery.hotkeys.js",
585-
"jquery-isonscreen/jquery.isonscreen.js",
586-
"jquery-tablesorter/jquery.tablesorter.min.js",
587-
]
588-
for fpath in INSTALLED:
589-
self.make_file(os.path.join("static_here", fpath), "Not real.")
590-
coverage.html.STATIC_PATH.insert(0, "static_here")
591-
592-
self.make_file("main.py", "print(17)")
593-
cov = coverage.Coverage()
594-
self.start_import_stop(cov, "main")
595-
cov.html_report()
596-
597-
for fpath in INSTALLED:
598-
the_file = os.path.basename(fpath)
599-
with open(os.path.join("htmlcov", the_file)) as f:
600-
contents = f.read()
601-
assert contents == "Not real."
602-
603-
def test_cant_find_static_files(self):
604-
# Make the path point to useless places.
605-
coverage.html.STATIC_PATH = ["/xyzzy"]
606-
607-
self.make_file("main.py", "print(17)")
608-
cov = coverage.Coverage()
609-
self.start_import_stop(cov, "main")
610-
msg = "Couldn't find static file '.*'"
611-
with pytest.raises(CoverageException, match=msg):
612-
cov.html_report()
613-
614558
def filepath_to_regex(path):
615559
"""Create a regex for scrubbing a file path."""
616560
regex = re.escape(path)

0 commit comments

Comments
 (0)