Skip to content

Commit 245fd1c

Browse files
authored
fix(run): a blank script crashes pdm run --list (#3842)
* fix(run): fix crash when listing blank scripts * chore(news): rename fragment to the PR number * fix(run): mark a blank script as <BLANK_SCRIPT> per review --------- Co-authored-by: VXNCXNX <VXNCXNX@users.noreply.github.com>
1 parent 3034fa0 commit 245fd1c

3 files changed

Lines changed: 18 additions & 1 deletion

File tree

news/3842.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Don't crash with an `IndexError` when listing scripts and one of them is blank. A blank script is now listed as `<BLANK_SCRIPT>`.

src/pdm/cli/commands/run.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,11 @@ def short_description(self) -> str:
151151
fallback = f" {termui.Emoji.ARROW_SEPARATOR} ".join(self.args)
152152
else:
153153
lines = [line.strip() for line in str(self.args).splitlines() if line.strip()]
154-
fallback = f"{lines[0]}{termui.Emoji.ELLIPSIS}" if len(lines) > 1 else lines[0]
154+
if not lines:
155+
# a blank script leaves no lines at all; mark it rather than showing an empty cell
156+
fallback = "<BLANK_SCRIPT>"
157+
else:
158+
fallback = f"{lines[0]}{termui.Emoji.ELLIPSIS}" if len(lines) > 1 else lines[0]
155159
return self.options.get("help", fallback)
156160

157161

tests/cli/test_run.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,18 @@ def test_run_show_list_of_scripts(project, pdm):
630630
assert result_lines[4][1:-1].strip() == "test_shell │ shell │ shell command"
631631

632632

633+
def test_run_show_list_of_scripts_with_blank_script(project, pdm):
634+
project.pyproject.settings["scripts"] = {
635+
"test_blank": " ",
636+
"test_cmd": "flask db upgrade",
637+
}
638+
project.pyproject.write()
639+
result = pdm(["run", "--list"], obj=project)
640+
result_lines = result.output.splitlines()[3:]
641+
assert result_lines[0][1:-1].strip() == "test_blank │ cmd │ <BLANK_SCRIPT>"
642+
assert result_lines[1][1:-1].strip() == "test_cmd │ cmd │ flask db upgrade"
643+
644+
633645
def test_run_show_list_of_scripts_hide_internals(project, pdm):
634646
project.pyproject.settings["scripts"] = {
635647
"public": "true",

0 commit comments

Comments
 (0)