Drop Py2 and six on tests/support/unit.py

This commit is contained in:
Pedro Algarvio 2021-02-19 17:30:59 +00:00
parent 05bbe14088
commit 575f50d8f0
2 changed files with 15 additions and 169 deletions

View file

@ -63,7 +63,7 @@ class WinTasksTest(ModuleCase):
</Actions>
</Task>
"""
self.assertEquals(
self.assertEqual(
self.run_function("task.create_task_from_xml", "foo", xml_text=xml_text),
True,
)

View file

@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
:codeauthor: Pedro Algarvio (pedro@algarvio.me)
@ -21,7 +20,6 @@
"""
# pylint: disable=unused-import,blacklisted-module,deprecated-method
from __future__ import absolute_import, print_function, unicode_literals
import inspect
import logging
@ -37,8 +35,6 @@ from unittest import TextTestRunner as _TextTestRunner
from unittest import expectedFailure, skip, skipIf
from unittest.case import SkipTest, _id
from salt.ext import six
try:
import psutil
@ -73,11 +69,11 @@ class TestSuite(_TestSuite):
currentClass == previousClass
or getattr(currentClass, "setUpClass", None) is None
):
return super(TestSuite, self)._handleClassSetUp(test, result)
return super()._handleClassSetUp(test, result)
# Store a reference to all class attributes before running the setUpClass method
initial_class_attributes = dir(test.__class__)
super(TestSuite, self)._handleClassSetUp(test, result)
super()._handleClassSetUp(test, result)
# Store the difference in in a variable in order to check later if they were deleted
test.__class__._prerun_class_attributes = [
attr for attr in dir(test.__class__) if attr not in initial_class_attributes
@ -85,7 +81,7 @@ class TestSuite(_TestSuite):
def _tearDownPreviousClass(self, test, result):
# Run any tearDownClass code defined
super(TestSuite, self)._tearDownPreviousClass(test, result)
super()._tearDownPreviousClass(test, result)
previousClass = getattr(result, "_previousTestClass", None)
currentClass = test.__class__
if currentClass == previousClass:
@ -103,9 +99,7 @@ class TestSuite(_TestSuite):
attr_value = getattr(previousClass, attr, None)
if attr_value is None:
continue
if isinstance(
attr_value, (bool,) + six.string_types + six.integer_types
):
if isinstance(attr_value, (bool, str, int)):
setattr(previousClass, attr, None)
continue
log.warning(
@ -154,7 +148,7 @@ class TestSuite(_TestSuite):
break
if skip_klass is True:
klass.__unittest_skip__ = True
return super(TestSuite, self)._handleModuleFixture(test, result)
return super()._handleModuleFixture(test, result)
class TestLoader(_TestLoader):
@ -189,7 +183,7 @@ class TestCase(_TestCase):
def run(self, result=None):
self._prerun_instance_attributes = dir(self)
self.maxDiff = None
outcome = super(TestCase, self).run(result=result)
outcome = super().run(result=result)
for attr in dir(self):
if attr == "_prerun_instance_attributes":
continue
@ -199,9 +193,7 @@ class TestCase(_TestCase):
attr_value = getattr(self, attr, None)
if attr_value is None:
continue
if isinstance(
attr_value, (bool,) + six.string_types + six.integer_types
):
if isinstance(attr_value, (bool, str, int)):
setattr(self, attr, None)
continue
log.warning(
@ -222,7 +214,7 @@ class TestCase(_TestCase):
desc = _TestCase.shortDescription(self)
if HAS_PSUTIL and SHOW_PROC:
show_zombie_processes = "SHOW_PROC_ZOMBIES" in os.environ
proc_info = "[CPU:{0}%|MEM:{1}%".format(
proc_info = "[CPU:{}%|MEM:{}%".format(
psutil.cpu_percent(), psutil.virtual_memory().percent
)
if show_zombie_processes:
@ -233,49 +225,12 @@ class TestCase(_TestCase):
found_zombies += 1
except Exception: # pylint: disable=broad-except
pass
proc_info += "|Z:{0}".format(found_zombies)
proc_info += "|Z:{}".format(found_zombies)
proc_info += "] {short_desc}".format(short_desc=desc if desc else "")
return proc_info
else:
return _TestCase.shortDescription(self)
def assertEquals(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("assertEquals", "assertEqual")
)
# return _TestCase.assertEquals(self, *args, **kwargs)
def assertNotEquals(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("assertNotEquals", "assertNotEqual")
)
# return _TestCase.assertNotEquals(self, *args, **kwargs)
def assert_(self, *args, **kwargs):
# The unittest2 library uses this deprecated method, we can't raise
# the exception.
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("assert_", "assertTrue")
)
# return _TestCase.assert_(self, *args, **kwargs)
def assertAlmostEquals(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("assertAlmostEquals", "assertAlmostEqual")
)
# return _TestCase.assertAlmostEquals(self, *args, **kwargs)
def assertNotAlmostEquals(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("assertNotAlmostEquals", "assertNotAlmostEqual")
)
# return _TestCase.assertNotAlmostEquals(self, *args, **kwargs)
def repack_state_returns(self, state_ret):
"""
Accepts a state return dict and returns it back with the top level key
@ -287,56 +242,7 @@ class TestCase(_TestCase):
after running states.
"""
assert isinstance(state_ret, dict), state_ret
return {x.split("_|-")[1]: y for x, y in six.iteritems(state_ret)}
def failUnlessEqual(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("failUnlessEqual", "assertEqual")
)
# return _TestCase.failUnlessEqual(self, *args, **kwargs)
def failIfEqual(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("failIfEqual", "assertNotEqual")
)
# return _TestCase.failIfEqual(self, *args, **kwargs)
def failUnless(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("failUnless", "assertTrue")
)
# return _TestCase.failUnless(self, *args, **kwargs)
def failIf(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("failIf", "assertFalse")
)
# return _TestCase.failIf(self, *args, **kwargs)
def failUnlessRaises(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("failUnlessRaises", "assertRaises")
)
# return _TestCase.failUnlessRaises(self, *args, **kwargs)
def failUnlessAlmostEqual(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("failUnlessAlmostEqual", "assertAlmostEqual")
)
# return _TestCase.failUnlessAlmostEqual(self, *args, **kwargs)
def failIfAlmostEqual(self, *args, **kwargs):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("failIfAlmostEqual", "assertNotAlmostEqual")
)
# return _TestCase.failIfAlmostEqual(self, *args, **kwargs)
return {x.split("_|-")[1]: y for x, y in state_ret.items()}
@staticmethod
def assert_called_once(mock):
@ -348,66 +254,6 @@ class TestCase(_TestCase):
except AttributeError:
log.warning("assert_called_once invoked, but not available")
if six.PY2:
def assertRegexpMatches(self, *args, **kwds):
raise DeprecationWarning(
"The {0}() function will be deprecated in python 3. Please start "
"using {1}() instead.".format("assertRegexpMatches", "assertRegex")
)
def assertRegex(
self, text, regex, msg=None
): # pylint: disable=arguments-differ
# In python 2, alias to the future python 3 function
return _TestCase.assertRegexpMatches(self, text, regex, msg=msg)
def assertNotRegexpMatches(self, *args, **kwds):
raise DeprecationWarning(
"The {0}() function will be deprecated in python 3. Please start "
"using {1}() instead.".format(
"assertNotRegexpMatches", "assertNotRegex"
)
)
def assertNotRegex(
self, text, regex, msg=None
): # pylint: disable=arguments-differ
# In python 2, alias to the future python 3 function
return _TestCase.assertNotRegexpMatches(self, text, regex, msg=msg)
def assertRaisesRegexp(self, *args, **kwds):
raise DeprecationWarning(
"The {0}() function will be deprecated in python 3. Please start "
"using {1}() instead.".format("assertRaisesRegexp", "assertRaisesRegex")
)
def assertRaisesRegex(
self, exception, regexp, *args, **kwds
): # pylint: disable=arguments-differ
# In python 2, alias to the future python 3 function
return _TestCase.assertRaisesRegexp(self, exception, regexp, *args, **kwds)
else:
def assertRegexpMatches(self, *args, **kwds):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("assertRegexpMatches", "assertRegex")
)
def assertNotRegexpMatches(self, *args, **kwds):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("assertNotRegexpMatches", "assertNotRegex")
)
def assertRaisesRegexp(self, *args, **kwds):
raise DeprecationWarning(
"The {0}() function is deprecated. Please start using {1}() "
"instead.".format("assertRaisesRegexp", "assertRaisesRegex")
)
class TextTestResult(_TextTestResult):
"""
@ -415,12 +261,12 @@ class TextTestResult(_TextTestResult):
"""
def startTest(self, test):
log.debug(">>>>> START >>>>> {0}".format(test.id()))
return super(TextTestResult, self).startTest(test)
log.debug(">>>>> START >>>>> %s", test.id())
return super().startTest(test)
def stopTest(self, test):
log.debug("<<<<< END <<<<<<< {0}".format(test.id()))
return super(TextTestResult, self).stopTest(test)
log.debug("<<<<< END <<<<<<< %s", test.id())
return super().stopTest(test)
class TextTestRunner(_TextTestRunner):