Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gracefully handle "task --list" when there are no tasks #52

Merged
merged 1 commit into from Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .vscode/settings.json
Expand Up @@ -2,5 +2,6 @@
"editor.tabSize": 4,
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.mypyEnabled": true
"python.linting.mypyEnabled": true,
"python.formatting.provider": "none"
}
10 changes: 10 additions & 0 deletions taskipy/exceptions.py
Expand Up @@ -81,6 +81,16 @@ def __str__(self):
)


class EmptyTasksSectionError(TaskipyError):
exit_code = 127

def __str__(self):
return (
'no tasks found. create your first task '
'by adding it to your pyproject.toml file under [tool.taskipy.tasks]'
)


class CircularVariableError(TaskipyError):
exit_code = 127

Expand Down
6 changes: 5 additions & 1 deletion taskipy/help.py → taskipy/list.py
Expand Up @@ -4,15 +4,19 @@
from typing import List

from taskipy.task import Task
from taskipy.exceptions import EmptyTasksSectionError


class HelpFormatter:
class TasksListFormatter:
def __init__(self, tasks: List[Task]):
self.__tasks = tasks

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

if not self.__tasks:
raise EmptyTasksSectionError()

tasks_col = [task.name for task in self.__tasks]
longest_item_in_tasks_col = len(max(tasks_col, key=len))

Expand Down
4 changes: 2 additions & 2 deletions taskipy/task_runner.py
Expand Up @@ -9,7 +9,7 @@
import psutil # type: ignore

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

def list(self):
"""lists tasks to stdout"""
formatter = HelpFormatter(self.__project.tasks.values())
formatter = TasksListFormatter(self.__project.tasks.values())
formatter.print()

def run(self, task_name: str, args: List[str]) -> int:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_taskipy.py
Expand Up @@ -221,6 +221,24 @@ def test_running_task_list_with_arg(self):
self.assertTerminalTextEqual(expected, stdout.strip())
self.assertEqual(exit_code, 0)

def test_running_task_list_no_tasks(self):
py_project_toml = '''
[tool.taskipy.tasks]
'''
cwd = self.create_test_dir_with_py_project_toml(py_project_toml)
exit_code, stdout, _ = self.run_task('--list', cwd=cwd)

self.assertTerminalTextEqual('no tasks found. create your first task by adding it to your pyproject.toml file under [tool.taskipy.tasks]', stdout.strip())
self.assertEqual(exit_code, 127)

def test_running_task_list_no_tasks_section(self):
py_project_toml = ''
cwd = self.create_test_dir_with_py_project_toml(py_project_toml)
exit_code, stdout, _ = self.run_task('--list', cwd=cwd)

self.assertTerminalTextEqual('no tasks found. add a [tool.taskipy.tasks] section to your pyproject.toml', stdout.strip())
self.assertEqual(exit_code, 127)


class TaskDescriptionTestCase(TaskipyTestCase):
def test_running_task_with_description(self):
Expand Down