I have this function in my code:
from stem.process import launch_torfrom stem.util import termfrom . import checksdef _launch(torrc: str, tmpdir: str, verbose: bool) -> None: try: tractor_process = launch_tor( torrc_path=torrc, init_msg_handler=msg_handler, timeout=120, ) except OSError as error: print(term.format(f"{error}\n", term.Color.RED)) else: _finish_notification(verbose)I wrote this testcase for that:
import unittestfrom unittest.mock import patchfrom tractor import actionsclass Launch(unittest.TestCase): @patch("os.rmdir") @patch("os.remove") @patch("sys.stdout") @patch("stem.process.launch_tor", side_effect=OSError) @patch("tractor.actions._finish_notification") def test_launch_os_error(self, mock_finish, *_):""" Couldn't launch""" actions._launch("torrc", "tmpdir", False) mock_finish.assert_not_called() @patch("os.rmdir") @patch("os.remove") @patch("stem.process.launch_tor", return_value=None) @patch("tractor.actions._finish_notification") def test_launch_ok(self, mock_finish, *_):""" successful""" actions._launch("torrc", "tmpdir", False) mock_finish.assert_called_once_with(False)But I get this error on test:
FAIL: test_launch_ok (test_actions.Launch.test_launch_ok)----------------------------------------------------------------------Traceback (most recent call last): File "/usr/lib/python3.11/unittest/mock.py", line 1369, in patched return func(*newargs, **newkeywargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/danialbehzadi/Documents/tractor/tests/test_actions.py", line 109, in test_launch_ok mock_finish.assert_called_once_with(False) File "/usr/lib/python3.11/unittest/mock.py", line 944, in assert_called_once_with raise AssertionError(msg)AssertionError: Expected '_finish_notification' to be called once. Called 0 times.Here is the snoop debug of it:
.Starting var:.. torrc = 'torrc'
Starting var:.. tmpdir = 'tmpdir'
Starting var:.. verbose = False
13:45:02.986628 call 61 def _launch(torrc: str, tmpdir: str, verbose: bool) -> None:
13:45:02.986693 line 65 msg_handler = checks.verbose_return(
13:45:02.986719 line 66 _print_bootstrap_lines, _print_all_lines, verbose
13:45:02.986740 line 65 msg_handler = checks.verbose_return(
New var:....... msg_handler = <function _print_bootstrap_lines at 0x7f2f2c6871a0>
13:45:02.986760 line 68 try:
13:45:02.986788 line 69 tractor_process = launch_tor(
13:45:02.986808 line 70 torrc_path=torrc,
13:45:02.986826 line 71 init_msg_handler=msg_handler,
13:45:02.986845 line 72 timeout=120,
13:45:02.986862 line 69 tractor_process = launch_tor(
13:45:02.986913 exception 69 tractor_process = launch_tor(
Exception:..... OSError: torrc doesnt exist (torrc)
13:45:02.986970 line 75 except OSError as error:
New var:....... error = OSError("torrc doesnt exist (torrc)")
13:45:02.986995 line 76 print(term.format(f"{error}\n", term.Color.RED))
torrc doesnt exist (torrc)
13:45:02.987049 line 82 os.remove(torrc)
13:45:02.987444 line 83 os.rmdir(tmpdir)
13:45:02.987835 return 83 os.rmdir(tmpdir)
Return value:.. None
Elapsed time: 00:00:00.001266
It seems that the function stem.process.launch_tor couldn't be patched and it's running, raising an OSError.