Skip to content

Commit ff3935a

Browse files
authored
Merge branch 'master' into cs/elasticsearch-module-deprecation
2 parents b5a47d9 + 1e0c9d7 commit ff3935a

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

changelog/65562.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve the condition of overriding target for pip with VENV_PIP_TARGET environment variable.

salt/modules/pip.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,6 @@ def _get_env_activate(bin_env):
253253

254254

255255
def _find_req(link):
256-
257256
logger.info("_find_req -- link = %s", link)
258257

259258
with salt.utils.files.fopen(link) as fh_link:
@@ -849,9 +848,11 @@ def install(
849848
cmd.extend(["--build", build])
850849

851850
# Use VENV_PIP_TARGET environment variable value as target
852-
# if set and no target specified on the function call
851+
# if set and no target specified on the function call.
852+
# Do not set target if bin_env specified, use default
853+
# for specified binary environment or expect explicit target specification.
853854
target_env = os.environ.get("VENV_PIP_TARGET", None)
854-
if target is None and target_env is not None:
855+
if target is None and target_env is not None and bin_env is None:
855856
target = target_env
856857

857858
if target:

salt/states/schedule.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ def present(name, **kwargs):
216216
217217
offline
218218
Add the scheduled job to the Salt minion when the Salt minion is not running.
219+
220+
.. versionadded:: 3006.3
219221
"""
220222

221223
ret = {"name": name, "result": True, "changes": {}, "comment": []}

tests/pytests/unit/modules/test_pip.py

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,7 +1101,6 @@ def test_install_extra_args_arguments_recursion_error():
11011101
pkg = "pep8"
11021102
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
11031103
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
1104-
11051104
pytest.raises(
11061105
TypeError,
11071106
lambda: pip.install(
@@ -1522,7 +1521,7 @@ def test_list_upgrades_gt9(python_binary):
15221521
{"latest_filetype": "wheel", "version": "1.4.1", "name": "appdirs", "latest_version": "1.4.3"},
15231522
{"latest_filetype": "sdist", "version": "1.11.63", "name": "awscli", "latest_version": "1.12.1"}
15241523
]"""
1525-
mock = MagicMock(return_value={"retcode": 0, "stdout": "{}".format(eggs)})
1524+
mock = MagicMock(return_value={"retcode": 0, "stdout": f"{eggs}"})
15261525
with patch.dict(pip.__salt__, {"cmd.run_all": mock}):
15271526
with patch("salt.modules.pip.version", MagicMock(return_value="9.1.1")):
15281527
ret = pip.list_upgrades()
@@ -1738,28 +1737,44 @@ def test_when_version_is_called_with_a_user_it_should_be_passed_to_undelying_run
17381737
)
17391738

17401739

1741-
def test_install_target_from_VENV_PIP_TARGET_in_resulting_command(python_binary):
1740+
@pytest.mark.parametrize(
1741+
"bin_env,target,target_env,expected_target",
1742+
[
1743+
(None, None, None, None),
1744+
(None, "/tmp/foo", None, "/tmp/foo"),
1745+
(None, None, "/tmp/bar", "/tmp/bar"),
1746+
(None, "/tmp/foo", "/tmp/bar", "/tmp/foo"),
1747+
("/tmp/venv", "/tmp/foo", None, "/tmp/foo"),
1748+
("/tmp/venv", None, "/tmp/bar", None),
1749+
("/tmp/venv", "/tmp/foo", "/tmp/bar", "/tmp/foo"),
1750+
],
1751+
)
1752+
def test_install_target_from_VENV_PIP_TARGET_in_resulting_command(
1753+
python_binary, bin_env, target, target_env, expected_target
1754+
):
17421755
pkg = "pep8"
1743-
target = "/tmp/foo"
1744-
target_env = "/tmp/bar"
17451756
mock = MagicMock(return_value={"retcode": 0, "stdout": ""})
17461757
environment = os.environ.copy()
1747-
environment["VENV_PIP_TARGET"] = target_env
1758+
real_get_pip_bin = pip._get_pip_bin
1759+
1760+
def mock_get_pip_bin(bin_env):
1761+
if not bin_env:
1762+
return real_get_pip_bin(bin_env)
1763+
return [f"{bin_env}/bin/pip"]
1764+
1765+
if target_env is not None:
1766+
environment["VENV_PIP_TARGET"] = target_env
17481767
with patch.dict(pip.__salt__, {"cmd.run_all": mock}), patch.object(
17491768
os, "environ", environment
1750-
):
1751-
pip.install(pkg)
1752-
expected = [*python_binary, "install", "--target", target_env, pkg]
1753-
mock.assert_called_with(
1754-
expected,
1755-
saltenv="base",
1756-
runas=None,
1757-
use_vt=False,
1758-
python_shell=False,
1759-
)
1760-
mock.reset_mock()
1761-
pip.install(pkg, target=target)
1762-
expected = [*python_binary, "install", "--target", target, pkg]
1769+
), patch.object(pip, "_get_pip_bin", mock_get_pip_bin):
1770+
pip.install(pkg, bin_env=bin_env, target=target)
1771+
expected_binary = python_binary
1772+
if bin_env is not None:
1773+
expected_binary = [f"{bin_env}/bin/pip"]
1774+
if expected_target is not None:
1775+
expected = [*expected_binary, "install", "--target", expected_target, pkg]
1776+
else:
1777+
expected = [*expected_binary, "install", pkg]
17631778
mock.assert_called_with(
17641779
expected,
17651780
saltenv="base",

0 commit comments

Comments
 (0)