2015-08-12 22:03:39 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
2015-08-29 23:16:14 +00:00
|
|
|
from wakatime.main import execute
|
2015-08-12 22:03:39 +00:00
|
|
|
from wakatime.packages import requests
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
import os
|
2015-08-12 22:03:39 +00:00
|
|
|
import time
|
|
|
|
from wakatime.compat import u
|
2017-11-09 06:54:33 +00:00
|
|
|
from wakatime.constants import SUCCESS
|
2015-09-29 09:24:51 +00:00
|
|
|
from wakatime.stats import guess_language
|
2015-08-12 22:03:39 +00:00
|
|
|
from . import utils
|
2017-11-23 02:34:15 +00:00
|
|
|
from .utils import ANY, CustomResponse
|
2015-08-12 22:03:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LanguagesTestCase(utils.TestCase):
|
|
|
|
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-08-12 22:03:39 +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-08-12 22:03:39 +00:00
|
|
|
]
|
|
|
|
|
2018-12-06 05:32:53 +00:00
|
|
|
def shared(self, expected_language='', entity='', entity_type='file', extra_args=[]):
|
2017-11-23 02:34:15 +00:00
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
|
2015-09-07 03:06:36 +00:00
|
|
|
|
2015-09-30 05:34:14 +00:00
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
2018-12-06 05:32:53 +00:00
|
|
|
if entity_type == 'file':
|
|
|
|
entity = os.path.join('tests/samples/codefiles', entity)
|
2015-09-08 21:06:10 +00:00
|
|
|
|
|
|
|
now = u(int(time.time()))
|
2018-12-06 05:32:53 +00:00
|
|
|
args = ['--entity', entity, '--config', config, '--time', now] + extra_args
|
2015-09-08 21:06:10 +00:00
|
|
|
|
|
|
|
retval = execute(args)
|
2017-11-09 06:54:33 +00:00
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
|
|
|
|
heartbeat = {
|
|
|
|
'language': expected_language,
|
|
|
|
'lines': ANY,
|
2018-12-06 05:32:53 +00:00
|
|
|
'entity': os.path.realpath(entity) if entity_type == 'file' else entity,
|
2017-11-09 06:54:33 +00:00
|
|
|
'project': ANY,
|
|
|
|
'branch': ANY,
|
|
|
|
'dependencies': ANY,
|
|
|
|
'time': float(now),
|
2018-12-06 05:32:53 +00:00
|
|
|
'type': entity_type,
|
2017-11-09 06:54:33 +00:00
|
|
|
'is_write': False,
|
|
|
|
'user_agent': ANY,
|
|
|
|
}
|
|
|
|
self.assertHeartbeatSent(heartbeat)
|
|
|
|
|
|
|
|
self.assertHeartbeatNotSavedOffline()
|
|
|
|
self.assertOfflineHeartbeatsSynced()
|
|
|
|
self.assertSessionCacheSaved()
|
2015-09-08 21:06:10 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
def test_c_language_detected_for_header_with_c_files_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='C',
|
|
|
|
entity='c_only/see.h',
|
|
|
|
)
|
2015-09-08 21:06:10 +00:00
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
def test_cpp_language_detected_for_header_with_c_and_cpp_files_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='C++',
|
2018-03-15 08:44:38 +00:00
|
|
|
entity='c_and_cpp/cpp.h',
|
2017-11-09 06:54:33 +00:00
|
|
|
)
|
2015-09-08 21:06:10 +00:00
|
|
|
|
2019-03-11 02:20:23 +00:00
|
|
|
def test_cpp_language_detected_for_header_with_c_and_cxx_files_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='C++',
|
|
|
|
entity='c_and_cxx/cpp.h',
|
|
|
|
)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
def test_c_not_detected_for_non_header_with_c_files_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Python',
|
|
|
|
entity='c_and_python/see.py',
|
|
|
|
)
|
2015-09-29 09:24:51 +00:00
|
|
|
|
2018-03-15 08:15:16 +00:00
|
|
|
def test_objectivec_language_detected_when_header_files_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Objective-C',
|
|
|
|
entity='c_and_cpp/empty.m',
|
|
|
|
)
|
|
|
|
|
2018-03-15 08:44:38 +00:00
|
|
|
def test_objectivec_language_detected_when_m_files_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Objective-C',
|
|
|
|
entity='c_and_cpp/objective-c.h',
|
|
|
|
)
|
|
|
|
|
2018-03-15 08:15:16 +00:00
|
|
|
def test_objectivecpp_language_detected_when_header_files_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Objective-C++',
|
|
|
|
entity='c_and_cpp/empty.mm',
|
|
|
|
)
|
|
|
|
|
2018-03-15 08:44:38 +00:00
|
|
|
def test_objectivecpp_language_detected_when_m_files_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Objective-C++',
|
|
|
|
entity='c_and_cpp/objective-cpp.h',
|
|
|
|
)
|
|
|
|
|
2015-09-29 09:24:51 +00:00
|
|
|
def test_guess_language(self):
|
|
|
|
with utils.mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer:
|
|
|
|
mock_guess_lexer.return_value = None
|
|
|
|
source_file = 'tests/samples/codefiles/python.py'
|
2018-09-01 01:15:00 +00:00
|
|
|
local_file = None
|
|
|
|
result = guess_language(source_file, local_file)
|
|
|
|
mock_guess_lexer.assert_called_once_with(source_file, local_file)
|
2015-09-29 09:24:51 +00:00
|
|
|
self.assertEquals(result, (None, None))
|
2016-04-18 15:00:42 +00:00
|
|
|
|
|
|
|
def test_guess_language_from_vim_modeline(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language='Python',
|
|
|
|
entity='python_without_extension',
|
|
|
|
)
|
|
|
|
|
2018-09-01 01:15:00 +00:00
|
|
|
def test_guess_language_when_entity_not_exist_but_local_file_exists(self):
|
|
|
|
source_file = 'tests/samples/codefiles/does_not_exist.py'
|
|
|
|
local_file = 'tests/samples/codefiles/python.py'
|
|
|
|
self.assertFalse(os.path.exists(source_file))
|
|
|
|
result = guess_language(source_file, local_file)
|
|
|
|
self.assertEquals(result[0], 'Python')
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
def test_language_arg_takes_priority_over_detected_language(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Java',
|
|
|
|
entity='python.py',
|
2018-12-06 05:32:53 +00:00
|
|
|
extra_args=['--language', 'JAVA'],
|
2017-11-09 06:54:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_language_arg_is_used_when_not_guessed(self):
|
2016-04-19 10:30:44 +00:00
|
|
|
with utils.mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer:
|
|
|
|
mock_guess_lexer.return_value = None
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language='Java',
|
|
|
|
entity='python.py',
|
|
|
|
extra_args=['--language', 'JAVA']
|
|
|
|
)
|
2016-04-19 10:30:44 +00:00
|
|
|
|
2018-12-06 05:32:53 +00:00
|
|
|
def test_language_defaults_to_none_for_entity_type_app(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language=None,
|
|
|
|
entity='not-a-file',
|
|
|
|
entity_type='domain',
|
|
|
|
extra_args=['--entity-type', 'domain'],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_language_arg_used_for_entity_type_app(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Java',
|
|
|
|
entity='not-a-file',
|
|
|
|
entity_type='app',
|
|
|
|
extra_args=['--entity-type', 'app', '--language', 'JAVA'],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_language_arg_used_for_entity_type_domain(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Java',
|
|
|
|
entity='not-a-file',
|
|
|
|
entity_type='domain',
|
|
|
|
extra_args=['--entity-type', 'domain', '--language', 'JAVA'],
|
|
|
|
)
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
def test_vim_language_arg_is_used_when_not_guessed(self):
|
2016-04-19 10:30:44 +00:00
|
|
|
with utils.mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer:
|
|
|
|
mock_guess_lexer.return_value = None
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language='Java',
|
|
|
|
entity='python.py',
|
|
|
|
extra_args=['--language', 'java', '--plugin', 'NeoVim/703 vim-wakatime/4.0.9']
|
|
|
|
)
|
2016-04-28 17:25:52 +00:00
|
|
|
|
|
|
|
def test_alternate_language_not_used_when_invalid(self):
|
|
|
|
with utils.mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer:
|
|
|
|
mock_guess_lexer.return_value = None
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language=None,
|
|
|
|
entity='python.py',
|
|
|
|
extra_args=['--language', 'foo', '--plugin', 'NeoVim/703 vim-wakatime/4.0.9']
|
|
|
|
)
|
2016-04-28 17:25:52 +00:00
|
|
|
|
|
|
|
def test_error_reading_alternate_language_json_map_file(self):
|
|
|
|
with utils.mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer:
|
|
|
|
mock_guess_lexer.return_value = None
|
|
|
|
|
|
|
|
with utils.mock.patch('wakatime.stats.open') as mock_open:
|
|
|
|
mock_open.side_effect = IOError('')
|
|
|
|
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language=None,
|
|
|
|
entity='python.py',
|
|
|
|
extra_args=['--language', 'foo', '--plugin', 'NeoVim/703 vim-wakatime/4.0.9']
|
|
|
|
)
|
2017-02-20 21:22:14 +00:00
|
|
|
|
2017-02-27 01:05:55 +00:00
|
|
|
def test_typescript_detected_over_typoscript(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language='TypeScript',
|
2018-03-11 20:29:30 +00:00
|
|
|
entity='empty.ts',
|
2017-11-09 06:54:33 +00:00
|
|
|
extra_args=['--language', 'foo', '--plugin', 'NeoVim/703 vim-wakatime/4.0.9']
|
|
|
|
)
|
2017-02-27 01:05:55 +00:00
|
|
|
|
|
|
|
def test_perl_detected_over_prolog(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language='Perl',
|
|
|
|
entity='perl.pl',
|
|
|
|
)
|
2017-03-04 21:42:44 +00:00
|
|
|
|
|
|
|
def test_fsharp_detected_over_forth(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language='F#',
|
|
|
|
entity='fsharp.fs',
|
|
|
|
)
|
2017-06-08 06:32:03 +00:00
|
|
|
|
|
|
|
def test_matlab_detected(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language='Matlab',
|
|
|
|
entity='matlab/matlab.m',
|
|
|
|
)
|
2017-06-08 06:32:03 +00:00
|
|
|
|
|
|
|
def test_matlab_detected_over_objectivec_when_mat_file_in_folder(self):
|
2017-11-09 06:54:33 +00:00
|
|
|
self.shared(
|
|
|
|
expected_language='Matlab',
|
|
|
|
entity='matlab/with_mat_files/empty.m',
|
|
|
|
)
|
2018-03-11 20:47:37 +00:00
|
|
|
|
2018-03-15 08:15:16 +00:00
|
|
|
def test_objectivec_detected_over_matlab_with_matching_header(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Objective-C',
|
|
|
|
entity='matlab/with_mat_files/objective-c.m',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_objectivec_detected_over_matlab_with_non_maching_headers_present(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Objective-C',
|
|
|
|
entity='matlab/with_headers/empty.m',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_matlab_detected_over_objectivec_when_header_in_folder(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Matlab',
|
|
|
|
entity='matlab/with_headers/matlab.m',
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_heartbeat_skipped_when_matlab_same_accuracy(self):
|
|
|
|
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
|
|
|
|
|
|
|
|
entity = 'matlab/without_headers/empty.m'
|
|
|
|
|
|
|
|
config = 'tests/samples/configs/good_config.cfg'
|
|
|
|
entity = os.path.join('tests/samples/codefiles', entity)
|
|
|
|
|
|
|
|
now = u(int(time.time()))
|
|
|
|
args = ['--file', entity, '--config', config, '--time', now]
|
|
|
|
|
|
|
|
retval = execute(args)
|
|
|
|
self.assertEquals(retval, SUCCESS)
|
|
|
|
self.assertNothingPrinted()
|
|
|
|
self.assertHeartbeatNotSent()
|
|
|
|
self.assertHeartbeatNotSavedOffline()
|
|
|
|
self.assertOfflineHeartbeatsSynced()
|
|
|
|
self.assertSessionCacheUntouched()
|
|
|
|
|
2018-03-11 20:47:37 +00:00
|
|
|
def test_mjs_javascript_module_extension_detected(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='JavaScript',
|
|
|
|
entity='javascript_module.mjs',
|
|
|
|
)
|
2019-03-28 00:31:21 +00:00
|
|
|
|
|
|
|
def test_go_mod_detected(self):
|
|
|
|
self.shared(
|
|
|
|
expected_language='Go',
|
|
|
|
entity='go.mod',
|
|
|
|
)
|