From cd8457fb0f16b2f9b8441da038e17d00bbcb33bd Mon Sep 17 00:00:00 2001 From: 1nF0rmed Date: Fri, 20 Aug 2021 23:13:19 +0530 Subject: [PATCH] Adds File existence check to Uploader This handles the TODO for checking the existence of the files being uploaded by the Uploader class. In the case that a file does not exist, it raises an UploadError. Co-authored-by: Bjorn Neergaard --- src/poetry/publishing/uploader.py | 5 +++-- tests/publishing/test_uploader.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/poetry/publishing/uploader.py b/src/poetry/publishing/uploader.py index 60eee2deae4..65163e1bd9d 100644 --- a/src/poetry/publishing/uploader.py +++ b/src/poetry/publishing/uploader.py @@ -212,8 +212,6 @@ def _upload( skip_existing: bool = False, ) -> None: for file in self.files: - # TODO: Check existence - self._upload_file(session, url, file, dry_run, skip_existing) def _upload_file( @@ -226,6 +224,9 @@ def _upload_file( ) -> None: from cleo.ui.progress_bar import ProgressBar + if not file.is_file(): + raise UploadError(f"Archive ({file}) does not exist") + data = self.post_data(file) data.update( { diff --git a/tests/publishing/test_uploader.py b/tests/publishing/test_uploader.py index 9a7346825f3..be6256052a8 100644 --- a/tests/publishing/test_uploader.py +++ b/tests/publishing/test_uploader.py @@ -119,3 +119,14 @@ def test_uploader_skip_existing_bubbles_unskippable_errors( with pytest.raises(UploadError): uploader.upload("https://foo.com", skip_existing=True) + + +def test_uploader_properly_handles_file_not_existing( + mocker: MockerFixture, http: type[httpretty.httpretty], uploader: Uploader +): + mocker.patch("pathlib.Path.is_file", return_value=False) + + with pytest.raises(UploadError) as e: + uploader.upload("https://foo.com") + + assert f"Archive ({uploader.files[0]}) does not exist" == str(e.value)