Skip to content

Commit a09b171

Browse files
committed
Simplify the testing of the toml extra, fixing nedbat#1084
1 parent 94239ad commit a09b171

File tree

6 files changed

+44
-92
lines changed

6 files changed

+44
-92
lines changed

CHANGES.rst

+4
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ Unreleased
3838
- Combining files on Windows across drives how works properly, fixing `issue
3939
577`_. Thanks, `Valentine Lab <pr1080_>`_.
4040

41+
- Fix an obscure warning from deep in the _decimal module, as reported in
42+
`issue 1084`_.
43+
4144
- Update to support Python 3.10 alphas in progress, including `PEP 626: Precise
4245
line numbers for debugging and other tools <pep626_>`_.
4346

4447
.. _issue 577: https://github.com/nedbat/coveragepy/issues/577
4548
.. _issue 732: https://github.com/nedbat/coveragepy/issues/732
4649
.. _issue 922: https://github.com/nedbat/coveragepy/issues/922
50+
.. _issue 1084: https://github.com/nedbat/coveragepy/issues/1084
4751
.. _issue 1086: https://github.com/nedbat/coveragepy/issues/1086
4852
.. _issue 1090: https://github.com/nedbat/coveragepy/issues/1090
4953
.. _pr1080: https://github.com/nedbat/coveragepy/pull/1080

coverage/optional.py

-76
This file was deleted.

coverage/tomlconfig.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
from coverage.backward import configparser, path_types
1212
from coverage.misc import CoverageException, substitute_variables
1313

14+
# TOML support is an install-time extra option.
15+
try:
16+
import toml
17+
except ImportError: # pragma: not covered
18+
toml = None
19+
1420

1521
class TomlDecodeError(Exception):
1622
"""An exception class that exists even when toml isn't installed."""
@@ -29,8 +35,6 @@ def __init__(self, our_file):
2935
self.data = None
3036

3137
def read(self, filenames):
32-
from coverage.optional import toml
33-
3438
# RawConfigParser takes a filename or list of filenames, but we only
3539
# ever call this with a single filename.
3640
assert isinstance(filenames, path_types)

tests/helpers.py

+19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import subprocess
1111
import sys
1212

13+
import mock
1314
from unittest_mixins import ModuleCleaner
1415

1516
from coverage import env
@@ -203,3 +204,21 @@ def arcs_to_arcz_repr(arcs):
203204
line += _arcs_to_arcz_repr_one(b)
204205
repr_list.append(line)
205206
return "\n".join(repr_list) + "\n"
207+
208+
209+
def without_module(using_module, missing_module_name):
210+
"""
211+
Hide a module for testing.
212+
213+
Use this in a test function to make an optional module unavailable during
214+
the test::
215+
216+
with without_module(product.something, 'toml'):
217+
use_toml_somehow()
218+
219+
Arguments:
220+
using_module: a module in which to hide `missing_module_name`.
221+
missing_module_name (str): the name of the module to hide.
222+
223+
"""
224+
return mock.patch.object(using_module, missing_module_name, None)

tests/test_config.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
import coverage
1212
from coverage.misc import CoverageException
13-
import coverage.optional
1413

1514
from tests.coveragetest import CoverageTest, UsingModulesMixin
15+
from tests.helpers import without_module
1616

1717

1818
class ConfigTest(CoverageTest):
@@ -712,15 +712,15 @@ def test_nocoveragerc_file_when_specified(self):
712712

713713
def test_no_toml_installed_no_toml(self):
714714
# Can't read a toml file that doesn't exist.
715-
with coverage.optional.without('toml'):
715+
with without_module(coverage.tomlconfig, 'toml'):
716716
msg = "Couldn't read 'cov.toml' as a config file"
717717
with self.assertRaisesRegex(CoverageException, msg):
718718
coverage.Coverage(config_file="cov.toml")
719719

720720
def test_no_toml_installed_explicit_toml(self):
721721
# Can't specify a toml config file if toml isn't installed.
722722
self.make_file("cov.toml", "# A toml file!")
723-
with coverage.optional.without('toml'):
723+
with without_module(coverage.tomlconfig, 'toml'):
724724
msg = "Can't read 'cov.toml' without TOML support"
725725
with self.assertRaisesRegex(CoverageException, msg):
726726
coverage.Coverage(config_file="cov.toml")
@@ -732,7 +732,7 @@ def test_no_toml_installed_pyproject_toml(self):
732732
[tool.coverage.run]
733733
xyzzy = 17
734734
""")
735-
with coverage.optional.without('toml'):
735+
with without_module(coverage.tomlconfig, 'toml'):
736736
msg = "Can't read 'pyproject.toml' without TOML support"
737737
with self.assertRaisesRegex(CoverageException, msg):
738738
coverage.Coverage()
@@ -744,7 +744,7 @@ def test_no_toml_installed_pyproject_no_coverage(self):
744744
[tool.something]
745745
xyzzy = 17
746746
""")
747-
with coverage.optional.without('toml'):
747+
with without_module(coverage.tomlconfig, 'toml'):
748748
cov = coverage.Coverage()
749749
# We get default settings:
750750
self.assertFalse(cov.config.timid)

tests/test_testing.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
import pytest
1313

1414
import coverage
15+
from coverage import tomlconfig
1516
from coverage.backunittest import TestCase, unittest
1617
from coverage.files import actual_path
1718
from coverage.misc import StopEverything
18-
import coverage.optional
1919

2020
from tests.coveragetest import CoverageTest, convert_skip_exceptions
21-
from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs
22-
from tests.helpers import CheckUniqueFilenames, re_lines, re_line
21+
from tests.helpers import (
22+
arcs_to_arcz_repr, arcz_to_arcs,
23+
CheckUniqueFilenames, re_lines, re_line, without_module,
24+
)
2325

2426

2527
def test_xdist_sys_path_nuttiness_is_fixed():
@@ -323,12 +325,11 @@ def _same_python_executable(e1, e2):
323325
return False # pragma: only failure
324326

325327

326-
def test_optional_without():
327-
# pylint: disable=reimported
328-
from coverage.optional import toml as toml1
329-
with coverage.optional.without('toml'):
330-
from coverage.optional import toml as toml2
331-
from coverage.optional import toml as toml3
328+
def test_without_module():
329+
toml1 = tomlconfig.toml
330+
with without_module(tomlconfig, 'toml'):
331+
toml2 = tomlconfig.toml
332+
toml3 = tomlconfig.toml
332333

333334
assert toml1 is toml3 is not None
334335
assert toml2 is None

0 commit comments

Comments
 (0)