Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/test_ctags.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import json | |||||
import unittest | import unittest | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
import pytest | |||||
from swh.indexer.ctags import ( | from swh.indexer.ctags import ( | ||||
CtagsIndexer, run_ctags | CtagsIndexer, run_ctags | ||||
) | ) | ||||
from swh.indexer.tests.test_utils import ( | from swh.indexer.tests.test_utils import ( | ||||
CommonContentIndexerTest, | CommonContentIndexerTest, | ||||
CommonIndexerWithErrorsTest, CommonIndexerNoTool, | CommonIndexerWithErrorsTest, CommonIndexerNoTool, | ||||
SHA1_TO_CTAGS, NoDiskIndexer, BASE_TEST_CONFIG | SHA1_TO_CTAGS, NoDiskIndexer, BASE_TEST_CONFIG, | ||||
OBJ_STORAGE_DATA, fill_storage, fill_obj_storage | |||||
) | ) | ||||
class BasicTest(unittest.TestCase): | class BasicTest(unittest.TestCase): | ||||
@patch('swh.indexer.ctags.subprocess') | @patch('swh.indexer.ctags.subprocess') | ||||
def test_run_ctags(self, mock_subprocess): | def test_run_ctags(self, mock_subprocess): | ||||
"""Computing licenses from a raw content should return results | """Computing licenses from a raw content should return results | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | class TestCtagsIndexer(CommonContentIndexerTest, unittest.TestCase): | ||||
""" | """ | ||||
def get_indexer_results(self, ids): | def get_indexer_results(self, ids): | ||||
yield from self.idx_storage.content_ctags_get(ids) | yield from self.idx_storage.content_ctags_get(ids) | ||||
def setUp(self): | def setUp(self): | ||||
self.indexer = CtagsIndexerTest() | self.indexer = CtagsIndexerTest() | ||||
self.idx_storage = self.indexer.idx_storage | self.idx_storage = self.indexer.idx_storage | ||||
fill_storage(self.indexer.storage) | |||||
fill_obj_storage(self.indexer.objstorage) | |||||
# Prepare test input | # Prepare test input | ||||
self.id0 = '01c9379dfc33803963d07c1ccc748d3fe4c96bb5' | self.id0 = '01c9379dfc33803963d07c1ccc748d3fe4c96bb5' | ||||
self.id1 = 'd4c647f0fc257591cc9ba1722484229780d1c607' | self.id1 = 'd4c647f0fc257591cc9ba1722484229780d1c607' | ||||
self.id2 = '688a5ef812c53907562fe379d4b3851e69c7cb15' | self.id2 = '688a5ef812c53907562fe379d4b3851e69c7cb15' | ||||
tool_id = self.indexer.tool['id'] | tool = {k.replace('tool_', ''): v | ||||
for (k, v) in self.indexer.tool.items()} | |||||
self.expected_results = { | self.expected_results = { | ||||
self.id0: { | self.id0: { | ||||
'id': self.id0, | 'id': self.id0, | ||||
'indexer_configuration_id': tool_id, | 'tool': tool, | ||||
'ctags': SHA1_TO_CTAGS[self.id0], | **SHA1_TO_CTAGS[self.id0][0], | ||||
}, | }, | ||||
self.id1: { | self.id1: { | ||||
'id': self.id1, | 'id': self.id1, | ||||
'indexer_configuration_id': tool_id, | 'tool': tool, | ||||
'ctags': SHA1_TO_CTAGS[self.id1], | **SHA1_TO_CTAGS[self.id1][0], | ||||
}, | }, | ||||
self.id2: { | self.id2: { | ||||
'id': self.id2, | 'id': self.id2, | ||||
'indexer_configuration_id': tool_id, | 'tool': tool, | ||||
'ctags': SHA1_TO_CTAGS[self.id2], | **SHA1_TO_CTAGS[self.id2][0], | ||||
} | } | ||||
} | } | ||||
def _set_mocks(self, mock_subprocess, mock_compute_language): | |||||
def find_ctags_for_content(raw_content): | |||||
for (sha1, ctags) in SHA1_TO_CTAGS.items(): | |||||
if OBJ_STORAGE_DATA[sha1] == raw_content: | |||||
return ctags | |||||
else: | |||||
raise ValueError(('%r not found in objstorage, can\'t mock ' | |||||
'its ctags.') % raw_content) | |||||
def fake_language(raw_content, *args, **kwargs): | |||||
ctags = find_ctags_for_content(raw_content) | |||||
return {'lang': ctags[0]['lang']} | |||||
mock_compute_language.side_effect = fake_language | |||||
def fake_subprocess(cmd, *args, **kwargs): | |||||
print(cmd) | |||||
id_ = cmd[-1] # when using NoDiskIndexer, path is replaced by id | |||||
return '\n'.join( | |||||
json.dumps({'language': ctag['lang'], **ctag}) | |||||
for ctag in SHA1_TO_CTAGS[id_]) | |||||
mock_subprocess.check_output.side_effect = fake_subprocess | |||||
@pytest.mark.fs | |||||
@patch('swh.indexer.ctags.compute_language') | |||||
@patch('swh.indexer.ctags.subprocess') | |||||
def test_index(self, mock_subprocess, mock_compute_language): | |||||
self._set_mocks(mock_subprocess, mock_compute_language) | |||||
super().test_index() | |||||
@pytest.mark.fs | |||||
@patch('swh.indexer.ctags.compute_language') | |||||
@patch('swh.indexer.ctags.subprocess') | |||||
def test_index_one_unknown_sha1( | |||||
self, mock_subprocess, mock_compute_language): | |||||
self._set_mocks(mock_subprocess, mock_compute_language) | |||||
super().test_index() | |||||
class CtagsIndexerUnknownToolTestStorage( | class CtagsIndexerUnknownToolTestStorage( | ||||
CommonIndexerNoTool, CtagsIndexerTest): | CommonIndexerNoTool, CtagsIndexerTest): | ||||
"""Fossology license indexer with wrong configuration""" | """Fossology license indexer with wrong configuration""" | ||||
class TestCtagsIndexersErrors( | class TestCtagsIndexersErrors( | ||||
CommonIndexerWithErrorsTest, unittest.TestCase): | CommonIndexerWithErrorsTest, unittest.TestCase): | ||||
"""Test the indexer raise the right errors when wrongly initialized""" | """Test the indexer raise the right errors when wrongly initialized""" | ||||
Indexer = CtagsIndexerUnknownToolTestStorage | Indexer = CtagsIndexerUnknownToolTestStorage |