From 09acf4729b8f15f30b88d2a3033633887c603fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20Marczykowski-G=C3=B3recki?= Date: Sun, 30 Apr 2017 22:52:47 +0200 Subject: [PATCH] tests: import assertNotRaises() from core-admin --- qubesmgmt/tests/__init__.py | 63 +++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/qubesmgmt/tests/__init__.py b/qubesmgmt/tests/__init__.py index dfbd8360..b9a535a9 100644 --- a/qubesmgmt/tests/__init__.py +++ b/qubesmgmt/tests/__init__.py @@ -17,6 +17,7 @@ # # You should have received a copy of the GNU Lesser General Public License along # with this program; if not, see . +import traceback import unittest import io @@ -73,6 +74,39 @@ def wait(self): self.stdin_close() return 0 +class _AssertNotRaisesContext(object): + """A context manager used to implement TestCase.assertNotRaises methods. + + Stolen from unittest and hacked. Regexp support stripped. + """ # pylint: disable=too-few-public-methods + + def __init__(self, expected, test_case, expected_regexp=None): + if expected_regexp is not None: + raise NotImplementedError('expected_regexp is unsupported') + + self.expected = expected + self.exception = None + + self.failureException = test_case.failureException + + + def __enter__(self): + return self + + + def __exit__(self, exc_type, exc_value, tb): + if exc_type is None: + return True + + if issubclass(exc_type, self.expected): + raise self.failureException( + "{!r} raised, traceback:\n{!s}".format( + exc_value, ''.join(traceback.format_tb(tb)))) + else: + # pass through + return False + + class QubesTest(qubesmgmt.app.QubesBase): expected_calls = None actual_calls = None @@ -110,3 +144,32 @@ def assertAllCalled(self): self.assertEqual( set(self.app.expected_calls.keys()), set(self.app.actual_calls)) + + def assertNotRaises(self, excClass, callableObj=None, *args, **kwargs): + """Fail if an exception of class excClass is raised + by callableObj when invoked with arguments args and keyword + arguments kwargs. If a different type of exception is + raised, it will not be caught, and the test case will be + deemed to have suffered an error, exactly as for an + unexpected exception. + + If called with callableObj omitted or None, will return a + context object used like this:: + + with self.assertRaises(SomeException): + do_something() + + The context manager keeps a reference to the exception as + the 'exception' attribute. This allows you to inspect the + exception after the assertion:: + + with self.assertRaises(SomeException) as cm: + do_something() + the_exception = cm.exception + self.assertEqual(the_exception.error_code, 3) + """ + context = _AssertNotRaisesContext(excClass, self) + if callableObj is None: + return context + with context: + callableObj(*args, **kwargs)