Project

General

Profile

« Previous | Next » 

Revision 34072d8d

Added by koszko over 1 year ago

extend the automated test case

View differences:

src/hydrilla_builder/build.py
280 280
        root_dir_path = Path(root_dir_name)
281 281

  
282 282
        def zippath(file_path):
283
            return str(root_dir_path / file_path.relative_to(self.srcdir))
283
            file_path = root_dir_path / file_path.relative_to(self.srcdir)
284
            return file_path.as_posix()
284 285

  
285 286
        with zipfile.ZipFile(fb, 'w') as xpi:
286 287
            for file_ref in self.files_by_path.values():
......
295 296
        """
296 297
        Process 'item_def' as definition of a resource/mapping and store in
297 298
        memory its processed form and files used by it.
299

  
300
        Return a minimal item reference suitable for using in source
301
        description.
298 302
        """
299 303
        copy_props = ['type', 'identifier', 'long_name', 'uuid', 'description']
300 304
        if 'comment' in item_def:
......
326 330

  
327 331
        new_item_obj['version'] = normalize_version(item_def['version'])
328 332
        new_item_obj['api_schema_version'] = [1, 0, 1]
333
        new_item_obj['source_copyright'] = self.copyright_file_refs
334
        new_item_obj['source_name'] = self.source_name
329 335

  
330 336
        item_list.append(new_item_obj)
331 337

  
338
        return dict([(prop, new_item_obj[prop])
339
                     for prop in ('type', 'identifier', 'version')])
340

  
332 341
    def _process_index_json(self, index_obj: dict):
333 342
        """
334 343
        Process 'index_obj' as contents of source package's index.json and store
......
338 347
        """
339 348
        jsonschema.validate(index_obj, index_json_schema)
340 349

  
350
        self.source_name = index_obj['source_name']
351

  
341 352
        generate_spdx = index_obj.get('reuse_generate_spdx_report', False)
342 353
        if generate_spdx:
343 354
            contents  = generate_spdx_report(self.srcdir)
......
347 358
            spdx_ref.include_in_zipfile = False
348 359
            self.files_by_path[spdx_path] = spdx_ref
349 360

  
350
        copyright_file_refs = \
361
        self.copyright_file_refs = \
351 362
            [self._process_file(f['file']) for f in index_obj['copyright']]
352 363

  
353 364
        if generate_spdx and not spdx_ref.include_in_distribution:
......
358 369
        for file_ref in index_obj.get('additional_files', []):
359 370
            self._process_file(file_ref['file'], include_in_distribution=False)
360 371

  
361
        root_dir_path = Path(index_obj['source_name'])
372
        root_dir_path = Path(self.source_name)
