Skip to content

Commit

Permalink
fix(entrypoint): properly handle whitespace depending on the script kind
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed Dec 16, 2023
1 parent 0c79364 commit 29d9a25
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/pdm_dockerize/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io
import re
import shlex
from typing import TYPE_CHECKING, Any

from pdm.cli.commands.run import TaskRunner, exec_opts
Expand Down Expand Up @@ -76,9 +77,12 @@ def usage(project: Project, runner: TaskRunner) -> str:
if task is None:
continue
if task.kind == "cmd" and isinstance(task.args, list):
out.write(f'{INDENT}echo "{script}: {" ".join(task.args)}"\n')
description = " ".join(task.args)
else:
out.write(f'{INDENT}echo "{script}: {task.short_description}"\n')
description = task.short_description
if "\n" in description:
description = f"{description.splitlines()[0]}…"
out.write(f'{INDENT}echo "{script}: {description}"\n')
out.write("}\n")
return out.getvalue()

Expand Down Expand Up @@ -123,11 +127,14 @@ def as_script(task: Task, opts: dict[str, Any]):
fn = m.group("fn")
args = m.group("args") or ""
out.write(f'{2 * INDENT}python -c "from {pkg} import {fn}; {fn}({args})"\n')
elif task.kind == "cmd" and isinstance(task.args, list):
out.write(f"{2 * INDENT}{' '.join(task.args)}\n")
elif task.kind == "cmd":
args = task.args if isinstance(task.args, list) else shlex.split(task.args)
out.write(f"{2 * INDENT}{' '.join(args)}\n")
elif task.kind == "composite":
for cmd in task.args:
out.write(f"{2 * INDENT}{cmd}\n")
normalized = " ".join(shlex.split(cmd))
out.write(f"{2 * INDENT}{normalized}\n")
else:
out.write(f"{2 * INDENT}{task.args}\n")
for line in task.args.splitlines():
out.write(f"{2 * INDENT}{line}\n")
return out.getvalue()
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/sh

export PYTHONPATH=./lib
export PATH=./bin:$PATH

usage() {
echo "Available commands"
echo "=================="
echo "cmd: whitespaces…"
echo "shell: whitespaces…"
echo "composite: whitespaces…"
}

case ${1} in
cmd)
whitespaces are ignored
;;
shell)
whitespaces
should be
preserved
;;
composite)
whitespaces are ignored
;;
*)
usage
;;
esac
30 changes: 30 additions & 0 deletions tests/test_entrypoint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from textwrap import dedent
from typing import TYPE_CHECKING, Any

import pytest
Expand Down Expand Up @@ -48,6 +49,35 @@ def entrypoint_for(project: Project, hooks: HookManager | None = None) -> str:
"test": "pytest",
"post_test": "post",
},
"whitespaces": {
"cmd": dedent(
"""\
whitespaces
are
ignored
"""
),
"shell": {
"shell": dedent(
"""\
whitespaces
should be
preserved
"""
)
},
"composite": {
"composite": [
dedent(
"""\
whitespaces
are
ignored
"""
)
]
},
},
}


Expand Down

0 comments on commit 29d9a25

Please sign in to comment.