Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/package/npm/tests/test_npm.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 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.model.model import Person, Snapshot, SnapshotBranch, TargetType | from swh.model.model import Person, Snapshot, SnapshotBranch, TargetType | ||||
from swh.loader.package.npm.loader import ( | from swh.loader.package.npm.loader import ( | ||||
_author_str, | |||||
NpmLoader, | NpmLoader, | ||||
extract_npm_package_author, | extract_npm_package_author, | ||||
artifact_to_revision_id, | artifact_to_revision_id, | ||||
) | ) | ||||
from swh.loader.package.tests.common import check_metadata_paths | from swh.loader.package.tests.common import check_metadata_paths | ||||
from swh.loader.tests import ( | from swh.loader.tests import ( | ||||
assert_last_visit_matches, | assert_last_visit_matches, | ||||
check_snapshot, | check_snapshot, | ||||
get_stats, | get_stats, | ||||
) | ) | ||||
def test_npm_author_str(): | |||||
for author, expected_author in [ | |||||
("author", "author"), | |||||
( | |||||
["Al from quantum leap", "hal from 2001 space odyssey"], | |||||
"Al from quantum leap", | |||||
), | |||||
([], ""), | |||||
({"name": "groot", "email": "groot@galaxy.org",}, "groot <groot@galaxy.org>"), | |||||
({"name": "somebody",}, "somebody"), | |||||
({"email": "no@one.org"}, " <no@one.org>"), # note first elt is an extra blank | |||||
({"name": "no one", "email": None,}, "no one"), | |||||
({"email": None,}, ""), | |||||
({"name": None}, ""), | |||||
({"name": None, "email": None,}, ""), | |||||
({}, ""), | |||||
(None, None), | |||||
]: | |||||
assert _author_str(author) == expected_author | |||||
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) | ||||
Show All 29 Lines | package_json = json.loads( | ||||
}, | }, | ||||
"author": "Yauheni Pakala <evgeniy.pakalo@gmail.com>", | "author": "Yauheni Pakala <evgeniy.pakalo@gmail.com>", | ||||
"license": "MIT", | "license": "MIT", | ||||
"bugs": { | "bugs": { | ||||
"url": "https://github.com/wcoder/highlightjs-line-numbers.js/issues" | "url": "https://github.com/wcoder/highlightjs-line-numbers.js/issues" | ||||
}, | }, | ||||
"homepage": "http://wcoder.github.io/highlightjs-line-numbers.js/" | "homepage": "http://wcoder.github.io/highlightjs-line-numbers.js/" | ||||
}""" | }""" | ||||
) # noqa | ) | ||||
assert extract_npm_package_author(package_json) == Person( | assert extract_npm_package_author(package_json) == Person( | ||||
fullname=b"Yauheni Pakala <evgeniy.pakalo@gmail.com>", | fullname=b"Yauheni Pakala <evgeniy.pakalo@gmail.com>", | ||||
name=b"Yauheni Pakala", | name=b"Yauheni Pakala", | ||||
email=b"evgeniy.pakalo@gmail.com", | email=b"evgeniy.pakalo@gmail.com", | ||||
) | ) | ||||
package_json = json.loads( | package_json = json.loads( | ||||
▲ Show 20 Lines • Show All 541 Lines • Show Last 20 Lines |