Skip to content

Commit b0710b1

Browse files
committed
skip_covered and skip_empty for HTML. nedbat#1090
1 parent 6957366 commit b0710b1

File tree

6 files changed

+74
-39
lines changed

6 files changed

+74
-39
lines changed

CHANGES.rst

+8-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,19 @@ Unreleased
2929
of the output easier. Thanks, Judson Neer. This had been requested a number
3030
of times (`issue 1086`_, `issue 922`_, `issue 732`_).
3131

32+
- The ``skip_covered`` and ``skip_empty`` settings in the configuration file
33+
can now be specified in the ``[html]`` section, so that text reports and HTML
34+
reports can use separate settings. The HTML report will still use the
35+
``[report]`` settings if there isn't a value in the ``[html]`` section.
36+
Closes `issue 1090`_.
37+
3238
- Update to support Python 3.10 alphas in progress, including `PEP 626: Precise
3339
line numbers for debugging and other tools <pep626_>`_.
3440

35-
.. _issue 1086: https://github.com/nedbat/coveragepy/issues/1086
3641
.. _issue 732: https://github.com/nedbat/coveragepy/issues/732
3742
.. _issue 922: https://github.com/nedbat/coveragepy/issues/922
43+
.. _issue 1086: https://github.com/nedbat/coveragepy/issues/1086
44+
.. _issue 1090: https://github.com/nedbat/coveragepy/issues/1090
3845

3946
.. _pep626: https://www.python.org/dev/peps/pep-0626/
4047

coverage/config.py

+4
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ def __init__(self):
217217
# Defaults for [html]
218218
self.extra_css = None
219219
self.html_dir = "htmlcov"
220+
self.html_skip_covered = None
221+
self.html_skip_empty = None
220222
self.html_title = "Coverage report"
221223
self.show_contexts = False
222224

@@ -384,6 +386,8 @@ def copy(self):
384386
# [html]
385387
('extra_css', 'html:extra_css'),
386388
('html_dir', 'html:directory'),
389+
('html_skip_covered', 'html:skip_covered', 'boolean'),
390+
('html_skip_empty', 'html:skip_empty', 'boolean'),
387391
('html_title', 'html:title'),
388392
('show_contexts', 'html:show_contexts', 'boolean'),
389393

coverage/control.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -955,8 +955,8 @@ def html_report(
955955
with override_config(self,
956956
ignore_errors=ignore_errors, report_omit=omit, report_include=include,
957957
html_dir=directory, extra_css=extra_css, html_title=title,
958-
skip_covered=skip_covered, show_contexts=show_contexts, report_contexts=contexts,
959-
skip_empty=skip_empty, precision=precision,
958+
html_skip_covered=skip_covered, show_contexts=show_contexts, report_contexts=contexts,
959+
html_skip_empty=skip_empty, precision=precision,
960960
):
961961
reporter = HtmlReporter(self)
962962
return reporter.report(morfs)

coverage/html.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,14 @@ def __init__(self, cov):
173173
self.coverage = cov
174174
self.config = self.coverage.config
175175
self.directory = self.config.html_dir
176+
177+
self.skip_covered = self.config.html_skip_covered
178+
if self.skip_covered is None:
179+
self.skip_covered = self.config.skip_covered
180+
self.skip_empty = self.config.html_skip_empty
181+
if self.skip_empty is None:
182+
self.skip_empty= self.config.skip_empty
183+
176184
title = self.config.html_title
177185
if env.PY2:
178186
title = title.decode("utf8")
@@ -271,7 +279,7 @@ def html_file(self, fr, analysis):
271279
nums = analysis.numbers
272280
self.all_files_nums.append(nums)
273281

274-
if self.config.skip_covered:
282+
if self.skip_covered:
275283
# Don't report on 100% files.
276284
no_missing_lines = (nums.n_missing == 0)
277285
no_missing_branches = (nums.n_partial_branches == 0)
@@ -280,7 +288,7 @@ def html_file(self, fr, analysis):
280288
file_be_gone(html_path)
281289
return
282290

283-
if self.config.skip_empty:
291+
if self.skip_empty:
284292
# Don't report on empty files.
285293
if nums.n_statements == 0:
286294
file_be_gone(html_path)

doc/config.rst

+17-15
Original file line numberDiff line numberDiff line change
@@ -315,16 +315,6 @@ setting also affects the interpretation of the ``fail_under`` setting.
315315
``show_missing`` (boolean, default False): when running a summary report, show
316316
missing lines. See :ref:`cmd_report` for more information.
317317

318-
.. _config_report_skip_covered:
319-
320-
``skip_covered`` (boolean, default False): Don't include files in the report
321-
that are 100% covered files. See :ref:`cmd_report` for more information.
322-
323-
.. _config_report_skip_empty:
324-
325-
``skip_empty`` (boolean, default False): Don't include empty files (those that
326-
have 0 statements) in the report. See :ref:`cmd_report` for more information.
327-
328318
.. _config_report_sort:
329319

330320
``sort`` (string, default "Name"): Sort the text report by the named column.
@@ -344,18 +334,30 @@ also apply to HTML output, where appropriate.
344334
``directory`` (string, default "htmlcov"): where to write the HTML report
345335
files.
346336

