Skip to content

Commit

Permalink
fix(scripts): merge the Script and Description field from listing
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed Jun 22, 2022
1 parent c80b7ff commit 70a5bac
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
21 changes: 17 additions & 4 deletions pdm/cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ class Task(NamedTuple):
def __str__(self) -> str:
return f"<task [cyan]{self.name}[/]>"

@property
def short_description(self) -> str:
"""
A short one line task description
"""
if self.kind == "composite":
fallback = " ➤ ".join(self.args)
else:
lines = [
line.strip() for line in str(self.args).splitlines() if line.strip()
]
fallback = f"{lines[0]}⮨" if len(lines) > 1 else lines[0]
return self.options.get("help", fallback)


class TaskRunner:
"""The task runner for pdm project"""
Expand Down Expand Up @@ -261,9 +275,9 @@ def run(
def show_list(self) -> None:
if not self.project.scripts:
return
columns = ["Name", "Type", "Script", "Description"]
columns = ["Name", "Type", "Description"]
result = []
for name in self.project.scripts:
for name in sorted(self.project.scripts):
if name == "_":
continue
task = self._get_task(name)
Expand All @@ -272,8 +286,7 @@ def show_list(self) -> None:
(
f"[green]{name}[/]",
task.kind,
str(task.args),
task.options.get("help", ""),
task.short_description,
)
)
self.project.core.ui.display_columns(result, columns)
Expand Down
18 changes: 15 additions & 3 deletions tests/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,22 +274,34 @@ def test_run_script_override_global_env(project, invoke, capfd):

def test_run_show_list_of_scripts(project, invoke):
project.tool_settings["scripts"] = {
"test_composite": {"composite": ["test_cmd", "test_script", "test_shell"]},
"test_cmd": "flask db upgrade",
"test_multi": """\
I am a multilines
command
""",
"test_script": {"call": "test_script:main", "help": "call a python function"},
"test_shell": {"shell": "echo $FOO", "help": "shell command"},
}
project.write_pyproject()
result = invoke(["run", "--list"], obj=project)
result_lines = result.output.splitlines()[3:]
assert result_lines[0][1:-1].strip() == "test_cmd │ cmd │ flask db upgrade │"
assert (
result_lines[0][1:-1].strip() == "test_cmd │ cmd │ flask db upgrade"
)
assert (
result_lines[1][1:-1].strip()
== "test_scriptcall │ test_script:main │ call a python function"
== "test_compositecomposite │ test_cmd ➤ test_script ➤ test_shell"
)
assert (
result_lines[2][1:-1].strip()
== "test_shell │ shell │ echo $FOO │ shell command"
== "test_multi │ cmd │ I am a multilines⮨"
)
assert (
result_lines[3][1:-1].strip()
== "test_script │ call │ call a python function"
)
assert result_lines[4][1:-1].strip() == "test_shell │ shell │ shell command"


def test_run_with_another_project_root(project, local_finder, invoke, capfd):
Expand Down

0 comments on commit 70a5bac

Please sign in to comment.