diff options
| author | Roy Sommer <illBeRoy@users.noreply.github.com> | 2020-02-01 17:11:46 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-01 17:11:46 +0200 |
| commit | f6248d4521a4a23d58137058cd400af4fef33af7 (patch) | |
| tree | f667db8e7db80755e349bc456f5e65d1f4edb620 | |
| parent | renamed hooks dir (diff) | |
| download | taskipy2-f6248d4521a4a23d58137058cd400af4fef33af7.zip | |
Ignore SIGINT While Running Tasks (#8)
fix: sigint is now ignored by taskipy process, reflects output of task instead
| -rw-r--r-- | poetry.lock | 26 | ||||
| -rw-r--r-- | pyproject.toml | 3 | ||||
| -rw-r--r-- | taskipy/run.py | 8 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py | 11 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_that_handle_interrupts/loop_without_sigint_handler.py | 12 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_that_handle_interrupts/pyproject.toml | 7 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 53 |
7 files changed, 112 insertions, 8 deletions
diff --git a/poetry.lock b/poetry.lock index f1521bf..01b093a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -80,6 +80,17 @@ version = "0.4.3" [[package]] category = "dev" +description = "Cross-platform lib for process and system monitoring in Python." +name = "psutil" +optional = false +python-versions = ">=2.6, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +version = "5.6.7" + +[package.extras] +enum = ["enum34"] + +[[package]] +category = "dev" description = "python code static checker" name = "pylint" optional = false @@ -141,7 +152,7 @@ python-versions = "*" version = "1.11.2" [metadata] -content-hash = "c9a9ad81a21b3edfe2b0695af814eaf3974c6fca8a4d1913f666eaabd432f72e" +content-hash = "038310699e756391b0ed0ae9e0597be14230fd9972a32aadc8afb80544a1f2e0" python-versions = "^3.5" [metadata.files] @@ -204,6 +215,19 @@ mypy-extensions = [ {file = "mypy_extensions-0.4.3-py2.py3-none-any.whl", hash = "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d"}, {file = "mypy_extensions-0.4.3.tar.gz", hash = "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"}, ] +psutil = [ + {file = "psutil-5.6.7-cp27-none-win32.whl", hash = "sha256:1b1575240ca9a90b437e5a40db662acd87bbf181f6aa02f0204978737b913c6b"}, + {file = "psutil-5.6.7-cp27-none-win_amd64.whl", hash = "sha256:28f771129bfee9fc6b63d83a15d857663bbdcae3828e1cb926e91320a9b5b5cd"}, + {file = "psutil-5.6.7-cp35-cp35m-win32.whl", hash = "sha256:21231ef1c1a89728e29b98a885b8e0a8e00d09018f6da5cdc1f43f988471a995"}, + {file = "psutil-5.6.7-cp35-cp35m-win_amd64.whl", hash = "sha256:b74b43fecce384a57094a83d2778cdfc2e2d9a6afaadd1ebecb2e75e0d34e10d"}, + {file = "psutil-5.6.7-cp36-cp36m-win32.whl", hash = "sha256:e85f727ffb21539849e6012f47b12f6dd4c44965e56591d8dec6e8bc9ab96f4a"}, + {file = "psutil-5.6.7-cp36-cp36m-win_amd64.whl", hash = "sha256:b560f5cd86cf8df7bcd258a851ca1ad98f0d5b8b98748e877a0aec4e9032b465"}, + {file = "psutil-5.6.7-cp37-cp37m-win32.whl", hash = "sha256:094f899ac3ef72422b7e00411b4ed174e3c5a2e04c267db6643937ddba67a05b"}, + {file = "psutil-5.6.7-cp37-cp37m-win_amd64.whl", hash = "sha256:fd2e09bb593ad9bdd7429e779699d2d47c1268cbde4dda95fcd1bd17544a0217"}, + {file = "psutil-5.6.7-cp38-cp38-win32.whl", hash = "sha256:70387772f84fa5c3bb6a106915a2445e20ac8f9821c5914d7cbde148f4d7ff73"}, + {file = "psutil-5.6.7-cp38-cp38-win_amd64.whl", hash = "sha256:10b7f75cc8bd676cfc6fa40cd7d5c25b3f45a0e06d43becd7c2d2871cbb5e806"}, + {file = "psutil-5.6.7.tar.gz", hash = "sha256:ffad8eb2ac614518bbe3c0b8eb9dffdb3a8d2e3a7d5da51c5b974fb723a5c5aa"}, +] pylint = [ {file = "pylint-2.4.4-py3-none-any.whl", hash = "sha256:886e6afc935ea2590b462664b161ca9a5e40168ea99e5300935f6591ad467df4"}, {file = "pylint-2.4.4.tar.gz", hash = "sha256:3db5468ad013380e987410a8d6956226963aed94ecb5f9d3a28acca6d9ac36cd"}, diff --git a/pyproject.toml b/pyproject.toml index 4e18b10..f8b14fb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,13 +20,14 @@ packages = [ task = 'taskipy.cli:main' [tool.poetry.dependencies] -python = "^3.5" +python = "^3.6" toml = "^0.10.0" [tool.poetry.dev-dependencies] pylint = "^2.4.4" mypy = "^0.761" rope = "^0.14.0" +psutil = "^5.6.7" [tool.taskipy.tasks] test = "python -m unittest -v tests/test_*.py" diff --git a/taskipy/run.py b/taskipy/run.py index 014d0ae..ab7824d 100644 --- a/taskipy/run.py +++ b/taskipy/run.py @@ -9,7 +9,12 @@ def run_task(task_name: str, args: List[str], cwd=os.curdir): def run_commands_and_bail_on_first_fail(cmds: List[str]) -> int: for cmd in cmds: p = subprocess.Popen(cmd, shell=True, cwd=cwd) - p.wait() + + try: + p.wait() + except KeyboardInterrupt: + pass + if p.returncode != 0: return p.returncode @@ -22,7 +27,6 @@ def run_task(task_name: str, args: List[str], cwd=os.curdir): sys.exit(1) except toml.TomlDecodeError: print('pyproject.toml file is malformed and could not be read') - # should print exception details sys.exit(1) try: 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) |