From c37f67c5585c99ac6b86629c326460313605203c Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Tue, 15 Aug 2023 02:31:46 +0300 Subject: [PATCH] close descriptor after uploading archive on github --- src/ahriman/core/upload/github.py | 4 +++- tests/ahriman/core/upload/test_github.py | 12 +++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ahriman/core/upload/github.py b/src/ahriman/core/upload/github.py index c595ebbf..b511895a 100644 --- a/src/ahriman/core/upload/github.py +++ b/src/ahriman/core/upload/github.py @@ -79,7 +79,9 @@ class Github(HttpUpload): (url, _) = release["upload_url"].split("{") # it is parametrized url (mime, _) = mimetypes.guess_type(path) headers = {"Content-Type": mime} if mime is not None else {"Content-Type": "application/octet-stream"} - self._request("POST", url, params={"name": path.name}, data=path.open("rb"), headers=headers) + + with path.open("rb") as archive: + self._request("POST", url, params={"name": path.name}, data=archive, headers=headers) def get_local_files(self, path: Path) -> dict[Path, str]: """ diff --git a/tests/ahriman/core/upload/test_github.py b/tests/ahriman/core/upload/test_github.py index b810b9f9..dbf5d5fb 100644 --- a/tests/ahriman/core/upload/test_github.py +++ b/tests/ahriman/core/upload/test_github.py @@ -31,13 +31,14 @@ def test_asset_upload(github: Github, github_release: dict[str, Any], mocker: Mo """ must upload asset to the repository """ - mocker.patch("pathlib.Path.open", return_value=b"") + mocker.patch("pathlib.Path.open") request_mock = mocker.patch("ahriman.core.upload.github.Github._request") remove_mock = mocker.patch("ahriman.core.upload.github.Github.asset_remove") github.asset_upload(github_release, Path("/root/new.tar.xz")) request_mock.assert_called_once_with("POST", "upload_url", params={"name": "new.tar.xz"}, - data=b"", headers={"Content-Type": "application/x-tar"}) + data=pytest.helpers.anyvar(int), + headers={"Content-Type": "application/x-tar"}) remove_mock.assert_not_called() @@ -45,7 +46,7 @@ def test_asset_upload_with_removal(github: Github, github_release: dict[str, Any """ must remove existing file before upload """ - mocker.patch("pathlib.Path.open", return_value=b"") + mocker.patch("pathlib.Path.open") mocker.patch("ahriman.core.upload.github.Github._request") remove_mock = mocker.patch("ahriman.core.upload.github.Github.asset_remove") @@ -61,14 +62,15 @@ def test_asset_upload_empty_mimetype(github: Github, github_release: dict[str, A """ must upload asset to the repository with empty mime type if the library cannot guess it """ - mocker.patch("pathlib.Path.open", return_value=b"") + mocker.patch("pathlib.Path.open") mocker.patch("ahriman.core.upload.github.Github.asset_remove") mocker.patch("mimetypes.guess_type", return_value=(None, None)) request_mock = mocker.patch("ahriman.core.upload.github.Github._request") github.asset_upload(github_release, Path("/root/new.tar.xz")) request_mock.assert_called_once_with("POST", "upload_url", params={"name": "new.tar.xz"}, - data=b"", headers={"Content-Type": "application/octet-stream"}) + data=pytest.helpers.anyvar(int), + headers={"Content-Type": "application/octet-stream"}) def test_get_local_files(github: Github, resource_path_root: Path, mocker: MockerFixture) -> None: