diff options
author | Eli Collins <elic@assurancetechnologies.com> | 2012-03-11 18:37:49 -0400 |
---|---|---|
committer | Eli Collins <elic@assurancetechnologies.com> | 2012-03-11 18:37:49 -0400 |
commit | 09b88be2e515ebeddaf64a0f15309b6a3f04416f (patch) | |
tree | 9868ccb551c5dfd6e36d95e1b90c70ca440d6b14 /passlib/tests | |
parent | 29468d26fa3c68a56bd739d4dd9be50d8dd6725c (diff) | |
download | passlib-09b88be2e515ebeddaf64a0f15309b6a3f04416f.tar.gz |
added hack to clear warnings registry so assertWarningList() tests will pass reliably
Diffstat (limited to 'passlib/tests')
-rw-r--r-- | passlib/tests/test_context.py | 20 | ||||
-rw-r--r-- | passlib/tests/test_ext_django.py | 6 | ||||
-rw-r--r-- | passlib/tests/test_handlers.py | 4 | ||||
-rw-r--r-- | passlib/tests/test_registry.py | 6 | ||||
-rw-r--r-- | passlib/tests/test_utils.py | 6 | ||||
-rw-r--r-- | passlib/tests/test_utils_handlers.py | 12 | ||||
-rw-r--r-- | passlib/tests/utils.py | 31 |
7 files changed, 53 insertions, 32 deletions
diff --git a/passlib/tests/test_context.py b/passlib/tests/test_context.py index c3bf622..f788ee9 100644 --- a/passlib/tests/test_context.py +++ b/passlib/tests/test_context.py @@ -22,7 +22,7 @@ from passlib.exc import PasslibConfigWarning from passlib.utils import tick, to_bytes, to_unicode from passlib.utils.compat import irange, u import passlib.utils.handlers as uh -from passlib.tests.utils import TestCase, mktemp, catch_warnings, \ +from passlib.tests.utils import TestCase, mktemp, catch_all_warnings, \ gae_env, set_file from passlib.registry import register_crypt_handler_path, has_crypt_handler, \ _unload_handler_name as unload_handler_name @@ -467,7 +467,7 @@ admin__context__deprecated = des_crypt, bsdi_crypt def test_15_min_verify_time(self): "test get_min_verify_time() method" # silence deprecation warnings for min verify time - with catch_warnings(): + with catch_all_warnings(): warnings.filterwarnings("ignore", category=DeprecationWarning) self._test_15() @@ -624,7 +624,7 @@ class CryptContextTest(TestCase): ) # min rounds - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: # set below handler min c2 = cc.replace(all__min_rounds=500, all__max_rounds=None, @@ -655,7 +655,7 @@ class CryptContextTest(TestCase): self.consumeWarningList(wlog) # max rounds - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: # set above handler max c2 = cc.replace(all__max_rounds=int(1e9)+500, all__min_rounds=None, all__default_rounds=int(1e9)+500) @@ -816,7 +816,7 @@ class CryptContextTest(TestCase): # which is much cheaper, and shares the same codebase. # min rounds - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: self.assertEqual( cc.encrypt("password", rounds=1999, salt="nacl"), '$5$rounds=2000$nacl$9/lTZ5nrfPuz8vphznnmHuDGFuvjSNvOEDsGmGfsS97', @@ -827,7 +827,7 @@ class CryptContextTest(TestCase): cc.encrypt("password", rounds=2001, salt="nacl"), '$5$rounds=2001$nacl$8PdeoPL4aXQnJ0woHhqgIw/efyfCKC2WHneOpnvF.31' ) - self.consumeWarningList() + self.consumeWarningList(wlog) # max rounds, etc tested in genconfig() @@ -942,7 +942,7 @@ class CryptContextTest(TestCase): return to_unicode(secret + 'x') # silence deprecation warnings for min verify time - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: cc = CryptContext([TimedHash], min_verify_time=min_verify_time) self.consumeWarningList(wlog, DeprecationWarning) @@ -969,7 +969,7 @@ class CryptContextTest(TestCase): #ensure taking longer emits a warning. TimedHash.delay = max_delay - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: elapsed, result = timecall(cc.verify, "blob", "stubx") self.assertFalse(result) self.assertAlmostEqual(elapsed, max_delay, delta=delta) @@ -1016,9 +1016,7 @@ class CryptContextTest(TestCase): GOOD1 = "$2a$12$oaQbBqq8JnSM1NHRPQGXOOm4GCUMqp7meTnkft4zgSnrbhoKdDV0C" ctx = CryptContext(["bcrypt"]) - with catch_warnings(record=True) as wlog: - warnings.simplefilter("always") - + with catch_all_warnings(record=True) as wlog: self.assertTrue(ctx.hash_needs_update(BAD1)) self.assertFalse(ctx.hash_needs_update(GOOD1)) diff --git a/passlib/tests/test_ext_django.py b/passlib/tests/test_ext_django.py index 05db8a0..4627d30 100644 --- a/passlib/tests/test_ext_django.py +++ b/passlib/tests/test_ext_django.py @@ -13,7 +13,7 @@ from passlib.context import CryptContext, CryptPolicy from passlib.apps import django_context from passlib.ext.django import utils from passlib.hash import sha256_crypt -from passlib.tests.utils import TestCase, unittest, ut_version, catch_warnings +from passlib.tests.utils import TestCase, unittest, ut_version, catch_all_warnings import passlib.tests.test_handlers as th from passlib.utils.compat import iteritems, get_method_function, unicode from passlib.registry import get_crypt_handler @@ -223,9 +223,7 @@ class PatchTest(TestCase): def dummy(): pass - with catch_warnings(record=True) as wlog: - warnings.simplefilter("always") - + with catch_all_warnings(record=True) as wlog: #patch to use stock django context utils.set_django_password_context(django_context) self.assert_patched(context=django_context) diff --git a/passlib/tests/test_handlers.py b/passlib/tests/test_handlers.py index 4547e56..580cc08 100644 --- a/passlib/tests/test_handlers.py +++ b/passlib/tests/test_handlers.py @@ -12,7 +12,7 @@ import warnings from passlib import hash from passlib.utils.compat import irange from passlib.tests.utils import TestCase, HandlerCase, create_backend_case, \ - enable_option, b, catch_warnings, UserHandlerMixin, randintgauss + enable_option, b, catch_all_warnings, UserHandlerMixin, randintgauss from passlib.utils.compat import u #module @@ -262,7 +262,7 @@ class _bcrypt_test(HandlerCase): check_padding(bcrypt.encrypt("bob", rounds=bcrypt.min_rounds)) # some things that will raise warnings - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: # # test genconfig() corrects invalid salts & issues warning. # diff --git a/passlib/tests/test_registry.py b/passlib/tests/test_registry.py index 882309c..df2f618 100644 --- a/passlib/tests/test_registry.py +++ b/passlib/tests/test_registry.py @@ -16,7 +16,7 @@ from passlib import hash, registry from passlib.registry import register_crypt_handler, register_crypt_handler_path, \ get_crypt_handler, list_crypt_handlers, _unload_handler_name as unload_handler_name import passlib.utils.handlers as uh -from passlib.tests.utils import TestCase, mktemp, catch_warnings +from passlib.tests.utils import TestCase, mktemp, catch_all_warnings #module log = getLogger(__name__) @@ -112,7 +112,7 @@ class RegistryTest(TestCase): #TODO: check lazy load which calls register_crypt_handler (warning should be issued) sys.modules.pop("passlib.tests._test_bad_register", None) register_crypt_handler_path("dummy_bad", "passlib.tests._test_bad_register") - with catch_warnings(): + with catch_all_warnings(): warnings.filterwarnings("ignore", "xxxxxxxxxx", DeprecationWarning) h = get_crypt_handler("dummy_bad") from passlib.tests import _test_bad_register as tbr @@ -158,7 +158,7 @@ class RegistryTest(TestCase): register_crypt_handler(dummy_1) self.assertIs(get_crypt_handler("dummy_1"), dummy_1) - with catch_warnings(): + with catch_all_warnings(): warnings.filterwarnings("ignore", "handler names should be lower-case, and use underscores instead of hyphens:.*", UserWarning) self.assertIs(get_crypt_handler("DUMMY-1"), dummy_1) diff --git a/passlib/tests/test_utils.py b/passlib/tests/test_utils.py index eedb7e0..1baebd7 100644 --- a/passlib/tests/test_utils.py +++ b/passlib/tests/test_utils.py @@ -13,7 +13,7 @@ import warnings #module from passlib.utils.compat import b, bytes, bascii_to_str, irange, PY2, PY3, u, \ unicode, bjoin -from passlib.tests.utils import TestCase, Params as ak, enable_option, catch_warnings +from passlib.tests.utils import TestCase, Params as ak, enable_option, catch_all_warnings def hb(source): return unhexlify(b(source)) @@ -1010,8 +1010,8 @@ class CryptoTest(TestCase): self.assertRaises(TypeError, norm_hash_name, None) # test selected results - with catch_warnings(): - warnings.filterwarnings("ignore", 'encountered unknown hash') + with catch_all_warnings(): + warnings.filterwarnings("ignore", '.*unknown hash') for row in chain(_nhn_hash_names, self.ndn_values): for idx, format in enumerate(self.ndn_formats): correct = row[idx] diff --git a/passlib/tests/test_utils_handlers.py b/passlib/tests/test_utils_handlers.py index 8ae5a96..9b28f3c 100644 --- a/passlib/tests/test_utils_handlers.py +++ b/passlib/tests/test_utils_handlers.py @@ -18,7 +18,7 @@ from passlib.utils import getrandstr, JYTHON, rng, to_unicode from passlib.utils.compat import b, bytes, bascii_to_str, str_to_uascii, \ uascii_to_str, unicode, PY_MAX_25 import passlib.utils.handlers as uh -from passlib.tests.utils import HandlerCase, TestCase, catch_warnings +from passlib.tests.utils import HandlerCase, TestCase, catch_all_warnings from passlib.utils.compat import u #module log = getLogger(__name__) @@ -190,7 +190,7 @@ class SkeletonTest(TestCase): self.assertIn(norm_salt(use_defaults=True), salts3) # check explicit salts - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: # check too-small salts self.assertRaises(ValueError, norm_salt, salt='') @@ -211,7 +211,7 @@ class SkeletonTest(TestCase): self.consumeWarningList(wlog, PasslibHashWarning) #check generated salts - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: # check too-small salt size self.assertRaises(ValueError, gen_salt, 0) @@ -229,11 +229,11 @@ class SkeletonTest(TestCase): self.consumeWarningList(wlog) self.assertIn(gen_salt(5, relaxed=True), salts4) - self.consumeWarningList(wlog, PasslibHashWarning) + self.consumeWarningList(wlog, ["salt too large"]) # test with max_salt_size=None del d1.max_salt_size - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: self.assertEqual(len(gen_salt(None)), 3) self.assertEqual(len(gen_salt(5)), 5) self.consumeWarningList(wlog) @@ -259,7 +259,7 @@ class SkeletonTest(TestCase): self.assertEqual(norm_rounds(use_defaults=True), 2) # check explicit rounds - with catch_warnings(record=True) as wlog: + with catch_all_warnings(record=True) as wlog: # too small self.assertRaises(ValueError, norm_rounds, rounds=0) self.consumeWarningList(wlog) diff --git a/passlib/tests/utils.py b/passlib/tests/utils.py index fde0f87..c309bc4 100644 --- a/passlib/tests/utils.py +++ b/passlib/tests/utils.py @@ -628,11 +628,9 @@ class HandlerCase(TestCase): def setUp(self): # backup warning filter state; set to display all warnings during tests; # and restore filter state after test. - ctx = catch_warnings() + ctx = catch_all_warnings() ctx.__enter__() self._restore_warnings = ctx.__exit__ - warnings.resetwarnings() - warnings.simplefilter("always") # if needed, select specific backend for duration of test handler = self.handler @@ -1894,6 +1892,33 @@ except ImportError: self._module.filters = self._filters self._module.showwarning = self._showwarning +class catch_all_warnings(catch_warnings): + "catch_warnings() wrapper which clears filter" + def __init__(self, reset=".*", **kwds): + super(catch_all_warnings, self).__init__(**kwds) + self._reset_pat = reset + + def __enter__(self): + # let parent class archive filter state + ret = super(catch_all_warnings, self).__enter__() + + # reset the filter to list everything + warnings.resetwarnings() + warnings.simplefilter("always") + + # wipe the __warningregistry__ off the map, so warnings + # reliably get reported per-test. + # XXX: *could* restore state + pattern = self._reset_pat + if pattern: + import sys + key = "__warningregistry__" + for mod in sys.modules.values(): + if hasattr(mod, key) and re.match(pattern, mod.__name__): + getattr(mod, key).clear() + + return ret + #========================================================= #EOF #========================================================= |