From f6248d4521a4a23d58137058cd400af4fef33af7 Mon Sep 17 00:00:00 2001 From: Roy Sommer Date: Sat, 1 Feb 2020 17:11:46 +0200 Subject: Ignore SIGINT While Running Tasks (#8) fix: sigint is now ignored by taskipy process, reflects output of task instead --- .../loop_with_sigint_handler.py | 11 +++++ .../loop_without_sigint_handler.py | 12 +++++ .../pyproject.toml | 7 +++ tests/test_taskipy.py | 53 ++++++++++++++++++++-- 4 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py create mode 100644 tests/fixtures/project_with_tasks_that_handle_interrupts/loop_without_sigint_handler.py create mode 100644 tests/fixtures/project_with_tasks_that_handle_interrupts/pyproject.toml (limited to 'tests') diff --git a/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py b/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py new file mode 100644 index 0000000..80f7b23 --- /dev/null +++ b/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py @@ -0,0 +1,11 @@ +import time + +def main(): + try: + while True: + time.sleep(.1) + except KeyboardInterrupt: + print('failing gracefully...') + +if __name__ == '__main__': + main() diff --git a/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_without_sigint_handler.py b/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_without_sigint_handler.py new file mode 100644 index 0000000..87189f9 --- /dev/null +++ b/tests/fixtures/project_with_tasks_that_handle_interrupts/loop_without_sigint_handler.py @@ -0,0 +1,12 @@ +import time +import sys + +def main(): + try: + while True: + time.sleep(.1) + except: + sys.exit(130) + +if __name__ == '__main__': + main() diff --git a/tests/fixtures/project_with_tasks_that_handle_interrupts/pyproject.toml b/tests/fixtures/project_with_tasks_that_handle_interrupts/pyproject.toml new file mode 100644 index 0000000..f406cbb --- /dev/null +++ b/tests/fixtures/project_with_tasks_that_handle_interrupts/pyproject.toml @@ -0,0 +1,7 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.tasks] +run_loop_with_interrupt_handling = "python3 loop_with_sigint_handler.py" +run_loop_without_interrupt_handling = "python3 loop_without_sigint_handler.py" diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 9d74d9d..87b62a2 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -2,6 +2,10 @@ import os import unittest import subprocess import random +import time +import signal +import psutil # type: ignore +import warnings from os import path from typing import List, Tuple from tests.utils.fixture import FixtureTempDir @@ -15,14 +19,17 @@ class TaskipyTestCase(unittest.TestCase): tmp_dir.clean() def run_task(self, task: str, args: List[str] = None, cwd=os.curdir) -> Tuple[int, str, str]: - executable_path = path.abspath('task') - args = args or [] - - proc = subprocess.Popen([executable_path, task] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) + proc = self.start_taskipy_process(task, args=args, cwd=cwd) stdout, stderr = proc.communicate() return proc.returncode, str(stdout), str(stderr) + def start_taskipy_process(self, task: str, args: List[str] = None, cwd=os.curdir) -> subprocess.Popen: + executable_path = path.abspath('task') + args = args or [] + + return subprocess.Popen([executable_path, task] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) + def create_test_dir_from_fixture(self, fixture_name: str): tmp_dir = FixtureTempDir(path.join('tests', 'fixtures', fixture_name)) self._tmp_dirs.append(tmp_dir) @@ -173,3 +180,41 @@ class TaskRunFailTestCase(TaskipyTestCase): self.assertSubstr('pyproject.toml file is malformed and could not be read', stdout) self.assertEqual(exit_code, 1) + +class InterruptingTaskTestCase(TaskipyTestCase): + def setUp(self): + super().setUp() + + # suppress resource warnings, as they are false positives caused by psutil + warnings.simplefilter('ignore', category=ResourceWarning) + + def interrupt_task(self, process: subprocess.Popen): + psutil_process_wrapper = psutil.Process(process.pid) + + processes = psutil_process_wrapper.children(recursive=True) + + innermost_process = next(filter(lambda p: p.name().startswith('python'), processes)) + innermost_process.send_signal(signal.SIGINT) + + def test_handling_sigint_according_to_subprocess_if_it_handles_it_gracefully(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_that_handle_interrupts') + process = self.start_taskipy_process('run_loop_with_interrupt_handling', cwd=cwd) + + time.sleep(.2) + + self.interrupt_task(process) + exit_code = process.wait() + + self.assertEqual(exit_code, 0) + + def test_handling_sigint_according_to_subprocess_if_it_does_not_handle_it_gracefully(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_that_handle_interrupts') + process = self.start_taskipy_process('run_loop_without_interrupt_handling', cwd=cwd) + + time.sleep(.2) + + self.interrupt_task(process) + + exit_code = process.wait() + + self.assertEqual(exit_code, 130) -- cgit v1.2.3