From 64aac7b884a8c452436722c6b4163e9a9f92b87d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Eustace?= Date: Fri, 24 Jul 2020 10:39:43 +0200 Subject: [PATCH] Fix errors on Python 2.7 when a file could not be downloaded --- poetry/installation/executor.py | 6 ++++- tests/installation/test_executor.py | 34 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/poetry/installation/executor.py b/poetry/installation/executor.py index 6e48278b029..13afbc0522b 100644 --- a/poetry/installation/executor.py +++ b/poetry/installation/executor.py @@ -583,7 +583,11 @@ def _download_link(self, operation, link): archive = self._download_archive(operation, link) except BaseException: cache_directory = self._chef.get_cache_directory_for_link(link) - cache_directory.joinpath(link.filename).unlink(missing_ok=True) + cached_file = cache_directory.joinpath(link.filename) + # We can't use unlink(missing_ok=True) because it's not available + # in pathlib2 for Python 2.7 + if cached_file.exists(): + cached_file.unlink() raise diff --git a/tests/installation/test_executor.py b/tests/installation/test_executor.py index 7f9b20a8aa9..a9dea0d136a 100644 --- a/tests/installation/test_executor.py +++ b/tests/installation/test_executor.py @@ -2,6 +2,7 @@ from __future__ import unicode_literals import re +import shutil import pytest @@ -179,3 +180,36 @@ def test_execute_should_show_operation_as_cancelled_on_subprocess_keyboard_inter """ assert expected == io.fetch_output() + + +def test_executor_should_delete_incomplete_downloads( + config, io, tmp_dir, mocker, pool, mock_file_downloads +): + fixture = Path(__file__).parent.parent.joinpath( + "fixtures/distributions/demo-0.1.0-py2.py3-none-any.whl" + ) + destination_fixture = Path(tmp_dir) / "tomlkit-0.5.3-py2.py3-none-any.whl" + shutil.copyfile(str(fixture), str(destination_fixture)) + mocker.patch( + "poetry.installation.executor.Executor._download_archive", + side_effect=Exception("Download error"), + ) + mocker.patch( + "poetry.installation.chef.Chef.get_cached_archive_for_link", + side_effect=lambda link: link, + ) + mocker.patch( + "poetry.installation.chef.Chef.get_cache_directory_for_link", + return_value=Path(tmp_dir), + ) + + config = Config() + config.merge({"cache-dir": tmp_dir}) + + env = MockEnv(path=Path(tmp_dir)) + executor = Executor(env, pool, config, io) + + with pytest.raises(Exception, match="Download error"): + executor._download(Install(Package("tomlkit", "0.5.3"))) + + assert not destination_fixture.exists()