From 3823cc6d41956d0b8cfb55d6151673017a825c49 Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Tue, 27 Dec 2022 19:08:47 -0500 Subject: [PATCH] fix: [tools.coverage] is valid for settings in a toml file. #1516 --- CHANGES.rst | 9 +++++++++ coverage/tomlconfig.py | 2 +- tests/test_config.py | 13 +++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index ae3428cd6..54bbe7891 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -20,9 +20,18 @@ development at the same time, such as 4.5.x and 5.0. Unreleased ---------- +- Fix: if Python doesn't provide tomllib, then TOML configuration files can + only be read if coverage.py is installed with the ``[toml]`` extra. + Coverage.py will raise an error if toml support is not installed when it sees + your settings are in a .toml file. But it didn't understand that + ``[tools.coverage]`` was a valid section header, so the error wasn't + reported, and settings were silently ignored. This is now fixed, closing + `issue 1516`_. + - Fix: adjusted how decorators are traced on PyPy 7.3.10, fixing `issue 1515`_. .. _issue 1515: https://github.com/nedbat/coveragepy/issues/1515 +.. _issue 1516: https://github.com/nedbat/coveragepy/issues/1516 .. _changes_7-0-1: diff --git a/coverage/tomlconfig.py b/coverage/tomlconfig.py index a7d390420..a25b3e35d 100644 --- a/coverage/tomlconfig.py +++ b/coverage/tomlconfig.py @@ -52,7 +52,7 @@ def read(self, filenames): raise TomlDecodeError(str(err)) from err return [filename] else: - has_toml = re.search(r"^\[tool\.coverage\.", toml_text, flags=re.MULTILINE) + has_toml = re.search(r"^\[tool\.coverage(\.|])", toml_text, flags=re.MULTILINE) if self.our_file or has_toml: # Looks like they meant to read TOML, but we can't read it. msg = "Can't read {!r} without TOML support. Install with [toml] extra" diff --git a/tests/test_config.py b/tests/test_config.py index ccc4305fb..d88a1a4f7 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -739,6 +739,19 @@ def test_no_toml_installed_pyproject_toml(self): with pytest.raises(ConfigError, match=msg): coverage.Coverage() + @pytest.mark.skipif(sys.version_info >= (3, 11), reason="Python 3.11 has toml in stdlib") + def test_no_toml_installed_pyproject_toml_shorter_syntax(self): + # Can't have coverage config in pyproject.toml without toml installed. + self.make_file("pyproject.toml", """\ + # A toml file! + [tool.coverage] + run.parallel = true + """) + with without_module(coverage.tomlconfig, 'tomllib'): + msg = "Can't read 'pyproject.toml' without TOML support" + with pytest.raises(ConfigError, match=msg): + coverage.Coverage() + def test_no_toml_installed_pyproject_no_coverage(self): # It's ok to have non-coverage pyproject.toml without toml installed. self.make_file("pyproject.toml", """\