Support language argument for non file types

This commit is contained in:
Alan Hamlett 2018-12-05 21:32:53 -08:00
parent 1d2318a937
commit ae834f679f
3 changed files with 54 additions and 33 deletions

View file

@ -25,14 +25,15 @@ class LanguagesTestCase(utils.TestCase):
['wakatime.session_cache.SessionCache.connect', None],
]
def shared(self, expected_language='', entity='', extra_args=[]):
def shared(self, expected_language='', entity='', entity_type='file', extra_args=[]):
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = CustomResponse()
config = 'tests/samples/configs/good_config.cfg'
entity = os.path.join('tests/samples/codefiles', entity)
if entity_type == 'file':
entity = os.path.join('tests/samples/codefiles', entity)
now = u(int(time.time()))
args = ['--file', entity, '--config', config, '--time', now] + extra_args
args = ['--entity', entity, '--config', config, '--time', now] + extra_args
retval = execute(args)
self.assertEquals(retval, SUCCESS)
@ -41,12 +42,12 @@ class LanguagesTestCase(utils.TestCase):
heartbeat = {
'language': expected_language,
'lines': ANY,
'entity': os.path.realpath(entity),
'entity': os.path.realpath(entity) if entity_type == 'file' else entity,
'project': ANY,
'branch': ANY,
'dependencies': ANY,
'time': float(now),
'type': 'file',
'type': entity_type,
'is_write': False,
'user_agent': ANY,
}
@ -124,7 +125,7 @@ class LanguagesTestCase(utils.TestCase):
self.shared(
expected_language='Java',
entity='python.py',
extra_args=['--language', 'JAVA']
extra_args=['--language', 'JAVA'],
)
def test_language_arg_is_used_when_not_guessed(self):
@ -137,6 +138,30 @@ class LanguagesTestCase(utils.TestCase):
extra_args=['--language', 'JAVA']
)
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'],
)
def test_vim_language_arg_is_used_when_not_guessed(self):
with utils.mock.patch('wakatime.stats.smart_guess_lexer') as mock_guess_lexer:
mock_guess_lexer.return_value = None

View file

@ -724,7 +724,7 @@ class OfflineQueueTestCase(TestCase):
args = ['--file', entity, '--key', key, '--config', config, '--time', now]
with mock.patch('wakatime.stats.standardize_language') as mock_language:
mock_language.return_value = (language, None)
mock_language.return_value = language
with mock.patch('wakatime.heartbeat.get_project_info') as mock_project:
mock_project.return_value = (project, branch)

View file

@ -41,31 +41,28 @@ log = logging.getLogger('WakaTime')
def get_file_stats(file_name, entity_type='file', lineno=None, cursorpos=None,
plugin=None, language=None, local_file=None):
if entity_type != 'file':
stats = {
'language': None,
'dependencies': [],
'lines': None,
'lineno': lineno,
'cursorpos': cursorpos,
}
else:
language, lexer = standardize_language(language, plugin)
"""Returns a hash of information about the entity."""
language = standardize_language(language, plugin)
stats = {
'language': language,
'dependencies': [],
'lines': None,
'lineno': lineno,
'cursorpos': cursorpos,
}
if entity_type == 'file':
lexer = get_lexer(language)
if not language:
language, lexer = guess_language(file_name, local_file)
language = use_root_language(language, lexer)
parser = DependencyParser(local_file or file_name, lexer)
dependencies = parser.parse()
stats = {
'language': language,
'dependencies': dependencies,
stats.update({
'language': use_root_language(language, lexer),
'dependencies': parser.parse(),
'lines': number_lines_in_file(local_file or file_name),
'lineno': lineno,
'cursorpos': cursorpos,
}
})
return stats
@ -222,22 +219,21 @@ def number_lines_in_file(file_name):
def standardize_language(language, plugin):
"""Maps a string to the equivalent Pygments language.
Returns a tuple of (language_str, lexer_obj).
Returns the standardized language string.
"""
if not language:
return None, None
return None
# standardize language for this plugin
if plugin:
plugin = plugin.split(' ')[-1].split('/')[0].split('-')[0]
standardized = get_language_from_json(language, plugin)
if standardized is not None:
return standardized, get_lexer(standardized)
return standardized
# standardize language against default languages
standardized = get_language_from_json(language, 'default')
return standardized, get_lexer(standardized)
return get_language_from_json(language, 'default')
def get_lexer(language):