From ad34dfa2f983bb3159af8b5780193e0427b505e9 Mon Sep 17 00:00:00 2001 From: Vojtech Trefny Date: Thu, 10 Jun 2021 15:01:26 +0200 Subject: [PATCH] Fix/unify importing mock module in tests mock is available in the unittest module since Python 3.3, we need to use the old mock module not only with Python 2 but also with the early versions of Python 3. --- tests/action_test.py | 5 ++- tests/dbus_test.py | 9 +++-- tests/devicefactory_test.py | 6 ++-- tests/devicelibs_test/disk_test.py | 6 ++-- tests/devicelibs_test/edd_test.py | 6 +++- tests/devices_test/dependencies_test.py | 6 ++-- tests/devices_test/device_methods_test.py | 8 +++-- tests/devices_test/device_names_test.py | 6 ++-- tests/devices_test/device_properties_test.py | 8 ++--- tests/devices_test/disk_test.py | 7 ++-- tests/devices_test/lvm_test.py | 7 ++-- tests/devices_test/partition_test.py | 6 ++-- tests/devices_test/tags_test.py | 6 ++-- tests/devicetree_test.py | 6 ++-- tests/events_test.py | 6 ++-- tests/formats_test/disklabel_test.py | 6 ++-- tests/formats_test/luks_test.py | 6 ++-- tests/formats_test/lvmpv_test.py | 2 -- tests/formats_test/methods_test.py | 6 ++-- tests/formats_test/selinux_test.py | 6 ++-- tests/formats_test/swap_test.py | 2 -- tests/misc_test.py | 6 ++-- tests/partitioning_test.py | 6 ++-- tests/populator_test.py | 6 ++-- tests/storagetestcase.py | 5 ++- tests/test_compat.py | 38 -------------------- tests/udev_test.py | 6 +++- tests/unsupported_disklabel_test.py | 7 ++-- tests/util_test.py | 6 ++-- 29 files changed, 104 insertions(+), 102 deletions(-) delete mode 100644 tests/test_compat.py diff --git a/tests/action_test.py b/tests/action_test.py index 38a2e872..f60cf5d7 100644 --- a/tests/action_test.py +++ b/tests/action_test.py @@ -1,9 +1,8 @@ -from six import PY3 import unittest -if PY3: +try: from unittest.mock import Mock -else: +except ImportError: from mock import Mock from tests.storagetestcase import StorageTestCase diff --git a/tests/dbus_test.py b/tests/dbus_test.py index 293ac073..9bb9102a 100644 --- a/tests/dbus_test.py +++ b/tests/dbus_test.py @@ -1,7 +1,10 @@ -import test_compat # pylint: disable=unused-import - import random -from six.moves.mock import Mock, patch # pylint: disable=no-name-in-module,import-error + +try: + from unittest.mock import patch, Mock +except ImportError: + from mock import patch, Mock + from unittest import TestCase import dbus diff --git a/tests/devicefactory_test.py b/tests/devicefactory_test.py index dc0d6408..93c8bdb7 100644 --- a/tests/devicefactory_test.py +++ b/tests/devicefactory_test.py @@ -4,8 +4,10 @@ import unittest from decimal import Decimal import os -import test_compat # pylint: disable=unused-import -from six.moves.mock import patch # pylint: disable=no-name-in-module,import-error +try: + from unittest.mock import patch +except ImportError: + from mock import patch import blivet diff --git a/tests/devicelibs_test/disk_test.py b/tests/devicelibs_test/disk_test.py index e67ef5b1..9cb39951 100644 --- a/tests/devicelibs_test/disk_test.py +++ b/tests/devicelibs_test/disk_test.py @@ -1,8 +1,10 @@ # pylint: skip-file -import test_compat +try: + from unittest.mock import Mock, patch, sentinel +except ImportError: + from mock import Mock, patch, sentinel import six -from six.moves.mock import Mock, patch, sentinel import unittest from blivet.devicelibs import disk as disklib diff --git a/tests/devicelibs_test/edd_test.py b/tests/devicelibs_test/edd_test.py index 23d736f4..21bbcffc 100644 --- a/tests/devicelibs_test/edd_test.py +++ b/tests/devicelibs_test/edd_test.py @@ -1,5 +1,9 @@ +try: + from unittest import mock +except ImportError: + import mock + import unittest -import mock import os import inspect import logging diff --git a/tests/devices_test/dependencies_test.py b/tests/devices_test/dependencies_test.py index c012751d..493d1c9f 100644 --- a/tests/devices_test/dependencies_test.py +++ b/tests/devices_test/dependencies_test.py @@ -1,8 +1,6 @@ -# vim:set fileencoding=utf-8 -from six import PY3 -if PY3: +try: from unittest.mock import patch, PropertyMock -else: +except ImportError: from mock import patch, PropertyMock import unittest diff --git a/tests/devices_test/device_methods_test.py b/tests/devices_test/device_methods_test.py index f00509be..8a70b5bb 100644 --- a/tests/devices_test/device_methods_test.py +++ b/tests/devices_test/device_methods_test.py @@ -1,9 +1,11 @@ -import test_compat # pylint: disable=unused-import - import six -from six.moves.mock import patch, Mock, PropertyMock # pylint: disable=no-name-in-module,import-error import unittest +try: + from unittest.mock import patch, Mock, PropertyMock +except ImportError: + from mock import patch, PropertyMock + from blivet.devices import StorageDevice from blivet.devices import DiskDevice, PartitionDevice from blivet.devices import LVMVolumeGroupDevice, LVMLogicalVolumeDevice diff --git a/tests/devices_test/device_names_test.py b/tests/devices_test/device_names_test.py index 2a66f983..ca44d38c 100644 --- a/tests/devices_test/device_names_test.py +++ b/tests/devices_test/device_names_test.py @@ -1,7 +1,9 @@ # vim:set fileencoding=utf-8 -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch +except ImportError: + from mock import patch -from six.moves.mock import patch # pylint: disable=no-name-in-module,import-error import six import unittest diff --git a/tests/devices_test/device_properties_test.py b/tests/devices_test/device_properties_test.py index 240ac088..8928707f 100644 --- a/tests/devices_test/device_properties_test.py +++ b/tests/devices_test/device_properties_test.py @@ -1,6 +1,3 @@ -# vim:set fileencoding=utf-8 -import test_compat # pylint: disable=unused-import - import six import unittest @@ -9,7 +6,10 @@ gi.require_version("BlockDev", "2.0") from gi.repository import BlockDev as blockdev -from six.moves.mock import Mock, patch # pylint: disable=no-name-in-module,import-error +try: + from unittest.mock import patch, Mock +except ImportError: + from mock import patch, Mock import blivet diff --git a/tests/devices_test/disk_test.py b/tests/devices_test/disk_test.py index e9852303..cc8454e1 100644 --- a/tests/devices_test/disk_test.py +++ b/tests/devices_test/disk_test.py @@ -1,7 +1,8 @@ # pylint: skip-file -import test_compat - -from six.moves.mock import patch +try: + from unittest.mock import patch +except ImportError: + from mock import patch import unittest from blivet.devices import DiskDevice diff --git a/tests/devices_test/lvm_test.py b/tests/devices_test/lvm_test.py index 670c91c9..f50933c4 100644 --- a/tests/devices_test/lvm_test.py +++ b/tests/devices_test/lvm_test.py @@ -1,8 +1,9 @@ -# vim:set fileencoding=utf-8 -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch, PropertyMock +except ImportError: + from mock import patch, PropertyMock import six -from six.moves.mock import patch, PropertyMock # pylint: disable=no-name-in-module,import-error import unittest import blivet diff --git a/tests/devices_test/partition_test.py b/tests/devices_test/partition_test.py index 0abd88df..4748dafe 100644 --- a/tests/devices_test/partition_test.py +++ b/tests/devices_test/partition_test.py @@ -1,5 +1,4 @@ # vim:set fileencoding=utf-8 -import test_compat # pylint: disable=unused-import from collections import namedtuple import os @@ -7,7 +6,10 @@ import six import unittest import parted -from six.moves.mock import Mock, patch # pylint: disable=no-name-in-module,import-error +try: + from unittest.mock import patch, Mock +except ImportError: + from mock import patch, Mock from blivet.devices import DiskFile from blivet.devices import PartitionDevice diff --git a/tests/devices_test/tags_test.py b/tests/devices_test/tags_test.py index 1edc37f6..49a2d72e 100644 --- a/tests/devices_test/tags_test.py +++ b/tests/devices_test/tags_test.py @@ -1,6 +1,8 @@ -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch +except ImportError: + from mock import patch -from six.moves.mock import patch # pylint: disable=no-name-in-module,import-error import unittest from blivet.devices import DiskDevice, FcoeDiskDevice, iScsiDiskDevice, MultipathDevice, StorageDevice, ZFCPDiskDevice diff --git a/tests/devicetree_test.py b/tests/devicetree_test.py index 11f8469d..fbf31c77 100644 --- a/tests/devicetree_test.py +++ b/tests/devicetree_test.py @@ -1,6 +1,8 @@ -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch, Mock, PropertyMock, sentinel +except ImportError: + from mock import patch, Mock, PropertyMock, sentinel -from six.moves.mock import Mock, patch, PropertyMock, sentinel # pylint: disable=no-name-in-module,import-error import os import six import unittest diff --git a/tests/events_test.py b/tests/events_test.py index 5906b4e2..22666f6d 100644 --- a/tests/events_test.py +++ b/tests/events_test.py @@ -1,6 +1,8 @@ -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch, Mock +except ImportError: + from mock import patch, Mock -from six.moves.mock import Mock, patch # pylint: disable=no-name-in-module,import-error import time from unittest import TestCase diff --git a/tests/formats_test/disklabel_test.py b/tests/formats_test/disklabel_test.py index 0cfa736d..f514a778 100644 --- a/tests/formats_test/disklabel_test.py +++ b/tests/formats_test/disklabel_test.py @@ -1,7 +1,9 @@ -import test_compat # pylint: disable=unused-import +try: + from unittest import mock +except ImportError: + import mock import parted -from six.moves import mock # pylint: disable=no-name-in-module,import-error import unittest import blivet diff --git a/tests/formats_test/luks_test.py b/tests/formats_test/luks_test.py index be0d50b0..1edbdcb2 100644 --- a/tests/formats_test/luks_test.py +++ b/tests/formats_test/luks_test.py @@ -1,6 +1,8 @@ -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch +except ImportError: + from mock import patch -from six.moves.mock import patch # pylint: disable=no-name-in-module,import-error import unittest from blivet.formats.luks import LUKS diff --git a/tests/formats_test/lvmpv_test.py b/tests/formats_test/lvmpv_test.py index 792a2f1d..cbd2c419 100644 --- a/tests/formats_test/lvmpv_test.py +++ b/tests/formats_test/lvmpv_test.py @@ -1,5 +1,3 @@ -import test_compat # pylint: disable=unused-import - from blivet.formats.lvmpv import LVMPhysicalVolume from blivet.size import Size diff --git a/tests/formats_test/methods_test.py b/tests/formats_test/methods_test.py index b2674ea7..2743b7db 100644 --- a/tests/formats_test/methods_test.py +++ b/tests/formats_test/methods_test.py @@ -1,7 +1,9 @@ -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch, sentinel, PropertyMock +except ImportError: + from mock import patch, sentinel, PropertyMock import six -from six.moves.mock import patch, sentinel, PropertyMock # pylint: disable=no-name-in-module,import-error import unittest from blivet.errors import DeviceFormatError diff --git a/tests/formats_test/selinux_test.py b/tests/formats_test/selinux_test.py index 02e39011..26df5fe9 100644 --- a/tests/formats_test/selinux_test.py +++ b/tests/formats_test/selinux_test.py @@ -1,9 +1,9 @@ # pylint: disable=unused-import import os -from six import PY3 -if PY3: + +try: from unittest.mock import patch, ANY -else: +except ImportError: from mock import patch, ANY import unittest diff --git a/tests/formats_test/swap_test.py b/tests/formats_test/swap_test.py index 56356144..8968ca15 100644 --- a/tests/formats_test/swap_test.py +++ b/tests/formats_test/swap_test.py @@ -1,5 +1,3 @@ -import test_compat # pylint: disable=unused-import - import six import unittest diff --git a/tests/misc_test.py b/tests/misc_test.py index 3c8cf344..10ea1320 100644 --- a/tests/misc_test.py +++ b/tests/misc_test.py @@ -1,7 +1,9 @@ import unittest -import test_compat # pylint: disable=unused-import -from six.moves.mock import patch # pylint: disable=no-name-in-module,import-error +try: + from unittest.mock import patch +except ImportError: + from mock import patch import blivet diff --git a/tests/partitioning_test.py b/tests/partitioning_test.py index b7aa5045..9b27f0c0 100644 --- a/tests/partitioning_test.py +++ b/tests/partitioning_test.py @@ -1,6 +1,8 @@ -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch, Mock +except ImportError: + from mock import patch, Mock -from six.moves.mock import Mock, patch # pylint: disable=no-name-in-module,import-error import six import unittest diff --git a/tests/populator_test.py b/tests/populator_test.py index a7748a9d..2a8532f0 100644 --- a/tests/populator_test.py +++ b/tests/populator_test.py @@ -1,7 +1,9 @@ -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import call, patch, sentinel, Mock, PropertyMock +except ImportError: + from mock import call, patch, sentinel, Mock, PropertyMock import gi -from six.moves.mock import call, patch, sentinel, Mock, PropertyMock # pylint: disable=no-name-in-module,import-error import six import unittest diff --git a/tests/storagetestcase.py b/tests/storagetestcase.py index 1844dec5..1b856914 100644 --- a/tests/storagetestcase.py +++ b/tests/storagetestcase.py @@ -1,6 +1,9 @@ import unittest -from mock import Mock +try: + from unittest.mock import Mock +except ImportError: + from mock import Mock import parted diff --git a/tests/test_compat.py b/tests/test_compat.py deleted file mode 100644 index d0859e24..00000000 --- a/tests/test_compat.py +++ /dev/null @@ -1,38 +0,0 @@ -# test_compat.py -# Python (2 -v- 3) compatibility functions. -# -# Copyright (C) 2017 Red Hat, Inc. -# -# This copyrighted material is made available to anyone wishing to use, -# modify, copy, or redistribute it subject to the terms and conditions of -# the GNU Lesser General Public License v.2, or (at your option) any later -# version. This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY expressed or implied, including the implied -# warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See -# the GNU Lesser General Public License for more details. You should have -# received a copy of the GNU Lesser General Public License along with this -# program; if not, write to the Free Software Foundation, Inc., 51 Franklin -# Street, Fifth Floor, Boston, MA 02110-1301, USA. Any Red Hat trademarks -# that are incorporated in the source code or documentation are not subject -# to the GNU Lesser General Public License and may only be used or -# replicated with the express permission of Red Hat, Inc. -# -# Red Hat Author(s): David Lehman -# - -import six as _six - -mock_move = _six.MovedModule('mock', 'mock', 'unittest.mock') - - -def add_move(mod): - _six.add_move(mod) - # https://bitbucket.org/gutworth/six/issues/116/enable-importing-from-within-custom - _six._importer._add_module(mod, "moves." + mod.name) - - -def setup(): - add_move(mock_move) - - -setup() diff --git a/tests/udev_test.py b/tests/udev_test.py index f9b10620..569a144e 100644 --- a/tests/udev_test.py +++ b/tests/udev_test.py @@ -1,6 +1,10 @@ import unittest -import mock + +try: + from unittest import mock +except ImportError: + import mock from udev_data import raid_data diff --git a/tests/unsupported_disklabel_test.py b/tests/unsupported_disklabel_test.py index f5b24779..38055333 100644 --- a/tests/unsupported_disklabel_test.py +++ b/tests/unsupported_disklabel_test.py @@ -1,7 +1,8 @@ -# vim:set fileencoding=utf-8 -import test_compat # pylint: disable=unused-import +try: + from unittest.mock import patch, sentinel, DEFAULT +except ImportError: + from mock import patch, sentinel, DEFAULT -from six.moves.mock import patch, sentinel, DEFAULT # pylint: disable=no-name-in-module,import-error import six import unittest diff --git a/tests/util_test.py b/tests/util_test.py index 853b6166..b4f82c1b 100644 --- a/tests/util_test.py +++ b/tests/util_test.py @@ -1,7 +1,9 @@ # pylint: skip-file -import test_compat +try: + from unittest import mock +except ImportError: + import mock -from six.moves import mock import os import six import tempfile -- 2.31.1