From d561491c48ee30472e0d4699ba389648ef0d863a Mon Sep 17 00:00:00 2001 From: Victor Zhestkov Date: Mon, 27 Jun 2022 18:02:31 +0300 Subject: [PATCH] Set default target for pip from VENV_PIP_TARGET environment variable * Use VENV_PIP_TARGET as a target for pkg.install if set and no target specified on the call * Add test for VENV_PIP_TARGET environment variable * Changelog entry --- changelog/62089.changed | 1 + salt/modules/pip.py | 6 +++++ tests/pytests/unit/modules/test_pip.py | 31 ++++++++++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 changelog/62089.changed diff --git a/changelog/62089.changed b/changelog/62089.changed new file mode 100644 index 0000000000..09feb2e922 --- /dev/null +++ b/changelog/62089.changed @@ -0,0 +1 @@ +Use VENV_PIP_TARGET environment variable as a default target for pip if present. diff --git a/salt/modules/pip.py b/salt/modules/pip.py index da26416662..9410024fd5 100644 --- a/salt/modules/pip.py +++ b/salt/modules/pip.py @@ -858,6 +858,12 @@ def install( if build: cmd.extend(["--build", build]) + # Use VENV_PIP_TARGET environment variable value as target + # if set and no target specified on the function call + target_env = os.environ.get("VENV_PIP_TARGET", None) + if target is None and target_env is not None: + target = target_env + if target: cmd.extend(["--target", target]) diff --git a/tests/pytests/unit/modules/test_pip.py b/tests/pytests/unit/modules/test_pip.py index 405ec6c82e..ae9005d806 100644 --- a/tests/pytests/unit/modules/test_pip.py +++ b/tests/pytests/unit/modules/test_pip.py @@ -1773,3 +1773,34 @@ def test_when_version_is_called_with_a_user_it_should_be_passed_to_undelying_run cwd=None, python_shell=False, ) + + +def test_install_target_from_VENV_PIP_TARGET_in_resulting_command(): + pkg = "pep8" + target = "/tmp/foo" + target_env = "/tmp/bar" + mock = MagicMock(return_value={"retcode": 0, "stdout": ""}) + environment = os.environ.copy() + environment["VENV_PIP_TARGET"] = target_env + with patch.dict(pip.__salt__, {"cmd.run_all": mock}), patch.object( + os, "environ", environment + ): + pip.install(pkg) + expected = [sys.executable, "-m", "pip", "install", "--target", target_env, pkg] + mock.assert_called_with( + expected, + saltenv="base", + runas=None, + use_vt=False, + python_shell=False, + ) + mock.reset_mock() + pip.install(pkg, target=target) + expected = [sys.executable, "-m", "pip", "install", "--target", target, pkg] + mock.assert_called_with( + expected, + saltenv="base", + runas=None, + use_vt=False, + python_shell=False, + ) -- 2.37.3