diff --git a/swh/indexer/metadata_dictionary.py b/swh/indexer/metadata_dictionary.py --- a/swh/indexer/metadata_dictionary.py +++ b/swh/indexer/metadata_dictionary.py @@ -41,7 +41,11 @@ >>> merge_values({'@list': ['a', 'b']}, {'@list': ['c']}) {'@list': ['a', 'b', 'c']} """ - if isinstance(v1, dict) and set(v1) == {'@list'}: + if v1 is None: + return v2 + elif v2 is None: + return v1 + elif isinstance(v1, dict) and set(v1) == {'@list'}: assert isinstance(v1['@list'], list) if isinstance(v2, dict) and set(v2) == {'@list'}: assert isinstance(v2['@list'], list) @@ -325,7 +329,8 @@ >>> NpmMapping().normalize_homepage('https://example.org/~john.doe') {'@id': 'https://example.org/~john.doe'} """ - return {"@id": s} + if isinstance(s, str): + return {"@id": s} @register_mapping @@ -591,7 +596,8 @@ return evaluator(tree.body) def normalize_homepage(self, s): - return {"@id": s} + if isinstance(s, str): + return {"@id": s} def normalize_license(self, s): if isinstance(s, str): @@ -604,10 +610,13 @@ if isinstance(license, str)] def normalize_author(self, author): - return {"@list": [author]} + if isinstance(author, str): + return {"@list": [author]} def normalize_authors(self, authors): - return {"@list": authors} + if isinstance(authors, list): + return {"@list": [author for author in authors + if isinstance(author, str)]} def main(): diff --git a/swh/indexer/tests/test_metadata.py b/swh/indexer/tests/test_metadata.py --- a/swh/indexer/tests/test_metadata.py +++ b/swh/indexer/tests/test_metadata.py @@ -85,12 +85,14 @@ self.assertEqual( merge_values('a', ['b', 'c']), ['a', 'b', 'c']) + self.assertEqual( merge_values({'@list': ['a']}, {'@list': ['b']}), {'@list': ['a', 'b']}) self.assertEqual( merge_values({'@list': ['a', 'b']}, {'@list': ['c']}), {'@list': ['a', 'b', 'c']}) + with self.assertRaises(ValueError): merge_values({'@list': ['a']}, 'b') with self.assertRaises(ValueError): @@ -100,6 +102,22 @@ with self.assertRaises(ValueError): merge_values(['a'], {'@list': ['b']}) + self.assertEqual( + merge_values('a', None), + 'a') + self.assertEqual( + merge_values(['a', 'b'], None), + ['a', 'b']) + self.assertEqual( + merge_values(None, ['b', 'c']), + ['b', 'c']) + self.assertEqual( + merge_values({'@list': ['a']}, None), + {'@list': ['a']}) + self.assertEqual( + merge_values(None, {'@list': ['a']}), + {'@list': ['a']}) + def test_compute_metadata_none(self): """ testing content empty content is empty @@ -808,6 +826,15 @@ def test_gemspec_invalid_author(self): raw_content = b""" Gem::Specification.new do |s| + s.author = ["Ruby Coder"] +end""" + result = MAPPINGS['GemspecMapping'].translate(raw_content) + self.assertEqual(result, { + '@context': 'https://doi.org/10.5063/schema/codemeta-2.0', + 'type': 'SoftwareSourceCode', + }) + raw_content = b""" +Gem::Specification.new do |s| s.author = "Ruby Coder1", end""" result = MAPPINGS['GemspecMapping'].translate(raw_content) @@ -815,6 +842,16 @@ '@context': 'https://doi.org/10.5063/schema/codemeta-2.0', 'type': 'SoftwareSourceCode', }) + raw_content = b""" +Gem::Specification.new do |s| + s.authors = ["Ruby Coder1", ["Ruby Coder2"]] +end""" + result = MAPPINGS['GemspecMapping'].translate(raw_content) + self.assertEqual(result, { + '@context': 'https://doi.org/10.5063/schema/codemeta-2.0', + 'type': 'SoftwareSourceCode', + 'author': ['Ruby Coder1'], + }) def test_revision_metadata_indexer(self): metadata_indexer = RevisionMetadataIndexer(