improve test coverage for dependency detection
This commit is contained in:
parent
5eff290e38
commit
e4fea84b5e
5 changed files with 213 additions and 36 deletions
37
tests/samples/codefiles/html-django.html
Normal file
37
tests/samples/codefiles/html-django.html
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
{% extends "common/base.html" %}
|
||||||
|
{% block subtitle %}Login{% endblock %}
|
||||||
|
|
||||||
|
{% block js %}
|
||||||
|
{% compress 'js' %}
|
||||||
|
<script src="{{ STATIC_URL }}libs/json2.js"></script>
|
||||||
|
{% endcompress %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block content %}
|
||||||
|
<form class="login form-horizontal" method="POST">
|
||||||
|
<input type="hidden" name="csrftoken" value="{{ csrf_token() }}" />
|
||||||
|
{% if error %}
|
||||||
|
<div class="form-group error">
|
||||||
|
<div class="col-lg-offset-1">
|
||||||
|
<div id="flash-error" class="text-danger">{{error}}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
{% with messages = get_flashed_messages() %}
|
||||||
|
{% if messages %}
|
||||||
|
{% for message in messages %}
|
||||||
|
<div class="form-group error">
|
||||||
|
<div class="col-lg-offset-1">
|
||||||
|
<div id="flash-error" class="text-danger">{{ message }}</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
{% endif %}
|
||||||
|
{% endwith %}
|
||||||
|
<label class="col-lg-2 control-label" for="email">Email</label>
|
||||||
|
<input class="form-control" type="text" name="email" id="email" value="{{form.data.email or ''}}" placeholder="Email" style="width:80%;" />
|
||||||
|
{% if form.errors.email %}
|
||||||
|
<p class="text-danger">{{form.errors.email[0]}}</p>
|
||||||
|
{% endif %}
|
||||||
|
<missingquote class="text-danger>{{form.errors.email[0]}}</p>
|
||||||
|
{% endblock %}
|
|
@ -47,6 +47,53 @@ class DependenciesTestCase(utils.TestCase):
|
||||||
parser.tokens
|
parser.tokens
|
||||||
mock_extract_tokens.assert_called_once_with()
|
mock_extract_tokens.assert_called_once_with()
|
||||||
|
|
||||||
|
def test_io_error_when_parsing_dependencies(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'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--config', config, '--time', now]
|
||||||
|
|
||||||
|
with utils.mock.patch('wakatime.dependencies.open') as mock_open:
|
||||||
|
mock_open.side_effect = IOError('')
|
||||||
|
retval = execute(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': u('Python'),
|
||||||
|
'lines': 36,
|
||||||
|
'entity': os.path.realpath(entity),
|
||||||
|
'project': u(os.path.basename(os.path.realpath('.'))),
|
||||||
|
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
||||||
|
'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(heartbeat, ANY, None)
|
||||||
|
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):
|
def test_python_dependencies_detected(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
response.status_code = 0
|
response.status_code = 0
|
||||||
|
@ -440,6 +487,53 @@ class DependenciesTestCase(utils.TestCase):
|
||||||
self.assertListsEqual(dependencies, expected_dependencies)
|
self.assertListsEqual(dependencies, expected_dependencies)
|
||||||
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
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'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--config', config, '--time', now]
|
||||||
|
|
||||||
|
retval = execute(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': u('HTML+Django/Jinja'),
|
||||||
|
'lines': ANY,
|
||||||
|
'dependencies': ANY,
|
||||||
|
'entity': os.path.realpath(entity),
|
||||||
|
'project': u(os.path.basename(os.path.realpath('.'))),
|
||||||
|
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
||||||
|
'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(heartbeat, ANY, None)
|
||||||
|
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):
|
def test_go_dependencies_detected(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
response.status_code = 0
|
response.status_code = 0
|
||||||
|
|
|
@ -142,12 +142,41 @@ class LanguagesTestCase(utils.TestCase):
|
||||||
execute(args)
|
execute(args)
|
||||||
|
|
||||||
self.assertEquals('git', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
self.assertEquals('git', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
||||||
|
self.assertEquals('master', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
|
||||||
|
|
||||||
|
def test_ioerror_when_reading_git_branch(self):
|
||||||
|
response = Response()
|
||||||
|
response.status_code = 0
|
||||||
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
|
||||||
|
tempdir = tempfile.mkdtemp()
|
||||||
|
shutil.copytree('tests/samples/projects/git', os.path.join(tempdir, 'git'))
|
||||||
|
shutil.move(os.path.join(tempdir, 'git', 'dot_git'), os.path.join(tempdir, 'git', '.git'))
|
||||||
|
|
||||||
|
now = u(int(time.time()))
|
||||||
|
entity = os.path.join(tempdir, 'git', 'emptyfile.txt')
|
||||||
|
config = 'tests/samples/configs/good_config.cfg'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--config', config, '--time', now]
|
||||||
|
|
||||||
|
with utils.mock.patch('wakatime.projects.git.open') as mock_open:
|
||||||
|
mock_open.side_effect = IOError('')
|
||||||
|
execute(args)
|
||||||
|
|
||||||
|
self.assertEquals('git', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
||||||
|
self.assertEquals('master', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('branch'))
|
||||||
|
|
||||||
def test_svn_project_detected(self):
|
def test_svn_project_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()))
|
||||||
|
entity = 'tests/samples/projects/svn/afolder/emptyfile.txt'
|
||||||
|
config = 'tests/samples/configs/good_config.cfg'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--config', config, '--time', now]
|
||||||
|
|
||||||
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
||||||
mock_git.return_value = False
|
mock_git.return_value = False
|
||||||
|
|
||||||
|
@ -156,15 +185,9 @@ class LanguagesTestCase(utils.TestCase):
|
||||||
stderr = ''
|
stderr = ''
|
||||||
mock_popen.return_value = (stdout, stderr)
|
mock_popen.return_value = (stdout, stderr)
|
||||||
|
|
||||||
now = u(int(time.time()))
|
|
||||||
entity = 'tests/samples/projects/svn/afolder/emptyfile.txt'
|
|
||||||
config = 'tests/samples/configs/good_config.cfg'
|
|
||||||
|
|
||||||
args = ['--file', entity, '--config', config, '--time', now]
|
|
||||||
|
|
||||||
execute(args)
|
execute(args)
|
||||||
|
|
||||||
self.assertEquals('svn', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
self.assertEquals('svn', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
||||||
|
|
||||||
def test_svn_exception_handled(self):
|
def test_svn_exception_handled(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
|
@ -174,21 +197,45 @@ class LanguagesTestCase(utils.TestCase):
|
||||||
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
||||||
mock_git.return_value = False
|
mock_git.return_value = False
|
||||||
|
|
||||||
|
now = u(int(time.time()))
|
||||||
|
entity = 'tests/samples/projects/svn/afolder/emptyfile.txt'
|
||||||
|
config = 'tests/samples/configs/good_config.cfg'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--config', config, '--time', now]
|
||||||
|
|
||||||
with utils.mock.patch('wakatime.projects.subversion.Popen') as mock_popen:
|
with utils.mock.patch('wakatime.projects.subversion.Popen') as mock_popen:
|
||||||
mock_popen.side_effect = OSError('')
|
mock_popen.side_effect = OSError('')
|
||||||
|
|
||||||
with utils.mock.patch('wakatime.projects.subversion.Popen.communicate') as mock_communicate:
|
with utils.mock.patch('wakatime.projects.subversion.Popen.communicate') as mock_communicate:
|
||||||
mock_communicate.side_effect = OSError('')
|
mock_communicate.side_effect = OSError('')
|
||||||
|
|
||||||
now = u(int(time.time()))
|
execute(args)
|
||||||
entity = 'tests/samples/projects/svn/afolder/emptyfile.txt'
|
|
||||||
config = 'tests/samples/configs/good_config.cfg'
|
|
||||||
|
|
||||||
args = ['--file', entity, '--config', config, '--time', now]
|
self.assertNotIn('project', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0])
|
||||||
|
|
||||||
|
def test_svn_on_mac(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/projects/svn/afolder/emptyfile.txt'
|
||||||
|
config = 'tests/samples/configs/good_config.cfg'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--config', config, '--time', now]
|
||||||
|
|
||||||
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
||||||
|
mock_git.return_value = False
|
||||||
|
|
||||||
|
with utils.mock.patch('wakatime.projects.subversion.platform.system') as mock_system:
|
||||||
|
mock_system.return_value = 'Darwin'
|
||||||
|
|
||||||
|
with utils.mock.patch('wakatime.projects.subversion.Popen') as mock_popen:
|
||||||
|
mock_popen.side_effect = OSError('')
|
||||||
|
|
||||||
execute(args)
|
execute(args)
|
||||||
|
|
||||||
self.assertNotIn('project', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0])
|
self.assertNotIn('project', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0])
|
||||||
|
|
||||||
def test_mercurial_project_detected(self):
|
def test_mercurial_project_detected(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
|
@ -209,6 +256,27 @@ class LanguagesTestCase(utils.TestCase):
|
||||||
self.assertEquals('hg', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
self.assertEquals('hg', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
||||||
self.assertEquals('test-hg-branch', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
|
self.assertEquals('test-hg-branch', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
|
||||||
|
|
||||||
|
def test_ioerror_when_reading_mercurial_branch(self):
|
||||||
|
response = Response()
|
||||||
|
response.status_code = 0
|
||||||
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||||
|
|
||||||
|
with utils.mock.patch('wakatime.projects.git.Git.process') as mock_git:
|
||||||
|
mock_git.return_value = False
|
||||||
|
|
||||||
|
now = u(int(time.time()))
|
||||||
|
entity = 'tests/samples/projects/hg/emptyfile.txt'
|
||||||
|
config = 'tests/samples/configs/good_config.cfg'
|
||||||
|
|
||||||
|
args = ['--file', entity, '--config', config, '--time', now]
|
||||||
|
|
||||||
|
with utils.mock.patch('wakatime.projects.mercurial.open') as mock_open:
|
||||||
|
mock_open.side_effect = IOError('')
|
||||||
|
execute(args)
|
||||||
|
|
||||||
|
self.assertEquals('hg', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['project'])
|
||||||
|
self.assertEquals('default', self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['branch'])
|
||||||
|
|
||||||
def test_project_map(self):
|
def test_project_map(self):
|
||||||
response = Response()
|
response = Response()
|
||||||
response.status_code = 0
|
response.status_code = 0
|
||||||
|
|
|
@ -69,28 +69,6 @@ KEYWORDS = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class LassoJavascriptParser(TokenParser):
|
|
||||||
|
|
||||||
def parse(self):
|
|
||||||
for index, token, content in self.tokens:
|
|
||||||
self._process_token(token, content)
|
|
||||||
return self.dependencies
|
|
||||||
|
|
||||||
def _process_token(self, token, content):
|
|
||||||
if u(token) == 'Token.Name.Other':
|
|
||||||
self._process_name(token, content)
|
|
||||||
elif u(token) == 'Token.Literal.String.Single' or u(token) == 'Token.Literal.String.Double':
|
|
||||||
self._process_literal_string(token, content)
|
|
||||||
|
|
||||||
def _process_name(self, token, content):
|
|
||||||
if content.lower() in KEYWORDS:
|
|
||||||
self.append(content.lower())
|
|
||||||
|
|
||||||
def _process_literal_string(self, token, content):
|
|
||||||
if 'famous/core/' in content.strip('"').strip("'"):
|
|
||||||
self.append('famous')
|
|
||||||
|
|
||||||
|
|
||||||
class HtmlDjangoParser(TokenParser):
|
class HtmlDjangoParser(TokenParser):
|
||||||
tags = []
|
tags = []
|
||||||
getting_attrs = False
|
getting_attrs = False
|
||||||
|
|
|
@ -47,12 +47,12 @@ class Git(BaseProject):
|
||||||
log.traceback('warn')
|
log.traceback('warn')
|
||||||
except IOError: # pragma: nocover
|
except IOError: # pragma: nocover
|
||||||
log.traceback('warn')
|
log.traceback('warn')
|
||||||
return None
|
return u('master')
|
||||||
|
|
||||||
def _project_base(self):
|
def _project_base(self):
|
||||||
if self.configFile:
|
if self.configFile:
|
||||||
return os.path.dirname(os.path.dirname(self.configFile))
|
return os.path.dirname(os.path.dirname(self.configFile))
|
||||||
return None
|
return None # pragma: nocover
|
||||||
|
|
||||||
def _find_git_config_file(self, path):
|
def _find_git_config_file(self, path):
|
||||||
path = os.path.realpath(path)
|
path = os.path.realpath(path)
|
||||||
|
|
Loading…
Reference in a new issue