diff options
| author | Roy Sommer <royso@wix.com> | 2020-01-02 19:12:08 +0200 |
|---|---|---|
| committer | Roy Sommer <royso@wix.com> | 2020-01-02 19:12:08 +0200 |
| commit | b7c1dc794d4e4faa068594139e945fbeac7b02b5 (patch) | |
| tree | c63f4ee8a615bafbb735820388993cfa4635159d | |
| parent | updated pytoml (diff) | |
| download | taskipy2-b7c1dc794d4e4faa068594139e945fbeac7b02b5.zip | |
added support for passing arguments into taskipy tasks
| -rw-r--r-- | README.md | 17 | ||||
| -rwxr-xr-x | taskipy/cli.py | 3 | ||||
| -rw-r--r-- | taskipy/run.py | 5 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_that_accept_arguments/count.sh | 1 | ||||
| -rw-r--r-- | tests/fixtures/project_with_tasks_that_accept_arguments/pyproject.toml | 14 | ||||
| -rw-r--r-- | tests/test_taskipy.py | 48 |
6 files changed, 83 insertions, 5 deletions
@@ -54,6 +54,23 @@ In order to run a task, run the following command in your terminal: poetry run task test ``` +### Passing Command Line Args to Tasks +If you want to pass command line arguments to tasks (positional or named), simply append them to the end of the task command. + +For example, running the above task like this: +```bash +poetry run task test -h +``` + +Is equivalent to running: +```bash +python -m unittest tests/test_*.py -h +``` + +And will show unittest's help instead of actually running it. + +> ⚠️ Note: if you are using pre \ post hooks, do notice that arguments are not passed to them, only to the task itself. + ### Composing Tasks #### Grouping Subtasks Together Some tasks are composed of multiple subtasks. Instead of writing plain shell commands and stringing them together, you can break them down into multiple subtasks: diff --git a/taskipy/cli.py b/taskipy/cli.py index a450db0..f37d5f6 100755 --- a/taskipy/cli.py +++ b/taskipy/cli.py @@ -5,9 +5,10 @@ from taskipy.run import run_task def main(): parser = argparse.ArgumentParser(prog='task', description='runs a task specified in your pyproject.toml under [tool.taskipy.tasks]') parser.add_argument('name', help='name of the task') + parser.add_argument('args', nargs=argparse.REMAINDER, help='arguments to pass to the task') args = parser.parse_args() - run_task(args.name) + run_task(args.name, args.args) if __name__ == '__main__': main() diff --git a/taskipy/run.py b/taskipy/run.py index b8ae4ab..ff2db44 100644 --- a/taskipy/run.py +++ b/taskipy/run.py @@ -5,7 +5,7 @@ import toml from os import path from typing import List -def run_task(task_name: str, cwd=os.curdir): +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) @@ -34,7 +34,8 @@ def run_task(task_name: str, cwd=os.curdir): try: task = tasks[task_name] - commands.append(task) + command_with_passed_args = ' '.join([task] + args) + commands.append(command_with_passed_args) except: print(f'could not find task "{task_name}""') sys.exit(127) 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') |