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

Proposal: pipx run --inspect script.py #1457

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions src/pipx/commands/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def run_script(
venv_args: List[str],
verbose: bool,
use_cache: bool,
inspect: bool,
) -> NoReturn:
requirements = _get_requirements_from_script(content)
if requirements is None:
Expand All @@ -103,7 +104,8 @@ def run_script(
python_path = venv.python_path

if isinstance(content, Path):
exec_app([python_path, content, *app_args])
cmd: List[str | Path] = [python_path, "-i", content, *app_args] if inspect else [python_path, content, *app_args]
exec_app(cmd)
else:
exec_app([python_path, "-c", content, *app_args])

Expand Down Expand Up @@ -185,6 +187,7 @@ def run(
pypackages: bool,
verbose: bool,
use_cache: bool,
inspect: bool,
) -> NoReturn:
"""Installs venv to temporary dir (or reuses cache), then runs app from
package
Expand All @@ -200,7 +203,7 @@ def run(

content = None if spec is not None else maybe_script_content(app, is_path)
if content is not None:
run_script(content, app_args, python, pip_args, venv_args, verbose, use_cache)
run_script(content, app_args, python, pip_args, venv_args, verbose, use_cache, inspect)
else:
package_or_url = spec if spec is not None else app
run_package(
Expand Down
2 changes: 2 additions & 0 deletions src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ def run_pipx_command(args: argparse.Namespace, subparsers: Dict[str, argparse.Ar
args.pypackages,
verbose,
not args.no_cache,
args.inspect,
)
# We should never reach here because run() is NoReturn.
return ExitCode(1)
Expand Down Expand Up @@ -834,6 +835,7 @@ def _add_run(subparsers: argparse._SubParsersAction, shared_parser: argparse.Arg
p.add_argument("--spec", help=SPEC_HELP)
add_python_options(p)
add_pip_venv_args(p)
p.add_argument("--inspect", action="store_true", help="Inspect interactively after running script")
p.set_defaults(subparser=p)

# modify usage text to show required app argument
Expand Down
10 changes: 10 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,3 +414,13 @@ def test_run_local_path_entry_point(pipx_temp_env, caplog, root):
run_pipx_cli_exit(["run", empty_project_path])

assert "Using discovered entry point for 'pipx run'" in caplog.text


@mock.patch("pipx.commands.run.exec_app")
def test_run_inspect(exec_app, tmp_path):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is difficult to use execvpe_mock in this (python -i) case.

I will appreciate your comment about this test implementation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’d be quite difficult to actually test this. I would be satisfied if this just mock out the actual process invocation and just chheck the right flags are passed to the child process.

script = tmp_path / "test.py"
script.write_text("a = 1 + 1")

run_pipx_cli(["run", "--inspect", str(script)])

exec_app.assert_called_once_with([mock.ANY, "-i", script])
Loading