summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2021-10-23 11:27:52 -0400
committerNed Batchelder <ned@nedbatchelder.com>2021-10-23 17:34:53 -0400
commitbb2bc1a6295ee8fc4f3cccfe72aeb3d3ae5246cb (patch)
tree09ec0e9c4ae2dc14d20bd2f7cb0c07f4d6020148
parentb5a53740700e59dbebbf655d4914bbaf9470870a (diff)
downloadpython-coveragepy-git-bb2bc1a6295ee8fc4f3cccfe72aeb3d3ae5246cb.tar.gz
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.
-rw-r--r--coverage/html.py62
-rw-r--r--tests/test_html.py58
2 files changed, 14 insertions, 106 deletions
diff --git a/coverage/html.py b/coverage/html.py
index e56e3079..6246a9b9 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -23,42 +23,12 @@ from coverage.templite import Templite
os = isolate_module(os)
-# Static files are looked for in a list of places.
-STATIC_PATH = [
- # The place Debian puts system Javascript libraries.
- "/usr/share/javascript",
-
- # Our htmlfiles directory.
- os.path.join(os.path.dirname(__file__), "htmlfiles"),
-]
-
-
-def data_filename(fname, pkgdir=""):
- """Return the path to a data file of ours.
-
- The file is searched for on `STATIC_PATH`, and the first place it's found,
- is returned.
-
- Each directory in `STATIC_PATH` is searched as-is, and also, if `pkgdir`
- is provided, at that sub-directory.
-
+def data_filename(fname):
+ """Return the path to an "htmlfiles" data file of ours.
"""
- tried = []
- for static_dir in STATIC_PATH:
- static_filename = os.path.join(static_dir, fname)
- if os.path.exists(static_filename):
- return static_filename
- else:
- tried.append(static_filename)
- if pkgdir:
- static_filename = os.path.join(static_dir, pkgdir, fname)
- if os.path.exists(static_filename):
- return static_filename
- else:
- tried.append(static_filename)
- raise CoverageException(
- f"Couldn't find static file {fname!r} from {os.getcwd()!r}, tried: {tried!r}"
- )
+ static_dir = os.path.join(os.path.dirname(__file__), "htmlfiles")
+ static_filename = os.path.join(static_dir, fname)
+ return static_filename
def read_data(fname):
@@ -158,11 +128,11 @@ class HtmlReporter:
# These files will be copied from the htmlfiles directory to the output
# directory.
STATIC_FILES = [
- ("style.css", ""),
- ("coverage_html.js", ""),
- ("keybd_closed.png", ""),
- ("keybd_open.png", ""),
- ("favicon_32.png", ""),
+ "style.css",
+ "coverage_html.js",
+ "keybd_closed.png",
+ "keybd_open.png",
+ "favicon_32.png",
]
def __init__(self, cov):
@@ -251,18 +221,12 @@ class HtmlReporter:
def make_local_static_report_files(self):
"""Make local instances of static files for HTML report."""
# The files we provide must always be copied.
- for static, pkgdir in self.STATIC_FILES:
- shutil.copyfile(
- data_filename(static, pkgdir),
- os.path.join(self.directory, static)
- )
+ for static in self.STATIC_FILES:
+ shutil.copyfile(data_filename(static), os.path.join(self.directory, static))
# The user may have extra CSS they want copied.
if self.extra_css:
- shutil.copyfile(
- self.config.extra_css,
- os.path.join(self.directory, self.extra_css)
- )
+ shutil.copyfile(self.config.extra_css, os.path.join(self.directory, self.extra_css))
def html_file(self, fr, analysis):
"""Generate an HTML file for one source file."""
diff --git a/tests/test_html.py b/tests/test_html.py
index 06dcb886..fa60952d 100644
--- a/tests/test_html.py
+++ b/tests/test_html.py
@@ -16,7 +16,7 @@ import pytest
import coverage
from coverage import env
-from coverage.exceptions import CoverageException, NotPython, NoSource
+from coverage.exceptions import NotPython, NoSource
from coverage.files import abs_file, flat_rootname
import coverage.html
from coverage.report import get_analysis_to_report
@@ -555,62 +555,6 @@ class HtmlTest(HtmlTestHelpers, CoverageTest):
self.assert_doesnt_exist("htmlcov/submodule___init___py.html")
-class HtmlStaticFileTest(CoverageTest):
- """Tests of the static file copying for the HTML report."""
-
- def setup_test(self):
- super().setup_test()
- original_path = list(coverage.html.STATIC_PATH)
- self.addCleanup(setattr, coverage.html, 'STATIC_PATH', original_path)
-
- def test_copying_static_files_from_system(self):
- # Make a new place for static files.
- self.make_file("static_here/jquery.min.js", "Not Really JQuery!")
- coverage.html.STATIC_PATH.insert(0, "static_here")
-
- self.make_file("main.py", "print(17)")
- cov = coverage.Coverage()
- self.start_import_stop(cov, "main")
- cov.html_report()
-
- with open("htmlcov/jquery.min.js") as f:
- jquery = f.read()
- assert jquery == "Not Really JQuery!"
-
- def test_copying_static_files_from_system_in_dir(self):
- # Make a new place for static files.
- INSTALLED = [
- "jquery/jquery.min.js",
- "jquery-hotkeys/jquery.hotkeys.js",
- "jquery-isonscreen/jquery.isonscreen.js",
- "jquery-tablesorter/jquery.tablesorter.min.js",
- ]
- for fpath in INSTALLED:
- self.make_file(os.path.join("static_here", fpath), "Not real.")
- coverage.html.STATIC_PATH.insert(0, "static_here")
-
- self.make_file("main.py", "print(17)")
- cov = coverage.Coverage()
- self.start_import_stop(cov, "main")
- cov.html_report()
-
- for fpath in INSTALLED:
- the_file = os.path.basename(fpath)
- with open(os.path.join("htmlcov", the_file)) as f:
- contents = f.read()
- assert contents == "Not real."
-
- def test_cant_find_static_files(self):
- # Make the path point to useless places.
- coverage.html.STATIC_PATH = ["/xyzzy"]
-
- self.make_file("main.py", "print(17)")
- cov = coverage.Coverage()
- self.start_import_stop(cov, "main")
- msg = "Couldn't find static file '.*'"
- with pytest.raises(CoverageException, match=msg):
- cov.html_report()
-
def filepath_to_regex(path):
"""Create a regex for scrubbing a file path."""
regex = re.escape(path)