diff --git a/telegram_upload/client/telegram_upload_client.py b/telegram_upload/client/telegram_upload_client.py index ec6d6bc4..bd0adab9 100644 --- a/telegram_upload/client/telegram_upload_client.py +++ b/telegram_upload/client/telegram_upload_client.py @@ -134,6 +134,7 @@ def send_files(self, entity, files: Iterable[File], delete_on_success=False, pri pack_bot_file_id(message.media))) if message and delete_on_success: click.echo('Deleting "{}"'.format(file)) + file.close() os.remove(file.path) if message: self.forward_to(message, forward) diff --git a/tests/test_client/test_telegram_upload_client.py b/tests/test_client/test_telegram_upload_client.py index a11ebfea..b37133d9 100644 --- a/tests/test_client/test_telegram_upload_client.py +++ b/tests/test_client/test_telegram_upload_client.py @@ -134,13 +134,20 @@ def test_send_files(self): mock_os.remove.assert_called_once_with("thumb.jpg") with self.subTest("Test send files with delete mode"), patch('os.remove') as mock_remove: file = File(MagicMock(max_caption_length=200), self.upload_file_path) - self.client.send_files(entity, [file], delete_on_success=True) + calls = Mock() + with patch.object(file, 'close') as mock_close: + calls.attach_mock(mock_close, 'close') + calls.attach_mock(mock_remove, 'remove') + self.client.send_files(entity, [file], delete_on_success=True) self.client.send_file.assert_called_with( entity, file, thumb=None, file_size=file.file_size, caption=os.path.basename(self.upload_file_path).split('.')[0], force_document=False, progress_callback=AnyArg(), attributes=[], ) - mock_remove.assert_called_once_with(self.upload_file_path) + calls.assert_has_calls([ + call.close(), + call.remove(self.upload_file_path), + ]) def test_send_files_data_loss(self): mock_client = MagicMock(max_caption_length=200)