mirror of
https://github.com/certbot/certbot.git
synced 2026-06-05 14:54:24 -04:00
Part of #7886. This PR conditionally installs mock in `acme/setup.py` based on setuptools version and python version, when possible. It then updates `acme` tests to use `unittest.mock` when `mock` isn't available. Now with `type: ignore` as appropriate. Once the "future steps" of #7886 are finished, and mypy is on Python 3, the `pragma no cover`s and `type ignore`s will be gone. * Conditionally install mock in acme * error out on newer python and older setuptools * error when trying to build wheels with old setuptools * use unittest.mock when third-party mock isn't available in acme, with no cover and type ignore
44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""Tests for acme.magic_typing."""
|
|
import sys
|
|
import unittest
|
|
|
|
try:
|
|
import mock
|
|
except ImportError: # pragma: no cover
|
|
from unittest import mock # type: ignore
|
|
|
|
|
|
class MagicTypingTest(unittest.TestCase):
|
|
"""Tests for acme.magic_typing."""
|
|
def test_import_success(self):
|
|
try:
|
|
import typing as temp_typing
|
|
except ImportError: # pragma: no cover
|
|
temp_typing = None # pragma: no cover
|
|
typing_class_mock = mock.MagicMock()
|
|
text_mock = mock.MagicMock()
|
|
typing_class_mock.Text = text_mock
|
|
sys.modules['typing'] = typing_class_mock
|
|
if 'acme.magic_typing' in sys.modules:
|
|
del sys.modules['acme.magic_typing'] # pragma: no cover
|
|
from acme.magic_typing import Text
|
|
self.assertEqual(Text, text_mock)
|
|
del sys.modules['acme.magic_typing']
|
|
sys.modules['typing'] = temp_typing
|
|
|
|
def test_import_failure(self):
|
|
try:
|
|
import typing as temp_typing
|
|
except ImportError: # pragma: no cover
|
|
temp_typing = None # pragma: no cover
|
|
sys.modules['typing'] = None
|
|
if 'acme.magic_typing' in sys.modules:
|
|
del sys.modules['acme.magic_typing'] # pragma: no cover
|
|
from acme.magic_typing import Text
|
|
self.assertTrue(Text is None)
|
|
del sys.modules['acme.magic_typing']
|
|
sys.modules['typing'] = temp_typing
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main() # pragma: no cover
|