Fix pip.install when log parameter contains a file that doesn't exist yet

This commit is contained in:
MKLeb 2023-09-19 17:56:54 -04:00 committed by Pedro Algarvio
parent 00d7e2c35d
commit 0bc2acc03e
2 changed files with 41 additions and 2 deletions

View file

@ -464,7 +464,7 @@ def install(
cache_dir=None,
no_binary=None,
disable_version_check=False,
**kwargs
**kwargs,
):
"""
Install packages with pip
@ -758,6 +758,16 @@ def install(
if log:
if os.path.isdir(log):
raise OSError("'{}' is a directory. Use --log path_to_file".format(log))
if not os.path.exists(log):
parent = os.path.dirname(log)
if not os.path.exists(parent):
raise OSError(
f"Trying to create '{log}' but parent directory '{parent}' does not exist."
)
elif not os.access(parent, os.W_OK):
raise OSError(
f"Trying to create '{log}' but parent directory '{parent}' is not writeable."
)
elif not os.access(log, os.W_OK):
raise OSError("'{}' is not writeable".format(log))
@ -1336,7 +1346,7 @@ def list_(prefix=None, bin_env=None, user=None, cwd=None, env_vars=None, **kwarg
user=user,
cwd=cwd,
env_vars=env_vars,
**kwargs
**kwargs,
)
cmd = _get_pip_bin(bin_env)

View file

@ -591,6 +591,35 @@ def test_pip_install_multiple_editables_and_pkgs(venv_dir, salt_cli, salt_minion
)
@pytest.mark.parametrize("touch", [True, False])
@pytest.mark.slow_test
def test_pip_non_existent_log_file(venv_dir, salt_cli, salt_minion, tmp_path, touch):
log_file = tmp_path / "tmp-pip-install.log"
if touch:
log_file.touch()
# Create the testing virtualenv
with VirtualEnv(venv_dir):
ret = salt_cli.run(
"pip.install",
["pep8"],
log=str(log_file),
bin_env=venv_dir,
minion_tgt=salt_minion.id,
)
if not isinstance(ret.data, dict):
pytest.fail(
"The 'pip.install' command did not return the expected dictionary."
" Output:\n{}".format(ret)
)
if _check_download_error(ret.stdout):
pytest.skipTest("Test skipped due to pip download error")
assert ret.returncode == 0
assert log_file.exists()
assert "pep8" in log_file.read_text()
@pytest.mark.skipif(
shutil.which("/bin/pip3") is None, reason="Could not find /bin/pip3"
)