337+
.. _config_html_extra_css:
338+
339+
``extra_css`` (string): the path to a file of CSS to apply to the HTML report.
340+
The file will be copied into the HTML output directory. Don't name it
341+
"style.css". This CSS is in addition to the CSS normally used, though you can
342+
overwrite as many of the rules as you like.
343+
347344
.. _config_html_show_context:
348345

349346
``show_contexts`` (boolean): should the HTML report include an indication on
350347
each line of which contexts executed the line. See :ref:`dynamic_contexts` for
351348
details.
352349

353-
.. _config_html_extra_css:
350+
.. _config_html_skip_covered:
354351

355-
``extra_css`` (string): the path to a file of CSS to apply to the HTML report.
356-
The file will be copied into the HTML output directory. Don't name it
357-
"style.css". This CSS is in addition to the CSS normally used, though you can
358-
overwrite as many of the rules as you like.
352+
``skip_covered`` (boolean, defaulted from ``[report] skip_covered``): Don't
353+
include files in the report that are 100% covered files. See :ref:`cmd_report`
354+
for more information.
355+
356+
.. _config_html_skip_empty:
357+
358+
``skip_empty`` (boolean, defaulted from ``[report] skip_empty``): Don't include
359+
empty files (those that have 0 statements) in the report. See :ref:`cmd_report`
360+
for more information.
359361

360362
.. _config_html_title:
361363

tests/test_html.py

+33-19
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,8 @@ def test_reporting_on_unmeasured_file(self):
495495
self.assert_exists("htmlcov/index.html")
496496
self.assert_exists("htmlcov/other_py.html")
497497

498-
def test_report_skip_covered_no_branches(self):
498+
def make_main_and_not_covered(self):
499+
"""Helper to create files for skip_covered scenarios."""
499500
self.make_file("main_file.py", """
500501
import not_covered
501502
@@ -507,39 +508,41 @@ def normal():
507508
def not_covered():
508509
print("n")
509510
""")
511+
512+
def test_report_skip_covered(self):
513+
self.make_main_and_not_covered()
510514
self.run_coverage(htmlargs=dict(skip_covered=True))
511515
self.assert_exists("htmlcov/index.html")
512516
self.assert_doesnt_exist("htmlcov/main_file_py.html")
513517
self.assert_exists("htmlcov/not_covered_py.html")
514518

515-
def test_report_skip_covered_100(self):
516-
self.make_file("main_file.py", """
517-
def normal():
518-
print("z")
519-
normal()
520-
""")
521-
res = self.run_coverage(covargs=dict(source="."), htmlargs=dict(skip_covered=True))
522-
self.assertEqual(res, 100.0)
519+
def test_html_skip_covered(self):
520+
self.make_main_and_not_covered()
521+
self.make_file(".coveragerc", "[html]\nskip_covered = True")
522+
self.run_coverage()
523+
self.assert_exists("htmlcov/index.html")
523524
self.assert_doesnt_exist("htmlcov/main_file_py.html")
525+
self.assert_exists("htmlcov/not_covered_py.html")
524526

525527
def test_report_skip_covered_branches(self):
526-
self.make_file("main_file.py", """
527-
import not_covered
528+
self.make_main_and_not_covered()
529+
self.run_coverage(covargs=dict(branch=True), htmlargs=dict(skip_covered=True))
530+
self.assert_exists("htmlcov/index.html")
531+
self.assert_doesnt_exist("htmlcov/main_file_py.html")
532+
self.assert_exists("htmlcov/not_covered_py.html")
528533

534+
def test_report_skip_covered_100(self):
535+
self.make_file("main_file.py", """
529536
def normal():
530537
print("z")
531538
normal()
532539
""")
533-
self.make_file("not_covered.py", """
534-
def not_covered():
535-
print("n")
536-
""")
537-
self.run_coverage(covargs=dict(branch=True), htmlargs=dict(skip_covered=True))
538-
self.assert_exists("htmlcov/index.html")
540+
res = self.run_coverage(covargs=dict(source="."), htmlargs=dict(skip_covered=True))
541+
self.assertEqual(res, 100.0)
539542
self.assert_doesnt_exist("htmlcov/main_file_py.html")
540-
self.assert_exists("htmlcov/not_covered_py.html")
541543

542-
def test_report_skip_empty_files(self):
544+
def make_init_and_main(self):
545+
"""Helper to create files for skip_empty scenarios."""
543546
self.make_file("submodule/__init__.py", "")
544547
self.make_file("main_file.py", """
545548
import submodule
@@ -548,11 +551,22 @@ def normal():
548551
print("z")
549552
normal()
550553
""")
554+
555+
def test_report_skip_empty(self):
556+
self.make_init_and_main()
551557
self.run_coverage(htmlargs=dict(skip_empty=True))
552558
self.assert_exists("htmlcov/index.html")
553559
self.assert_exists("htmlcov/main_file_py.html")
554560
self.assert_doesnt_exist("htmlcov/submodule___init___py.html")
555561

562+
def test_html_skip_empty(self):
563+
self.make_init_and_main()
564+
self.make_file(".coveragerc", "[html]\nskip_empty = True")
565+
self.run_coverage()
566+
self.assert_exists("htmlcov/index.html")
567+
self.assert_exists("htmlcov/main_file_py.html")
568+
self.assert_doesnt_exist("htmlcov/submodule___init___py.html")
569+
556570

557571
class HtmlStaticFileTest(CoverageTest):
558572
"""Tests of the static file copying for the HTML report."""

0 commit comments

Comments
 (0)