Skip to content

Commit

Permalink
Move --global option into shared parser (pypa#1397)
Browse files Browse the repository at this point in the history
* Move `--global` option into shared parser

* Add changelog

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Update tests

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and DimitriPapadopoulos committed May 12, 2024
1 parent 2bcb5b7 commit f6c2107
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 24 deletions.
1 change: 1 addition & 0 deletions changelog.d/1397.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Move `--global` option into shared parser, such that it can be passed after the subcommand, for example `pipx ensurepath --global`.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ lint.extend-select = [
"PLE",
"PLW",
"RSE",
"RUF100",
"W",
]
lint.isort = {known-first-party = ["helpers", "package_info", "pipx"]}
Expand Down
15 changes: 8 additions & 7 deletions src/pipx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,14 @@ def get_command_parser() -> Tuple[argparse.ArgumentParser, Dict[str, argparse.Ar
),
)

if not constants.WINDOWS:
shared_parser.add_argument(
"--global",
action="store_true",
dest="is_global",
help="Perform action globally for all users.",
)

parser = argparse.ArgumentParser(
prog=prog_name(),
formatter_class=LineWrapRawTextHelpFormatter,
Expand Down Expand Up @@ -906,13 +914,6 @@ def get_command_parser() -> Tuple[argparse.ArgumentParser, Dict[str, argparse.Ar
_add_ensurepath(subparsers, shared_parser)
_add_environment(subparsers, shared_parser)

if not constants.WINDOWS:
parser.add_argument(
"--global",
action="store_true",
dest="is_global",
help="Perform action globally for all users.",
)
parser.add_argument("--version", action="store_true", help="Print version and exit")
subparsers.add_parser(
"completions",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_resolve_user_dir_in_env_paths(monkeypatch):

@skip_if_windows
def test_cli_global(pipx_temp_env, monkeypatch, capsys):
assert not run_pipx_cli(["--global", "environment"])
assert not run_pipx_cli(["environment", "--global"])
captured = capsys.readouterr()
assert fnmatch.fnmatch(captured.out, "*PIPX_HOME=*global/pipxhome*")
assert fnmatch.fnmatch(captured.out, "*PIPX_BIN_DIR=*global_otherdir/pipxbindir*")
Expand Down
4 changes: 2 additions & 2 deletions tests/test_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def test_inject_simple(pipx_temp_env, capsys):

@skip_if_windows
def test_inject_simple_global(pipx_temp_env, capsys):
assert not run_pipx_cli(["--global", "install", "pycowsay"])
assert not run_pipx_cli(["--global", "inject", "pycowsay", PKG["black"]["spec"]])
assert not run_pipx_cli(["install", "--global", "pycowsay"])
assert not run_pipx_cli(["inject", "--global", "pycowsay", PKG["black"]["spec"]])


@pytest.mark.parametrize("metadata_version", PIPX_METADATA_LEGACY_VERSIONS)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def test_cli_global(pipx_temp_env, monkeypatch, capsys):
captured = capsys.readouterr()
assert "installed package" in captured.out

assert not run_pipx_cli(["--global", "list"])
assert not run_pipx_cli(["list", "--global"])
captured = capsys.readouterr()
assert "nothing has been installed with pipx" in captured.err

Expand Down
4 changes: 2 additions & 2 deletions tests/test_reinstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ def test_reinstall(pipx_temp_env, capsys):

@skip_if_windows
def test_reinstall_global(pipx_temp_env, capsys):
assert not run_pipx_cli(["--global", "install", "pycowsay"])
assert not run_pipx_cli(["--global", "reinstall", "--python", sys.executable, "pycowsay"])
assert not run_pipx_cli(["install", "--global", "pycowsay"])
assert not run_pipx_cli(["reinstall", "--global", "--python", sys.executable, "pycowsay"])


def test_reinstall_nonexistent(pipx_temp_env, capsys):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_runpip.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ def test_runpip(pipx_temp_env, monkeypatch, capsys):

@skip_if_windows
def test_runpip_global(pipx_temp_env, monkeypatch, capsys):
assert not run_pipx_cli(["--global", "install", "pycowsay"])
assert not run_pipx_cli(["--global", "runpip", "pycowsay", "list"])
assert not run_pipx_cli(["install", "--global", "pycowsay"])
assert not run_pipx_cli(["runpip", "--global", "pycowsay", "list"])
8 changes: 4 additions & 4 deletions tests/test_uninject.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ def test_uninject_simple(pipx_temp_env, capsys):

@skip_if_windows
def test_uninject_simple_global(pipx_temp_env, capsys):
assert not run_pipx_cli(["--global", "install", "pycowsay"])
assert not run_pipx_cli(["--global", "inject", "pycowsay", PKG["black"]["spec"]])
assert not run_pipx_cli(["--global", "uninject", "pycowsay", "black"])
assert not run_pipx_cli(["install", "--global", "pycowsay"])
assert not run_pipx_cli(["inject", "--global", "pycowsay", PKG["black"]["spec"]])
assert not run_pipx_cli(["uninject", "--global", "pycowsay", "black"])
captured = capsys.readouterr()
assert "Uninjected package black" in captured.out
assert not run_pipx_cli(["--global", "list", "--include-injected"])
assert not run_pipx_cli(["list", "--global", "--include-injected"])
captured = capsys.readouterr()
assert "black" not in captured.out

Expand Down
4 changes: 2 additions & 2 deletions tests/test_uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def test_uninstall(pipx_temp_env):

@skip_if_windows
def test_uninstall_global(pipx_temp_env):
assert not run_pipx_cli(["--global", "install", "pycowsay"])
assert not run_pipx_cli(["--global", "uninstall", "pycowsay"])
assert not run_pipx_cli(["install", "--global", "pycowsay"])
assert not run_pipx_cli(["uninstall", "--global", "pycowsay"])


def test_uninstall_circular_deps(pipx_temp_env):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ def test_upgrade(pipx_temp_env, capsys):

@skip_if_windows
def test_upgrade_global(pipx_temp_env, capsys):
assert run_pipx_cli(["--global", "upgrade", "pycowsay"])
assert run_pipx_cli(["upgrade", "--global", "pycowsay"])
captured = capsys.readouterr()
assert "Package is not installed" in captured.err

assert not run_pipx_cli(["--global", "install", "pycowsay"])
assert not run_pipx_cli(["install", "--global", "pycowsay"])
captured = capsys.readouterr()
assert "installed package pycowsay" in captured.out

assert not run_pipx_cli(["--global", "upgrade", "pycowsay"])
assert not run_pipx_cli(["upgrade", "--global", "pycowsay"])
captured = capsys.readouterr()
assert "pycowsay is already at latest version" in captured.out

Expand Down

0 comments on commit f6c2107

Please sign in to comment.