Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/npm/tests/test_npm.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 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 json | ||||
import os | import os | ||||
import pytest | import pytest | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
from swh.loader.package.npm.loader import ( | from swh.loader.package.npm.loader import ( | ||||
NpmLoader, parse_npm_package_author, extract_npm_package_author, | NpmLoader, extract_npm_package_author, | ||||
artifact_to_revision_id | artifact_to_revision_id | ||||
) | ) | ||||
from swh.loader.package.tests.common import ( | from swh.loader.package.tests.common import ( | ||||
check_snapshot, check_metadata_paths, get_stats | check_snapshot, check_metadata_paths, get_stats | ||||
) | ) | ||||
def _parse_author_string_test(author_str, expected_result): | |||||
assert parse_npm_package_author(author_str) == expected_result | |||||
assert parse_npm_package_author(' %s' % author_str) == expected_result | |||||
assert parse_npm_package_author('%s ' % author_str) == expected_result | |||||
def test_parse_npm_package_author(): | |||||
_parse_author_string_test( | |||||
'John Doe', | |||||
{ | |||||
'name': 'John Doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'<john.doe@foo.bar>', | |||||
{ | |||||
'email': 'john.doe@foo.bar' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'(https://john.doe)', | |||||
{ | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe <john.doe@foo.bar>', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe<john.doe@foo.bar>', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe (https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe(https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'<john.doe@foo.bar> (https://john.doe)', | |||||
{ | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'(https://john.doe) <john.doe@foo.bar>', | |||||
{ | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe <john.doe@foo.bar> (https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe (https://john.doe) <john.doe@foo.bar>', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe<john.doe@foo.bar> (https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe<john.doe@foo.bar>(https://john.doe)', | |||||
{ | |||||
'name': 'John Doe', | |||||
'email': 'john.doe@foo.bar', | |||||
'url': 'https://john.doe' | |||||
} | |||||
) | |||||
_parse_author_string_test('', {}) | |||||
_parse_author_string_test('<>', {}) | |||||
_parse_author_string_test(' <>', {}) | |||||
_parse_author_string_test('<>()', {}) | |||||
_parse_author_string_test('<> ()', {}) | |||||
_parse_author_string_test('()', {}) | |||||
_parse_author_string_test(' ()', {}) | |||||
_parse_author_string_test( | |||||
'John Doe <> ()', | |||||
{ | |||||
'name': 'John Doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe <>', | |||||
{ | |||||
'name': 'John Doe' | |||||
} | |||||
) | |||||
_parse_author_string_test( | |||||
'John Doe ()', | |||||
{ | |||||
'name': 'John Doe' | |||||
} | |||||
) | |||||
def test_extract_npm_package_author(datadir): | def test_extract_npm_package_author(datadir): | ||||
package_metadata_filepath = os.path.join( | package_metadata_filepath = os.path.join( | ||||
datadir, 'https_replicate.npmjs.com', 'org_visit1') | datadir, 'https_replicate.npmjs.com', 'org_visit1') | ||||
with open(package_metadata_filepath) as json_file: | with open(package_metadata_filepath) as json_file: | ||||
package_metadata = json.load(json_file) | package_metadata = json.load(json_file) | ||||
extract_npm_package_author(package_metadata['versions']['0.0.2']) == \ | extract_npm_package_author(package_metadata['versions']['0.0.2']) == \ | ||||
▲ Show 20 Lines • Show All 483 Lines • Show Last 20 Lines |