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

View file

@ -724,7 +724,7 @@ class OfflineQueueTestCase(TestCase):
args = ['--file', entity, '--key', key, '--config', config, '--time', now] args = ['--file', entity, '--key', key, '--config', config, '--time', now]
with mock.patch('wakatime.stats.standardize_language') as mock_language: 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: with mock.patch('wakatime.heartbeat.get_project_info') as mock_project:
mock_project.return_value = (project, branch) 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, def get_file_stats(file_name, entity_type='file', lineno=None, cursorpos=None,
plugin=None, language=None, local_file=None): plugin=None, language=None, local_file=None):
if entity_type != 'file': """Returns a hash of information about the entity."""
stats = {
'language': None, language = standardize_language(language, plugin)
'dependencies': [], stats = {
'lines': None, 'language': language,
'lineno': lineno, 'dependencies': [],
'cursorpos': cursorpos, 'lines': None,
} 'lineno': lineno,
else: 'cursorpos': cursorpos,
language, lexer = standardize_language(language, plugin) }
if entity_type == 'file':
lexer = get_lexer(language)
if not language: if not language:
language, lexer = guess_language(file_name, local_file) language, lexer = guess_language(file_name, local_file)
language = use_root_language(language, lexer)
parser = DependencyParser(local_file or file_name, lexer) parser = DependencyParser(local_file or file_name, lexer)
dependencies = parser.parse() stats.update({
'language': use_root_language(language, lexer),
stats = { 'dependencies': parser.parse(),
'language': language,
'dependencies': dependencies,
'lines': number_lines_in_file(local_file or file_name), 'lines': number_lines_in_file(local_file or file_name),
'lineno': lineno, })
'cursorpos': cursorpos,
}
return stats return stats
@ -222,22 +219,21 @@ def number_lines_in_file(file_name):
def standardize_language(language, plugin): def standardize_language(language, plugin):
"""Maps a string to the equivalent Pygments language. """Maps a string to the equivalent Pygments language.
Returns a tuple of (language_str, lexer_obj). Returns the standardized language string.
""" """
if not language: if not language:
return None, None return None
# standardize language for this plugin # standardize language for this plugin
if plugin: if plugin:
plugin = plugin.split(' ')[-1].split('/')[0].split('-')[0] plugin = plugin.split(' ')[-1].split('/')[0].split('-')[0]
standardized = get_language_from_json(language, plugin) standardized = get_language_from_json(language, plugin)
if standardized is not None: if standardized is not None:
return standardized, get_lexer(standardized) return standardized
# standardize language against default languages # standardize language against default languages
standardized = get_language_from_json(language, 'default') return get_language_from_json(language, 'default')
return standardized, get_lexer(standardized)
def get_lexer(language): def get_lexer(language):