2015-09-08 22:48:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
from wakatime.main import execute
|
|
|
|
from wakatime.packages import requests
|
|
|
|
|
2016-08-31 16:43:22 +00:00
|
|
|
import logging
|
2015-09-08 22:48:28 +00:00
|
|
|
import os
|
|
|
|
import time
|
2016-06-08 18:33:36 +00:00
|
|
|
import shutil
|
2016-08-31 16:43:22 +00:00
|
|
|
from testfixtures import log_capture
|
2015-09-08 22:48:28 +00:00
|
|
|
from wakatime.compat import u
|
2017-11-09 06:54:33 +00:00
|
|
|
from wakatime.constants import SUCCESS
|
2015-09-26 19:17:16 +00:00
|
|
|
from wakatime.exceptions import NotYetImplemented
|
2016-08-31 16:43:22 +00:00
|
|
|
from wakatime.dependencies import DependencyParser, TokenParser
|
|
|
|
from wakatime.packages.pygments.lexers import ClassNotFound, PythonLexer
|
2016-06-15 19:09:40 +00:00
|
|
|
from wakatime.stats import get_lexer_by_name
|
2017-11-23 02:34:15 +00:00
|
|
|
from .utils import mock, ANY, CustomResponse, TemporaryDirectory, TestCase
|
2015-09-08 22:48:28 +00:00
|
|
|
|
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
class DependenciesTestCase(TestCase):
|
2015-09-08 22:48:28 +00:00
|
|
|
patch_these = [
|
|
|
|
'wakatime.packages.requests.adapters.HTTPAdapter.send',
|
|
|
|
'wakatime.offlinequeue.Queue.push',
|
|
|
|
['wakatime.offlinequeue.Queue.pop', None],
|
2015-09-27 10:17:54 +00:00
|
|
|
['wakatime.offlinequeue.Queue.connect', None],
|
2015-09-08 22:48:28 +00:00
|
|
|
'wakatime.session_cache.SessionCache.save',
|
|
|
|
'wakatime.session_cache.SessionCache.delete',
|
|
|
|
['wakatime.session_cache.SessionCache.get', requests.session],
|
2015-09-27 10:17:54 +00:00
|
|
|
['wakatime.session_cache.SessionCache.connect', None],
|
2015-09-08 22:48:28 +00:00
|
|
|
]
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
def shared(self, expected_dependencies=[], expected_language=ANY, expected_lines=ANY, entity='', config='good_config.cfg', extra_args=[]):
|
2017-11-23 02:34:15 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
|
2017-11-09 06:54:33 +00:00
|
|
|
|
|
|
|
config = os.path.join('tests/samples/configs', config)
|
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
with TemporaryDirectory() as tempdir:
|
2017-11-09 06:54:33 +00:00
|
|
|
shutil.copy(os.path.join('tests/samples/codefiles', entity), os.path.join(tempdir, os.path.basename(entity)))
|
|
|
|
entity = os.path.realpath(os.path.join(tempdir, os.path.basename(entity)))
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now] + extra_args
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': expected_language,
|
|
|
|
'lines': expected_lines,
|
|
|
|
'entity': os.path.realpath(entity),
|
|
|
|
'project': ANY,
|
|
|
|
'branch': ANY,
|
|
|
|
'dependencies': expected_dependencies,
|
|
|
|
'time': float(now),
|
|
|
|
'type': 'file',
|
|
|
|
'is_write': False,
|
|
|
|
'user_agent': ANY,
|
|
|
|
}
|
|
|
|
self.assertHeartbeatSent(heartbeat)
|
|
|
|
|
|
|
|
self.assertHeartbeatNotSavedOffline()
|
|
|
|
self.assertOfflineHeartbeatsSynced()
|
|
|
|
self.assertSessionCacheSaved()
|
|
|
|
|
2015-09-26 19:17:16 +00:00
|
|
|
def test_token_parser(self):
|
2015-09-26 20:04:35 +00:00
|
|
|
with self.assertRaises(NotYetImplemented):
|
2017-02-27 00:24:43 +00:00
|
|
|
source_file = 'tests/samples/codefiles/c_only/non_empty.h'
|
2015-09-26 20:04:35 +00:00
|
|
|
parser = TokenParser(source_file)
|
|
|
|
parser.parse()
|
2015-09-26 19:17:16 +00:00
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
with mock.patch('wakatime.dependencies.TokenParser._extract_tokens') as mock_extract_tokens:
|
2015-09-26 20:04:35 +00:00
|
|
|
source_file = 'tests/samples/codefiles/see.h'
|
|
|
|
parser = TokenParser(source_file)
|
|
|
|
parser.tokens
|
2015-09-26 19:17:16 +00:00
|
|
|
mock_extract_tokens.assert_called_once_with()
|
|
|
|
|
2016-08-31 15:42:40 +00:00
|
|
|
parser = TokenParser(None)
|
|
|
|
parser.append('one.two.three', truncate=True, truncate_to=1)
|
|
|
|
parser.append('one.two.three', truncate=True, truncate_to=2)
|
|
|
|
parser.append('one.two.three', truncate=True, truncate_to=3)
|
|
|
|
parser.append('one.two.three', truncate=True, truncate_to=4)
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
'one',
|
|
|
|
'one.two',
|
|
|
|
'one.two.three',
|
|
|
|
'one.two.three',
|
|
|
|
]
|
|
|
|
self.assertEquals(parser.dependencies, expected)
|
|
|
|
|
2016-08-31 16:43:22 +00:00
|
|
|
@log_capture()
|
|
|
|
def test_dependency_parser(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
lexer = PythonLexer
|
|
|
|
lexer.__class__.__name__ = 'FooClass'
|
|
|
|
parser = DependencyParser(None, lexer)
|
|
|
|
|
|
|
|
dependencies = parser.parse()
|
|
|
|
|
|
|
|
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
|
|
|
self.assertEquals(log_output, '')
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2016-08-31 16:43:22 +00:00
|
|
|
|
|
|
|
expected = []
|
|
|
|
self.assertEquals(dependencies, expected)
|
|
|
|
|
2016-10-16 08:50:40 +00:00
|
|
|
@log_capture()
|
|
|
|
def test_missing_dependency_parser_in_debug_mode(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
# turn on debug mode
|
|
|
|
log = logging.getLogger('WakaTime')
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
lexer = PythonLexer
|
|
|
|
lexer.__class__.__name__ = 'FooClass'
|
|
|
|
parser = DependencyParser(None, lexer)
|
|
|
|
|
|
|
|
# parse dependencies
|
|
|
|
dependencies = parser.parse()
|
|
|
|
|
|
|
|
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
|
|
|
expected = 'WakaTime DEBUG Parsing dependencies not supported for python.FooClass'
|
|
|
|
self.assertEquals(log_output, expected)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2016-10-16 08:50:40 +00:00
|
|
|
|
|
|
|
expected = []
|
|
|
|
self.assertEquals(dependencies, expected)
|
|
|
|
|
|
|
|
@log_capture()
|
|
|
|
def test_missing_dependency_parser_importerror_in_debug_mode(self, logs):
|
|
|
|
logging.disable(logging.NOTSET)
|
|
|
|
|
|
|
|
# turn on debug mode
|
|
|
|
log = logging.getLogger('WakaTime')
|
|
|
|
log.setLevel(logging.DEBUG)
|
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
with mock.patch('wakatime.dependencies.import_module') as mock_import:
|
2016-10-16 08:50:40 +00:00
|
|
|
mock_import.side_effect = ImportError('foo')
|
|
|
|
|
|
|
|
lexer = PythonLexer
|
|
|
|
lexer.__class__.__name__ = 'FooClass'
|
|
|
|
parser = DependencyParser(None, lexer)
|
|
|
|
|
|
|
|
# parse dependencies
|
|
|
|
dependencies = parser.parse()
|
|
|
|
|
|
|
|
log_output = u("\n").join([u(' ').join(x) for x in logs.actual()])
|
|
|
|
expected = 'WakaTime DEBUG Parsing dependencies not supported for python.FooClass'
|
|
|
|
self.assertEquals(log_output, expected)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertNothingPrinted()
|
2016-10-16 08:50:40 +00:00
|
|
|
|
|
|
|
expected = []
|
|
|
|
self.assertEquals(dependencies, expected)
|
|
|
|
|
|
|
|
def test_io_error_suppressed_when_parsing_dependencies(self):
|
2017-11-23 02:34:15 +00:00
|
|
|
with mock.patch('wakatime.dependencies.open') as mock_open:
|
2017-11-09 06:54:33 +00:00
|
|
|
mock_open.side_effect = IOError('')
|
2016-06-08 18:33:36 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[],
|
|
|
|
expected_language='Python',
|
|
|
|
expected_lines=37,
|
|
|
|
entity='python.py',
|
|
|
|
)
|
2016-05-21 12:21:55 +00:00
|
|
|
|
2016-10-16 08:50:40 +00:00
|
|
|
def test_classnotfound_error_raised_when_passing_none_to_pygments(self):
|
2016-06-15 19:09:40 +00:00
|
|
|
with self.assertRaises(ClassNotFound):
|
|
|
|
get_lexer_by_name(None)
|
|
|
|
|
2016-10-16 08:50:40 +00:00
|
|
|
def test_classnotfound_error_suppressed_when_parsing_dependencies(self):
|
2017-11-23 02:34:15 +00:00
|
|
|
with mock.patch('wakatime.stats.guess_lexer_using_filename') as mock_guess:
|
2017-11-09 06:54:33 +00:00
|
|
|
mock_guess.return_value = (None, None)
|
2016-06-15 19:09:40 +00:00
|
|
|
|
2017-11-23 02:34:15 +00:00
|
|
|
with mock.patch('wakatime.stats.get_filetype_from_buffer') as mock_filetype:
|
2017-11-09 06:54:33 +00:00
|
|
|
mock_filetype.return_value = 'foo'
|
2016-06-15 19:09:40 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[],
|
|
|
|
expected_language=None,
|
|
|
|
expected_lines=37,
|
|
|
|
entity='python.py',
|
|
|
|
)
|
2016-06-15 19:09:40 +00:00
|
|
|
|
2018-03-11 20:29:30 +00:00
|
|
|
def test_dependencies_still_detected_when_alternate_language_used(self):
|
|
|
|
with mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer:
|
|
|
|
mock_guess_lexer.return_value = None
|
|
|
|
|
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
|
|
|
'app',
|
|
|
|
'django',
|
|
|
|
'flask',
|
|
|
|
'jinja',
|
|
|
|
'mock',
|
|
|
|
'pygments',
|
|
|
|
'simplejson',
|
|
|
|
'sqlalchemy',
|
|
|
|
'unittest',
|
|
|
|
],
|
|
|
|
expected_language='Python',
|
|
|
|
expected_lines=37,
|
|
|
|
entity='python.py',
|
|
|
|
extra_args=['--alternate-language', 'PYTHON'],
|
|
|
|
)
|
|
|
|
|
2015-09-16 19:57:49 +00:00
|
|
|
def test_python_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'app',
|
|
|
|
'django',
|
|
|
|
'flask',
|
|
|
|
'jinja',
|
|
|
|
'mock',
|
|
|
|
'pygments',
|
|
|
|
'simplejson',
|
|
|
|
'sqlalchemy',
|
|
|
|
'unittest',
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
|
|
|
expected_language='Python',
|
|
|
|
expected_lines=37,
|
|
|
|
entity='python.py',
|
|
|
|
)
|
2015-09-08 22:48:28 +00:00
|
|
|
|
2015-09-16 20:00:44 +00:00
|
|
|
def test_bower_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
|
|
|
'bootstrap',
|
|
|
|
'bootstrap-daterangepicker',
|
|
|
|
'moment',
|
|
|
|
'moment-timezone',
|
|
|
|
'bower',
|
|
|
|
'animate.css',
|
|
|
|
],
|
|
|
|
expected_language='JSON',
|
|
|
|
expected_lines=11,
|
|
|
|
entity='bower.json',
|
|
|
|
)
|
2016-08-31 10:41:07 +00:00
|
|
|
|
|
|
|
def test_grunt_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
|
|
|
'grunt',
|
|
|
|
],
|
|
|
|
expected_language=None,
|
|
|
|
expected_lines=23,
|
|
|
|
entity='Gruntfile',
|
|
|
|
)
|
2015-09-16 21:59:30 +00:00
|
|
|
|
|
|
|
def test_java_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'colorfulwolf.webcamapplet',
|
|
|
|
'foobar',
|
2017-11-23 17:48:14 +00:00
|
|
|
'googlecode.javacv',
|
|
|
|
'apackage.something',
|
|
|
|
'anamespace.other',
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
|
|
|
expected_language='Java',
|
2017-11-23 17:48:14 +00:00
|
|
|
expected_lines=22,
|
2017-11-09 06:54:33 +00:00
|
|
|
entity='java.java',
|
|
|
|
)
|
2015-09-26 20:04:35 +00:00
|
|
|
|
|
|
|
def test_c_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'openssl',
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
|
|
|
expected_language='C',
|
|
|
|
expected_lines=8,
|
|
|
|
entity='c_only/non_empty.c',
|
|
|
|
)
|
2015-09-26 20:04:35 +00:00
|
|
|
|
|
|
|
def test_cpp_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'openssl',
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
|
|
|
expected_language='C++',
|
|
|
|
expected_lines=8,
|
|
|
|
entity='c_and_cpp/non_empty.cpp',
|
|
|
|
)
|
2015-09-30 03:42:47 +00:00
|
|
|
|
|
|
|
def test_csharp_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'Proper',
|
|
|
|
'Fart',
|
|
|
|
'Math',
|
|
|
|
'WakaTime',
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
|
|
|
expected_language='C#',
|
|
|
|
expected_lines=18,
|
|
|
|
entity='csharp/seesharp.cs',
|
|
|
|
)
|
2015-09-30 05:00:17 +00:00
|
|
|
|
|
|
|
def test_php_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'Interop',
|
|
|
|
'FooBarOne',
|
|
|
|
'FooBarTwo',
|
|
|
|
'FooBarThree',
|
|
|
|
'FooBarFour',
|
|
|
|
'FooBarSeven',
|
|
|
|
'FooBarEight',
|
|
|
|
'ArrayObject',
|
|
|
|
"'ServiceLocator.php'",
|
|
|
|
"'ServiceLocatorTwo.php'",
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
|
|
|
expected_language='PHP',
|
|
|
|
expected_lines=116,
|
|
|
|
entity='php.php',
|
|
|
|
)
|
2015-09-30 05:00:17 +00:00
|
|
|
|
|
|
|
def test_php_in_html_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"',
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
2018-03-11 20:29:30 +00:00
|
|
|
expected_language='HTML',
|
2017-11-09 06:54:33 +00:00
|
|
|
expected_lines=22,
|
|
|
|
entity='html-with-php.html',
|
|
|
|
)
|
2016-01-03 03:35:52 +00:00
|
|
|
|
2016-05-21 12:21:55 +00:00
|
|
|
def test_html_django_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'"libs/json2.js"',
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
2018-03-11 20:29:30 +00:00
|
|
|
expected_language='HTML',
|
2017-11-09 06:54:33 +00:00
|
|
|
expected_lines=40,
|
|
|
|
entity='html-django.html',
|
|
|
|
)
|
2016-05-21 12:21:55 +00:00
|
|
|
|
2016-01-03 03:35:52 +00:00
|
|
|
def test_go_dependencies_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
2016-06-08 18:33:36 +00:00
|
|
|
'"compress/gzip"',
|
|
|
|
'"direct"',
|
|
|
|
'"foobar"',
|
|
|
|
'"github.com/golang/example/stringutil"',
|
|
|
|
'"image/gif"',
|
|
|
|
'"log"',
|
|
|
|
'"math"',
|
|
|
|
'"oldname"',
|
|
|
|
'"os"',
|
|
|
|
'"supress"',
|
2017-11-09 06:54:33 +00:00
|
|
|
],
|
|
|
|
expected_language='Go',
|
|
|
|
expected_lines=24,
|
|
|
|
entity='go.go',
|
|
|
|
)
|
2017-02-20 22:23:02 +00:00
|
|
|
|
2018-03-11 20:29:30 +00:00
|
|
|
def test_es6_dependencies_detected(self):
|
|
|
|
self.shared(
|
|
|
|
expected_dependencies=[
|
|
|
|
'bravo',
|
|
|
|
'foxtrot',
|
|
|
|
'india',
|
|
|
|
'kilo',
|
|
|
|
'november',
|
|
|
|
'oscar',
|
|
|
|
'quebec',
|
|
|
|
'tango',
|
|
|
|
'uniform',
|
|
|
|
'victor',
|
|
|
|
'whiskey',
|
|
|
|
],
|
|
|
|
expected_language='JavaScript',
|
|
|
|
expected_lines=37,
|
|
|
|
entity='es6.js',
|
|
|
|
)
|