Skip to content

Commit

Permalink
Fix errors on Python 2.7 when a file could not be downloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater authored and abn committed Jul 24, 2020
1 parent 02d4d0c commit 6880fd6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion poetry/installation/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 34 additions & 0 deletions tests/installation/test_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import unicode_literals

import re
import shutil

import pytest

Expand Down Expand Up @@ -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()

0 comments on commit 6880fd6

Please sign in to comment.