aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorwbond <will@wbond.net>2019-12-28 15:49:22 -0500
committerwbond <will@wbond.net>2019-12-28 15:49:22 -0500
commitffddb9ab19912edd3cdcbbec17d146996228c70c (patch)
tree4d4fefd288dd4856fda7c56964db563f76398a1f
parenta83c3c77b0bd635ab990b779b507c3363ce74832 (diff)
downloadasn1crypto-ffddb9ab19912edd3cdcbbec17d146996228c70c.tar.gz
Have tests error out when a warning is raised
-rw-r--r--dev/tests.py3
-rw-r--r--tests/_unittest_compat.py78
-rw-r--r--tests/test_core.py2
-rw-r--r--tests/test_pem.py6
4 files changed, 74 insertions, 15 deletions
diff --git a/dev/tests.py b/dev/tests.py
index a065c38..5deb8cc 100644
--- a/dev/tests.py
+++ b/dev/tests.py
@@ -4,6 +4,7 @@ from __future__ import unicode_literals, division, absolute_import, print_functi
import unittest
import re
import sys
+import warnings
from . import requires_oscrypto
from ._import import _preload
@@ -37,6 +38,8 @@ def run(matcher=None, repeat=1, ci=False):
_preload(requires_oscrypto, not ci)
+ warnings.filterwarnings("error")
+
loader = unittest.TestLoader()
# We have to manually track the list of applicable tests because for
# some reason with Python 3.4 on Windows, the tests in a suite are replaced
diff --git a/tests/_unittest_compat.py b/tests/_unittest_compat.py
index 2d4985d..d54b1da 100644
--- a/tests/_unittest_compat.py
+++ b/tests/_unittest_compat.py
@@ -6,40 +6,98 @@ import unittest
import re
+if sys.version_info < (3,):
+ str_cls = unicode # noqa
+else:
+ str_cls = str
+
+
_non_local = {'patched': False}
def patch():
- if not sys.version_info < (2, 7):
+ if sys.version_info >= (3, 0):
return
if _non_local['patched']:
return
- unittest.TestCase.assertIsInstance = _assert_is_instance
- unittest.TestCase.assertRaises = _assert_raises
- unittest.TestCase.assertRaisesRegexp = _assert_raises_regexp
+ if sys.version_info < (2, 7):
+ unittest.TestCase.assertIsInstance = _assert_is_instance
+ unittest.TestCase.assertRegex = _assert_regex
+ unittest.TestCase.assertRaises = _assert_raises
+ unittest.TestCase.assertRaisesRegex = _assert_raises_regex
+ unittest.TestCase.assertLess = _assert_less
+ unittest.TestCase.assertLessEqual = _assert_less_equal
+ unittest.TestCase.assertIn = _assert_in
+ unittest.TestCase.assertNotIn = _assert_not_in
+ else:
+ unittest.TestCase.assertRegex = unittest.TestCase.assertRegexpMatches
+ unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
_non_local['patched'] = True
+def _safe_repr(obj):
+ try:
+ return repr(obj)
+ except Exception:
+ return object.__repr__(obj)
+
+
+def _format_message(msg, standard_msg):
+ return msg or standard_msg
+
+
+def _assert_less(self, a, b, msg=None):
+ if not a < b:
+ standard_msg = '%s not less than %s' % (_safe_repr(a), _safe_repr(b))
+ self.fail(_format_message(msg, standard_msg))
+
+
+def _assert_less_equal(self, a, b, msg=None):
+ if not a <= b:
+ standard_msg = '%s not less than or equal to %s' % (_safe_repr(a), _safe_repr(b))
+ self.fail(_format_message(msg, standard_msg))
+
+
def _assert_is_instance(self, obj, cls, msg=None):
- """Same as self.assertTrue(isinstance(obj, cls)), with a nicer
- default message."""
if not isinstance(obj, cls):
if not msg:
msg = '%s is not an instance of %r' % (obj, cls)
self.fail(msg)
-def _assert_raises(self, expected_exception, callableObj=None, *args, **kwargs): # noqa
- context = _AssertRaisesContext(expected_exception, self)
+def _assert_in(self, member, container, msg=None):
+ if member not in container:
+ standard_msg = '%s not found in %s' % (_safe_repr(member), _safe_repr(container))
+ self.fail(_format_message(msg, standard_msg))
+
+
+def _assert_not_in(self, member, container, msg=None):
+ if member in container:
+ standard_msg = '%s found in %s' % (_safe_repr(member), _safe_repr(container))
+ self.fail(_format_message(msg, standard_msg))
+
+
+def _assert_regex(self, text, expected_regexp, msg=None):
+ """Fail the test unless the text matches the regular expression."""
+ if isinstance(expected_regexp, str_cls):
+ expected_regexp = re.compile(expected_regexp)
+ if not expected_regexp.search(text):
+ msg = msg or "Regexp didn't match"
+ msg = '%s: %r not found in %r' % (msg, expected_regexp.pattern, text)
+ self.fail(msg)
+
+
+def _assert_raises(self, excClass, callableObj=None, *args, **kwargs): # noqa
+ context = _AssertRaisesContext(excClass, self)
if callableObj is None:
return context
with context:
callableObj(*args, **kwargs)
-def _assert_raises_regexp(self, expected_exception, expected_regexp, callable_obj=None, *args, **kwargs):
+def _assert_raises_regex(self, expected_exception, expected_regexp, callable_obj=None, *args, **kwargs):
if expected_regexp is not None:
expected_regexp = re.compile(expected_regexp)
context = _AssertRaisesContext(expected_exception, self, expected_regexp)
@@ -50,8 +108,6 @@ def _assert_raises_regexp(self, expected_exception, expected_regexp, callable_ob
class _AssertRaisesContext(object):
- """A context manager used to implement TestCase.assertRaises* methods."""
-
def __init__(self, expected, test_case, expected_regexp=None):
self.expected = expected
self.failureException = test_case.failureException
diff --git a/tests/test_core.py b/tests/test_core.py
index 41ea71d..b9a7a82 100644
--- a/tests/test_core.py
+++ b/tests/test_core.py
@@ -1182,7 +1182,7 @@ class CoreTests(unittest.TestCase):
self.assertEqual(b'\x6a\x03\x02\x01\x00', ati.dump(force=True))
def test_required_field(self):
- with self.assertRaisesRegexp(ValueError, '"id" is missing from structure'):
+ with self.assertRaisesRegex(ValueError, '"id" is missing from structure'):
Seq({'value': core.Integer(5)}).dump()
def test_explicit_application_tag_nested(self):
diff --git a/tests/test_pem.py b/tests/test_pem.py
index 8d7f274..db9857c 100644
--- a/tests/test_pem.py
+++ b/tests/test_pem.py
@@ -149,13 +149,13 @@ class PEMTests(unittest.TestCase):
self.assertEqual(expected_bytes, encoded_bytes)
def test_armor_wrong_type(self):
- with self.assertRaisesRegexp(TypeError, 'type_name must be a unicode string'):
+ with self.assertRaisesRegex(TypeError, 'type_name must be a unicode string'):
pem.armor(b'CERTIFICATE', b'')
def test_armor_wrong_type2(self):
- with self.assertRaisesRegexp(TypeError, 'der_bytes must be a byte string'):
+ with self.assertRaisesRegex(TypeError, 'der_bytes must be a byte string'):
pem.armor('CERTIFICATE', '')
def test_detect_wrong_type(self):
- with self.assertRaisesRegexp(TypeError, 'byte_string must be a byte string'):
+ with self.assertRaisesRegex(TypeError, 'byte_string must be a byte string'):
pem.detect('CERTIFICATE')