diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2013-10-10 21:14:04 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2013-10-10 21:14:04 -0400 |
commit | 298ac7937c3a7bec8e0c8397dd28ae1d13f60963 (patch) | |
tree | d66b5b324506293448918e119abe2337ba0c9c46 | |
parent | 90bde2c8efdad019a9ced2b2fa8518ab7e99f7b8 (diff) | |
download | python-coveragepy-298ac7937c3a7bec8e0c8397dd28ae1d13f60963.tar.gz |
Fix the mechanism for HTML to find OS-installed resources.
-rw-r--r-- | coverage/html.py | 30 | ||||
-rw-r--r-- | tests/test_html.py | 23 |
2 files changed, 42 insertions, 11 deletions
diff --git a/coverage/html.py b/coverage/html.py index b5cef11..956f070 100644 --- a/coverage/html.py +++ b/coverage/html.py @@ -20,19 +20,27 @@ STATIC_PATH = [ os.path.join(os.path.dirname(__file__), "htmlfiles"), ] -def data_filename(fname): +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 subdirectory. + """ for static_dir in STATIC_PATH: static_filename = os.path.join(static_dir, fname) if os.path.exists(static_filename): return static_filename + if pkgdir: + static_filename = os.path.join(static_dir, pkgdir, fname) + if os.path.exists(static_filename): + return static_filename raise CoverageException("Couldn't find static file %r" % fname) + def data(fname): """Return the contents of a data file of ours.""" data_file = open(data_filename(fname)) @@ -47,14 +55,14 @@ class HtmlReporter(Reporter): # These files will be copied from the htmlfiles dir to the output dir. STATIC_FILES = [ - "style.css", - "jquery.min.js", - "jquery.hotkeys.js", - "jquery.isonscreen.js", - "jquery.tablesorter.min.js", - "coverage_html.js", - "keybd_closed.png", - "keybd_open.png", + ("style.css", ""), + ("jquery.min.js", "jquery"), + ("jquery.hotkeys.js", "jquery-hotkeys"), + ("jquery.isonscreen.js", "jquery-isonscreen"), + ("jquery.tablesorter.min.js", "jquery-tablesorter"), + ("coverage_html.js", ""), + ("keybd_closed.png", ""), + ("keybd_open.png", ""), ] def __init__(self, cov, config): @@ -117,9 +125,9 @@ class HtmlReporter(Reporter): 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 in self.STATIC_FILES: + for static, pkgdir in self.STATIC_FILES: shutil.copyfile( - data_filename(static), + data_filename(static, pkgdir), os.path.join(self.directory, static) ) diff --git a/tests/test_html.py b/tests/test_html.py index e1d41d9..06132fb 100644 --- a/tests/test_html.py +++ b/tests/test_html.py @@ -317,9 +317,32 @@ class HtmlStaticFileTest(CoverageTest): cov = coverage.coverage() self.start_import_stop(cov, "main") cov.html_report() + jquery = open("htmlcov/jquery.min.js").read() self.assertEqual(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) + contents = open(os.path.join("htmlcov", the_file)).read() + self.assertEqual(contents, "Not real.") + def test_cant_find_static_files(self): # Make the path point to useless places. coverage.html.STATIC_PATH = ["/xyzzy"] |