Skip to content

Commit

Permalink
test: correct some config tests, and fully cover tomlconfig.py
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Oct 28, 2022
1 parent 44fbd3b commit b3a1d97
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
11 changes: 4 additions & 7 deletions coverage/tomlconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

"""TOML configuration support for coverage.py"""

import configparser
import os
import re

Expand Down Expand Up @@ -78,8 +77,6 @@ def _get_section(self, section):
"""
prefixes = ["tool.coverage."]
if self.our_file:
prefixes.append("")
for prefix in prefixes:
real_section = prefix + section
parts = real_section.split(".")
Expand All @@ -98,11 +95,11 @@ def _get(self, section, option):
"""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):
Expand All @@ -129,7 +126,7 @@ def has_section(self, section):
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):
Expand Down
25 changes: 21 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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
Expand Down Expand Up @@ -62,7 +63,7 @@ def test_named_config_file(self):
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]
Expand All @@ -80,7 +81,7 @@ def test_toml_config_file(self):
[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"]
Expand All @@ -91,13 +92,14 @@ def test_toml_config_file(self):
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)

Expand Down Expand Up @@ -435,7 +437,8 @@ def test_exceptions_from_missing_things(self):
[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'"):
Expand Down Expand Up @@ -756,3 +759,17 @@ def test_no_toml_installed_pyproject_no_coverage(self):
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")

0 comments on commit b3a1d97

Please sign in to comment.