43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
"""Tests for the local server restart helper."""
|
|
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from unittest import mock
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, os.path.join(ROOT, "tools"))
|
|
|
|
import restart_server
|
|
|
|
|
|
class RestartServerTests(unittest.TestCase):
|
|
def _run(self, call_result=None, call_exc=None):
|
|
with mock.patch.object(sys, "argv", ["restart_server.py"]), \
|
|
mock.patch.object(restart_server, "pid_from_file", return_value=None), \
|
|
mock.patch.object(restart_server, "kill_pid"), \
|
|
mock.patch.object(restart_server, "kill_port"), \
|
|
mock.patch.object(restart_server, "port_busy", return_value=False), \
|
|
mock.patch.object(restart_server, "free_port", return_value=8765), \
|
|
mock.patch.object(restart_server.subprocess, "call",
|
|
return_value=call_result, side_effect=call_exc):
|
|
return restart_server.main()
|
|
|
|
def test_keyboard_interrupt_is_clean_exit(self):
|
|
self.assertEqual(self._run(call_exc=KeyboardInterrupt), 0)
|
|
|
|
def test_windows_ctrl_c_exit_code_is_clean(self):
|
|
self.assertEqual(self._run(call_result=-1073741510), 0)
|
|
|
|
def test_other_error_code_is_returned(self):
|
|
self.assertEqual(self._run(call_result=3), 3)
|
|
|
|
def test_free_port_moves_forward(self):
|
|
with mock.patch.object(restart_server, "port_busy",
|
|
side_effect=[True, True, False]):
|
|
self.assertEqual(restart_server.free_port(8765), 8767)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|