Changeset View
Changeset View
Standalone View
Standalone View
swh/loader/npm/tests/common.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 os | import os | ||||
import os.path | import os.path | ||||
import chardet | from swh.loader.npm.utils import load_json | ||||
RESOURCES_PATH = os.path.join(os.path.dirname(__file__), 'resources') | RESOURCES_PATH = os.path.join(os.path.dirname(__file__), 'resources') | ||||
empty_package = '22u-validators' | empty_package = '22u-validators' | ||||
package = 'org' | package = 'org' | ||||
package_non_utf8_encoding = '0b' | package_non_utf8_encoding = '0b' | ||||
Show All 25 Lines | |||||
def init_test_data(m, package_metadata_json_file, package_metadata_url): | def init_test_data(m, package_metadata_json_file, package_metadata_url): | ||||
package_metadata_filepath = os.path.join(RESOURCES_PATH, | package_metadata_filepath = os.path.join(RESOURCES_PATH, | ||||
package_metadata_json_file) | package_metadata_json_file) | ||||
with open(package_metadata_filepath, 'rb') as json_file: | with open(package_metadata_filepath, 'rb') as json_file: | ||||
json_file_bytes = json_file.read() | json_file_bytes = json_file.read() | ||||
file_encoding = chardet.detect(json_file_bytes)['encoding'] | package_metadata = load_json(json_file_bytes) | ||||
package_metadata = json.loads(json_file_bytes.decode(file_encoding)) | |||||
m.register_uri('GET', package_metadata_url, json=package_metadata) | m.register_uri('GET', package_metadata_url, json=package_metadata) | ||||
for v, v_data in package_metadata['versions'].items(): | for v, v_data in package_metadata['versions'].items(): | ||||
tarball_url = v_data['dist']['tarball'] | tarball_url = v_data['dist']['tarball'] | ||||
tarball_filename = tarball_url.split('/')[-1] | tarball_filename = tarball_url.split('/')[-1] | ||||
tarball_filepath = os.path.join(RESOURCES_PATH, 'tarballs', | tarball_filepath = os.path.join(RESOURCES_PATH, 'tarballs', | ||||
tarball_filename) | tarball_filename) | ||||
Show All 21 Lines |