halig/tests/test_utils.py
cătălin ab870b5910
Some checks reported errors
continuous-integration/drone Build encountered an error
ci: debug
2023-04-13 22:22:59 +02:00

73 lines
1.5 KiB
Python

from collections.abc import Callable
from halig.utils import capture
def exec_capture(func: Callable):
return capture(func)()
def test_capture():
def func(): return 1
assert exec_capture(func) == 1
def test_capture_exits_with_custom_os_error(mocker):
exit_code = None
def mock_exit(code):
nonlocal exit_code
exit_code = code
mocker.patch('halig.utils.sys.exit', side_effect=mock_exit)
def func(): raise OSError(2, "os_error_func")
exec_capture(func)
assert exit_code == 2 # noqa: PLR2004
def test_capture_exits_with_os_error(mocker):
exit_code = None
def mock_exit(code):
nonlocal exit_code
exit_code = code
mocker.patch('halig.utils.sys.exit', side_effect=mock_exit)
def func(): raise OSError
exec_capture(func)
assert not exit_code
def test_capture_exits_with_value_error(mocker):
exit_code = None
def mock_exit(code):
nonlocal exit_code
exit_code = code
mocker.patch('halig.utils.sys.exit', side_effect=mock_exit)
def func(): raise ValueError("value_error_func") # noqa: EM101
exec_capture(func)
assert exit_code == 1
def test_capture_exits_with_other_error(mocker):
exit_code = None
def mock_exit(code):
nonlocal exit_code
exit_code = code
mocker.patch('halig.utils.sys.exit', side_effect=mock_exit)
def func(): raise ArithmeticError("arithmetic_error_func") # noqa: EM101
exec_capture(func)
assert exit_code == 2 # noqa: PLR2004