Skip to content

Commit

Permalink
Ensure pip.list_all_versions works across multiple pip versions
Browse files Browse the repository at this point in the history
Refs #60085
  • Loading branch information
s0undt3ch authored and garethgreenaway committed Sep 21, 2021
1 parent 1d87bda commit eaf061a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
16 changes: 12 additions & 4 deletions salt/modules/pip.py
Original file line number Diff line number Diff line change
Expand Up @@ -1597,7 +1597,6 @@ def list_all_versions(
"""
cwd = _pip_bin_env(cwd, bin_env)
cmd = _get_pip_bin(bin_env)
cmd.extend(["install", "{}==versions".format(pkg)])

if index_url:
if not salt.utils.url.validate(index_url, VALID_PROTOS):
Expand All @@ -1611,6 +1610,17 @@ def list_all_versions(
)
cmd.extend(["--extra-index-url", extra_index_url])

# Is the `pip index` command available
pip_version = version(bin_env=bin_env, cwd=cwd, user=user)
if salt.utils.versions.compare(ver1=pip_version, oper=">=", ver2="21.2"):
regex = re.compile(r"\s*Available versions: (.*)")
cmd.extend(["index", "versions", pkg])
else:
if salt.utils.versions.compare(ver1=pip_version, oper=">=", ver2="20.3"):
cmd.append("--use-deprecated=legacy-resolver")
regex = re.compile(r"\s*Could not find a version.* \(from versions: (.*)\)")
cmd.extend(["install", "{}==versions".format(pkg)])

cmd_kwargs = dict(
cwd=cwd, runas=user, output_loglevel="quiet", redirect_stderr=True
)
Expand All @@ -1633,9 +1643,7 @@ def list_all_versions(

versions = []
for line in result["stdout"].splitlines():
match = re.search(
r"\s*Could not find a version.* \(from versions: (.*)\)", line
)
match = regex.search(line)
if match:
versions = [
v for v in match.group(1).split(", ") if v and excludes.match(v)
Expand Down
25 changes: 25 additions & 0 deletions tests/pytests/functional/modules/test_pip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import sys

import pytest
from tests.support.helpers import VirtualEnv


@pytest.mark.parametrize(
"pip_version",
(
"pip==9.0.3",
"pip<20.0",
"pip<21.0",
"pip>=21.0",
),
)
def test_list_available_packages(modules, pip_version, tmp_path):
if sys.version_info < (3, 6) and pip_version == "pip>=21.0":
pytest.skip("{} is not avaiable on Py3.5".format(pip_version))
with VirtualEnv(venv_dir=tmp_path, pip_requirement=pip_version) as virtualenv:
virtualenv.install("-U", pip_version)
package_name = "pep8"
available_versions = modules.pip.list_all_versions(
package_name, bin_env=str(virtualenv.venv_bin_dir)
)
assert available_versions

0 comments on commit eaf061a

Please sign in to comment.