From e90836e8179dd5ef5043d2a72cc87ccb90827b5b Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Mon, 25 Sep 2017 13:34:05 -0700 Subject: [PATCH] Fix up some issues with one of the tests --- openlp/core/common/httputils.py | 14 ++++++++------ .../openlp_core_common/test_httputils.py | 8 +++----- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/openlp/core/common/httputils.py b/openlp/core/common/httputils.py index 9a73659bd..a92cd92c9 100644 --- a/openlp/core/common/httputils.py +++ b/openlp/core/common/httputils.py @@ -142,7 +142,7 @@ def url_get_file(callback, url, file_path, sha256=None): :param callback: the class which needs to be updated :param url: URL to download - :param f_path: Destination file + :param file_path: Destination file :param sha256: The check sum value to be checked against the download value """ block_count = 0 @@ -151,7 +151,7 @@ def url_get_file(callback, url, file_path, sha256=None): log.debug('url_get_file: %s', url) while retries < CONNECTION_RETRIES: try: - with open(file_path, 'wb') as saved_file: + with file_path.open('wb') as saved_file: response = requests.get(url, timeout=float(CONNECTION_TIMEOUT), stream=True) if sha256: hasher = hashlib.sha256() @@ -168,20 +168,22 @@ def url_get_file(callback, url, file_path, sha256=None): if sha256 and hasher.hexdigest() != sha256: log.error('sha256 sums did not match for file %s, got %s, expected %s', file_path, hasher.hexdigest(), sha256) - os.remove(file_path) + if file_path.exists(): + file_path.unlink() return False break except IOError: trace_error_handler(log) - os.remove(file_path) if retries > CONNECTION_RETRIES: + if file_path.exists(): + file_path.unlink() return False else: retries += 1 time.sleep(0.1) continue - if callback.was_cancelled: - os.remove(file_path) + if callback.was_cancelled and file_path.exists(): + file_path.unlink() return True diff --git a/tests/functional/openlp_core_common/test_httputils.py b/tests/functional/openlp_core_common/test_httputils.py index 26edf4056..e620fa04e 100644 --- a/tests/functional/openlp_core_common/test_httputils.py +++ b/tests/functional/openlp_core_common/test_httputils.py @@ -228,8 +228,7 @@ class TestHttpUtils(TestCase, TestMixin): assert file_size == 100 @patch('openlp.core.common.httputils.requests') - @patch('openlp.core.common.httputils.os.remove') - def test_socket_timeout(self, mocked_remove, mocked_requests): + def test_socket_timeout(self, mocked_requests): """ Test socket timeout gets caught """ @@ -237,9 +236,8 @@ class TestHttpUtils(TestCase, TestMixin): mocked_requests.get.side_effect = IOError # WHEN: Attempt to retrieve a file - url_get_file(MagicMock(), url='http://localhost/test', file_path=self.tempfile) + url_get_file(MagicMock(), url='http://localhost/test', file_path=Path(self.tempfile)) # THEN: socket.timeout should have been caught # NOTE: Test is if $tmpdir/tempfile is still there, then test fails since ftw deletes bad downloaded files - mocked_remove.assert_called_with(self.tempfile) - assert mocked_remove.call_count == 3, 'os.remove() should have been called 3 times' + assert not os.path.exists(self.tempfile), 'tempfile should have been deleted'