Skip to content

Commit 184dc78

Browse files
committed
fix: unknown 'patch' names should produce warnings
1 parent abeda08 commit 184dc78

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

coverage/patch.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
from coverage import Coverage
1414
from coverage.config import CoverageConfig
1515

16+
_old_os_exit = os._exit
17+
1618
def apply_patches(cov: Coverage, config: CoverageConfig) -> None:
1719
"""Apply invasive patches requested by `[run] patch=`."""
1820

19-
if "os._exit" in config.patch:
20-
_old_os_exit = os._exit
21-
def _coverage_os_exit_patch(status: int) -> NoReturn:
22-
try:
23-
cov.save()
24-
except: # pylint: disable=bare-except
25-
pass
26-
_old_os_exit(status)
27-
os._exit = _coverage_os_exit_patch
21+
for patch in set(config.patch):
22+
if patch == "os._exit":
23+
def _coverage_os_exit_patch(status: int) -> NoReturn:
24+
try:
25+
cov.save()
26+
except: # pylint: disable=bare-except
27+
pass
28+
_old_os_exit(status)
29+
os._exit = _coverage_os_exit_patch
30+
else:
31+
cov._warn(f"Unknown patch {patch!r}, ignored", slug="unknown-patch")

doc/cmd.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ sys.monitoring doesn't yet support dynamic contexts, using default core (no-sysm
294294
This isn't supported by coverage.py yet. A default core will be used
295295
instead.
296296

297+
Unknown patch 'XYZ', ignored (unknown-patch)
298+
You specified an incorrect patch name in the :ref:`config_run_patch` setting.
299+
297300
Individual warnings can be disabled with the :ref:`disable_warnings
298301
<config_run_disable_warnings>` configuration setting. It is a list of the
299302
short parenthetical nicknames in the warning messages. For example, to silence

tests/test_config.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from coverage.types import FilePathClasses, FilePathType
2020

2121
from tests.coveragetest import CoverageTest, UsingModulesMixin
22-
22+
from tests.helpers import assert_coverage_warnings
2323

2424
class ConfigTest(CoverageTest):
2525
"""Tests of the different sources of configuration settings."""
@@ -469,6 +469,19 @@ def test_unknown_option_toml(self) -> None:
469469
with pytest.warns(CoverageWarning, match=msg):
470470
_ = coverage.Coverage()
471471

472+
def test_unknown_patch(self) -> None:
473+
self.make_file("foo.py", "a = 1")
474+
self.make_file(".coveragerc", """\
475+
[run]
476+
patch =
477+
os._exit
478+
xyzzy
479+
""")
480+
with pytest.warns(CoverageWarning) as warns:
481+
cov = coverage.Coverage()
482+
self.start_import_stop(cov, "foo")
483+
assert_coverage_warnings(warns, "Unknown patch 'xyzzy', ignored (unknown-patch)")
484+
472485
def test_misplaced_option(self) -> None:
473486
self.make_file(".coveragerc", """\
474487
[report]

0 commit comments

Comments
 (0)