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

recursive script #1

Merged
merged 1 commit into from
Apr 3, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/xxxx.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refuse to run recursive composite scripts.
24 changes: 20 additions & 4 deletions src/pdm/cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ def forward_signal(signum: int, frame: FrameType | None) -> None:
signal.signal(signal.SIGINT, handle_int)
return process.returncode

def run_task(self, task: Task, args: Sequence[str] = (), opts: TaskOptions | None = None) -> int:
def run_task(
self, task: Task, args: Sequence[str] = (), opts: TaskOptions | None = None, seen: set[str] | None = None
) -> int:
kind, _, value, options = task
shell = False
if kind == "cmd":
Expand Down Expand Up @@ -281,26 +283,40 @@ def run_task(self, task: Task, args: Sequence[str] = (), opts: TaskOptions | Non
split = shlex.split(script)
cmd = split[0]
subargs = split[1:] + ([] if should_interpolate else args)
code = self.run(cmd, subargs, options, chdir=True)
code = self.run(cmd, subargs, options, chdir=True, seen=seen)
if code != 0:
if not keep_going:
return code
composite_code = code
return composite_code
return self._run_process(args, chdir=True, shell=shell, **exec_opts(self.global_options, options, opts))

def run(self, command: str, args: list[str], opts: TaskOptions | None = None, chdir: bool = False) -> int:
def run(
self,
command: str,
args: list[str],
opts: TaskOptions | None = None,
chdir: bool = False,
seen: set[str] | None = None,
) -> int:
if command in self.hooks.skip:
return 0
if seen is None:
seen = set()
task = self.get_task(command)
if task is not None:
if task.kind == "composite":
if command in seen:
raise PdmUsageError(f"Script {command} is recursive.")
seen.add(command)

self.hooks.try_emit("pre_script", script=command, args=args)
pre_task = self.get_task(f"pre_{command}")
if pre_task is not None and self.hooks.should_run(pre_task.name):
code = self.run_task(pre_task, opts=opts)
if code != 0:
return code
code = self.run_task(task, args, opts=opts)
code = self.run_task(task, args, opts=opts, seen=seen)
if code != 0:
return code
post_task = self.get_task(f"post_{command}")
Expand Down
20 changes: 20 additions & 0 deletions tests/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,26 @@ def test_composite_fail_on_first_missing_task(project, pdm, capfd, _echo):
assert "Second CALLED" not in out


def test_composite_fails_on_recursive_script(project, pdm):
project.pyproject.settings["scripts"] = {
"first": {"composite": ["first"]},
"second": {"composite": ["third"]},
"third": {"composite": ["second"]},
"forth": {"composite": ["python -V", "python -V"]},
}
project.pyproject.write()
result = pdm(["run", "first"], obj=project)
assert result.exit_code == 1
assert "Script first is recursive" in result.stderr

result = pdm(["run", "second"], obj=project)
assert result.exit_code == 1
assert "Script second is recursive" in result.stderr

result = pdm(["run", "forth"], obj=project)
assert result.exit_code == 0


def test_composite_runs_all_hooks(project, pdm, capfd, _echo):
project.pyproject.settings["scripts"] = {
"test": {"composite": ["first", "second"]},
Expand Down