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