rana-cli/tests/test_dependencies.py

507 lines
15 KiB
Python
Raw Normal View History

2015-09-08 22:48:28 +00:00
# -*- coding: utf-8 -*-
from wakatime.main import execute
from wakatime.packages import requests
import logging
2015-09-08 22:48:28 +00:00
import os
import time
import shutil
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
from wakatime.exceptions import NotYetImplemented
from wakatime.dependencies import DependencyParser, TokenParser
from wakatime.packages.pygments.lexers import ClassNotFound, PythonLexer
from wakatime.stats import get_lexer_by_name
from .utils import mock, ANY, CustomResponse, TemporaryDirectory, TestCase
2015-09-08 22:48:28 +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],
['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],
['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=[]):
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)
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()
def test_token_parser(self):
with self.assertRaises(NotYetImplemented):
source_file = 'tests/samples/codefiles/c_only/non_empty.h'
parser = TokenParser(source_file)
parser.parse()
with mock.patch('wakatime.dependencies.TokenParser._extract_tokens') as mock_extract_tokens:
source_file = 'tests/samples/codefiles/see.h'
parser = TokenParser(source_file)
parser.tokens
mock_extract_tokens.assert_called_once_with()
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)
@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()
expected = []
self.assertEquals(dependencies, expected)
@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()
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)
with mock.patch('wakatime.dependencies.import_module') as mock_import:
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()
expected = []
self.assertEquals(dependencies, expected)
def test_io_error_suppressed_when_parsing_dependencies(self):
with mock.patch('wakatime.dependencies.open') as mock_open:
2017-11-09 06:54:33 +00:00
mock_open.side_effect = IOError('')
2017-11-09 06:54:33 +00:00
self.shared(
expected_dependencies=[],
expected_language='Python',
expected_lines=37,
entity='python.py',
)
def test_classnotfound_error_raised_when_passing_none_to_pygments(self):
with self.assertRaises(ClassNotFound):
get_lexer_by_name(None)
def test_classnotfound_error_suppressed_when_parsing_dependencies(self):
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)
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'
2017-11-09 06:54:33 +00:00
self.shared(
expected_dependencies=[],
expected_language=None,
expected_lines=37,
entity='python.py',
)
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=[
'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=[
'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',
)
def test_c_dependencies_detected(self):
2017-11-09 06:54:33 +00:00
self.shared(
expected_dependencies=[
'openssl',
2017-11-09 06:54:33 +00:00
],
expected_language='C',
expected_lines=8,
entity='c_only/non_empty.c',
)
def test_cpp_dependencies_detected(self):
2017-11-09 06:54:33 +00:00
self.shared(
expected_dependencies=[
'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=[
'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=[
'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=[
'"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
def test_html_django_dependencies_detected(self):
2017-11-09 06:54:33 +00:00
self.shared(
expected_dependencies=[
'"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-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=[
'"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',
)
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',
)
2018-03-11 20:40:48 +00:00
def test_typescript_dependencies_detected(self):
self.shared(
expected_dependencies=[
'bravo',
'foxtrot',
'india',
'kilo',
'november',
'oscar',
'quebec',
'tango',
'uniform',
'victor',
'whiskey',
],
expected_language='TypeScript',
expected_lines=37,
entity='typescript.ts',
)
def test_swift_dependencies_detected(self):
self.shared(
expected_dependencies=[
'UIKit',
'PromiseKit',
],
expected_language='Swift',
expected_lines=16,
entity='swift.swift',
)
def test_objective_c_dependencies_detected(self):
self.shared(
expected_dependencies=[
'SomeViewController',
'UIKit',
'PromiseKit',
],
expected_language='Objective-C',
expected_lines=18,
entity='objective-c.m',
)
2018-03-12 01:09:25 +00:00
def test_scala_dependencies_detected(self):
self.shared(
expected_dependencies=[
'com.alpha.SomeClass',
'com.bravo.something',
'com.charlie',
'golf',
'com.hotel.india',
'juliett.kilo',
],
expected_language='Scala',
expected_lines=14,
entity='scala.scala',
)
2018-03-13 07:48:40 +00:00
def test_rust_dependencies_detected(self):
self.shared(
expected_dependencies=[
'proc_macro',
'phrases',
'syn',
'quote',
],
expected_language='Rust',
expected_lines=21,
entity='rust.rs',
)
2018-03-13 16:24:37 +00:00
def test_kotlin_dependencies_detected(self):
self.shared(
expected_dependencies=[
'alpha.time',
'bravo.charlie',
'delta.io',
2018-03-13 16:27:51 +00:00
'echo.Foxtrot',
2018-03-13 16:36:51 +00:00
'h',
2018-03-13 16:24:37 +00:00
],
expected_language='Kotlin',
2018-03-13 16:27:51 +00:00
expected_lines=24,
2018-03-13 16:24:37 +00:00
entity='kotlin.kt',
)
2018-03-15 06:35:14 +00:00
def test_haxe_dependencies_detected(self):
self.shared(
expected_dependencies=[
'alpha',
'bravo',
'Math',
'charlie',
'delta',
],
expected_language='Haxe',
expected_lines=18,
entity='haxe.hx',
)
def test_haskell_dependencies_detected(self):
self.shared(
expected_dependencies=[
'Control',
'Data',
'Network',
'System',
],
expected_language='Haskell',
expected_lines=20,
entity='haskell.hs',
)
def test_elm_dependencies_detected(self):
self.shared(
expected_dependencies=[
'Color',
'Dict',
'TempFontAwesome',
'Html',
'Markdown',
'String',
],
expected_language='Elm',
expected_lines=21,
entity='elm.elm',
)