aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_taskipy.py
diff options
context:
space:
mode:
authorRoy Sommer <illBeRoy@users.noreply.github.com>2020-02-01 17:11:46 +0200
committerGitHub <noreply@github.com>2020-02-01 17:11:46 +0200
commitf6248d4521a4a23d58137058cd400af4fef33af7 (patch)
treef667db8e7db80755e349bc456f5e65d1f4edb620 /tests/test_taskipy.py
parentrenamed hooks dir (diff)
downloadtaskipy2-f6248d4521a4a23d58137058cd400af4fef33af7.zip
Ignore SIGINT While Running Tasks (#8)
fix: sigint is now ignored by taskipy process, reflects output of task instead
Diffstat (limited to 'tests/test_taskipy.py')
-rw-r--r--tests/test_taskipy.py53
1 files changed, 49 insertions, 4 deletions
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)