wrap NamedTemporaryFile for Windows testing support
This commit is contained in:
parent
2ade22b7be
commit
9da0163c1a
4 changed files with 35 additions and 17 deletions
|
@ -8,7 +8,6 @@ from wakatime.packages.requests.models import Response
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import tempfile
|
|
||||||
import time
|
import time
|
||||||
import shutil
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
|
@ -67,7 +66,7 @@ class LoggingTestCase(utils.TestCase):
|
||||||
response.status_code = 0
|
response.status_code = 0
|
||||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
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()))
|
now = u(int(time.time()))
|
||||||
entity = 'tests/samples/codefiles/python.py'
|
entity = 'tests/samples/codefiles/python.py'
|
||||||
config = 'tests/samples/configs/good_config.cfg'
|
config = 'tests/samples/configs/good_config.cfg'
|
||||||
|
|
|
@ -9,7 +9,6 @@ import logging
|
||||||
import os
|
import os
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
|
||||||
import time
|
import time
|
||||||
from testfixtures import log_capture
|
from testfixtures import log_capture
|
||||||
from wakatime.compat import u
|
from wakatime.compat import u
|
||||||
|
@ -39,7 +38,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
]
|
]
|
||||||
|
|
||||||
def test_heartbeat_saved_from_error_response(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
@ -59,7 +58,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
||||||
|
|
||||||
def test_heartbeat_discarded_from_400_response(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
@ -79,7 +78,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
self.assertEquals(None, saved_heartbeat)
|
self.assertEquals(None, saved_heartbeat)
|
||||||
|
|
||||||
def test_offline_heartbeat_sent_after_success_response(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
@ -102,7 +101,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
self.assertEquals(None, saved_heartbeat)
|
self.assertEquals(None, saved_heartbeat)
|
||||||
|
|
||||||
def test_all_offline_heartbeats_sent_after_success_response(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
@ -172,7 +171,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
self.assertEquals(u(int(data.get('time'))), now2)
|
self.assertEquals(u(int(data.get('time'))), now2)
|
||||||
|
|
||||||
def test_auth_error_when_sending_offline_heartbeats(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
@ -227,7 +226,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
self.assertEquals(os.path.realpath(entity2), saved_heartbeat['entity'])
|
self.assertEquals(os.path.realpath(entity2), saved_heartbeat['entity'])
|
||||||
|
|
||||||
def test_500_error_when_sending_offline_heartbeats(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
@ -282,7 +281,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
self.assertEquals(os.path.realpath(entity2), saved_heartbeat['entity'])
|
self.assertEquals(os.path.realpath(entity2), saved_heartbeat['entity'])
|
||||||
|
|
||||||
def test_empty_project_can_be_saved(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
@ -304,7 +303,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
||||||
|
|
||||||
def test_get_handles_connection_exception(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
@ -334,7 +333,7 @@ class OfflineQueueTestCase(utils.TestCase):
|
||||||
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
self.assertEquals(os.path.realpath(entity), saved_heartbeat['entity'])
|
||||||
|
|
||||||
def test_push_handles_connection_exception(self):
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
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):
|
def test_heartbeat_saved_when_requests_raises_exception(self, logs):
|
||||||
logging.disable(logging.NOTSET)
|
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:
|
with utils.mock.patch('wakatime.offlinequeue.Queue.get_db_file') as mock_db_file:
|
||||||
mock_db_file.return_value = fh.name
|
mock_db_file.return_value = fh.name
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
|
||||||
import tempfile
|
|
||||||
from wakatime.session_cache import SessionCache
|
from wakatime.session_cache import SessionCache
|
||||||
from wakatime.logger import setup_logging
|
from wakatime.logger import setup_logging
|
||||||
from . import utils
|
from . import utils
|
||||||
|
@ -29,7 +28,7 @@ class SessionCacheTestCase(utils.TestCase):
|
||||||
setup_logging(self.args, self.args.version)
|
setup_logging(self.args, self.args.version)
|
||||||
|
|
||||||
def test_can_crud_session(self):
|
def test_can_crud_session(self):
|
||||||
with tempfile.NamedTemporaryFile() as fh:
|
with utils.NamedTemporaryFile() as fh:
|
||||||
cache = SessionCache()
|
cache = SessionCache()
|
||||||
cache.DB_FILE = fh.name
|
cache.DB_FILE = fh.name
|
||||||
|
|
||||||
|
@ -43,7 +42,7 @@ class SessionCacheTestCase(utils.TestCase):
|
||||||
self.assertEquals(session.headers.get('x-test'), None)
|
self.assertEquals(session.headers.get('x-test'), None)
|
||||||
|
|
||||||
def test_handles_connection_exception(self):
|
def test_handles_connection_exception(self):
|
||||||
with tempfile.NamedTemporaryFile() as fh:
|
with utils.NamedTemporaryFile() as fh:
|
||||||
cache = SessionCache()
|
cache = SessionCache()
|
||||||
cache.DB_FILE = fh.name
|
cache.DB_FILE = fh.name
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import tempfile
|
||||||
|
|
||||||
from wakatime.compat import u
|
from wakatime.compat import u
|
||||||
|
|
||||||
|
@ -17,6 +19,7 @@ except ImportError:
|
||||||
# Python >= 2.7
|
# Python >= 2.7
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
|
||||||
class TestCase(unittest.TestCase):
|
class TestCase(unittest.TestCase):
|
||||||
patch_these = []
|
patch_these = []
|
||||||
|
|
||||||
|
@ -62,7 +65,6 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
# Python < 3
|
# Python < 3
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
|
||||||
class TemporaryDirectory(object):
|
class TemporaryDirectory(object):
|
||||||
"""Context manager for tempfile.mkdtemp().
|
"""Context manager for tempfile.mkdtemp().
|
||||||
|
|
||||||
|
@ -77,6 +79,25 @@ except ImportError:
|
||||||
shutil.rmtree(self.name)
|
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):
|
class DynamicIterable(object):
|
||||||
def __init__(self, data, max_calls=None, raise_on_calls=None):
|
def __init__(self, data, max_calls=None, raise_on_calls=None):
|
||||||
self.called = 0
|
self.called = 0
|
||||||
|
|
Loading…
Reference in a new issue