Skip to content

Commit 2a0e7bc

Browse files
kejadlennedbat
authored andcommitted
feat: add extend_exclude option
1 parent 51f395d commit 2a0e7bc

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

coverage/config.py

+3
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ def __init__(self) -> None:
215215

216216
# Defaults for [report]
217217
self.exclude_list = DEFAULT_EXCLUDE[:]
218+
self.exclude_also: List[str] = []
218219
self.fail_under = 0.0
219220
self.format: Optional[str] = None
220221
self.ignore_errors = False
@@ -392,6 +393,7 @@ def copy(self) -> CoverageConfig:
392393

393394
# [report]
394395
('exclude_list', 'report:exclude_lines', 'regexlist'),
396+
('exclude_also', 'report:exclude_also', 'regexlist'),
395397
('fail_under', 'report:fail_under', 'float'),
396398
('format', 'report:format', 'boolean'),
397399
('ignore_errors', 'report:ignore_errors', 'boolean'),
@@ -523,6 +525,7 @@ def post_process(self) -> None:
523525
(k, [self.post_process_file(f) for f in v])
524526
for k, v in self.paths.items()
525527
)
528+
self.exclude_list += self.exclude_also
526529

527530
def debug_info(self) -> List[Tuple[str, Any]]:
528531
"""Make a list of (name, value) pairs for writing debug info."""

doc/config.rst

+12-2
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ Settings common to many kinds of reporting.
381381
......................
382382

383383
(multi-string) A list of regular expressions. Any line of your source code
384-
containing a match for one of these regexes is excluded from being reported as
384+
containing a match for one of these regexes is excluded from being reported as
385385
missing. More details are in :ref:`excluding`. If you use this option, you
386386
are replacing all the exclude regexes, so you'll need to also supply the
387387
"pragma: no cover" regex if you still want to use it.
@@ -395,12 +395,22 @@ you'll exclude any line with three or more of any character. If you write
395395
``pass``, you'll also exclude the line ``my_pass="foo"``, and so on.
396396

397397

398+
.. _config_report_exclude_also:
399+
400+
[report] exclude_also
401+
.....................
402+
403+
(multi-string) A list of regular expressions. This setting will preserve the
404+
default exclude pattern instead of overwriting it. See
405+
:ref:`config_report_exclude_lines` for details on exclusion regexes.
406+
407+
398408
.. _config_report_fail_under:
399409

400410
[report] fail_under
401411
...................
402412

403-
(float) A target coverage percentage. If the total coverage measurement is
413+
(float) A target coverage percentage. If the total coverage measurement is
404414
under this value, then exit with a status code of 2. If you specify a
405415
non-integral value, you must also set ``[report] precision`` properly to make
406416
use of the decimal places. A setting of 100 will fail any value under 100,

doc/excluding.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ For example, here's a list of exclusions I've used::
101101

102102
Note that when using the ``exclude_lines`` option in a configuration file, you
103103
are taking control of the entire list of regexes, so you need to re-specify the
104-
default "pragma: no cover" match if you still want it to apply.
104+
default "pragma: no cover" match if you still want it to apply. The
105+
``exclude_also`` option can be used instead to preserve the default
106+
exclusions while adding new ones.
105107

106108
The regexes only have to match part of a line. Be careful not to over-match. A
107109
value of ``...`` will match any line with more than three characters in it.

tests/test_config.py

+9
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,15 @@ def test_exceptions_from_missing_things(self) -> None:
449449
with pytest.raises(ConfigError, match="No option 'foo' in section: 'xyzzy'"):
450450
config.get("xyzzy", "foo")
451451

452+
def test_exclude_also(self) -> None:
453+
self.make_file("pyproject.toml", """\
454+
[tool.coverage.report]
455+
exclude_also = ["foobar"]
456+
""")
457+
cov = coverage.Coverage()
458+
459+
assert cov.config.exclude_list == coverage.config.DEFAULT_EXCLUDE + ["foobar"]
460+
452461

453462
class ConfigFileTest(UsingModulesMixin, CoverageTest):
454463
"""Tests of the config file settings in particular."""

0 commit comments

Comments
 (0)