Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/test_fossology_license.py
Show All 19 Lines | from swh.indexer.tests.utils import ( | ||||
BASE_TEST_CONFIG, | BASE_TEST_CONFIG, | ||||
RAW_CONTENT_IDS, | RAW_CONTENT_IDS, | ||||
SHA1_TO_LICENSES, | SHA1_TO_LICENSES, | ||||
CommonContentIndexerPartitionTest, | CommonContentIndexerPartitionTest, | ||||
CommonContentIndexerTest, | CommonContentIndexerTest, | ||||
fill_obj_storage, | fill_obj_storage, | ||||
fill_storage, | fill_storage, | ||||
filter_dict, | filter_dict, | ||||
mock_compute_license, | |||||
) | ) | ||||
from swh.model.hashutil import hash_to_bytes | |||||
class BasicTest(unittest.TestCase): | class BasicTest(unittest.TestCase): | ||||
@patch("swh.indexer.fossology_license.subprocess") | @patch("swh.indexer.fossology_license.subprocess") | ||||
def test_compute_license(self, mock_subprocess): | def test_compute_license(self, mock_subprocess): | ||||
"""Computing licenses from a raw content should return results""" | """Computing licenses from a raw content should return results""" | ||||
for path, intermediary_result, output in [ | for path, intermediary_result, output in [ | ||||
(b"some/path", None, []), | (b"some/path", None, []), | ||||
(b"some/path/2", [], []), | (b"some/path/2", [], []), | ||||
(b"other/path", " contains license(s) GPL,AGPL", ["GPL", "AGPL"]), | (b"other/path", " contains license(s) GPL,AGPL", ["GPL", "AGPL"]), | ||||
]: | ]: | ||||
mock_subprocess.check_output.return_value = intermediary_result | mock_subprocess.check_output.return_value = intermediary_result | ||||
actual_result = compute_license(path) | actual_result = compute_license(path) | ||||
self.assertEqual( | self.assertEqual( | ||||
actual_result, | actual_result, | ||||
{ | { | ||||
"licenses": output, | "licenses": output, | ||||
"path": path, | "path": path, | ||||
}, | }, | ||||
) | ) | ||||
def mock_compute_license(path): | |||||
"""path is the content identifier""" | |||||
if isinstance(id, bytes): | |||||
path = path.decode("utf-8") | |||||
# path is something like /tmp/tmpXXX/<sha1> so we keep only the sha1 part | |||||
id_ = path.split("/")[-1] | |||||
return {"licenses": SHA1_TO_LICENSES.get(hash_to_bytes(id_), [])} | |||||
CONFIG = { | CONFIG = { | ||||
**BASE_TEST_CONFIG, | **BASE_TEST_CONFIG, | ||||
"workdir": "/tmp", | "workdir": "/tmp", | ||||
"tools": { | "tools": { | ||||
"name": "nomos", | "name": "nomos", | ||||
"version": "3.1.0rc2-31-ga2cbb8c", | "version": "3.1.0rc2-31-ga2cbb8c", | ||||
"configuration": { | "configuration": { | ||||
"command_line": "nomossa <filepath>", | "command_line": "nomossa <filepath>", | ||||
▲ Show 20 Lines • Show All 89 Lines • Show Last 20 Lines |