From 9da0163c1a046d6c2046a320333ecbcf4eff44b0 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Sun, 21 May 2017 15:58:48 -0700 Subject: [PATCH] wrap NamedTemporaryFile for Windows testing support --- tests/test_logging.py | 3 +-- tests/test_offlinequeue.py | 21 ++++++++++----------- tests/test_session_cache.py | 5 ++--- tests/utils.py | 23 ++++++++++++++++++++++- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/tests/test_logging.py b/tests/test_logging.py index 9630bc8..37131d2 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -8,7 +8,6 @@ from wakatime.packages.requests.models import Response import logging import os -import tempfile import time import shutil import sys @@ -67,7 +66,7 @@ class LoggingTestCase(utils.TestCase): response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: now = u(int(time.time())) entity = 'tests/samples/codefiles/python.py' config = 'tests/samples/configs/good_config.cfg' diff --git a/tests/test_offlinequeue.py b/tests/test_offlinequeue.py index 6b9e157..1cca921 100644 --- a/tests/test_offlinequeue.py +++ b/tests/test_offlinequeue.py @@ -9,7 +9,6 @@ import logging import os import sqlite3 import sys -import tempfile import time from testfixtures import log_capture from wakatime.compat import u @@ -39,7 +38,7 @@ class OfflineQueueTestCase(utils.TestCase): ] def test_heartbeat_saved_from_error_response(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -59,7 +58,7 @@ class OfflineQueueTestCase(utils.TestCase): self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity']) def test_heartbeat_discarded_from_400_response(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -79,7 +78,7 @@ class OfflineQueueTestCase(utils.TestCase): self.assertEquals(None, saved_heartbeat) def test_offline_heartbeat_sent_after_success_response(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -102,7 +101,7 @@ class OfflineQueueTestCase(utils.TestCase): self.assertEquals(None, saved_heartbeat) def test_all_offline_heartbeats_sent_after_success_response(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -172,7 +171,7 @@ class OfflineQueueTestCase(utils.TestCase): self.assertEquals(u(int(data.get('time'))), now2) def test_auth_error_when_sending_offline_heartbeats(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -227,7 +226,7 @@ class OfflineQueueTestCase(utils.TestCase): self.assertEquals(os.path.realpath(entity2), saved_heartbeat['entity']) def test_500_error_when_sending_offline_heartbeats(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -282,7 +281,7 @@ class OfflineQueueTestCase(utils.TestCase): self.assertEquals(os.path.realpath(entity2), saved_heartbeat['entity']) def test_empty_project_can_be_saved(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -304,7 +303,7 @@ class OfflineQueueTestCase(utils.TestCase): self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity']) def test_get_handles_connection_exception(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -334,7 +333,7 @@ class OfflineQueueTestCase(utils.TestCase): self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity']) def test_push_handles_connection_exception(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name @@ -369,7 +368,7 @@ class OfflineQueueTestCase(utils.TestCase): def test_heartbeat_saved_when_requests_raises_exception(self, logs): logging.disable(logging.NOTSET) - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file: mock_db_file.return_value = fh.name diff --git a/tests/test_session_cache.py b/tests/test_session_cache.py index 1484a90..59e79e0 100644 --- a/tests/test_session_cache.py +++ b/tests/test_session_cache.py @@ -1,7 +1,6 @@ # -*- coding: utf-8 -*- -import tempfile from wakatime.session_cache import SessionCache from wakatime.logger import setup_logging from . import utils @@ -29,7 +28,7 @@ class SessionCacheTestCase(utils.TestCase): setup_logging(self.args, self.args.version) def test_can_crud_session(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: cache = SessionCache() cache.DB_FILE = fh.name @@ -43,7 +42,7 @@ class SessionCacheTestCase(utils.TestCase): self.assertEquals(session.headers.get('x-test'), None) def test_handles_connection_exception(self): - with tempfile.NamedTemporaryFile() as fh: + with utils.NamedTemporaryFile() as fh: cache = SessionCache() cache.DB_FILE = fh.name diff --git a/tests/utils.py b/tests/utils.py index 8dc92f9..67f9d67 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,7 +1,9 @@ # -*- coding: utf-8 -*- import logging +import os import sys +import tempfile from wakatime.compat import u @@ -17,6 +19,7 @@ except ImportError: # Python >= 2.7 import unittest + class TestCase(unittest.TestCase): patch_these = [] @@ -62,7 +65,6 @@ try: except ImportError: # Python < 3 import shutil - import tempfile class TemporaryDirectory(object): """Context manager for tempfile.mkdtemp(). @@ -77,6 +79,25 @@ except ImportError: shutil.rmtree(self.name) +class NamedTemporaryFile(object): + """Context manager for a named temporary file compatible with Windows. + + Provides the path to a closed temporary file that is writeable. Deletes the + temporary file when exiting the context manager. The built-in + tempfile.NamedTemporaryFile is not writeable on Windows. + """ + name = None + + def __enter__(self): + fh = tempfile.NamedTemporaryFile(delete=False) + self.name = fh.name + fh.close() + return self + + def __exit__(self, type, value, traceback): + os.unlink(self.name) + + class DynamicIterable(object): def __init__(self, data, max_calls=None, raise_on_calls=None): self.called = 0