Skip to content

Commit

Permalink
Simplify the testing of the toml extra, fixing #1084
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Jan 18, 2021
1 parent 94239ad commit a09b171
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ Unreleased
- Combining files on Windows across drives how works properly, fixing `issue
577`_. Thanks, `Valentine Lab <pr1080_>`_.

- Fix an obscure warning from deep in the _decimal module, as reported in
`issue 1084`_.

- Update to support Python 3.10 alphas in progress, including `PEP 626: Precise
line numbers for debugging and other tools <pep626_>`_.

.. _issue 577: https://github.com/nedbat/coveragepy/issues/577
.. _issue 732: https://github.com/nedbat/coveragepy/issues/732
.. _issue 922: https://github.com/nedbat/coveragepy/issues/922
.. _issue 1084: https://github.com/nedbat/coveragepy/issues/1084
.. _issue 1086: https://github.com/nedbat/coveragepy/issues/1086
.. _issue 1090: https://github.com/nedbat/coveragepy/issues/1090
.. _pr1080: https://github.com/nedbat/coveragepy/pull/1080
Expand Down
76 changes: 0 additions & 76 deletions coverage/optional.py

This file was deleted.

8 changes: 6 additions & 2 deletions coverage/tomlconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
from coverage.backward import configparser, path_types
from coverage.misc import CoverageException, substitute_variables

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


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

def read(self, filenames):
from coverage.optional import toml

# RawConfigParser takes a filename or list of filenames, but we only
# ever call this with a single filename.
assert isinstance(filenames, path_types)
Expand Down
19 changes: 19 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import subprocess
import sys

import mock
from unittest_mixins import ModuleCleaner

from coverage import env
Expand Down Expand Up @@ -203,3 +204,21 @@ def arcs_to_arcz_repr(arcs):
line += _arcs_to_arcz_repr_one(b)
repr_list.append(line)
return "\n".join(repr_list) + "\n"


def without_module(using_module, missing_module_name):
"""
Hide a module for testing.
Use this in a test function to make an optional module unavailable during
the test::
with without_module(product.something, 'toml'):
use_toml_somehow()
Arguments:
using_module: a module in which to hide `missing_module_name`.
missing_module_name (str): the name of the module to hide.
"""
return mock.patch.object(using_module, missing_module_name, None)
10 changes: 5 additions & 5 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@

import coverage
from coverage.misc import CoverageException
import coverage.optional

from tests.coveragetest import CoverageTest, UsingModulesMixin
from tests.helpers import without_module


class ConfigTest(CoverageTest):
Expand Down Expand Up @@ -712,15 +712,15 @@ def test_nocoveragerc_file_when_specified(self):

def test_no_toml_installed_no_toml(self):
# Can't read a toml file that doesn't exist.
with coverage.optional.without('toml'):
with without_module(coverage.tomlconfig, 'toml'):
msg = "Couldn't read 'cov.toml' as a config file"
with self.assertRaisesRegex(CoverageException, 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 coverage.optional.without('toml'):
with without_module(coverage.tomlconfig, 'toml'):
msg = "Can't read 'cov.toml' without TOML support"
with self.assertRaisesRegex(CoverageException, msg):
coverage.Coverage(config_file="cov.toml")
Expand All @@ -732,7 +732,7 @@ def test_no_toml_installed_pyproject_toml(self):
[tool.coverage.run]
xyzzy = 17
""")
with coverage.optional.without('toml'):
with without_module(coverage.tomlconfig, 'toml'):
msg = "Can't read 'pyproject.toml' without TOML support"
with self.assertRaisesRegex(CoverageException, msg):
coverage.Coverage()
Expand All @@ -744,7 +744,7 @@ def test_no_toml_installed_pyproject_no_coverage(self):
[tool.something]
xyzzy = 17
""")
with coverage.optional.without('toml'):
with without_module(coverage.tomlconfig, 'toml'):
cov = coverage.Coverage()
# We get default settings:
self.assertFalse(cov.config.timid)
Expand Down
19 changes: 10 additions & 9 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
import pytest

import coverage
from coverage import tomlconfig
from coverage.backunittest import TestCase, unittest
from coverage.files import actual_path
from coverage.misc import StopEverything
import coverage.optional

from tests.coveragetest import CoverageTest, convert_skip_exceptions
from tests.helpers import arcs_to_arcz_repr, arcz_to_arcs
from tests.helpers import CheckUniqueFilenames, re_lines, re_line
from tests.helpers import (
arcs_to_arcz_repr, arcz_to_arcs,
CheckUniqueFilenames, re_lines, re_line, without_module,
)


def test_xdist_sys_path_nuttiness_is_fixed():
Expand Down Expand Up @@ -323,12 +325,11 @@ def _same_python_executable(e1, e2):
return False # pragma: only failure


def test_optional_without():
# pylint: disable=reimported
from coverage.optional import toml as toml1
with coverage.optional.without('toml'):
from coverage.optional import toml as toml2
from coverage.optional import toml as toml3
def test_without_module():
toml1 = tomlconfig.toml
with without_module(tomlconfig, 'toml'):
toml2 = tomlconfig.toml
toml3 = tomlconfig.toml

assert toml1 is toml3 is not None
assert toml2 is None
Expand Down

0 comments on commit a09b171

Please sign in to comment.