diff --git a/swh/loader/package/npm/loader.py b/swh/loader/package/npm/loader.py --- a/swh/loader/package/npm/loader.py +++ b/swh/loader/package/npm/loader.py @@ -29,6 +29,9 @@ logger = logging.getLogger(__name__) +DEFAULT_PERSON = Person(fullname=b"", name=None, email=None) + + class NpmLoader(PackageLoader): """Load npm origin's artifact releases into swh archive. @@ -205,13 +208,13 @@ return result -def extract_npm_package_author(package_json) -> Person: +def extract_npm_package_author(package_json: Dict[str, Any]) -> Person: """ Extract package author from a ``package.json`` file content and return it in swh format. Args: - package_json (dict): Dict holding the content of parsed + package_json: Dict holding the content of parsed ``package.json`` file Returns: @@ -220,10 +223,13 @@ """ for author_key in ("author", "authors"): if author_key in package_json: - author_str = _author_str(package_json[author_key]) + author_data = package_json[author_key] + if author_data is None: + return DEFAULT_PERSON + author_str = _author_str(author_data) return Person.from_fullname(author_str.encode()) - return Person(fullname=b"", name=None, email=None) + return DEFAULT_PERSON def _lstrip_bom(s, bom=BOM_UTF8): diff --git a/swh/loader/package/npm/tests/test_npm.py b/swh/loader/package/npm/tests/test_npm.py --- a/swh/loader/package/npm/tests/test_npm.py +++ b/swh/loader/package/npm/tests/test_npm.py @@ -39,6 +39,7 @@ {"name": ["Susan McSween", "William H. Bonney", "Doc Scurlock",]}, "Susan McSween", ), + (None, None), ]: assert _author_str(author) == expected_author @@ -88,7 +89,7 @@ }, "homepage": "http://wcoder.github.io/highlightjs-line-numbers.js/" }""" - ) # noqa + ) assert extract_npm_package_author(package_json) == Person( fullname=b"Yauheni Pakala ", @@ -183,6 +184,17 @@ email=b"448627663@qq.com", ) + package_json_no_authors = json.loads( + """{ + "authors": null, + "license": "MIT" + }""" + ) + + assert extract_npm_package_author(package_json_no_authors) == Person( + fullname=b"", name=None, email=None + ) + def normalize_hashes(hashes): if isinstance(hashes, str):