From 85de7a3debc30fc6d45b17c59785fd821a8c6521 Mon Sep 17 00:00:00 2001 From: Alan Hamlett Date: Wed, 12 Aug 2015 15:03:39 -0700 Subject: [PATCH] test python dependency detection and --alternate-project argument --- tests/samples/codefile.py | 26 ++++++++++++ tests/test_wakatime_base.py | 68 ++++++++++++++++++++++++++++++++ tests/test_wakatime_languages.py | 63 +++++++++++++++++++++++++++++ wakatime/compat.py | 6 +-- 4 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 tests/samples/codefile.py create mode 100644 tests/test_wakatime_languages.py diff --git a/tests/samples/codefile.py b/tests/samples/codefile.py new file mode 100644 index 0000000..bb28b0f --- /dev/null +++ b/tests/samples/codefile.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- + + +import os +import django +import simplejson as json +from wakatime import utils +from mypackage.mymodule import myfunction +from . import privatemodule + +try: + from mock import ANY +except ImportError: + from unittest.mock import ANY + + +class MyClass(object): + """this class + """ + + def method1(self): + a = 1 + 2 + b = 'hello world!' + for x in y: + print(x) + raise Exception() diff --git a/tests/test_wakatime_base.py b/tests/test_wakatime_base.py index 4c57532..73dace9 100644 --- a/tests/test_wakatime_base.py +++ b/tests/test_wakatime_base.py @@ -221,3 +221,71 @@ class BaseTestCase(utils.TestCase): self.patched['wakatime.offlinequeue.Queue.push'].assert_not_called() self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + + def test_alternate_project(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/twolinefile.txt' + config = 'tests/samples/sample.cfg' + + args = ['--file', entity, '--alternate-project', 'xyz', '--config', config, '--time', now] + + retval = main(args) + self.assertEquals(retval, 102) + 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_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + + heartbeat = { + 'language': 'Text only', + 'lines': 2, + 'entity': os.path.abspath(entity), + 'project': os.path.basename(os.path.abspath('.')), + 'branch': os.environ.get('TRAVIS_COMMIT', ANY), + 'time': float(now), + 'type': 'file', + } + stats = '{"cursorpos": null, "dependencies": [], "lines": 2, "lineno": null, "language": "Text only"}' + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, stats, None) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() + + def test_set_project_from_command_line(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/twolinefile.txt' + config = 'tests/samples/sample.cfg' + + args = ['--file', entity, '--project', 'xyz', '--config', config, '--time', now] + + retval = main(args) + self.assertEquals(retval, 102) + 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_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + + heartbeat = { + 'language': 'Text only', + 'lines': 2, + 'entity': os.path.abspath(entity), + 'project': 'xyz', + 'branch': os.environ.get('TRAVIS_COMMIT', ANY), + 'time': float(now), + 'type': 'file', + } + stats = '{"cursorpos": null, "dependencies": [], "lines": 2, "lineno": null, "language": "Text only"}' + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, stats, None) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() diff --git a/tests/test_wakatime_languages.py b/tests/test_wakatime_languages.py new file mode 100644 index 0000000..3e0dd0e --- /dev/null +++ b/tests/test_wakatime_languages.py @@ -0,0 +1,63 @@ +# -*- coding: utf-8 -*- + + +from wakatime.base import main +from wakatime.packages import requests + +import os +import time +import sys +from wakatime.compat import u +from wakatime.packages.requests.models import Response +from . import utils + +try: + from mock import ANY +except ImportError: + from unittest.mock import ANY + + +class LanguagesTestCase(utils.TestCase): + patch_these = [ + 'wakatime.packages.requests.adapters.HTTPAdapter.send', + 'wakatime.offlinequeue.Queue.push', + ['wakatime.offlinequeue.Queue.pop', None], + 'wakatime.session_cache.SessionCache.save', + 'wakatime.session_cache.SessionCache.delete', + ['wakatime.session_cache.SessionCache.get', requests.session], + ] + + 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/codefile.py' + config = 'tests/samples/sample.cfg' + + args = ['--file', entity, '--config', config, '--time', now] + + retval = main(args) + self.assertEquals(retval, 102) + 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_called_once_with() + self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called() + + heartbeat = { + 'language': 'Python', + 'lines': 26, + 'entity': os.path.abspath(entity), + 'project': os.path.basename(os.path.abspath('.')), + 'dependencies': ['wakatime', 'os', 'mock', 'simplejson', 'django'], + 'branch': os.environ.get('TRAVIS_COMMIT', ANY), + 'time': float(now), + 'type': 'file', + } + stats = '{"cursorpos": null, "dependencies": ["wakatime", "os", "mock", "simplejson", "django"], "lines": 26, "lineno": null, "language": "Python"}' + + self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, stats, None) + self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called() diff --git a/wakatime/compat.py b/wakatime/compat.py index d946fb0..3abbfa5 100644 --- a/wakatime/compat.py +++ b/wakatime/compat.py @@ -17,7 +17,7 @@ is_py2 = (sys.version_info[0] == 2) is_py3 = (sys.version_info[0] == 3) -if is_py2: +if is_py2: # pragma: nocover def u(text): try: @@ -31,7 +31,7 @@ if is_py2: basestring = basestring -elif is_py3: +elif is_py3: # pragma: nocover def u(text): if isinstance(text, bytes): @@ -42,7 +42,7 @@ elif is_py3: try: from importlib import import_module -except ImportError: +except ImportError: # pragma: nocover def _resolve_name(name, package, level): """Return the absolute name of the module to be imported.""" if not hasattr(package, 'rindex'):