fix dependency truncation
This commit is contained in:
parent
3707bf8658
commit
1127e5c8da
5 changed files with 45 additions and 21 deletions
|
@ -2,12 +2,21 @@
|
||||||
# vim: set filetype=python
|
# vim: set filetype=python
|
||||||
|
|
||||||
|
|
||||||
import os
|
import os, sys
|
||||||
import django
|
import django
|
||||||
|
from app import *
|
||||||
|
from flask import session
|
||||||
import simplejson as json
|
import simplejson as json
|
||||||
from wakatime import utils
|
|
||||||
from mypackage.mymodule import myfunction
|
|
||||||
from . import privatemodule
|
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:
|
try:
|
||||||
from mock import ANY
|
from mock import ANY
|
||||||
|
|
|
@ -31,7 +31,7 @@ class LanguagesTestCase(utils.TestCase):
|
||||||
['wakatime.session_cache.SessionCache.get', requests.session],
|
['wakatime.session_cache.SessionCache.get', requests.session],
|
||||||
]
|
]
|
||||||
|
|
||||||
def python_dependencies_detected(self):
|
def test_python_dependencies_detected(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
response.status_code = 0
|
response.status_code = 0
|
||||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
@ -53,7 +53,7 @@ class LanguagesTestCase(utils.TestCase):
|
||||||
|
|
||||||
heartbeat = {
|
heartbeat = {
|
||||||
'language': u('Python'),
|
'language': u('Python'),
|
||||||
'lines': 27,
|
'lines': 36,
|
||||||
'entity': os.path.realpath(entity),
|
'entity': os.path.realpath(entity),
|
||||||
'project': u(os.path.basename(os.path.realpath('.'))),
|
'project': u(os.path.basename(os.path.realpath('.'))),
|
||||||
'dependencies': ANY,
|
'dependencies': ANY,
|
||||||
|
@ -66,23 +66,38 @@ class LanguagesTestCase(utils.TestCase):
|
||||||
u('dependencies'): ANY,
|
u('dependencies'): ANY,
|
||||||
u('language'): u('Python'),
|
u('language'): u('Python'),
|
||||||
u('lineno'): None,
|
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)
|
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
||||||
for dep in expected_dependencies:
|
dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies']
|
||||||
self.assertIn(dep, 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.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.pop'].assert_not_called()
|
||||||
|
|
||||||
def test_bower_dependencies_detected(self):
|
"""def test_bower_dependencies_detected(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
response.status_code = 0
|
response.status_code = 0
|
||||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
|
||||||
now = u(int(time.time()))
|
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'
|
config = 'tests/samples/configs/good_config.cfg'
|
||||||
|
|
||||||
args = ['--file', entity, '--config', config, '--time', now]
|
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.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.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.pop'].assert_not_called()
|
||||||
|
"""
|
||||||
|
|
|
@ -27,6 +27,7 @@ class TestCase(unittest.TestCase):
|
||||||
|
|
||||||
patcher = mock.patch(namespace)
|
patcher = mock.patch(namespace)
|
||||||
mocked = patcher.start()
|
mocked = patcher.start()
|
||||||
|
mocked.reset_mock()
|
||||||
self.patched[namespace] = mocked
|
self.patched[namespace] = mocked
|
||||||
|
|
||||||
if isinstance(patch_this, (list, set)) and len(patch_this) > 0:
|
if isinstance(patch_this, (list, set)) and len(patch_this) > 0:
|
||||||
|
|
|
@ -24,12 +24,10 @@ class TokenParser(object):
|
||||||
language, inherit from this class and implement the :meth:`parse` method
|
language, inherit from this class and implement the :meth:`parse` method
|
||||||
to return a list of dependency strings.
|
to return a list of dependency strings.
|
||||||
"""
|
"""
|
||||||
source_file = None
|
|
||||||
lexer = None
|
|
||||||
dependencies = []
|
|
||||||
tokens = []
|
|
||||||
|
|
||||||
def __init__(self, source_file, lexer=None):
|
def __init__(self, source_file, lexer=None):
|
||||||
|
self.tokens = []
|
||||||
|
self.dependencies = []
|
||||||
self.source_file = source_file
|
self.source_file = source_file
|
||||||
self.lexer = lexer
|
self.lexer = lexer
|
||||||
|
|
||||||
|
@ -42,8 +40,6 @@ class TokenParser(object):
|
||||||
|
|
||||||
def append(self, dep, truncate=False, separator=None, truncate_to=None,
|
def append(self, dep, truncate=False, separator=None, truncate_to=None,
|
||||||
strip_whitespace=True):
|
strip_whitespace=True):
|
||||||
if dep == 'as':
|
|
||||||
print('***************** as')
|
|
||||||
self._save_dependency(
|
self._save_dependency(
|
||||||
dep,
|
dep,
|
||||||
truncate=truncate,
|
truncate=truncate,
|
||||||
|
@ -73,12 +69,14 @@ class TokenParser(object):
|
||||||
separator = u('.')
|
separator = u('.')
|
||||||
separator = u(separator)
|
separator = u(separator)
|
||||||
dep = dep.split(separator)
|
dep = dep.split(separator)
|
||||||
if truncate_to is None or truncate_to < 0 or truncate_to > len(dep) - 1:
|
if truncate_to is None or truncate_to < 1:
|
||||||
truncate_to = len(dep) - 1
|
truncate_to = 1
|
||||||
dep = dep[0] if len(dep) == 1 else separator.join(dep[0:truncate_to])
|
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:
|
if strip_whitespace:
|
||||||
dep = dep.strip()
|
dep = dep.strip()
|
||||||
if dep:
|
if dep and (not separator or not dep.startswith(separator)):
|
||||||
self.dependencies.append(dep)
|
self.dependencies.append(dep)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue