From 057356cfd166868cba48c286ba32b96964892ff5 Mon Sep 17 00:00:00 2001 From: nikkie Date: Tue, 18 Jun 2024 11:49:21 +0000 Subject: [PATCH 1/2] [feat] pipx run --inspect --- src/pipx/commands/run.py | 7 +++++-- src/pipx/main.py | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pipx/commands/run.py b/src/pipx/commands/run.py index c68dfcf8a2..caa52d02c0 100644 --- a/src/pipx/commands/run.py +++ b/src/pipx/commands/run.py @@ -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: @@ -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]) @@ -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 @@ -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( diff --git a/src/pipx/main.py b/src/pipx/main.py index 72c6c347d5..a75cd92d19 100644 --- a/src/pipx/main.py +++ b/src/pipx/main.py @@ -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) @@ -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 From 42cef605d57457f6575be2f4743f94c3ff143337 Mon Sep 17 00:00:00 2001 From: nikkie Date: Tue, 18 Jun 2024 12:34:43 +0000 Subject: [PATCH 2/2] [test] pipx run --inspect script.py --- tests/test_run.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_run.py b/tests/test_run.py index 752253ccf3..4b5bd87cc8 100644 --- a/tests/test_run.py +++ b/tests/test_run.py @@ -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): + 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])