From 429c61967aca1385f6d5fbdb6cef984ce5aa0ead Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Wed, 8 Jun 2016 20:33:36 +0200 Subject: [PATCH] use temp directory for all code files in tests --- tests/test_dependencies.py | 911 +++++++++++++++++++------------------ tests/test_main.py | 707 +++++++++++++++------------- tests/utils.py | 21 + 3 files changed, 890 insertions(+), 749 deletions(-) diff --git a/tests/test_dependencies.py b/tests/test_dependencies.py index 07e0689..e3275f7 100644 --- a/tests/test_dependencies.py +++ b/tests/test_dependencies.py @@ -6,6 +6,7 @@ from wakatime.packages import requests import os import time +import shutil import sys from wakatime.compat import u from wakatime.exceptions import NotYetImplemented @@ -52,551 +53,595 @@ class DependenciesTestCase(utils.TestCase): response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/python.py' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/python.py' + shutil.copy(entity, os.path.join(tempdir, 'python.py')) + entity = os.path.realpath(os.path.join(tempdir, 'python.py')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - with utils.mock.patch('wakatime.dependencies.open') as mock_open: - mock_open.side_effect = IOError('') - retval = execute(args) + args = ['--file', entity, '--config', config, '--time', now] - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + with utils.mock.patch('wakatime.dependencies.open') as mock_open: + mock_open.side_effect = IOError('') + retval = execute(args) - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('Python'), - 'lines': 36, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): [], - u('language'): u('Python'), - u('lineno'): None, - u('lines'): 36, - } - expected_dependencies = [] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('dependencies', []) - self.assertListsEqual(dependencies, expected_dependencies) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('Python'), + 'lines': 36, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): [], + u('language'): u('Python'), + u('lineno'): None, + u('lines'): 36, + } + expected_dependencies = [] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('dependencies', []) + self.assertListsEqual(dependencies, expected_dependencies) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_python_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/python.py' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/python.py' + shutil.copy(entity, os.path.join(tempdir, 'python.py')) + entity = os.path.realpath(os.path.join(tempdir, 'python.py')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('Python'), - 'lines': 36, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'dependencies': ANY, - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('Python'), - u('lineno'): None, - u('lines'): 36, - } - expected_dependencies = [ - 'app', - 'django', - 'flask', - 'jinja', - 'mock', - 'pygments', - 'simplejson', - 'sqlalchemy', - 'sys', - 'unittest', - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('Python'), + 'lines': 36, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'dependencies': ANY, + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('Python'), + u('lineno'): None, + u('lines'): 36, + } + expected_dependencies = [ + 'app', + 'django', + 'flask', + 'jinja', + 'mock', + 'pygments', + 'simplejson', + 'sqlalchemy', + 'sys', + 'unittest', + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_bower_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/bower.json' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/bower.json' + shutil.copy(entity, os.path.join(tempdir, 'bower.json')) + entity = os.path.realpath(os.path.join(tempdir, 'bower.json')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') - self.assertEquals(retval, 102) + args = ['--file', entity, '--config', config, '--time', now] - heartbeat = { - 'language': u('JSON'), - 'lines': 11, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'dependencies': ANY, - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('JSON'), - u('lineno'): None, - u('lines'): 11, - } - expected_dependencies = ['animate.css', 'moment', 'moment-timezone'] + retval = execute(args) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') + self.assertEquals(retval, 102) - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - for dep in expected_dependencies: - self.assertIn(dep, self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies']) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('JSON'), + 'lines': 11, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'dependencies': ANY, + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('JSON'), + u('lineno'): None, + u('lines'): 11, + } + expected_dependencies = ['animate.css', 'moment', 'moment-timezone'] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + for dep in expected_dependencies: + self.assertIn(dep, self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies']) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_java_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/java.java' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/java.java' + shutil.copy(entity, os.path.join(tempdir, 'java.java')) + entity = os.path.realpath(os.path.join(tempdir, 'java.java')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('Java'), - 'lines': 20, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'dependencies': ANY, - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('Java'), - u('lineno'): None, - u('lines'): 20, - } - expected_dependencies = [ - 'googlecode.javacv', - 'colorfulwolf.webcamapplet', - 'foobar', - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('Java'), + 'lines': 20, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'dependencies': ANY, + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('Java'), + u('lineno'): None, + u('lines'): 20, + } + expected_dependencies = [ + 'googlecode.javacv', + 'colorfulwolf.webcamapplet', + 'foobar', + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_c_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/see.c' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/see.c' + shutil.copy(entity, os.path.join(tempdir, 'see.c')) + entity = os.path.realpath(os.path.join(tempdir, 'see.c')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('C'), - 'lines': 8, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'dependencies': ANY, - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('C'), - u('lineno'): None, - u('lines'): 8, - } - expected_dependencies = [ - 'openssl', - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('C'), + 'lines': 8, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'dependencies': ANY, + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('C'), + u('lineno'): None, + u('lines'): 8, + } + expected_dependencies = [ + 'openssl', + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_cpp_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/seeplusplus.cpp' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/seeplusplus.cpp' + shutil.copy(entity, os.path.join(tempdir, 'seeplusplus.cpp')) + entity = os.path.realpath(os.path.join(tempdir, 'seeplusplus.cpp')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('C++'), - 'lines': 8, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'dependencies': ANY, - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('C++'), - u('lineno'): None, - u('lines'): 8, - } - expected_dependencies = [ - 'openssl', - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('C++'), + 'lines': 8, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'dependencies': ANY, + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('C++'), + u('lineno'): None, + u('lines'): 8, + } + expected_dependencies = [ + 'openssl', + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_csharp_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/seesharp.cs' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/seesharp.cs' + shutil.copy(entity, os.path.join(tempdir, 'seesharp.cs')) + entity = os.path.realpath(os.path.join(tempdir, 'seesharp.cs')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('C#'), - 'lines': 18, - 'entity': os.path.realpath(entity), - 'dependencies': ANY, - 'project': u(os.path.basename(os.path.realpath('.'))), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('C#'), - u('lineno'): None, - u('lines'): 18, - } - expected_dependencies = [ - 'Proper', - 'Fart', - 'Math', - 'WakaTime', - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('C#'), + 'lines': 18, + 'entity': os.path.realpath(entity), + 'dependencies': ANY, + 'project': u(os.path.basename(os.path.realpath('.'))), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('C#'), + u('lineno'): None, + u('lines'): 18, + } + expected_dependencies = [ + 'Proper', + 'Fart', + 'Math', + 'WakaTime', + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_php_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/php.php' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/php.php' + shutil.copy(entity, os.path.join(tempdir, 'php.php')) + entity = os.path.realpath(os.path.join(tempdir, 'php.php')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('PHP'), - 'lines': ANY, - 'entity': os.path.realpath(entity), - 'dependencies': ANY, - 'project': u(os.path.basename(os.path.realpath('.'))), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('PHP'), - u('lineno'): None, - u('lines'): ANY, - } - expected_dependencies = [ - 'Interop', - 'FooBarOne', - 'FooBarTwo', - 'FooBarThree', - 'FooBarFour', - 'FooBarSeven', - 'FooBarEight', - 'ArrayObject', - "'ServiceLocator.php'", - "'ServiceLocatorTwo.php'", - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('PHP'), + 'lines': ANY, + 'entity': os.path.realpath(entity), + 'dependencies': ANY, + 'project': u(os.path.basename(os.path.realpath('.'))), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('PHP'), + u('lineno'): None, + u('lines'): ANY, + } + expected_dependencies = [ + 'Interop', + 'FooBarOne', + 'FooBarTwo', + 'FooBarThree', + 'FooBarFour', + 'FooBarSeven', + 'FooBarEight', + 'ArrayObject', + "'ServiceLocator.php'", + "'ServiceLocatorTwo.php'", + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_php_in_html_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/html-with-php.html' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/html-with-php.html' + shutil.copy(entity, os.path.join(tempdir, 'html-with-php.html')) + entity = os.path.realpath(os.path.join(tempdir, 'html-with-php.html')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('HTML+PHP'), - 'lines': ANY, - 'dependencies': ANY, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('HTML+PHP'), - u('lineno'): None, - u('lines'): ANY, - } - expected_dependencies = [ - '"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"', - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('HTML+PHP'), + 'lines': ANY, + 'dependencies': ANY, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('HTML+PHP'), + u('lineno'): None, + u('lines'): ANY, + } + expected_dependencies = [ + '"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"', + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_html_django_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/html-django.html' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/html-django.html' + shutil.copy(entity, os.path.join(tempdir, 'html-django.html')) + entity = os.path.realpath(os.path.join(tempdir, 'html-django.html')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('HTML+Django/Jinja'), - 'lines': ANY, - 'dependencies': ANY, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('HTML+Django/Jinja'), - u('lineno'): None, - u('lines'): ANY, - } - expected_dependencies = [ - '"libs/json2.js"', - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('HTML+Django/Jinja'), + 'lines': ANY, + 'dependencies': ANY, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('HTML+Django/Jinja'), + u('lineno'): None, + u('lines'): ANY, + } + expected_dependencies = [ + '"libs/json2.js"', + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_go_dependencies_detected(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/go.go' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/go.go' + shutil.copy(entity, os.path.join(tempdir, 'go.go')) + entity = os.path.realpath(os.path.join(tempdir, 'go.go')) - args = ['--file', entity, '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, 102) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, 102) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': u('Go'), - 'lines': 24, - 'entity': os.path.realpath(entity), - 'project': u(os.path.basename(os.path.realpath('.'))), - 'dependencies': ANY, - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): ANY, - u('language'): u('Go'), - u('lineno'): None, - u('lines'): 24, - } - expected_dependencies = [ - '"compress/gzip"', - '"direct"', - '"foobar"', - '"github.com/golang/example/stringutil"', - '"image/gif"', - '"log"', - '"math"', - '"oldname"', - '"os"', - '"supress"', - ] + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] - self.assertListsEqual(dependencies, expected_dependencies) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': u('Go'), + 'lines': 24, + 'entity': os.path.realpath(entity), + 'project': u(os.path.basename(os.path.realpath('.'))), + 'dependencies': ANY, + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): ANY, + u('language'): u('Go'), + u('lineno'): None, + u('lines'): 24, + } + expected_dependencies = [ + '"compress/gzip"', + '"direct"', + '"foobar"', + '"github.com/golang/example/stringutil"', + '"image/gif"', + '"log"', + '"math"', + '"oldname"', + '"os"', + '"supress"', + ] + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'] + self.assertListsEqual(dependencies, expected_dependencies) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() diff --git a/tests/test_main.py b/tests/test_main.py index a7e2c15..f6ddcf7 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -7,6 +7,7 @@ from wakatime.packages import requests import os import time import re +import shutil import sys from wakatime.compat import u, is_py3 from wakatime.constants import ( @@ -57,320 +58,372 @@ class BaseTestCase(utils.TestCase): response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - entity = 'tests/samples/codefiles/twolinefile.txt' - config = 'tests/samples/configs/good_config.cfg' - args = ['--file', entity, '--key', '123', '--config', config] + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) - retval = execute(args) - self.assertEquals(retval, SUCCESS) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + config = 'tests/samples/configs/good_config.cfg' + args = ['--file', entity, '--key', '123', '--config', config] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + retval = execute(args) + self.assertEquals(retval, SUCCESS) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() def test_config_file_not_passed_in_command_line_args(self): response = Response() response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - with utils.mock.patch('wakatime.main.open') as mock_open: - mock_open.side_effect = IOError('') - - config = os.path.join(os.path.expanduser('~'), '.wakatime.cfg') + with utils.TemporaryDirectory() as tempdir: entity = 'tests/samples/codefiles/emptyfile.txt' - args = ['--file', entity] + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) - with self.assertRaises(SystemExit): - execute(args) - expected_stdout = u("Error: Could not read from config file {0}\n").format(u(config)) - expected_stderr = open('tests/samples/output/test_missing_config_file').read() - self.assertEquals(sys.stdout.getvalue(), expected_stdout) - self.assertEquals(sys.stderr.getvalue(), expected_stderr) - self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() + with utils.mock.patch('wakatime.main.open') as mock_open: + mock_open.side_effect = IOError('') + + config = os.path.join(os.path.expanduser('~'), '.wakatime.cfg') + args = ['--file', entity] + + with self.assertRaises(SystemExit): + execute(args) + expected_stdout = u("Error: Could not read from config file {0}\n").format(u(config)) + expected_stderr = open('tests/samples/output/test_missing_config_file').read() + self.assertEquals(sys.stdout.getvalue(), expected_stdout) + self.assertEquals(sys.stderr.getvalue(), expected_stderr) + self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() def test_missing_config_file(self): config = 'foo' - entity = 'tests/samples/codefiles/emptyfile.txt' - args = ['--file', entity, '--config', config] - with self.assertRaises(SystemExit): - execute(args) - expected_stdout = u("Error: Could not read from config file foo\n") - expected_stderr = open('tests/samples/output/test_missing_config_file').read() - self.assertEquals(sys.stdout.getvalue(), expected_stdout) - self.assertEquals(sys.stderr.getvalue(), expected_stderr) - self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/emptyfile.txt' + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) + + args = ['--file', entity, '--config', config] + with self.assertRaises(SystemExit): + execute(args) + expected_stdout = u("Error: Could not read from config file foo\n") + expected_stderr = open('tests/samples/output/test_missing_config_file').read() + self.assertEquals(sys.stdout.getvalue(), expected_stdout) + self.assertEquals(sys.stderr.getvalue(), expected_stderr) + + self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() def test_good_config_file(self): response = Response() response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - entity = 'tests/samples/codefiles/emptyfile.txt' - config = 'tests/samples/configs/has_everything.cfg' - args = ['--file', entity, '--config', config] - retval = execute(args) - self.assertEquals(retval, SUCCESS) - expected_stdout = open('tests/samples/output/main_test_good_config_file').read() - traceback_file = os.path.realpath('wakatime/main.py') - lineno = int(re.search(r' line (\d+),', sys.stdout.getvalue()).group(1)) - self.assertEquals(sys.stdout.getvalue(), expected_stdout.format(file=traceback_file, lineno=lineno)) - self.assertEquals(sys.stderr.getvalue(), '') + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/emptyfile.txt' + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + config = 'tests/samples/configs/has_everything.cfg' + args = ['--file', entity, '--config', config] + retval = execute(args) + self.assertEquals(retval, SUCCESS) + expected_stdout = open('tests/samples/output/main_test_good_config_file').read() + traceback_file = os.path.realpath('wakatime/main.py') + lineno = int(re.search(r' line (\d+),', sys.stdout.getvalue()).group(1)) + self.assertEquals(sys.stdout.getvalue(), expected_stdout.format(file=traceback_file, lineno=lineno)) + self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() def test_api_key_without_underscore_accepted(self): response = Response() response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - entity = 'tests/samples/codefiles/emptyfile.txt' - config = 'tests/samples/configs/sample_alternate_apikey.cfg' - args = ['--file', entity, '--config', config] - retval = execute(args) - self.assertEquals(retval, SUCCESS) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/emptyfile.txt' + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + config = 'tests/samples/configs/sample_alternate_apikey.cfg' + args = ['--file', entity, '--config', config] + retval = execute(args) + self.assertEquals(retval, SUCCESS) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() def test_bad_config_file(self): - entity = 'tests/samples/codefiles/emptyfile.txt' - config = 'tests/samples/configs/bad_config.cfg' - args = ['--file', entity, '--config', config] - retval = execute(args) - self.assertEquals(retval, CONFIG_FILE_PARSE_ERROR) - self.assertIn('ParsingError', sys.stdout.getvalue()) - self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/emptyfile.txt' + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) + + config = 'tests/samples/configs/bad_config.cfg' + args = ['--file', entity, '--config', config] + retval = execute(args) + self.assertEquals(retval, CONFIG_FILE_PARSE_ERROR) + self.assertIn('ParsingError', sys.stdout.getvalue()) + self.assertEquals(sys.stderr.getvalue(), '') + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() def test_non_hidden_filename(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/twolinefile.txt' - config = 'tests/samples/configs/good_config.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) - args = ['--file', entity, '--key', '123', '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' - retval = execute(args) - self.assertEquals(retval, API_ERROR) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--key', '123', '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, API_ERROR) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': 'Text only', - 'lines': 2, - 'entity': os.path.abspath(entity), - 'project': os.path.basename(os.path.abspath('.')), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): [], - u('language'): u('Text only'), - u('lineno'): None, - u('lines'): 2, - } + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': 'Text only', + 'lines': 2, + 'entity': os.path.realpath(entity), + 'project': os.path.basename(os.path.abspath('.')), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): [], + u('language'): u('Text only'), + u('lineno'): None, + u('lines'): 2, + } + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_hidden_filename(self): response = Response() response.status_code = 0 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) - entity = 'tests/samples/codefiles/twolinefile.txt' - config = 'tests/samples/configs/paranoid.cfg' + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) - args = ['--file', entity, '--key', '123', '--config', config, '--time', now] + now = u(int(time.time())) + config = 'tests/samples/configs/paranoid.cfg' - retval = execute(args) - self.assertEquals(retval, API_ERROR) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + args = ['--file', entity, '--key', '123', '--config', config, '--time', now] - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + retval = execute(args) + self.assertEquals(retval, API_ERROR) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - heartbeat = { - 'language': 'Text only', - 'lines': 2, - 'entity': 'HIDDEN.txt', - 'project': os.path.basename(os.path.abspath('.')), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): [], - u('language'): u('Text only'), - u('lineno'): None, - u('lines'): 2, - } + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + heartbeat = { + 'language': 'Text only', + 'lines': 2, + 'entity': 'HIDDEN.txt', + 'project': os.path.basename(os.path.abspath('.')), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): [], + u('language'): u('Text only'), + u('lineno'): None, + u('lines'): 2, + } + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_timeout_passed_via_command_line(self): response = Response() response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - entity = 'tests/samples/codefiles/twolinefile.txt' - config = 'tests/samples/configs/good_config.cfg' - args = ['--file', entity, '--key', '123', '--config', config, '--timeout', 'abc'] + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) - with self.assertRaises(SystemExit): - execute(args) - self.assertEquals(sys.stdout.getvalue(), '') - expected_stderr = open('tests/samples/output/main_test_timeout_passed_via_command_line').read() - self.assertEquals(sys.stderr.getvalue(), expected_stderr) + config = 'tests/samples/configs/good_config.cfg' + args = ['--file', entity, '--key', '123', '--config', config, '--timeout', 'abc'] - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() + with self.assertRaises(SystemExit): + execute(args) + self.assertEquals(sys.stdout.getvalue(), '') + expected_stderr = open('tests/samples/output/main_test_timeout_passed_via_command_line').read() + self.assertEquals(sys.stderr.getvalue(), expected_stderr) + + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.get'].assert_not_called() def test_500_response(self): response = Response() response.status_code = 500 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) - args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123', - '--config', 'tests/samples/configs/paranoid.cfg', '--time', now] + now = u(int(time.time())) + + args = ['--file', entity, '--key', '123', + '--config', 'tests/samples/configs/paranoid.cfg', '--time', now] - retval = execute(args) - self.assertEquals(retval, API_ERROR) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + retval = execute(args) + self.assertEquals(retval, API_ERROR) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - heartbeat = { - 'language': 'Text only', - 'lines': 2, - 'entity': 'HIDDEN.txt', - 'project': os.path.basename(os.path.abspath('.')), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): [], - u('language'): u('Text only'), - u('lineno'): None, - u('lines'): 2, - } + heartbeat = { + 'language': 'Text only', + 'lines': 2, + 'entity': 'HIDDEN.txt', + 'project': os.path.basename(os.path.abspath('.')), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): [], + u('language'): u('Text only'), + u('lineno'): None, + u('lines'): 2, + } - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_400_response(self): response = Response() response.status_code = 400 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) - args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123', - '--config', 'tests/samples/configs/paranoid.cfg', '--time', now] + now = u(int(time.time())) + + args = ['--file', entity, '--key', '123', + '--config', 'tests/samples/configs/paranoid.cfg', '--time', now] - retval = execute(args) - self.assertEquals(retval, API_ERROR) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + retval = execute(args) + self.assertEquals(retval, API_ERROR) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_401_response(self): response = Response() response.status_code = 401 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - now = u(int(time.time())) + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) - args = ['--file', 'tests/samples/codefiles/twolinefile.txt', '--key', '123', - '--config', 'tests/samples/configs/paranoid.cfg', '--time', now] + now = u(int(time.time())) + + args = ['--file', entity, '--key', '123', + '--config', 'tests/samples/configs/paranoid.cfg', '--time', now] - retval = execute(args) - self.assertEquals(retval, AUTH_ERROR) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + retval = execute(args) + self.assertEquals(retval, AUTH_ERROR) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() - heartbeat = { - 'language': 'Text only', - 'lines': 2, - 'entity': 'HIDDEN.txt', - 'project': os.path.basename(os.path.abspath('.')), - 'time': float(now), - 'type': 'file', - } - stats = { - u('cursorpos'): None, - u('dependencies'): [], - u('language'): u('Text only'), - u('lineno'): None, - u('lines'): 2, - } + heartbeat = { + 'language': 'Text only', + 'lines': 2, + 'entity': 'HIDDEN.txt', + 'project': os.path.basename(os.path.abspath('.')), + 'time': float(now), + 'type': 'file', + } + stats = { + u('cursorpos'): None, + u('dependencies'): [], + u('language'): u('Text only'), + u('lineno'): None, + u('lines'): 2, + } - self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) - for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): - self.assertEquals(heartbeat[key], val) - self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) - self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(ANY, ANY, None) + for key, val in self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].items(): + self.assertEquals(heartbeat[key], val) + self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1])) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() def test_missing_entity_file(self): response = Response() @@ -378,6 +431,7 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response entity = 'tests/samples/codefiles/missingfile.txt' + config = 'tests/samples/configs/good_config.cfg' args = ['--file', entity, '--config', config] retval = execute(args) @@ -397,22 +451,26 @@ class BaseTestCase(utils.TestCase): response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - entity = 'tests/samples/codefiles/emptyfile.txt' - config = 'tests/samples/configs/good_config.cfg' - args = ['--file', entity, '--config', config, '--proxy', 'localhost:1234'] - retval = execute(args) - self.assertEquals(retval, SUCCESS) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/emptyfile.txt' + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + config = 'tests/samples/configs/good_config.cfg' + args = ['--file', entity, '--config', config, '--proxy', 'localhost:1234'] + retval = execute(args) + self.assertEquals(retval, SUCCESS) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) - self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': 'localhost:1234'}, stream=False, timeout=60, verify=True) + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].assert_called_once_with(ANY, cert=None, proxies={'https': 'localhost:1234'}, stream=False, timeout=60, verify=True) def test_entity_type_domain(self): response = Response() @@ -493,48 +551,83 @@ class BaseTestCase(utils.TestCase): response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - hostname = 'test汉语' if is_py3 else 'test\xe6\xb1\x89\xe8\xaf\xad' - with utils.mock.patch('socket.gethostname') as mock_gethostname: - mock_gethostname.return_value = hostname - self.assertEquals(type(hostname).__name__, 'str') - + with utils.TemporaryDirectory() as tempdir: entity = 'tests/samples/codefiles/emptyfile.txt' - config = 'tests/samples/configs/has_everything.cfg' - args = ['--file', entity, '--config', config] - retval = execute(args) - self.assertEquals(retval, SUCCESS) - expected_stdout = open('tests/samples/output/main_test_good_config_file').read() - traceback_file = os.path.realpath('wakatime/main.py') - lineno = int(re.search(r' line (\d+),', sys.stdout.getvalue()).group(1)) - self.assertEquals(sys.stdout.getvalue(), expected_stdout.format(file=traceback_file, lineno=lineno)) - self.assertEquals(sys.stderr.getvalue(), '') + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + hostname = 'test汉语' if is_py3 else 'test\xe6\xb1\x89\xe8\xaf\xad' + with utils.mock.patch('socket.gethostname') as mock_gethostname: + mock_gethostname.return_value = hostname + self.assertEquals(type(hostname).__name__, 'str') - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + config = 'tests/samples/configs/has_everything.cfg' + args = ['--file', entity, '--config', config] + retval = execute(args) + self.assertEquals(retval, SUCCESS) + expected_stdout = open('tests/samples/output/main_test_good_config_file').read() + traceback_file = os.path.realpath('wakatime/main.py') + lineno = int(re.search(r' line (\d+),', sys.stdout.getvalue()).group(1)) + self.assertEquals(sys.stdout.getvalue(), expected_stdout.format(file=traceback_file, lineno=lineno)) + self.assertEquals(sys.stderr.getvalue(), '') - headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers - self.assertEquals(headers.get('X-Machine-Name'), hostname.encode('utf-8') if is_py3 else hostname) + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + + headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers + self.assertEquals(headers.get('X-Machine-Name'), hostname.encode('utf-8') if is_py3 else hostname) def test_nonascii_timezone(self): response = Response() response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - package_path = 'wakatime.packages.py3.tzlocal.get_localzone' if is_py3 else 'wakatime.packages.py2.tzlocal.get_localzone' - timezone = tzlocal.get_localzone() - timezone.zone = 'tz汉语' if is_py3 else 'tz\xe6\xb1\x89\xe8\xaf\xad' - with utils.mock.patch(package_path) as mock_getlocalzone: - mock_getlocalzone.return_value = timezone - + with utils.TemporaryDirectory() as tempdir: entity = 'tests/samples/codefiles/emptyfile.txt' - config = 'tests/samples/configs/has_everything.cfg' - args = ['--file', entity, '--config', config, '--timeout', '15'] + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) + + package_path = 'wakatime.packages.py3.tzlocal.get_localzone' if is_py3 else 'wakatime.packages.py2.tzlocal.get_localzone' + timezone = tzlocal.get_localzone() + timezone.zone = 'tz汉语' if is_py3 else 'tz\xe6\xb1\x89\xe8\xaf\xad' + with utils.mock.patch(package_path) as mock_getlocalzone: + mock_getlocalzone.return_value = timezone + + config = 'tests/samples/configs/has_everything.cfg' + args = ['--file', entity, '--config', config, '--timeout', '15'] + retval = execute(args) + self.assertEquals(retval, SUCCESS) + + self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) + + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + + headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers + self.assertEquals(headers.get('TimeZone'), u(timezone.zone).encode('utf-8') if is_py3 else timezone.zone) + + def test_timezone_header(self): + response = Response() + response.status_code = 201 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/emptyfile.txt' + shutil.copy(entity, os.path.join(tempdir, 'emptyfile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'emptyfile.txt')) + + config = 'tests/samples/configs/good_config.cfg' + args = ['--file', entity, '--config', config] retval = execute(args) self.assertEquals(retval, SUCCESS) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() @@ -543,77 +636,59 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + timezone = tzlocal.get_localzone() headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers self.assertEquals(headers.get('TimeZone'), u(timezone.zone).encode('utf-8') if is_py3 else timezone.zone) - def test_timezone_header(self): - response = Response() - response.status_code = 201 - self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - - entity = 'tests/samples/codefiles/emptyfile.txt' - config = 'tests/samples/configs/good_config.cfg' - args = ['--file', entity, '--config', config] - retval = execute(args) - self.assertEquals(retval, SUCCESS) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') - - self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with() - self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.save'].assert_called_once_with(ANY) - - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() - - timezone = tzlocal.get_localzone() - headers = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args[0][0].headers - self.assertEquals(headers.get('TimeZone'), u(timezone.zone).encode('utf-8') if is_py3 else timezone.zone) - def test_extra_heartbeats_argument(self): response = Response() response.status_code = 201 self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response - project1 = os.path.basename(os.path.abspath('.')) - project2 = 'xyz' - entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt') - entity2 = os.path.abspath('tests/samples/codefiles/twolinefile.txt') - config = 'tests/samples/configs/good_config.cfg' - args = ['--file', entity1, '--config', config, '--extra-heartbeats'] + with utils.TemporaryDirectory() as tempdir: + entity = 'tests/samples/codefiles/twolinefile.txt' + shutil.copy(entity, os.path.join(tempdir, 'twolinefile.txt')) + entity = os.path.realpath(os.path.join(tempdir, 'twolinefile.txt')) - with utils.mock.patch('wakatime.main.sys.stdin') as mock_stdin: - now = int(time.time()) - heartbeats = json.dumps([{ - 'timestamp': now, - 'entity': entity2, - 'entity_type': 'file', - 'project': project2, - 'is_write': True, - }]) - mock_stdin.readline.return_value = heartbeats + project1 = os.path.basename(os.path.abspath('.')) + project2 = 'xyz' + entity1 = os.path.abspath('tests/samples/codefiles/emptyfile.txt') + entity2 = os.path.abspath('tests/samples/codefiles/twolinefile.txt') + config = 'tests/samples/configs/good_config.cfg' + args = ['--file', entity1, '--config', config, '--extra-heartbeats'] - retval = execute(args) + with utils.mock.patch('wakatime.main.sys.stdin') as mock_stdin: + now = int(time.time()) + heartbeats = json.dumps([{ + 'timestamp': now, + 'entity': entity2, + 'entity_type': 'file', + 'project': project2, + 'is_write': True, + }]) + mock_stdin.readline.return_value = heartbeats - self.assertEquals(retval, SUCCESS) - self.assertEquals(sys.stdout.getvalue(), '') - self.assertEquals(sys.stderr.getvalue(), '') + retval = execute(args) - self.patched['wakatime.session_cache.SessionCache.get'].assert_has_calls([call(), call()]) - self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() - self.patched['wakatime.session_cache.SessionCache.save'].assert_has_calls([call(ANY), call(ANY)]) + self.assertEquals(retval, SUCCESS) + self.assertEquals(sys.stdout.getvalue(), '') + self.assertEquals(sys.stderr.getvalue(), '') - self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() - self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() + self.patched['wakatime.session_cache.SessionCache.get'].assert_has_calls([call(), call()]) + self.patched['wakatime.session_cache.SessionCache.delete'].assert_not_called() + self.patched['wakatime.session_cache.SessionCache.save'].assert_has_calls([call(ANY), call(ANY)]) - calls = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args_list + self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() + self.patched['wakatime.offlinequeue.Queue.pop'].assert_called_once_with() - body = calls[0][0][0].body - data = json.loads(body) - self.assertEquals(data.get('entity'), entity1) - self.assertEquals(data.get('project'), project1) + calls = self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].call_args_list - body = calls[1][0][0].body - data = json.loads(body) - self.assertEquals(data.get('entity'), entity2) - self.assertEquals(data.get('project'), project2) + body = calls[0][0][0].body + data = json.loads(body) + self.assertEquals(data.get('entity'), entity1) + self.assertEquals(data.get('project'), project1) + + body = calls[1][0][0].body + data = json.loads(body) + self.assertEquals(data.get('entity'), entity2) + self.assertEquals(data.get('project'), project2) diff --git a/tests/utils.py b/tests/utils.py index 78b107b..3719687 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -52,3 +52,24 @@ class TestCase(unittest.TestCase): @property def isPy35(self): return (sys.version_info[0] == 3 and sys.version_info[1] == 5) + + +try: + # Python >= 3 + from tempfile import TemporaryDirectory +except ImportError: + # Python < 3 + import shutil + import tempfile + class TemporaryDirectory(object): + """Context manager for tempfile.mkdtemp(). + + Adds the ability to use with a `with` statement. + """ + + def __enter__(self): + self.name = tempfile.mkdtemp() + return self.name + + def __exit__(self, exc_type, exc_value, traceback): + shutil.rmtree(self.name)