From 2f4685bfaeba8c77da995cefaa2ac6ce17ea559f Mon Sep 17 00:00:00 2001 From: Jonathan Basseri Date: Thu, 11 May 2017 10:29:34 -0700 Subject: Support "with"-style errno assertions. This adds the option to use assertRaisesErrno as a context manager, so blocks of code can be tested instead of just callable objects. It is also a more readable style when the callable takes many args. Test: all_tests.sh passing on device kernels Change-Id: Ib6fd88c3a1a4fd1b69403b97712c7f27732ade3a --- net/test/net_test.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/net/test/net_test.py b/net/test/net_test.py index 9a5c635e..423228be 100755 --- a/net/test/net_test.py +++ b/net/test/net_test.py @@ -375,9 +375,27 @@ class RunAsUid(RunAsUidGid): class NetworkTest(unittest.TestCase): - def assertRaisesErrno(self, err_num, f, *args): + def assertRaisesErrno(self, err_num, f=None, *args): + """Test that the system returns an errno error. + + This works similarly to unittest.TestCase.assertRaises. You can call it as + an assertion, or use it as a context manager. + e.g. + self.assertRaisesErrno(errno.ENOENT, do_things, arg1, arg2) + or + with self.assertRaisesErrno(errno.ENOENT): + do_things(arg1, arg2) + + Args: + err_num: an errno constant + f: (optional) A callable that should result in error + *args: arguments passed to f + """ msg = os.strerror(err_num) - self.assertRaisesRegexp(EnvironmentError, msg, f, *args) + if f is None: + return self.assertRaisesRegexp(EnvironmentError, msg) + else: + self.assertRaisesRegexp(EnvironmentError, msg, f, *args) def ReadProcNetSocket(self, protocol): # Read file. -- cgit v1.2.3