From eaf061a688646518a124965f797fa1a7f99431cb Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Wed, 15 Sep 2021 09:35:23 +0100 Subject: [PATCH] Ensure `pip.list_all_versions` works across multiple pip versions Refs #60085 --- salt/modules/pip.py | 16 +++++++++---- tests/pytests/functional/modules/test_pip.py | 25 ++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 tests/pytests/functional/modules/test_pip.py diff --git a/salt/modules/pip.py b/salt/modules/pip.py index 47a5f716c76e..7135a9145fb4 100644 --- a/salt/modules/pip.py +++ b/salt/modules/pip.py @@ -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): @@ -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 ) @@ -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) diff --git a/tests/pytests/functional/modules/test_pip.py b/tests/pytests/functional/modules/test_pip.py new file mode 100644 index 000000000000..8f75fadfd4a9 --- /dev/null +++ b/tests/pytests/functional/modules/test_pip.py @@ -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