Skip to content

Commit b3a1d97

Browse files
committed
test: correct some config tests, and fully cover tomlconfig.py
1 parent 44fbd3b commit b3a1d97

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

coverage/tomlconfig.py

+4-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

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

6-
import configparser
76
import os
87
import re
98

@@ -78,8 +77,6 @@ def _get_section(self, section):
7877
7978
"""
8079
prefixes = ["tool.coverage."]
81-
if self.our_file:
82-
prefixes.append("")
8380
for prefix in prefixes:
8481
real_section = prefix + section
8582
parts = real_section.split(".")
@@ -98,11 +95,11 @@ def _get(self, section, option):
9895
"""Like .get, but returns the real section name and the value."""
9996
name, data = self._get_section(section)
10097
if data is None:
101-
raise configparser.NoSectionError(section)
98+
raise ConfigError(f"No section: {section!r}")
10299
try:
103100
value = data[option]
104-
except KeyError as exc:
105-
raise configparser.NoOptionError(option, name) from exc
101+
except KeyError:
102+
raise ConfigError(f"No option {option!r} in section: {name!r}") from None
106103
return name, value
107104

108105
def _get_single(self, section, option):
@@ -129,7 +126,7 @@ def has_section(self, section):
129126
def options(self, section):
130127
_, data = self._get_section(section)
131128
if data is None:
132-
raise configparser.NoSectionError(section)
129+
raise ConfigError(f"No section: {section!r}")
133130
return list(data.keys())
134131

135132
def get_section(self, section):

tests/test_config.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import coverage
1313
from coverage.config import HandyConfigParser
1414
from coverage.exceptions import ConfigError, CoverageWarning
15+
from coverage.tomlconfig import TomlConfigParser
1516

1617
from tests.coveragetest import CoverageTest, UsingModulesMixin
1718
from tests.helpers import without_module
@@ -62,7 +63,7 @@ def test_named_config_file(self):
6263
assert cov.config.data_file == "delete.me"
6364

6465
def test_toml_config_file(self):
65-
# A .coveragerc file will be read into the configuration.
66+
# A pyproject.toml file will be read into the configuration.
6667
self.make_file("pyproject.toml", """\
6768
# This is just a bogus toml file for testing.
6869
[tool.somethingelse]
@@ -80,7 +81,7 @@ def test_toml_config_file(self):
8081
[tool.coverage.plugins.a_plugin]
8182
hello = "world"
8283
""")
83-
cov = coverage.Coverage(config_file="pyproject.toml")
84+
cov = coverage.Coverage()
8485
assert cov.config.timid
8586
assert not cov.config.branch
8687
assert cov.config.concurrency == ["a", "b"]
@@ -91,13 +92,14 @@ def test_toml_config_file(self):
9192
assert cov.config.fail_under == 90.5
9293
assert cov.config.get_plugin_options("plugins.a_plugin") == {"hello": "world"}
9394

95+
def test_toml_ints_can_be_floats(self):
9496
# Test that our class doesn't reject integers when loading floats
9597
self.make_file("pyproject.toml", """\
9698
# This is just a bogus toml file for testing.
9799
[tool.coverage.report]
98100
fail_under = 90
99101
""")
100-
cov = coverage.Coverage(config_file="pyproject.toml")
102+
cov = coverage.Coverage()
101103
assert cov.config.fail_under == 90
102104
assert isinstance(cov.config.fail_under, float)
103105

@@ -435,7 +437,8 @@ def test_exceptions_from_missing_things(self):
435437
[run]
436438
branch = True
437439
""")
438-
config = HandyConfigParser("config.ini")
440+
config = HandyConfigParser(True)
441+
config.read(["config.ini"])
439442
with pytest.raises(ConfigError, match="No section: 'xyzzy'"):
440443
config.options("xyzzy")
441444
with pytest.raises(ConfigError, match="No option 'foo' in section: 'xyzzy'"):
@@ -756,3 +759,17 @@ def test_no_toml_installed_pyproject_no_coverage(self):
756759
assert not cov.config.timid
757760
assert not cov.config.branch
758761
assert cov.config.data_file == ".coverage"
762+
763+
def test_exceptions_from_missing_toml_things(self):
764+
self.make_file("pyproject.toml", """\
765+
[tool.coverage.run]
766+
branch = true
767+
""")
768+
config = TomlConfigParser(False)
769+
config.read("pyproject.toml")
770+
with pytest.raises(ConfigError, match="No section: 'xyzzy'"):
771+
config.options("xyzzy")
772+
with pytest.raises(ConfigError, match="No section: 'xyzzy'"):
773+
config.get("xyzzy", "foo")
774+
with pytest.raises(ConfigError, match="No option 'foo' in section: 'tool.coverage.run'"):
775+
config.get("run", "foo")

0 commit comments

Comments
 (0)