From 702ed89007b7458d08bd531a9c2e2879d10f2ee5 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Thu, 18 Aug 2016 14:49:11 -0700 Subject: [PATCH 1/5] use six instead of custom refresh function --- certbot/plugins/util_test.py | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/certbot/plugins/util_test.py b/certbot/plugins/util_test.py index e1a064fb3..63a472fa0 100644 --- a/certbot/plugins/util_test.py +++ b/certbot/plugins/util_test.py @@ -4,13 +4,7 @@ import unittest import sys import mock - -try: - # Python 3.5+ - from importlib import reload as refresh # pylint: disable=no-name-in-module -except ImportError: - # Python 2-3.4 - from imp import reload as refresh +from six.moves import reload_module # pylint: disable=import-error class PathSurgeryTest(unittest.TestCase): @@ -50,14 +44,14 @@ class AlreadyListeningTestNoPsutil(unittest.TestCase): sys.modules['psutil'] = None # Reload hackery to ensure getting non-psutil version # loaded to memory - refresh(certbot.plugins.util) + reload_module(certbot.plugins.util) def tearDown(self): # Need to reload the module to ensure # getting back to normal import certbot.plugins.util sys.modules["psutil"] = self.psutil - refresh(certbot.plugins.util) + reload_module(certbot.plugins.util) @mock.patch("certbot.plugins.util.zope.component.getUtility") def test_ports_available(self, mock_getutil): From 3fe5d9c3e0bd92ea82a2c6c1cf92baff79ed5e48 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Tue, 23 Aug 2016 11:39:35 -0700 Subject: [PATCH 2/5] add custom skipUnless function --- certbot/plugins/util_test.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/certbot/plugins/util_test.py b/certbot/plugins/util_test.py index 63a472fa0..dcb21e037 100644 --- a/certbot/plugins/util_test.py +++ b/certbot/plugins/util_test.py @@ -75,6 +75,42 @@ class AlreadyListeningTestNoPsutil(unittest.TestCase): self.assertEqual(mock_getutil.call_count, 2) +def psutil_available(): + """Checks if psutil can be imported. + + :rtype: bool + :returns: True iff psutil is installed and can be imported + + """ + try: + import psutil # pylint: disable=unused-variable + except ImportError: + return False + return True + + +def skipUnless(condition, reason): + """Skip tests unless a condition holds. + + This implements the basic functionality of unittest.skipUnless + which is only available on Python 2.7+. + + :param bool condition: skip the test iff condition is False + :param str reason: the reason for skipping the test + + :rtype: function + :returns: a decorator that will hide tests unless condition is True + + """ + if hasattr(unittest, "skipUnless"): + return unittest.skipUnless(condition, reason) + elif condition: + return lambda x: x + else: + return lambda x: None + + +@skipUnless(psutil_available(), "optional dependency psutil is not available") class AlreadyListeningTestPsutil(unittest.TestCase): """Tests for certbot.plugins.already_listening.""" def _call(self, *args, **kwargs): From a69ad2afa80bbdd63e32f90e3bfe78d79c33eefe Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Tue, 23 Aug 2016 11:52:28 -0700 Subject: [PATCH 3/5] Give lambda parameter a more useful name --- certbot/plugins/util_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/certbot/plugins/util_test.py b/certbot/plugins/util_test.py index dcb21e037..cb17cebe2 100644 --- a/certbot/plugins/util_test.py +++ b/certbot/plugins/util_test.py @@ -105,9 +105,9 @@ def skipUnless(condition, reason): if hasattr(unittest, "skipUnless"): return unittest.skipUnless(condition, reason) elif condition: - return lambda x: x + return lambda cls: cls else: - return lambda x: None + return lambda cls: None @skipUnless(psutil_available(), "optional dependency psutil is not available") From ad45a664a856b21083dcb5a2e31a3f89ea7823a2 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Wed, 24 Aug 2016 08:26:23 -0700 Subject: [PATCH 4/5] use "iff" iff it is common international shorthand --- certbot/plugins/util_test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/certbot/plugins/util_test.py b/certbot/plugins/util_test.py index cb17cebe2..58dcfdd38 100644 --- a/certbot/plugins/util_test.py +++ b/certbot/plugins/util_test.py @@ -79,7 +79,7 @@ def psutil_available(): """Checks if psutil can be imported. :rtype: bool - :returns: True iff psutil is installed and can be imported + :returns: True if psutil can be imported, otherwise, False """ try: @@ -95,7 +95,7 @@ def skipUnless(condition, reason): This implements the basic functionality of unittest.skipUnless which is only available on Python 2.7+. - :param bool condition: skip the test iff condition is False + :param bool condition: If False, the test will be skipped :param str reason: the reason for skipping the test :rtype: function From 80bcf9a67863ffebcd8685ed264f19f4e0c8ff62 Mon Sep 17 00:00:00 2001 From: Brad Warren Date: Wed, 24 Aug 2016 08:35:53 -0700 Subject: [PATCH 5/5] make docstring prettier when converted to html --- certbot/plugins/util_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/certbot/plugins/util_test.py b/certbot/plugins/util_test.py index 58dcfdd38..27ede6533 100644 --- a/certbot/plugins/util_test.py +++ b/certbot/plugins/util_test.py @@ -79,7 +79,7 @@ def psutil_available(): """Checks if psutil can be imported. :rtype: bool - :returns: True if psutil can be imported, otherwise, False + :returns: ``True`` if psutil can be imported, otherwise, ``False`` """ try: @@ -95,11 +95,11 @@ def skipUnless(condition, reason): This implements the basic functionality of unittest.skipUnless which is only available on Python 2.7+. - :param bool condition: If False, the test will be skipped + :param bool condition: If ``False``, the test will be skipped :param str reason: the reason for skipping the test - :rtype: function - :returns: a decorator that will hide tests unless condition is True + :rtype: callable + :returns: decorator that hides tests unless condition is ``True`` """ if hasattr(unittest, "skipUnless"):