diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/hydrilla_builder/build.py | 21 | ||||
m--------- | src/test/source-package-example | 0 | ||||
-rw-r--r-- | src/test/test_hydrilla_builder.py | 216 |
3 files changed, 229 insertions, 8 deletions
diff --git a/src/hydrilla_builder/build.py b/src/hydrilla_builder/build.py index e70e360..dcd7e01 100644 --- a/src/hydrilla_builder/build.py +++ b/src/hydrilla_builder/build.py @@ -280,7 +280,8 @@ class Build: root_dir_path = Path(root_dir_name) def zippath(file_path): - return str(root_dir_path / file_path.relative_to(self.srcdir)) + file_path = root_dir_path / file_path.relative_to(self.srcdir) + return file_path.as_posix() with zipfile.ZipFile(fb, 'w') as xpi: for file_ref in self.files_by_path.values(): @@ -295,6 +296,9 @@ class Build: """ Process 'item_def' as definition of a resource/mapping and store in memory its processed form and files used by it. + + Return a minimal item reference suitable for using in source + description. """ copy_props = ['type', 'identifier', 'long_name', 'uuid', 'description'] if 'comment' in item_def: @@ -326,9 +330,14 @@ class Build: new_item_obj['version'] = normalize_version(item_def['version']) new_item_obj['api_schema_version'] = [1, 0, 1] + new_item_obj['source_copyright'] = self.copyright_file_refs + new_item_obj['source_name'] = self.source_name item_list.append(new_item_obj) + return dict([(prop, new_item_obj[prop]) + for prop in ('type', 'identifier', 'version')]) + def _process_index_json(self, index_obj: dict): """ Process 'index_obj' as contents of source package's index.json and store @@ -338,6 +347,8 @@ class Build: """ jsonschema.validate(index_obj, index_json_schema) + self.source_name = index_obj['source_name'] + generate_spdx = index_obj.get('reuse_generate_spdx_report', False) if generate_spdx: contents = generate_spdx_report(self.srcdir) @@ -347,7 +358,7 @@ class Build: spdx_ref.include_in_zipfile = False self.files_by_path[spdx_path] = spdx_ref - copyright_file_refs = \ + self.copyright_file_refs = \ [self._process_file(f['file']) for f in index_obj['copyright']] if generate_spdx and not spdx_ref.include_in_distribution: @@ -358,7 +369,7 @@ class Build: for file_ref in index_obj.get('additional_files', []): self._process_file(file_ref['file'], include_in_distribution=False) - root_dir_path = Path(index_obj['source_name']) + root_dir_path = Path(self.source_name) source_archives_obj = { 'zip' : { @@ -368,8 +379,8 @@ class Build: self.source_description = { 'api_schema_version': [1, 0, 1], - 'source_name': index_obj['source_name'], - 'source_copyright': copyright_file_refs, + 'source_name': self.source_name, + 'source_copyright': self.copyright_file_refs, 'upstream_url': index_obj['upstream_url'], 'definitions': item_refs, 'source_archives': source_archives_obj diff --git a/src/test/source-package-example b/src/test/source-package-example -Subproject 96b8830c30d9052decaf9aaf99375cf4b5ad719 +Subproject e571b3911f198e3feccc8d06390c79131f9cf09 diff --git a/src/test/test_hydrilla_builder.py b/src/test/test_hydrilla_builder.py index b054192..52ed650 100644 --- a/src/test/test_hydrilla_builder.py +++ b/src/test/test_hydrilla_builder.py @@ -5,9 +5,12 @@ # Available under the terms of Creative Commons Zero v1.0 Universal. import pytest +import json from tempfile import TemporaryDirectory from pathlib import Path +from hashlib import sha256, sha1 +from zipfile import ZipFile here = Path(__file__).resolve().parent @@ -20,7 +23,214 @@ def test_build(tmpdir): """Build the sample source package and verify the produced files.""" from hydrilla_builder.build import Build - build = Build(here / 'source-package-example', Path('index.json')) - build.write_package_files(Path(tmpdir)) + # First, build the package + srcdir = here / 'source-package-example' + dstdir = Path(tmpdir) - # TODO: verify results + build = Build(srcdir, Path('index.json')) + build.write_package_files(dstdir) + + # Verify directories under destination directory + assert {'file', 'resource', 'mapping', 'source'} == \ + set([path.name for path in dstdir.iterdir()]) + + # Verify files under 'file/' + file_dir = dstdir / 'file' + js_filenames = ('bye.js', 'hello.js', 'message.js') + dist_filenames = (*js_filenames, str(Path('LICENSES') / 'CC0-1.0.txt')) + file_contents = {} + file_sha256_hashes = {} + file_sha1_hashes = {} + + for fn in dist_filenames: + with open(srcdir / fn, 'rb') as file_handle: + file_contents = file_handle.read() + + file_sha256_hashes[fn] = sha256(file_contents).digest().hex() + file_sha1_hashes[fn] = sha1(file_contents).digest().hex() + + dist_file_path = file_dir / f'sha256-{file_sha256_hashes[fn]}' + assert dist_file_path.is_file() + + with open(dist_file_path, 'rb') as file_handle: + assert file_handle.read() == file_contents + + sha256_hashes = set(file_sha256_hashes.values()) + spdx_report_sha256 = None + spdx_report_checked = False + + for path in file_dir.iterdir(): + assert path.name.startswith('sha256-') + if path.name[7:] in sha256_hashes: + continue + + assert spdx_report_sha256 is None + + with open(path, 'rt') as file_handle: + spdx_contents = file_handle.read() + + spdx_report_sha256 = sha256(spdx_contents.encode()).digest().hex() + assert spdx_report_sha256 == path.name[7:] + + for fn in js_filenames: + assert file_sha1_hashes[fn] in spdx_contents + + # Verify files under 'resource/' + resource_dir = dstdir / 'resource' + + expected_resource_jsons = [{ + 'api_schema_version': [1, 0, 1], + 'source_name': 'hello', + 'source_copyright': [{ + 'file': 'report.spdx', + 'sha256': spdx_report_sha256 + }, { + 'file': 'LICENSES/CC0-1.0.txt', + 'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt'] + }], + 'type': 'resource', + 'identifier': 'helloapple', + 'long_name': 'Hello Apple', + 'uuid': 'a6754dcb-58d8-4b7a-a245-24fd7ad4cd68', + 'version': [2021, 11, 10], + 'revision': 1, + 'description': 'greets an apple', + 'dependencies': ['hello-message'], + 'scripts': [{ + 'file': 'hello.js', + 'sha256': file_sha256_hashes['hello.js'] + }, { + 'file': 'bye.js', + 'sha256': file_sha256_hashes['bye.js'] + }] + }, { + 'api_schema_version': [1, 0, 1], + 'source_name': 'hello', + 'source_copyright': [{ + 'file': 'report.spdx', + 'sha256': spdx_report_sha256 + }, { + 'file': 'LICENSES/CC0-1.0.txt', + 'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt'] + }], + 'type': 'resource', + 'identifier': 'hello-message', + 'long_name': 'Hello Message', + 'uuid': '1ec36229-298c-4b35-8105-c4f2e1b9811e', + 'version': [2021, 11, 10], + 'revision': 2, + 'description': 'define messages for saying hello and bye', + 'dependencies': [], + 'scripts': [{ + 'file': 'message.js', + 'sha256': file_sha256_hashes['message.js'] + }] + }] + + assert set([rj['identifier'] for rj in expected_resource_jsons]) == \ + set([path.name for path in resource_dir.iterdir()]) + + for resource_json in expected_resource_jsons: + subdir = resource_dir / resource_json['identifier'] + assert ['2021.11.10'] == [path.name for path in subdir.iterdir()] + + with open(subdir / '2021.11.10', 'rt') as file_handle: + assert json.load(file_handle) == resource_json + + # Verify files under 'mapping/' + mapping_dir = dstdir / 'mapping' + assert ['helloapple'] == [path.name for path in mapping_dir.iterdir()] + + subdir = mapping_dir / 'helloapple' + assert ['2021.11.10'] == [path.name for path in subdir.iterdir()] + + expected_mapping_json = { + 'api_schema_version': [1, 0, 1], + 'source_name': 'hello', + 'source_copyright': [{ + 'file': 'report.spdx', + 'sha256': spdx_report_sha256 + }, { + 'file': 'LICENSES/CC0-1.0.txt', + 'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt'] + }], + 'type': 'mapping', + 'identifier': 'helloapple', + 'long_name': 'Hello Apple', + 'uuid': '54d23bba-472e-42f5-9194-eaa24c0e3ee7', + 'version': [2021, 11, 10], + 'description': 'causes apple to get greeted on Hydrillabugs issue tracker', + 'payloads': { + 'https://hydrillabugs.koszko.org/***': { + 'identifier': 'helloapple' + }, + 'https://hachettebugs.koszko.org/***': { + 'identifier': 'helloapple' + } + } + } + + with open(subdir / '2021.11.10', 'rt') as file_handle: + assert json.load(file_handle) == expected_mapping_json + + # Verify files under 'source/' + source_dir = dstdir / 'source' + assert {'hello.json', 'hello.zip'} == \ + set([path.name for path in source_dir.iterdir()]) + + src_filenames = ( + *dist_filenames, + 'README.txt', 'README.txt.license', '.reuse/dep5', 'index.json' + ) + zip_filenames = [f'hello/{fn}' for fn in src_filenames] + + with ZipFile(source_dir / 'hello.zip', 'r') as archive: + assert set([f.filename for f in archive.filelist]) == set(zip_filenames) + + for zip_fn, src_fn in zip(zip_filenames, src_filenames): + src_path = srcdir + for segment in src_fn.split('/'): + src_path = src_path / segment + + with archive.open(zip_fn, 'r') as zip_file_handle: + with open(src_path, 'rb') as disk_file_handle: + assert zip_file_handle.read() == disk_file_handle.read() + + with open(source_dir / 'hello.zip', 'rb') as file_handle: + zipfile_sha256_hash = sha256(file_handle.read()).digest().hex() + + expected_source_description = { + 'api_schema_version': [1, 0, 1], + 'source_name': 'hello', + 'source_copyright': [{ + 'file': 'report.spdx', + 'sha256': spdx_report_sha256 + }, { + 'file': 'LICENSES/CC0-1.0.txt', + 'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt'] + }], + 'source_archives': { + 'zip': { + 'sha256': zipfile_sha256_hash, + } + }, + 'upstream_url': 'https://git.koszko.org/hydrilla-source-package-example', + 'definitions': [{ + 'type': 'resource', + 'identifier': 'helloapple', + 'version': [2021, 11, 10], + }, { + 'type': 'resource', + 'identifier': 'hello-message', + 'version': [2021, 11, 10], + }, { + 'type': 'mapping', + 'identifier': 'helloapple', + 'version': [2021, 11, 10], + }] + } + + with open(source_dir / 'hello.json', 'rt') as file_handle: + assert json.load(file_handle) == expected_source_description + +# TODO: also verify on slightly different examples and check error handling |