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.
This commit is contained in:
parent
6e02488a9f
commit
8a195d7502
3 changed files with 80 additions and 15 deletions
43
test/mod/test_util_ctx.py
Normal file
43
test/mod/test_util_ctx.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#
|
||||
# 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")
|
||||
Loading…
Add table
Add a link
Reference in a new issue