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.model.model import Person | |||||
from swh.loader.package.npm.loader import ( | from swh.loader.package.npm.loader import ( | ||||
NpmLoader, 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 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']) == \ | ||||
{ | Person( | ||||
'fullname': b'mooz <stillpedant@gmail.com>', | fullname=b'mooz <stillpedant@gmail.com>', | ||||
'name': b'mooz', | name=b'mooz', | ||||
'email': b'stillpedant@gmail.com' | email=b'stillpedant@gmail.com' | ||||
} | ) | ||||
assert ( | assert ( | ||||
extract_npm_package_author(package_metadata['versions']['0.0.3']) == | extract_npm_package_author(package_metadata['versions']['0.0.3']) == | ||||
{ | Person( | ||||
'fullname': b'Masafumi Oyamada <stillpedant@gmail.com>', | fullname=b'Masafumi Oyamada <stillpedant@gmail.com>', | ||||
'name': b'Masafumi Oyamada', | name=b'Masafumi Oyamada', | ||||
'email': b'stillpedant@gmail.com' | email=b'stillpedant@gmail.com' | ||||
} | ) | ||||
) | ) | ||||
package_json = json.loads(''' | package_json = json.loads(''' | ||||
{ | { | ||||
"name": "highlightjs-line-numbers.js", | "name": "highlightjs-line-numbers.js", | ||||
"version": "2.7.0", | "version": "2.7.0", | ||||
"description": "Highlight.js line numbers plugin.", | "description": "Highlight.js line numbers plugin.", | ||||
"main": "src/highlightjs-line-numbers.js", | "main": "src/highlightjs-line-numbers.js", | ||||
Show All 12 Lines | package_json = json.loads(''' | ||||
"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 | }''') # noqa | ||||
assert extract_npm_package_author(package_json) == \ | 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(''' | ||||
{ | { | ||||
"name": "3-way-diff", | "name": "3-way-diff", | ||||
"version": "0.0.1", | "version": "0.0.1", | ||||
"description": "3-way diffing of JavaScript objects", | "description": "3-way diffing of JavaScript objects", | ||||
"main": "index.js", | "main": "index.js", | ||||
"authors": [ | "authors": [ | ||||
Show All 18 Lines | package_json = json.loads(''' | ||||
"mocha": "^3.0.2" | "mocha": "^3.0.2" | ||||
}, | }, | ||||
"dependencies": { | "dependencies": { | ||||
"lodash": "^4.15.0" | "lodash": "^4.15.0" | ||||
} | } | ||||
}''') | }''') | ||||
assert extract_npm_package_author(package_json) == \ | assert extract_npm_package_author(package_json) == \ | ||||
{ | Person( | ||||
'fullname': b'Shawn Walsh', | fullname=b'Shawn Walsh', | ||||
'name': b'Shawn Walsh', | name=b'Shawn Walsh', | ||||
'email': None | email=None | ||||
} | ) | ||||
package_json = json.loads(''' | package_json = json.loads(''' | ||||
{ | { | ||||
"name": "yfe-ynpm", | "name": "yfe-ynpm", | ||||
"version": "1.0.0", | "version": "1.0.0", | ||||
"homepage": "http://gitlab.ywwl.com/yfe/yfe-ynpm", | "homepage": "http://gitlab.ywwl.com/yfe/yfe-ynpm", | ||||
"repository": { | "repository": { | ||||
"type": "git", | "type": "git", | ||||
"url": "git@gitlab.ywwl.com:yfe/yfe-ynpm.git" | "url": "git@gitlab.ywwl.com:yfe/yfe-ynpm.git" | ||||
}, | }, | ||||
"author": [ | "author": [ | ||||
"fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)", | "fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)", | ||||
"xufuzi <xufuzi@ywwl.com> (https://7993.org)" | "xufuzi <xufuzi@ywwl.com> (https://7993.org)" | ||||
], | ], | ||||
"license": "MIT" | "license": "MIT" | ||||
}''') | }''') | ||||
assert extract_npm_package_author(package_json) == \ | assert extract_npm_package_author(package_json) == \ | ||||
{ | Person( | ||||
'fullname': b'fengmk2 <fengmk2@gmail.com>', | fullname=b'fengmk2 <fengmk2@gmail.com>', | ||||
'name': b'fengmk2', | name=b'fengmk2', | ||||
'email': b'fengmk2@gmail.com' | email=b'fengmk2@gmail.com' | ||||
} | ) | ||||
package_json = json.loads(''' | package_json = json.loads(''' | ||||
{ | { | ||||
"name": "umi-plugin-whale", | "name": "umi-plugin-whale", | ||||
"version": "0.0.8", | "version": "0.0.8", | ||||
"description": "Internal contract component", | "description": "Internal contract component", | ||||
"authors": { | "authors": { | ||||
"name": "xiaohuoni", | "name": "xiaohuoni", | ||||
"email": "448627663@qq.com" | "email": "448627663@qq.com" | ||||
}, | }, | ||||
"repository": "alitajs/whale", | "repository": "alitajs/whale", | ||||
"devDependencies": { | "devDependencies": { | ||||
"np": "^3.0.4", | "np": "^3.0.4", | ||||
"umi-tools": "*" | "umi-tools": "*" | ||||
}, | }, | ||||
"license": "MIT" | "license": "MIT" | ||||
}''') | }''') | ||||
assert extract_npm_package_author(package_json) == \ | assert extract_npm_package_author(package_json) == \ | ||||
{ | Person( | ||||
'fullname': b'xiaohuoni <448627663@qq.com>', | fullname=b'xiaohuoni <448627663@qq.com>', | ||||
'name': b'xiaohuoni', | name=b'xiaohuoni', | ||||
'email': b'448627663@qq.com' | email=b'448627663@qq.com' | ||||
} | ) | ||||
def normalize_hashes(hashes): | def normalize_hashes(hashes): | ||||
if isinstance(hashes, str): | if isinstance(hashes, str): | ||||
return hash_to_bytes(hashes) | return hash_to_bytes(hashes) | ||||
if isinstance(hashes, list): | if isinstance(hashes, list): | ||||
return [hash_to_bytes(x) for x in hashes] | return [hash_to_bytes(x) for x in hashes] | ||||
return {hash_to_bytes(k): hash_to_bytes(v) for k, v in hashes.items()} | return {hash_to_bytes(k): hash_to_bytes(v) for k, v in hashes.items()} | ||||
▲ Show 20 Lines • Show All 422 Lines • Show Last 20 Lines |