Skip to content

Commit

Permalink
Prefer unittest.mock for Python versions that are sufficient
Browse files Browse the repository at this point in the history
  • Loading branch information
ycedres committed Nov 20, 2023
1 parent 9942c48 commit 8ca767a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 32 deletions.
4 changes: 1 addition & 3 deletions tests/pytests/unit/cloud/clouds/test_dimensiondata.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from salt.exceptions import SaltCloudSystemExit
from salt.utils.versions import Version
from tests.support.mock import MagicMock
from tests.support.mock import __version__ as mock_version
from tests.support.mock import patch

try:
Expand Down Expand Up @@ -144,8 +143,7 @@ def test_import():
with patch("salt.config.check_driver_dependencies", return_value=True) as p:
get_deps = dimensiondata.get_dependencies()
assert get_deps is True
if Version(mock_version) >= Version("2.0.0"):
assert p.call_count >= 1
assert p.call_count >= 1


def test_provider_matches():
Expand Down
4 changes: 1 addition & 3 deletions tests/pytests/unit/cloud/clouds/test_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from salt.exceptions import SaltCloudSystemExit
from salt.utils.versions import Version
from tests.support.mock import MagicMock
from tests.support.mock import __version__ as mock_version
from tests.support.mock import call, patch


Expand Down Expand Up @@ -281,8 +280,7 @@ def test_import():
with patch("salt.config.check_driver_dependencies", return_value=True) as p:
get_deps = gce.get_dependencies()
assert get_deps is True
if Version(mock_version) >= Version("2.0.0"):
p.assert_called_once()
p.assert_called_once()


@pytest.mark.parametrize(
Expand Down
48 changes: 22 additions & 26 deletions tests/support/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,33 @@
import errno
import fnmatch
import sys

# By these days, we should blowup if mock is not available
import mock # pylint: disable=blacklisted-external-import

# pylint: disable=no-name-in-module,no-member
from mock import (
ANY,
DEFAULT,
FILTER_DIR,
MagicMock,
Mock,
NonCallableMagicMock,
NonCallableMock,
PropertyMock,
__version__,
call,
create_autospec,
patch,
sentinel,
)
import importlib

current_version = (sys.version_info.major, sys.version_info.minor)

# Prefer unittest.mock for Python versions that are sufficient
if current_version >= (3,8):
mock = importlib.import_module('unittest.mock')
else:
mock = importlib.import_module('mock')

ANY = mock.ANY
DEFAULT = mock.DEFAULT
FILTER_DIR = mock.FILTER_DIR
MagicMock = mock.MagicMock
Mock = mock.Mock
NonCallableMagicMock = mock.NonCallableMagicMock
NonCallableMock = mock.NonCallableMock
PropertyMock = mock.PropertyMock
call = mock.call
create_autospec = mock.create_autospec
patch = mock.patch
sentinel = mock.sentinel

import salt.utils.stringutils

# pylint: disable=no-name-in-module,no-member


__mock_version = tuple(
int(part) for part in mock.__version__.split(".") if part.isdigit()
) # pylint: disable=no-member


class MockFH:
def __init__(self, filename, read_data, *args, **kwargs):
self.filename = filename
Expand Down

0 comments on commit 8ca767a

Please sign in to comment.