diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2022-10-28 07:45:29 -0400 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2022-10-28 08:16:56 -0400 |
commit | b3a1d979f8625e4974eaa7211cdecb211ba90b50 (patch) | |
tree | acae62fba207edf0e55249e5b0a5f8c93abcee53 | |
parent | 44fbd3b02ad22326767dc37fe3b94aa93b36e8a3 (diff) | |
download | python-coveragepy-git-b3a1d979f8625e4974eaa7211cdecb211ba90b50.tar.gz |
test: correct some config tests, and fully cover tomlconfig.py
-rw-r--r-- | coverage/tomlconfig.py | 11 | ||||
-rw-r--r-- | tests/test_config.py | 25 |
2 files changed, 25 insertions, 11 deletions
diff --git a/coverage/tomlconfig.py b/coverage/tomlconfig.py index 0b5052b4..49282e92 100644 --- a/coverage/tomlconfig.py +++ b/coverage/tomlconfig.py @@ -3,7 +3,6 @@ """TOML configuration support for coverage.py""" -import configparser import os import re @@ -78,8 +77,6 @@ class TomlConfigParser: """ prefixes = ["tool.coverage."] - if self.our_file: - prefixes.append("") for prefix in prefixes: real_section = prefix + section parts = real_section.split(".") @@ -98,11 +95,11 @@ class TomlConfigParser: """Like .get, but returns the real section name and the value.""" name, data = self._get_section(section) if data is None: - raise configparser.NoSectionError(section) + raise ConfigError(f"No section: {section!r}") try: value = data[option] - except KeyError as exc: - raise configparser.NoOptionError(option, name) from exc + except KeyError: + raise ConfigError(f"No option {option!r} in section: {name!r}") from None return name, value def _get_single(self, section, option): @@ -129,7 +126,7 @@ class TomlConfigParser: def options(self, section): _, data = self._get_section(section) if data is None: - raise configparser.NoSectionError(section) + raise ConfigError(f"No section: {section!r}") return list(data.keys()) def get_section(self, section): diff --git a/tests/test_config.py b/tests/test_config.py index cb3edadb..4afdd1e3 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -12,6 +12,7 @@ import pytest import coverage from coverage.config import HandyConfigParser from coverage.exceptions import ConfigError, CoverageWarning +from coverage.tomlconfig import TomlConfigParser from tests.coveragetest import CoverageTest, UsingModulesMixin from tests.helpers import without_module @@ -62,7 +63,7 @@ class ConfigTest(CoverageTest): assert cov.config.data_file == "delete.me" def test_toml_config_file(self): - # A .coveragerc file will be read into the configuration. + # A pyproject.toml file will be read into the configuration. self.make_file("pyproject.toml", """\ # This is just a bogus toml file for testing. [tool.somethingelse] @@ -80,7 +81,7 @@ class ConfigTest(CoverageTest): [tool.coverage.plugins.a_plugin] hello = "world" """) - cov = coverage.Coverage(config_file="pyproject.toml") + cov = coverage.Coverage() assert cov.config.timid assert not cov.config.branch assert cov.config.concurrency == ["a", "b"] @@ -91,13 +92,14 @@ class ConfigTest(CoverageTest): assert cov.config.fail_under == 90.5 assert cov.config.get_plugin_options("plugins.a_plugin") == {"hello": "world"} + def test_toml_ints_can_be_floats(self): # Test that our class doesn't reject integers when loading floats self.make_file("pyproject.toml", """\ # This is just a bogus toml file for testing. [tool.coverage.report] fail_under = 90 """) - cov = coverage.Coverage(config_file="pyproject.toml") + cov = coverage.Coverage() assert cov.config.fail_under == 90 assert isinstance(cov.config.fail_under, float) @@ -435,7 +437,8 @@ class ConfigTest(CoverageTest): [run] branch = True """) - config = HandyConfigParser("config.ini") + config = HandyConfigParser(True) + config.read(["config.ini"]) with pytest.raises(ConfigError, match="No section: 'xyzzy'"): config.options("xyzzy") with pytest.raises(ConfigError, match="No option 'foo' in section: 'xyzzy'"): @@ -756,3 +759,17 @@ class ConfigFileTest(UsingModulesMixin, CoverageTest): assert not cov.config.timid assert not cov.config.branch assert cov.config.data_file == ".coverage" + + def test_exceptions_from_missing_toml_things(self): + self.make_file("pyproject.toml", """\ + [tool.coverage.run] + branch = true + """) + config = TomlConfigParser(False) + config.read("pyproject.toml") + with pytest.raises(ConfigError, match="No section: 'xyzzy'"): + config.options("xyzzy") + with pytest.raises(ConfigError, match="No section: 'xyzzy'"): + config.get("xyzzy", "foo") + with pytest.raises(ConfigError, match="No option 'foo' in section: 'tool.coverage.run'"): + config.get("run", "foo") |