debian-forge/test/mod/test_util_ctx.py
David Rheinsberg 8a195d7502 util/ctx: extract suppress_oserror()
Extract the `suppress_oserror()` function from the ObjectManager and
make it available as utility for other code as well.

This also adds a bunch of tests that verify it works as expected.
2020-05-11 18:05:12 +02:00

43 lines
1.5 KiB
Python

#
# Tests for the 'osbuild.util.ctx' module.
#
import errno
import unittest
from osbuild.util import ctx
class TestUtilCtx(unittest.TestCase):
def test_suppress_oserror(self):
#
# Verify the `suppress_oserror()` function.
#
# Empty list and empty statement is a no-op.
with ctx.suppress_oserror():
pass
# Single errno matches raised errno.
with ctx.suppress_oserror(errno.EPERM):
raise OSError(errno.EPERM, "Operation not permitted")
# Many errnos match raised errno regardless of their order.
with ctx.suppress_oserror(errno.EPERM, errno.ENOENT, errno.ESRCH):
raise OSError(errno.EPERM, "Operation not permitted")
with ctx.suppress_oserror(errno.ENOENT, errno.EPERM, errno.ESRCH):
raise OSError(errno.EPERM, "Operation not permitted")
with ctx.suppress_oserror(errno.ENOENT, errno.ESRCH, errno.EPERM):
raise OSError(errno.EPERM, "Operation not permitted")
# Empty list re-raises exceptions.
with self.assertRaises(OSError):
with ctx.suppress_oserror():
raise OSError(errno.EPERM, "Operation not permitted")
# Non-matching lists re-raise exceptions.
with self.assertRaises(OSError):
with ctx.suppress_oserror(errno.ENOENT):
raise OSError(errno.EPERM, "Operation not permitted")
with ctx.suppress_oserror(errno.ENOENT, errno.ESRCH):
raise OSError(errno.EPERM, "Operation not permitted")