Skip to content

Commit

Permalink
feat(run): allow customizing scripts display from settings (fix #3313)
Browse files Browse the repository at this point in the history
  • Loading branch information
noirbizarre committed Dec 2, 2024
1 parent 2631439 commit 80a928f
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 5 deletions.
1 change: 1 addition & 0 deletions news/3313.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow customizing scripts display with `scripts.show_header` settings.
20 changes: 15 additions & 5 deletions src/pdm/cli/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,11 +341,8 @@ def run_task(
elif kind == "composite":
assert isinstance(value, list)

self.project.core.ui.echo(
f"Running {task}: [success]{args}[/]\n",
err=True,
verbosity=termui.Verbosity.NORMAL,
)
self.display_task(task, args)

if kind == "composite":
args = list(args)
should_interpolate = any(RE_ARGS_PLACEHOLDER.search(script) for script in value)
Expand All @@ -366,6 +363,19 @@ def run_task(
return composite_code
return self._run_process(args, chdir=True, shell=shell, **merge_options(self.global_options, options, opts)) # type: ignore[misc]

def display_task(self, task: Task, args: Sequence[str]) -> None:
"""Display a task given current verbosity and settings"""
is_verbose = self.project.core.ui.verbosity >= termui.Verbosity.DETAIL
if not (is_verbose or self.project.config.get("scripts.show_header")):
return
args = task.args if task.kind == "composite" else args
content = args if is_verbose else task.short_description
self.project.core.ui.echo(
f"Running {task}: [success]{content}[/]",
err=True,
verbosity=termui.Verbosity.NORMAL,
)

def run(
self,
command: str,
Expand Down
6 changes: 6 additions & 0 deletions src/pdm/project/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,12 @@ class Config(MutableMapping[str, str]):
env_var="PDM_PYPI_JSON_API",
coerce=ensure_boolean,
),
"scripts.show_header": ConfigItem(
"Display script name and help before running",
default=False,
env_var="PDM_SCRIPTS_SHOW_HEADER",
coerce=ensure_boolean,
),
"venv.location": ConfigItem(
"Parent directory for virtualenvs",
os.path.join(platformdirs.user_data_dir("pdm"), "venvs"),
Expand Down
106 changes: 106 additions & 0 deletions tests/cli/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,3 +1028,109 @@ def test_run_script_pass_run_cwd_to_original_working_dir_when_working_dir_of_scr
capfd.readouterr()
pdm(["run", "test_script"], obj=project, strict=True)
assert capfd.readouterr()[0].strip() == str(Path.cwd())


def test_run_script_default_verbosity(project, pdm):
project.pyproject.settings["scripts"] = {"test": {"cmd": "python -V", "help": "help"}}
project.pyproject.write()

result = pdm(["run", "test"], strict=True, obj=project)

assert "task test" not in result.stderr
assert "['python', '-V']" not in result.stderr
assert "help" not in result.stderr


def test_run_script_default_verbosity_with_show_header(project, pdm):
project.project_config.update({"scripts.show_header": True})
project.pyproject.settings["scripts"] = {"test": {"cmd": "python -V", "help": "help"}}
project.pyproject.write()

result = pdm(["run", "test"], strict=True, obj=project)

assert "task test" in result.stderr
assert "['python', '-V']" not in result.stderr
assert "python -V" not in result.stderr
assert "help" in result.stderr


def test_run_script_default_verbosity_with_show_header_no_help(project, pdm):
project.project_config.update({"scripts.show_header": True})
project.pyproject.settings["scripts"] = {"test": {"cmd": "python -V"}}
project.pyproject.write()

result = pdm(["run", "test"], strict=True, obj=project)

assert "task test" in result.stderr
assert "python -V" in result.stderr
assert "['python', '-V']" not in result.stderr


def test_run_script_verbose(project, pdm):
project.pyproject.settings["scripts"] = {"test": {"cmd": "python -V", "help": "help"}}
project.pyproject.write()

result = pdm(["run", "-v", "test"], strict=True, obj=project)

assert "task test" in result.stderr
assert "['python', '-V']" in result.stderr
assert "python -V" not in result.stderr
assert "help" not in result.stderr


def test_run_composite_script_default_verbosity_with_show_header(project, pdm):
project.project_config.update({"scripts.show_header": True})
project.pyproject.settings["scripts"] = {
"test": {"cmd": "python -V", "help": "help"},
"parent": {"composite": ["test", "test"]},
}
project.pyproject.write()

result = pdm(["run", "parent"], strict=True, obj=project)

assert "task parent" in result.stderr
assert f"test {termui.Emoji.ARROW_SEPARATOR} test" in result.stderr
assert "['test', 'test']" not in result.stderr
assert "task test" in result.stderr
assert "python -V" not in result.stderr
assert "['python', '-V']" not in result.stderr
assert "help" in result.stderr


def test_run_composite_script_default_verbosity_with_show_header_and_help(project, pdm):
project.project_config.update({"scripts.show_header": True})
project.pyproject.settings["scripts"] = {
"test": {"cmd": "python -V", "help": "help"},
"parent": {"composite": ["test", "test"], "help": "composite"},
}
project.pyproject.write()

result = pdm(["run", "parent"], strict=True, obj=project)

assert "task parent" in result.stderr
assert "composite" in result.stderr
assert f"test {termui.Emoji.ARROW_SEPARATOR} test" not in result.stderr
assert "['test', 'test']" not in result.stderr
assert "task test" in result.stderr
assert "['python', '-V']" not in result.stderr
assert "python -V" not in result.stderr
assert "help" in result.stderr


def test_run_composite_script_verbose(project, pdm):
project.pyproject.settings["scripts"] = {
"test": {"cmd": "python -V", "help": "help"},
"parent": {"composite": ["test", "test"], "help": "composite"},
}
project.pyproject.write()

result = pdm(["run", "-v", "parent"], strict=True, obj=project)

assert "task parent" in result.stderr
assert "composite" not in result.stderr
assert f"test {termui.Emoji.ARROW_SEPARATOR} test" not in result.stderr
assert "['test', 'test']" in result.stderr
assert "task test" in result.stderr
assert "['python', '-V']" in result.stderr
assert "python -V" not in result.stderr
assert "help" not in result.stderr

0 comments on commit 80a928f

Please sign in to comment.