Skip to content

Commit 62b47b6

Browse files
authored
handle listing tasks when there are no tasks (#52)
1 parent 9f3e527 commit 62b47b6

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"editor.tabSize": 4,
33
"python.linting.enabled": true,
44
"python.linting.pylintEnabled": true,
5-
"python.linting.mypyEnabled": true
5+
"python.linting.mypyEnabled": true,
6+
"python.formatting.provider": "none"
67
}

taskipy/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ def __str__(self):
8181
)
8282

8383

84+
class EmptyTasksSectionError(TaskipyError):
85+
exit_code = 127
86+
87+
def __str__(self):
88+
return (
89+
'no tasks found. create your first task '
90+
'by adding it to your pyproject.toml file under [tool.taskipy.tasks]'
91+
)
92+
93+
8494
class CircularVariableError(TaskipyError):
8595
exit_code = 127
8696

taskipy/help.py renamed to taskipy/list.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
from typing import List
55

66
from taskipy.task import Task
7+
from taskipy.exceptions import EmptyTasksSectionError
78

89

9-
class HelpFormatter:
10+
class TasksListFormatter:
1011
def __init__(self, tasks: List[Task]):
1112
self.__tasks = tasks
1213

1314
def print(self, line_width=shutil.get_terminal_size().columns):
1415
colorama.init()
1516

17+
if not self.__tasks:
18+
raise EmptyTasksSectionError()
19+
1620
tasks_col = [task.name for task in self.__tasks]
1721
longest_item_in_tasks_col = len(max(tasks_col, key=len))
1822

taskipy/task_runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import psutil # type: ignore
1010

1111
from taskipy.exceptions import CircularVariableError, TaskNotFoundError, MalformedTaskError
12-
from taskipy.help import HelpFormatter
12+
from taskipy.list import TasksListFormatter
1313
from taskipy.pyproject import PyProject
1414
from taskipy.task import Task
1515
from taskipy.variable import Variable
@@ -28,7 +28,7 @@ def __init__(self, cwd: Union[str, Path]):
2828

2929
def list(self):
3030
"""lists tasks to stdout"""
31-
formatter = HelpFormatter(self.__project.tasks.values())
31+
formatter = TasksListFormatter(self.__project.tasks.values())
3232
formatter.print()
3333

3434
def run(self, task_name: str, args: List[str]) -> int:

tests/test_taskipy.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,24 @@ def test_running_task_list_with_arg(self):
221221
self.assertTerminalTextEqual(expected, stdout.strip())
222222
self.assertEqual(exit_code, 0)
223223

224+
def test_running_task_list_no_tasks(self):
225+
py_project_toml = '''
226+
[tool.taskipy.tasks]
227+
'''
228+
cwd = self.create_test_dir_with_py_project_toml(py_project_toml)
229+
exit_code, stdout, _ = self.run_task('--list', cwd=cwd)
230+
231+
self.assertTerminalTextEqual('no tasks found. create your first task by adding it to your pyproject.toml file under [tool.taskipy.tasks]', stdout.strip())
232+
self.assertEqual(exit_code, 127)
233+
234+
def test_running_task_list_no_tasks_section(self):
235+
py_project_toml = ''
236+
cwd = self.create_test_dir_with_py_project_toml(py_project_toml)
237+
exit_code, stdout, _ = self.run_task('--list', cwd=cwd)
238+
239+
self.assertTerminalTextEqual('no tasks found. add a [tool.taskipy.tasks] section to your pyproject.toml', stdout.strip())
240+
self.assertEqual(exit_code, 127)
241+
224242

225243
class TaskDescriptionTestCase(TaskipyTestCase):
226244
def test_running_task_with_description(self):

0 commit comments

Comments
 (0)