aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/fixtures/project_with_tasks_that_handle_interrupts/loop_with_sigint_handler.py11
-rw-r--r--tests/fixtures/project_with_tasks_that_handle_interrupts/loop_without_sigint_handler.py12
-rw-r--r--tests/fixtures/project_with_tasks_that_handle_interrupts/pyproject.toml7
-rw-r--r--tests/test_taskipy.py53
4 files changed, 79 insertions, 4 deletions
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)