From b7c1dc794d4e4faa068594139e945fbeac7b02b5 Mon Sep 17 00:00:00 2001 From: Roy Sommer Date: Thu, 2 Jan 2020 19:12:08 +0200 Subject: added support for passing arguments into taskipy tasks --- .../count.sh | 1 + .../pyproject.toml | 14 +++++++ tests/test_taskipy.py | 48 +++++++++++++++++++++- 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/project_with_tasks_that_accept_arguments/count.sh create mode 100644 tests/fixtures/project_with_tasks_that_accept_arguments/pyproject.toml (limited to 'tests') diff --git a/tests/fixtures/project_with_tasks_that_accept_arguments/count.sh b/tests/fixtures/project_with_tasks_that_accept_arguments/count.sh new file mode 100644 index 0000000..17153d5 --- /dev/null +++ b/tests/fixtures/project_with_tasks_that_accept_arguments/count.sh @@ -0,0 +1 @@ +echo the argument count is $# \ No newline at end of file diff --git a/tests/fixtures/project_with_tasks_that_accept_arguments/pyproject.toml b/tests/fixtures/project_with_tasks_that_accept_arguments/pyproject.toml new file mode 100644 index 0000000..1459281 --- /dev/null +++ b/tests/fixtures/project_with_tasks_that_accept_arguments/pyproject.toml @@ -0,0 +1,14 @@ +[tool.poetry] +name = "taskipy" +description = "tasks runner for python projects" + +[tool.taskipy.tasks] +echo_number = "echo the number is " +echo_named = "echo got a named argument " +echo_args_count = "bash count.sh" + +pre_echo_on_prehook = "echo the number in prehook is " +echo_on_prehook = "echo ok" + +echo_on_posthook = "echo ok" +post_echo_on_posthook = "echo the number in posthook is " diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py index 8e220c9..9d74d9d 100644 --- a/tests/test_taskipy.py +++ b/tests/test_taskipy.py @@ -1,6 +1,7 @@ import os import unittest import subprocess +import random from os import path from typing import List, Tuple from tests.utils.fixture import FixtureTempDir @@ -13,10 +14,11 @@ class TaskipyTestCase(unittest.TestCase): for tmp_dir in self._tmp_dirs: tmp_dir.clean() - def run_task(self, task: str, cwd=os.curdir) -> Tuple[int, str, str]: + 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], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) + proc = subprocess.Popen([executable_path, task] + args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd) stdout, stderr = proc.communicate() return proc.returncode, str(stdout), str(stderr) @@ -101,6 +103,48 @@ class TaskPrePostHooksTestCase(TaskipyTestCase): self.assertSubstr('post_task', stdout) self.assertEqual(exit_code, 1) +class PassArgumentsTestCase(TaskipyTestCase): + def test_running_task_with_positional_arguments(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_that_accept_arguments') + some_random_number = random.randint(1, 1000) + exit_code, stdout, _ = self.run_task('echo_number', args=[f'{some_random_number}'], cwd=cwd) + + self.assertSubstr(f'the number is {some_random_number}', stdout) + self.assertEqual(exit_code, 0) + + def test_running_task_with_named_arguments(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_that_accept_arguments') + exit_code, stdout, _ = self.run_task('echo_named', args=['-h'], cwd=cwd) + + self.assertSubstr(f'got a named argument -h', stdout) + self.assertEqual(exit_code, 0) + + def test_running_task_with_multiple_arguments(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_that_accept_arguments') + args = ['one', 'two', 'three', 'four', 'five'] + exit_code, stdout, _ = self.run_task('echo_args_count', args=args, cwd=cwd) + + self.assertSubstr(f'the argument count is 5', stdout) + self.assertEqual(exit_code, 0) + + def test_running_task_arguments_not_passed_to_pre_hook(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_that_accept_arguments') + some_random_number = random.randint(1, 1000) + exit_code, stdout, _ = self.run_task('echo_on_prehook', args=[f'{some_random_number}'], cwd=cwd) + + self.assertSubstr(f'the number in prehook is', stdout) + self.assertNotSubstr(f'the number in prehook is {some_random_number}', stdout) + self.assertEqual(exit_code, 0) + + def test_running_task_arguments_not_passed_to_post_hook(self): + cwd = self.create_test_dir_from_fixture('project_with_tasks_that_accept_arguments') + some_random_number = random.randint(1, 1000) + exit_code, stdout, _ = self.run_task('echo_on_posthook', args=[f'{some_random_number}'], cwd=cwd) + + self.assertSubstr(f'the number in posthook is', stdout) + self.assertNotSubstr(f'the number in posthook is {some_random_number}', stdout) + self.assertEqual(exit_code, 0) + class TaskRunFailTestCase(TaskipyTestCase): def test_exiting_with_code_127_and_printing_if_task_not_found(self): cwd = self.create_test_dir_from_fixture('project_with_pyproject_and_tasks') -- cgit v1.2.3