fix dependency truncation

This commit is contained in:
Alan Hamlett 2015-09-16 12:57:49 -07:00
parent 3707bf8658
commit 1127e5c8da
5 changed files with 45 additions and 21 deletions

View file

@ -2,12 +2,21 @@
# vim: set filetype=python
import os
import os, sys
import django
from app import *
from flask import session
import simplejson as json
from wakatime import utils
from mypackage.mymodule import myfunction
from . import privatemodule
from jinja import tags
from pygments.lexers import BaseLexer
from . import LocalClass
from . import MyClass as MyParser
from ..compat import u
from sqlalchemy import (
functions as sqlfunctions,
orm as sqlorm,
)
try:
from mock import ANY

View file

@ -31,7 +31,7 @@ class LanguagesTestCase(utils.TestCase):
['wakatime.session_cache.SessionCache.get', requests.session],
]
def python_dependencies_detected(self):
def test_python_dependencies_detected(self):
response = Response()
response.status_code = 0
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
@ -53,7 +53,7 @@ class LanguagesTestCase(utils.TestCase):
heartbeat = {
'language': u('Python'),
'lines': 27,
'lines': 36,
'entity': os.path.realpath(entity),
'project': u(os.path.basename(os.path.realpath('.'))),
'dependencies': ANY,
@ -66,23 +66,38 @@ class LanguagesTestCase(utils.TestCase):
u('dependencies'): ANY,
u('language'): u('Python'),
u('lineno'): None,
u('lines'): 27,
u('lines'): 36,
}
expected_dependencies = ['wakatime', 'mock', 'django', 'simplejson', 'os']
expected_dependencies = [
'app',
'django',
'flask',
'jinja',
'mock',
'os',
'pygments',
'simplejson',
'sqlalchemy',
'sys',
'unittest',
]
def normalize(items):
return sorted([u(x) for x in items])
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
for dep in expected_dependencies:
self.assertIn(dep, self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies'])
dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies']
self.assertEquals(normalize(dependencies), normalize(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):
"""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'
entity = 'tests/samples/codefiles/js/bower.json'
config = 'tests/samples/configs/good_config.cfg'
args = ['--file', entity, '--config', config, '--time', now]
@ -116,3 +131,4 @@ class LanguagesTestCase(utils.TestCase):
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()
"""

View file

@ -27,6 +27,7 @@ class TestCase(unittest.TestCase):
patcher = mock.patch(namespace)
mocked = patcher.start()
mocked.reset_mock()
self.patched[namespace] = mocked
if isinstance(patch_this, (list, set)) and len(patch_this) > 0:

View file

@ -24,12 +24,10 @@ class TokenParser(object):
language, inherit from this class and implement the :meth:`parse` method
to return a list of dependency strings.
"""
source_file = None
lexer = None
dependencies = []
tokens = []
def __init__(self, source_file, lexer=None):
self.tokens = []
self.dependencies = []
self.source_file = source_file
self.lexer = lexer
@ -42,8 +40,6 @@ class TokenParser(object):
def append(self, dep, truncate=False, separator=None, truncate_to=None,
strip_whitespace=True):
if dep == 'as':
print('***************** as')
self._save_dependency(
dep,
truncate=truncate,
@ -73,12 +69,14 @@ class TokenParser(object):
separator = u('.')
separator = u(separator)
dep = dep.split(separator)
if truncate_to is None or truncate_to < 0 or truncate_to > len(dep) - 1:
truncate_to = len(dep) - 1
dep = dep[0] if len(dep) == 1 else separator.join(dep[0:truncate_to])
if truncate_to is None or truncate_to < 1:
truncate_to = 1
if truncate_to > len(dep):
truncate_to = len(dep)
dep = dep[0] if len(dep) == 1 else separator.join(dep[:truncate_to])
if strip_whitespace:
dep = dep.strip()
if dep:
if dep and (not separator or not dep.startswith(separator)):
self.dependencies.append(dep)