1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
import logging
import re
import textwrap
import pytest # type: ignore[import-not-found]
from helpers import PIPX_METADATA_LEGACY_VERSIONS, mock_legacy_venv, run_pipx_cli, skip_if_windows
from package_info import PKG
# Note that this also checks that packages used in other tests can be injected individually
@pytest.mark.parametrize(
"pkg_spec,",
[
PKG["black"]["spec"],
PKG["nox"]["spec"],
PKG["pylint"]["spec"],
PKG["ipython"]["spec"],
"jaraco.clipboard==2.0.1", # tricky character
],
)
def test_inject_single_package(pipx_temp_env, capsys, caplog, pkg_spec):
assert not run_pipx_cli(["install", "pycowsay"])
assert not run_pipx_cli(["inject", "pycowsay", pkg_spec])
# Check arguments have been parsed correctly
assert f"Injecting packages: {[pkg_spec]!r}" in caplog.text
# Check it's actually being installed and into correct venv
captured = capsys.readouterr()
injected = re.findall(r"injected package (.+?) into venv pycowsay", captured.out)
pkg_name = pkg_spec.split("=", 1)[0].replace(".", "-") # assuming spec is always of the form <name>==<version>
assert set(injected) == {pkg_name}
@skip_if_windows
def test_inject_simple_global(pipx_temp_env, capsys):
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)
def test_inject_simple_legacy_venv(pipx_temp_env, capsys, metadata_version):
assert not run_pipx_cli(["install", "pycowsay"])
mock_legacy_venv("pycowsay", metadata_version=metadata_version)
if metadata_version is not None:
assert not run_pipx_cli(["inject", "pycowsay", PKG["black"]["spec"]])
else:
# no metadata in venv should result in PipxError with message
assert run_pipx_cli(["inject", "pycowsay", PKG["black"]["spec"]])
assert "Please uninstall and install" in capsys.readouterr().err
@pytest.mark.parametrize("with_suffix,", [(False,), (True,)])
def test_inject_include_apps(pipx_temp_env, capsys, with_suffix):
install_args = []
suffix = ""
if with_suffix:
suffix = "_x"
install_args = [f"--suffix={suffix}"]
assert not run_pipx_cli(["install", "pycowsay", *install_args])
assert not run_pipx_cli(["inject", f"pycowsay{suffix}", PKG["black"]["spec"], "--include-deps"])
if suffix:
assert run_pipx_cli(["inject", "pycowsay", PKG["black"]["spec"], "--include-deps"])
assert not run_pipx_cli(["inject", f"pycowsay{suffix}", PKG["black"]["spec"], "--include-deps"])
@pytest.mark.parametrize(
"with_packages,",
[
(), # no extra packages
("black",), # duplicate from requirements file
("ipython",), # additional package
],
)
def test_inject_with_req_file(pipx_temp_env, capsys, caplog, tmp_path, with_packages):
caplog.set_level(logging.INFO)
req_file = tmp_path / "inject-requirements.txt"
req_file.write_text(
textwrap.dedent(
f"""
{PKG["black"]["spec"]} # a comment inline
{PKG["nox"]["spec"]}
{PKG["pylint"]["spec"]}
# comment on separate line
"""
).strip()
)
assert not run_pipx_cli(["install", "pycowsay"])
assert not run_pipx_cli(
["inject", "pycowsay", *(PKG[pkg]["spec"] for pkg in with_packages), "--requirement", str(req_file)]
)
packages = [
("black", PKG["black"]["spec"]),
("nox", PKG["nox"]["spec"]),
("pylint", PKG["pylint"]["spec"]),
]
packages.extend((pkg, PKG[pkg]["spec"]) for pkg in with_packages)
packages = sorted(set(packages))
# Check arguments and files have been parsed correctly
assert f"Injecting packages: {[p for _, p in packages]!r}" in caplog.text
# Check they're actually being installed and into correct venv
captured = capsys.readouterr()
injected = re.findall(r"injected package (.+?) into venv pycowsay", captured.out)
assert set(injected) == {pkg for pkg, _ in packages}
|