362 373

  
363 374
        source_archives_obj = {
364 375
            'zip' : {
......
368 379

  
369 380
        self.source_description = {
370 381
            'api_schema_version': [1, 0, 1],
371
            'source_name':        index_obj['source_name'],
372
            'source_copyright':   copyright_file_refs,
382
            'source_name':        self.source_name,
383
            'source_copyright':   self.copyright_file_refs,
373 384
            'upstream_url':       index_obj['upstream_url'],
374 385
            'definitions':        item_refs,
375 386
            'source_archives':    source_archives_obj
src/test/source-package-example
1
Subproject commit 96b8830c30d9052decaf9aaf99375cf4b5ad719f
1
Subproject commit e571b3911f198e3feccc8d06390c79131f9cf09d
src/test/test_hydrilla_builder.py
5 5
# Available under the terms of Creative Commons Zero v1.0 Universal.
6 6

  
7 7
import pytest
8
import json
8 9

  
9 10
from tempfile import TemporaryDirectory
10 11
from pathlib import Path
12
from hashlib import sha256, sha1
13
from zipfile import ZipFile
11 14

  
12 15
here = Path(__file__).resolve().parent
13 16

  
......
20 23
    """Build the sample source package and verify the produced files."""
21 24
    from hydrilla_builder.build import Build
22 25

  
23
    build = Build(here / 'source-package-example', Path('index.json'))
24
    build.write_package_files(Path(tmpdir))
26
    # First, build the package
27
    srcdir = here / 'source-package-example'
28
    dstdir = Path(tmpdir)
25 29

  
26
    # TODO: verify results
30
    build = Build(srcdir, Path('index.json'))
31
    build.write_package_files(dstdir)
32

  
33
    # Verify directories under destination directory
34
    assert {'file', 'resource', 'mapping', 'source'} == \
35
        set([path.name for path in dstdir.iterdir()])
36

  
37
    # Verify files under 'file/'
38
    file_dir = dstdir / 'file'
39
    js_filenames = ('bye.js', 'hello.js', 'message.js')
40
    dist_filenames = (*js_filenames, str(Path('LICENSES') / 'CC0-1.0.txt'))
41
    file_contents = {}
42
    file_sha256_hashes = {}
43
    file_sha1_hashes = {}
44

  
45
    for fn in dist_filenames:
46
        with open(srcdir / fn, 'rb') as file_handle:
47
            file_contents = file_handle.read()
48

  
49
        file_sha256_hashes[fn] = sha256(file_contents).digest().hex()
50
        file_sha1_hashes[fn]   = sha1(file_contents).digest().hex()
51

  
52
        dist_file_path = file_dir / f'sha256-{file_sha256_hashes[fn]}'
53
        assert dist_file_path.is_file()
54

  
55
        with open(dist_file_path, 'rb') as file_handle:
56
            assert file_handle.read() == file_contents
57

  
58
    sha256_hashes = set(file_sha256_hashes.values())
59
    spdx_report_sha256 = None
60
    spdx_report_checked = False
61

  
62
    for path in file_dir.iterdir():
63
        assert path.name.startswith('sha256-')
64
        if path.name[7:] in sha256_hashes:
65
            continue
66

  
67
        assert spdx_report_sha256 is None
68

  
69
        with open(path, 'rt') as file_handle:
70
            spdx_contents = file_handle.read()
71

  
72
        spdx_report_sha256 = sha256(spdx_contents.encode()).digest().hex()
73
        assert spdx_report_sha256 == path.name[7:]
74

  
75
        for fn in js_filenames:
76
            assert file_sha1_hashes[fn] in spdx_contents
77

  
78
    # Verify files under 'resource/'
79
    resource_dir = dstdir / 'resource'
80

  
81
    expected_resource_jsons = [{
82
        'api_schema_version': [1, 0, 1],
83
        'source_name': 'hello',
84
        'source_copyright': [{
85
            'file': 'report.spdx',
86
            'sha256': spdx_report_sha256
87
        }, {
88
            'file': 'LICENSES/CC0-1.0.txt',
89
            'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt']
90
        }],
91
        'type': 'resource',
92
        'identifier': 'helloapple',
93
        'long_name': 'Hello Apple',
94
        'uuid': 'a6754dcb-58d8-4b7a-a245-24fd7ad4cd68',
95
        'version': [2021, 11, 10],
96
        'revision': 1,
97
        'description': 'greets an apple',
98
        'dependencies': ['hello-message'],
99
        'scripts': [{
100
            'file': 'hello.js',
101
            'sha256': file_sha256_hashes['hello.js']
102
        }, {
103
            'file': 'bye.js',
104
            'sha256': file_sha256_hashes['bye.js']
105
        }]
106
    }, {
107
        'api_schema_version': [1, 0, 1],
108
        'source_name': 'hello',
109
        'source_copyright': [{
110
            'file': 'report.spdx',
111
            'sha256': spdx_report_sha256
112
        }, {
113
            'file': 'LICENSES/CC0-1.0.txt',
114
            'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt']
115
        }],
116
        'type': 'resource',
117
        'identifier': 'hello-message',
118
        'long_name': 'Hello Message',
119
        'uuid': '1ec36229-298c-4b35-8105-c4f2e1b9811e',
120
        'version': [2021, 11, 10],
121
        'revision': 2,
122
        'description': 'define messages for saying hello and bye',
123
        'dependencies': [],
124
        'scripts': [{
125
            'file': 'message.js',
126
            'sha256': file_sha256_hashes['message.js']
127
        }]
128
    }]
129

  
130
    assert set([rj['identifier'] for rj in expected_resource_jsons]) == \
131
        set([path.name for path in resource_dir.iterdir()])
132

  
133
    for resource_json in expected_resource_jsons:
134
        subdir = resource_dir / resource_json['identifier']
135
        assert ['2021.11.10'] == [path.name for path in subdir.iterdir()]
136

  
137
        with open(subdir / '2021.11.10', 'rt') as file_handle:
138
            assert json.load(file_handle) == resource_json
139

  
140
    # Verify files under 'mapping/'
141
    mapping_dir = dstdir / 'mapping'
142
    assert ['helloapple'] == [path.name for path in mapping_dir.iterdir()]
143

  
144
    subdir = mapping_dir / 'helloapple'
145
    assert ['2021.11.10'] == [path.name for path in subdir.iterdir()]
146

  
147
    expected_mapping_json = {
148
        'api_schema_version': [1, 0, 1],
149
        'source_name': 'hello',
150
        'source_copyright': [{
151
            'file': 'report.spdx',
152
            'sha256': spdx_report_sha256
153
        }, {
154
            'file': 'LICENSES/CC0-1.0.txt',
155
            'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt']
156
        }],
157
        'type': 'mapping',
158
	'identifier': 'helloapple',
159
	'long_name': 'Hello Apple',
160
	'uuid': '54d23bba-472e-42f5-9194-eaa24c0e3ee7',
161
	'version': [2021, 11, 10],
162
	'description': 'causes apple to get greeted on Hydrillabugs issue tracker',
163
	'payloads': {
164
	    'https://hydrillabugs.koszko.org/***': {
165
		'identifier': 'helloapple'
166
	    },
167
	    'https://hachettebugs.koszko.org/***': {
168
		'identifier': 'helloapple'
169
            }
170
        }
171
    }
