test python dependency detection and --alternate-project argument

This commit is contained in:
Alan Hamlett 2015-08-12 15:03:39 -07:00
parent 5ec3cebcdf
commit 85de7a3deb
4 changed files with 160 additions and 3 deletions

26
tests/samples/codefile.py Normal file
View file

@ -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()

View file

@ -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()

View file

@ -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()

View file

@ -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'):