aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBernardas AliĊĦauskas <bernardas.alisauskas@pm.me>2020-10-07 15:37:44 +0700
committerGitHub <noreply@github.com>2020-10-07 11:37:44 +0300
commitff080abe550af90a8445332058434743ee7529c7 (patch)
tree1ddc301d4751f1a52ee5dbabff5d1d27cd58ed4e
parentRelease version 1.3.0 (diff)
downloadtaskipy2-ff080abe550af90a8445332058434743ee7529c7.zip
add list command which lists tasks (#21)
* add list command which lists tasks * refactor list argument as a flag add tests and readme for --list flag * more tests * Update tests/test_taskipy.py Co-authored-by: Roy Sommer <illBeRoy@users.noreply.github.com> * refactor listing to taskrunner fix tests and cli to handle --list following arguments cases Co-authored-by: Roy Sommer <illBeRoy@users.noreply.github.com>
-rw-r--r--README.md5
-rwxr-xr-xtaskipy/cli.py14
-rw-r--r--taskipy/task_runner.py10
-rw-r--r--tests/fixtures/project_with_tasks_to_list/poetry.lock8
-rw-r--r--tests/fixtures/project_with_tasks_to_list/pyproject.toml10
-rw-r--r--tests/test_taskipy.py42
6 files changed, 77 insertions, 12 deletions
diff --git a/README.md b/README.md
index 7dc7cda..a259126 100644
--- a/README.md
+++ b/README.md
@@ -54,7 +54,10 @@ lint = "pylint tests taskipy"
### Running Tasks
In order to run a task, run the following command in your terminal:
```bash
-poetry run task test
+$ poetry run task --list
+test python -m unittest tests/test_*.py
+lint pylint tests taskipy
+$ poetry run task test
```
### Passing Command Line Args to Tasks
diff --git a/taskipy/cli.py b/taskipy/cli.py
index 06acd6c..d558e3b 100755
--- a/taskipy/cli.py
+++ b/taskipy/cli.py
@@ -12,14 +12,16 @@ def main():
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'
- )
+ parser.add_argument('--list', help='show list of available tasks', action='store_true')
+ parser.add_argument('name', help='name of the task', nargs='?')
+ parser.add_argument('args', nargs=argparse.REMAINDER, help='arguments to pass to the task')
args = parser.parse_args()
-
try:
- exit_code = TaskRunner(Path.cwd() / 'pyproject.toml').run(args.name, args.args)
+ runner = TaskRunner(Path.cwd() / 'pyproject.toml')
+ if args.list:
+ runner.list()
+ sys.exit(0)
+ exit_code = runner.run(args.name, args.args)
sys.exit(exit_code)
except TaskipyError as e:
print(e)
diff --git a/taskipy/task_runner.py b/taskipy/task_runner.py
index 4c126dc..5c16da1 100644
--- a/taskipy/task_runner.py
+++ b/taskipy/task_runner.py
@@ -13,10 +13,16 @@ from taskipy.task import Task
class TaskRunner:
def __init__(self, pyproject_path: Union[str, Path]):
self.__pyproject_path = pyproject_path
- self.__project = PyProject(pyproject_path)
+ self.project = PyProject(pyproject_path)
+
+ def list(self):
+ """lists tasks to stdout"""
+ longest = len(max(self.project.tasks, key=len))
+ for k, v in self.project.tasks.items():
+ print(f'{k:<{longest}} {v}')
def run(self, task_name: str, args: List[str]) -> int:
- task = Task(task_name, args, self.__project)
+ task = Task(task_name, args, self.project)
for command in task.commands:
if task.runner is not None:
diff --git a/tests/fixtures/project_with_tasks_to_list/poetry.lock b/tests/fixtures/project_with_tasks_to_list/poetry.lock
new file mode 100644
index 0000000..7df12a4
--- /dev/null
+++ b/tests/fixtures/project_with_tasks_to_list/poetry.lock
@@ -0,0 +1,8 @@
+package = []
+
+[metadata]
+content-hash = "115cf985d932e9bf5f540555bbdd75decbb62cac81e399375fc19f6277f8c1d8"
+lock-version = "1.0"
+python-versions = "*"
+
+[metadata.files]
diff --git a/tests/fixtures/project_with_tasks_to_list/pyproject.toml b/tests/fixtures/project_with_tasks_to_list/pyproject.toml
new file mode 100644
index 0000000..ff6f30a
--- /dev/null
+++ b/tests/fixtures/project_with_tasks_to_list/pyproject.toml
@@ -0,0 +1,10 @@
+[tool.poetry]
+name = "taskipy"
+description = "tasks runner for python projects"
+version = "1.0.0"
+authors = ["foobar"]
+
+[tool.taskipy.tasks]
+one = "echo first task"
+two = "echo second task"
+three = "echo third task"
diff --git a/tests/test_taskipy.py b/tests/test_taskipy.py
index 37b322d..3389de6 100644
--- a/tests/test_taskipy.py
+++ b/tests/test_taskipy.py
@@ -28,13 +28,11 @@ class TaskipyTestCase(unittest.TestCase):
def run_task(self, task: str, args: List[str] = None, cwd=os.curdir) -> Tuple[int, str, str]:
proc = self.start_taskipy_process(task, args=args, cwd=cwd)
stdout, stderr = proc.communicate()
-
- return proc.returncode, str(stdout), str(stderr)
+ return proc.returncode, stdout.decode(), 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):
@@ -179,6 +177,44 @@ class PassArgumentsTestCase(TaskipyTestCase):
self.assertEqual(exit_code, 0)
+class ListTasksTestCase(TaskipyTestCase):
+
+ def test_running_task_list(self):
+ cwd = self.create_test_dir_from_fixture('project_with_tasks_to_list')
+ exit_code, stdout, _ = self.run_task('--list', cwd=cwd)
+
+ expected = "\n".join([
+ "one echo first task",
+ "two echo second task",
+ "three echo third task",
+ ])
+ self.maxDiff = None
+ self.assertEqual(expected, stdout.strip())
+ self.assertEqual(exit_code, 0)
+
+ def test_running_task_list_before_name(self):
+ cwd = self.create_test_dir_from_fixture('project_with_tasks_to_list')
+ # anything following the flag should be ignored
+ exit_code, stdout, _ = self.run_task('--list', ['one'], cwd=cwd)
+ expected = "\n".join([
+ "one echo first task",
+ "two echo second task",
+ "three echo third task",
+ ])
+ self.maxDiff = None
+ self.assertEqual(expected, stdout.strip())
+ self.assertEqual(exit_code, 0)
+
+ def test_running_task_list_with_arg(self):
+ cwd = self.create_test_dir_from_fixture('project_with_tasks_to_list')
+ # when --list follows after task name it should be passed as an argument
+ exit_code, stdout, _ = self.run_task('one', ['--list'], cwd=cwd)
+ expected = "first task --list"
+ self.maxDiff = None
+ self.assertEqual(expected, stdout.strip())
+ 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')