172

  
173
    with open(subdir / '2021.11.10', 'rt') as file_handle:
174
        assert json.load(file_handle) == expected_mapping_json
175

  
176
    # Verify files under 'source/'
177
    source_dir = dstdir / 'source'
178
    assert {'hello.json', 'hello.zip'} == \
179
        set([path.name for path in source_dir.iterdir()])
180

  
181
    src_filenames = (
182
        *dist_filenames,
183
        'README.txt', 'README.txt.license', '.reuse/dep5', 'index.json'
184
    )
185
    zip_filenames = [f'hello/{fn}' for fn in src_filenames]
186

  
187
    with ZipFile(source_dir / 'hello.zip', 'r') as archive:
188
        assert set([f.filename for f in archive.filelist]) == set(zip_filenames)
189

  
190
        for zip_fn, src_fn in zip(zip_filenames, src_filenames):
191
            src_path = srcdir
192
            for segment in src_fn.split('/'):
193
                src_path = src_path / segment
194

  
195
            with archive.open(zip_fn, 'r') as zip_file_handle:
196
                with open(src_path, 'rb') as disk_file_handle:
197
                    assert zip_file_handle.read() == disk_file_handle.read()
198

  
199
    with open(source_dir / 'hello.zip', 'rb') as file_handle:
200
        zipfile_sha256_hash = sha256(file_handle.read()).digest().hex()
201

  
202
    expected_source_description = {
203
        'api_schema_version': [1, 0, 1],
204
        'source_name': 'hello',
205
        'source_copyright': [{
206
            'file': 'report.spdx',
207
            'sha256': spdx_report_sha256
208
        }, {
209
            'file': 'LICENSES/CC0-1.0.txt',
210
            'sha256': file_sha256_hashes['LICENSES/CC0-1.0.txt']
211
        }],
212
        'source_archives': {
213
            'zip': {
214
                'sha256': zipfile_sha256_hash,
215
            }
216
        },
217
        'upstream_url': 'https://git.koszko.org/hydrilla-source-package-example',
218
        'definitions': [{
219
            'type': 'resource',
220
            'identifier': 'helloapple',
221
            'version': [2021, 11, 10],
222
        }, {
223
            'type':       'resource',
224
            'identifier': 'hello-message',
225
            'version':     [2021, 11, 10],
226
        }, {
227
            'type': 'mapping',
228
            'identifier': 'helloapple',
229
            'version': [2021, 11, 10],
230
        }]
231
    }
232

  
233
    with open(source_dir / 'hello.json', 'rt') as file_handle:
234
        assert json.load(file_handle) == expected_source_description
235

  
236
# TODO: also verify on slightly different examples and check error handling

Also available in: Unified diff