Skip to content

Commit

Permalink
Support TOML v1.0.0 syntax in pyproject.toml
Browse files Browse the repository at this point in the history
fixes nedbat#1180

Co-authored-by: Taneli Hukkinen <[email protected]>
  • Loading branch information
graingert and hukkin committed Jul 9, 2021
1 parent 809cccb commit c3340b6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
12 changes: 6 additions & 6 deletions coverage/tomlconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

# TOML support is an install-time extra option.
try:
import toml
import tomli
except ImportError: # pragma: not covered
toml = None
tomli = None


class TomlDecodeError(Exception):
Expand Down Expand Up @@ -44,12 +44,12 @@ def read(self, filenames):
toml_text = fp.read()
except OSError:
return []
if toml:
if tomli is not None:
toml_text = substitute_variables(toml_text, os.environ)
try:
self.data = toml.loads(toml_text)
except toml.TomlDecodeError as err:
raise TomlDecodeError(*err.args)
self.data = tomli.loads(toml_text)
except tomli.TOMLDecodeError as err:
raise TomlDecodeError(str(err))
return [filename]
else:
has_toml = re.search(r"^\[tool\.coverage\.", toml_text, flags=re.MULTILINE)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def better_set_verbosity(v):

extras_require={
# Enable pyproject.toml support.
'toml': ['toml'],
'toml': ['tomli'],
},

# We need to get HTML assets from our htmlfiles directory.
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def without_module(using_module, missing_module_name):
Use this in a test function to make an optional module unavailable during
the test::
with without_module(product.something, 'toml'):
with without_module(product.something, 'tomli'):
use_toml_somehow()
Arguments:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,15 +715,15 @@ def test_note_is_obsolete(self):

def test_no_toml_installed_no_toml(self):
# Can't read a toml file that doesn't exist.
with without_module(coverage.tomlconfig, 'toml'):
with without_module(coverage.tomlconfig, 'tomli'):
msg = "Couldn't read 'cov.toml' as a config file"
with pytest.raises(CoverageException, match=msg):
coverage.Coverage(config_file="cov.toml")

def test_no_toml_installed_explicit_toml(self):
# Can't specify a toml config file if toml isn't installed.
self.make_file("cov.toml", "# A toml file!")
with without_module(coverage.tomlconfig, 'toml'):
with without_module(coverage.tomlconfig, 'tomli'):
msg = "Can't read 'cov.toml' without TOML support"
with pytest.raises(CoverageException, match=msg):
coverage.Coverage(config_file="cov.toml")
Expand All @@ -735,7 +735,7 @@ def test_no_toml_installed_pyproject_toml(self):
[tool.coverage.run]
xyzzy = 17
""")
with without_module(coverage.tomlconfig, 'toml'):
with without_module(coverage.tomlconfig, 'tomli'):
msg = "Can't read 'pyproject.toml' without TOML support"
with pytest.raises(CoverageException, match=msg):
coverage.Coverage()
Expand All @@ -747,7 +747,7 @@ def test_no_toml_installed_pyproject_no_coverage(self):
[tool.something]
xyzzy = 17
""")
with without_module(coverage.tomlconfig, 'toml'):
with without_module(coverage.tomlconfig, 'tomli'):
cov = coverage.Coverage()
# We get default settings:
assert not cov.config.timid
Expand Down
2 changes: 1 addition & 1 deletion tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def _same_python_executable(e1, e2):

def test_without_module():
toml1 = tomlconfig.toml
with without_module(tomlconfig, 'toml'):
with without_module(tomlconfig, 'tomli'):
toml2 = tomlconfig.toml
toml3 = tomlconfig.toml

Expand Down

0 comments on commit c3340b6

Please sign in to comment.