summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2012-11-09 21:34:42 -0500
committerNed Batchelder <ned@nedbatchelder.com>2012-11-09 21:34:42 -0500
commit99fee9149a879b786d1974115d5c8afc43a21d08 (patch)
tree78002675350b6b31fe0a351266bd2c68555de5b3
parentf683ba3af6c539882dd0b4f750b476e2d9745ebe (diff)
downloadpython-coveragepy-99fee9149a879b786d1974115d5c8afc43a21d08.tar.gz
Now the title of the HTML report can be set.
-rw-r--r--CHANGES.txt3
-rw-r--r--coverage/cmdline.py9
-rw-r--r--coverage/config.py3
-rw-r--r--coverage/control.py7
-rw-r--r--coverage/html.py6
-rw-r--r--coverage/htmlfiles/index.html4
-rw-r--r--doc/cmd.rst12
-rw-r--r--doc/config.rst3
-rw-r--r--test/test_cmdline.py22
-rw-r--r--test/test_config.py2
-rw-r--r--test/test_html.py34
11 files changed, 83 insertions, 22 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index 40fb576..752738c 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -17,6 +17,9 @@ Version 3.5.4b1
switch that indicates in the exit status whether the coverage percentage was
less than a particular value. Closes `issue 139`_.
+- The HTML report's title can now be set in the configuration file, with the
+ ``--title`` switch on the command line, or via the API.
+
- Embarrassingly, the `[xml] output=' setting in the .coveragerc file simply
didn't work. Now it does.
diff --git a/coverage/cmdline.py b/coverage/cmdline.py
index fe8b995..9e83e21 100644
--- a/coverage/cmdline.py
+++ b/coverage/cmdline.py
@@ -92,6 +92,10 @@ class Opts(object):
help="Use a simpler but slower trace method. Try this if you get "
"seemingly impossible results!"
)
+ title = optparse.make_option(
+ '', '--title', action='store', metavar="TITLE",
+ help="A text string to use as the title on the HTML."
+ )
version = optparse.make_option(
'', '--version', action='store_true',
help="Display version information and exit."
@@ -126,6 +130,7 @@ class CoverageOptionParser(optparse.OptionParser, object):
show_missing=None,
source=None,
timid=None,
+ title=None,
erase_first=None,
version=None,
)
@@ -281,6 +286,7 @@ CMDS = {
Opts.ignore_errors,
Opts.omit,
Opts.include,
+ Opts.title,
] + GLOBAL_ARGS,
usage = "[options] [modules]",
description = "Create an HTML report of the coverage of the files. "
@@ -442,7 +448,8 @@ class CoverageScript(object):
directory=options.directory, **report_args)
if 'html' in options.actions:
total = self.coverage.html_report(
- directory=options.directory, **report_args)
+ directory=options.directory, title=options.title,
+ **report_args)
if 'xml' in options.actions:
outfile = options.outfile
total = self.coverage.xml_report(outfile=outfile, **report_args)
diff --git a/coverage/config.py b/coverage/config.py
index 0d1da5f..7b77e46 100644
--- a/coverage/config.py
+++ b/coverage/config.py
@@ -54,6 +54,7 @@ class CoverageConfig(object):
# Defaults for [html]
self.html_dir = "htmlcov"
self.extra_css = None
+ self.html_title = "Coverage report"
# Defaults for [xml]
self.xml_output = "coverage.xml"
@@ -133,6 +134,8 @@ class CoverageConfig(object):
self.html_dir = cp.get('html', 'directory')
if cp.has_option('html', 'extra_css'):
self.extra_css = cp.get('html', 'extra_css')
+ if cp.has_option('html', 'title'):
+ self.html_title = cp.get('html', 'title')
# [xml]
if cp.has_option('xml', 'output'):
diff --git a/coverage/control.py b/coverage/control.py
index 726d3f3..bda19a9 100644
--- a/coverage/control.py
+++ b/coverage/control.py
@@ -561,7 +561,7 @@ class coverage(object):
reporter.report(morfs, directory=directory)
def html_report(self, morfs=None, directory=None, ignore_errors=None,
- omit=None, include=None, extra_css=None):
+ omit=None, include=None, extra_css=None, title=None):
"""Generate an HTML report.
The HTML is written to `directory`. The file "index.html" is the
@@ -571,6 +571,9 @@ class coverage(object):
`extra_css` is a path to a file of other CSS to apply on the page.
It will be copied into the HTML directory.
+ `title` is a text string (not HTML) to use as the title of the HTML
+ report.
+
See `coverage.report()` for other arguments.
Returns a float, the total percentage covered.
@@ -578,7 +581,7 @@ class coverage(object):
"""
self.config.from_args(
ignore_errors=ignore_errors, omit=omit, include=include,
- html_dir=directory, extra_css=extra_css,
+ html_dir=directory, extra_css=extra_css, html_title=title,
)
reporter = HtmlReporter(self, self.config)
return reporter.report(morfs)
diff --git a/coverage/html.py b/coverage/html.py
index 6994711..fc1bee4 100644
--- a/coverage/html.py
+++ b/coverage/html.py
@@ -47,6 +47,7 @@ class HtmlReporter(Reporter):
self.directory = None
self.template_globals = {
'escape': escape,
+ 'title': self.config.html_title,
'__url__': coverage.__url__,
'__version__': coverage.__version__,
}
@@ -252,9 +253,12 @@ class HtmlReporter(Reporter):
self.totals = totals = sum([f['nums'] for f in files])
extra_css = self.extra_css
+ html = index_tmpl.render(locals())
+ if sys.version_info < (3, 0):
+ html = html.decode("utf-8")
self.write_html(
os.path.join(self.directory, "index.html"),
- index_tmpl.render(locals())
+ html
)
# Write the latest hashes for next time.
diff --git a/coverage/htmlfiles/index.html b/coverage/htmlfiles/index.html
index c6d9eec..5a7c8c2 100644
--- a/coverage/htmlfiles/index.html
+++ b/coverage/htmlfiles/index.html
@@ -2,7 +2,7 @@
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
- <title>Coverage report</title>
+ <title>{{ title|escape }}</title>
<link rel='stylesheet' href='style.css' type='text/css'>
{% if extra_css %}
<link rel='stylesheet' href='{{ extra_css }}' type='text/css'>
@@ -19,7 +19,7 @@
<div id='header'>
<div class='content'>
- <h1>Coverage report:
+ <h1>{{ title|escape }}:
<span class='pc_cov'>{{totals.pc_covered_str}}%</span>
</h1>
<img id='keyboard_icon' src='keybd_closed.png'>
diff --git a/doc/cmd.rst b/doc/cmd.rst
index c15857c..e8e5f39 100644
--- a/doc/cmd.rst
+++ b/doc/cmd.rst
@@ -274,13 +274,17 @@ Lines are highlighted green for executed, red for missing, and gray for
excluded. The counts at the top of the file are buttons to turn on and off
the highlighting.
-If you prefer a different style for your HTML report, you can provide your
-own CSS file to apply, by specifying a CSS file in the [html] section of the
-configuration file. See :ref:`config_html` for details.
-
A number of keyboard shortcuts are available for navigating the report.
Click the keyboard icon in the upper right to see the complete list.
+The title of the report can be set with the ``title`` setting in the
+``[html]`` section of the configuration file, or the ``--title`` switch on
+the command line.
+
+If you prefer a different style for your HTML report, you can provide your
+own CSS file to apply, by specifying a CSS file in the ``[html]`` section of
+the configuration file. See :ref:`config_html` for details.
+
The ``-d`` argument specifies an output directory, defaulting to "htmlcov"::
$ coverage html -d coverage_html
diff --git a/doc/config.rst b/doc/config.rst
index 74cab4f..159a42f 100644
--- a/doc/config.rst
+++ b/doc/config.rst
@@ -180,6 +180,9 @@ The file will be copied into the HTML output directory. Don't name it
"style.css". This CSS is in addition to the CSS normally used, though you can
overwrite as many of the rules as you like.
+``title`` (string, default "Coverage report"): the title to use for the report.
+Note this is text, not HTML.
+
[xml]
-----
diff --git a/test/test_cmdline.py b/test/test_cmdline.py
index af46d95..eb7fe0f 100644
--- a/test/test_cmdline.py
+++ b/test/test_cmdline.py
@@ -251,35 +251,35 @@ class ClassicCmdLineTest(CmdLineTest):
def test_html_report(self):
# coverage -b -d DIR [-i] [-o DIR,...] [FILE1 FILE2 ...]
self.cmd_executes("-b", self.INIT_LOAD + """\
- .html_report(directory=None, ignore_errors=None,
+ .html_report(directory=None, ignore_errors=None, title=None,
omit=None, include=None, morfs=[])
""")
self.cmd_executes("-b -d dir1", self.INIT_LOAD + """\
- .html_report(directory="dir1", ignore_errors=None,
+ .html_report(directory="dir1", ignore_errors=None, title=None,
omit=None, include=None, morfs=[])
""")
self.cmd_executes("-b -i", self.INIT_LOAD + """\
- .html_report(directory=None, ignore_errors=True,
+ .html_report(directory=None, ignore_errors=True, title=None,
omit=None, include=None, morfs=[])
""")
self.cmd_executes("-b -o fooey", """\
.coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True, source=None, include=None, omit=["fooey"])
.load()
- .html_report(directory=None, ignore_errors=None,
+ .html_report(directory=None, ignore_errors=None, title=None,
omit=["fooey"], include=None, morfs=[])
""")
self.cmd_executes("-b -o fooey,booey", """\
.coverage(cover_pylib=None, data_suffix=None, timid=None, branch=None, config_file=True, source=None, include=None, omit=["fooey", "booey"])
.load()
- .html_report(directory=None, ignore_errors=None,
+ .html_report(directory=None, ignore_errors=None, title=None,
omit=["fooey", "booey"], include=None, morfs=[])
""")
self.cmd_executes("-b mod1", self.INIT_LOAD + """\
- .html_report(directory=None, ignore_errors=None,
+ .html_report(directory=None, ignore_errors=None, title=None,
omit=None, include=None, morfs=["mod1"])
""")
self.cmd_executes("-b mod1 mod2 mod3", self.INIT_LOAD + """\
- .html_report(directory=None, ignore_errors=None,
+ .html_report(directory=None, ignore_errors=None, title=None,
omit=None, include=None, morfs=["mod1", "mod2", "mod3"])
""")
@@ -447,6 +447,14 @@ class NewCmdLineTest(CmdLineTest):
self.cmd_executes_same("html --omit f,b", "-b --omit f,b")
self.cmd_executes_same("html m1", "-b m1")
self.cmd_executes_same("html m1 m2 m3", "-b m1 m2 m3")
+ self.cmd_executes("html", self.INIT_LOAD + """\
+ .html_report(ignore_errors=None, omit=None, include=None, morfs=[],
+ directory=None, title=None)
+ """)
+ self.cmd_executes("html --title=Hello_there", self.INIT_LOAD + """\
+ .html_report(ignore_errors=None, omit=None, include=None, morfs=[],
+ directory=None, title='Hello_there')
+ """)
def test_report(self):
self.cmd_executes_same("report", "-r")
diff --git a/test/test_config.py b/test/test_config.py
index 4fc658f..a73ef34 100644
--- a/test/test_config.py
+++ b/test/test_config.py
@@ -139,6 +139,7 @@ class ConfigFileTest(CoverageTest):
directory = c:\\tricky\\dir.somewhere
extra_css=something/extra.css
+ title = My report & stuff # numbers
[xml]
output=mycov.xml
@@ -177,6 +178,7 @@ class ConfigFileTest(CoverageTest):
self.assertTrue(cov.config.show_missing)
self.assertEqual(cov.config.html_dir, r"c:\tricky\dir.somewhere")
self.assertEqual(cov.config.extra_css, "something/extra.css")
+ self.assertEqual(cov.config.html_title, "My report & stuff # numbers")
self.assertEqual(cov.config.xml_output, "mycov.xml")
diff --git a/test/test_html.py b/test/test_html.py
index 1877a30..73e46b2 100644
--- a/test/test_html.py
+++ b/test/test_html.py
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
"""Tests that HTML generation is awesome."""
import os.path, sys
@@ -38,14 +39,14 @@ class HtmlTest(CoverageTest):
print("x is %d" % x)
""")
- def run_coverage(self, **kwargs):
+ def run_coverage(self, covargs=None, htmlargs=None):
"""Run coverage on main_file.py, and create an HTML report."""
self.clean_local_file_imports()
- cov = coverage.coverage(**kwargs)
+ cov = coverage.coverage(**(covargs or {}))
cov.start()
self.import_local_file("main_file")
cov.stop()
- cov.html_report()
+ cov.html_report(**(htmlargs or {}))
def remove_html_files(self):
"""Remove the HTML files created as part of the HTML report."""
@@ -118,11 +119,11 @@ class HtmlTest(CoverageTest):
# In this case, everything changes because the coverage settings have
# changed.
self.create_initial_files()
- self.run_coverage(timid=False)
+ self.run_coverage(covargs=dict(timid=False))
index1 = open("htmlcov/index.html").read()
self.remove_html_files()
- self.run_coverage(timid=True)
+ self.run_coverage(covargs=dict(timid=True))
# All the files have been reported again.
self.assert_exists("htmlcov/index.html")
@@ -155,6 +156,29 @@ class HtmlTest(CoverageTest):
fixed_index2 = index2.replace("XYZZY", self.real_coverage_version)
self.assertMultiLineEqual(index1, fixed_index2)
+ def test_default_title(self):
+ self.create_initial_files()
+ self.run_coverage()
+ index = open("htmlcov/index.html").read()
+ self.assertIn("<title>Coverage report</title>", index)
+ self.assertIn("<h1>Coverage report:", index)
+
+ def test_title_set_in_config_file(self):
+ self.create_initial_files()
+ self.make_file(".coveragerc", "[html]\ntitle = My nüms & stüff!\n")
+ self.run_coverage()
+ index = open("htmlcov/index.html").read()
+ self.assertIn("<title>My n&#252;ms &amp; st&#252;ff!</title>", index)
+ self.assertIn("<h1>My n&#252;ms &amp; st&#252;ff!:", index)
+
+ def test_title_set_in_args(self):
+ self.create_initial_files()
+ self.make_file(".coveragerc", "[html]\ntitle = Good title\n")
+ self.run_coverage(htmlargs=dict(title="My nüms & stüff!"))
+ index = open("htmlcov/index.html").read()
+ self.assertIn("<title>My n&#252;ms &amp; st&#252;ff!</title>", index)
+ self.assertIn("<h1>My n&#252;ms &amp; st&#252;ff!:", index)
+
class HtmlWithUnparsableFilesTest(CoverageTest):
"""Test the behavior when measuring unparsable files."""