aboutsummaryrefslogtreecommitdiff
path: root/tests/test_build.py
blob: a30cff415201104e1308c8e5730f7e0139bb24e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# SPDX-License-Identifier: CC0-1.0

# Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
#
# Available under the terms of Creative Commons Zero v1.0 Universal.

# Enable using with Python 3.7.
from __future__ import annotations

import pytest
import json
import shutil

from tempfile import TemporaryDirectory
from pathlib import Path, PurePosixPath
from hashlib import sha256
from zipfile import ZipFile
from contextlib import contextmanager

from jsonschema import ValidationError

from hydrilla import util as hydrilla_util
from hydrilla.builder import build, _version, local_apt
from hydrilla.builder.common_errors import *

from .helpers import *

here = Path(__file__).resolve().parent

expected_generated_by = {
    'name': 'hydrilla.builder',
    'version': _version.version
}

orig_srcdir = here / 'source-package-example'

index_text = (orig_srcdir / 'index.json').read_text()
index_obj = json.loads(hydrilla_util.strip_json_comments(index_text))

def read_files(*file_list):
    """
    Take names of files under srcdir and return a dict that maps them to their
    contents (as bytes).
    """
    return dict((name, (orig_srcdir / name).read_bytes()) for name in file_list)

dist_files = {
    **read_files('LICENSES/CC0-1.0.txt', 'bye.js', 'hello.js', 'message.js'),
    'report.spdx': b'dummy spdx output'
}
src_files = {
    **dist_files,
    **read_files('README.txt', 'README.txt.license', '.reuse/dep5',
                 'index.json')
}
extra_archive_files = {
}

sha256_hashes = dict((name, sha256(contents).digest().hex())
                     for name, contents in src_files.items())

del src_files['report.spdx']

expected_resources = [{
    '$schema': 'https://hydrilla.koszko.org/schemas/api_resource_description-1.schema.json',
    'source_name': 'hello',
    'source_copyright': [{
        'file': 'report.spdx',
        'sha256': sha256_hashes['report.spdx']
    }, {
        'file': 'LICENSES/CC0-1.0.txt',
        'sha256': 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': [{'identifier': 'hello-message'}],
    'scripts': [{
        'file': 'hello.js',
        'sha256': sha256_hashes['hello.js']
    }, {
        'file': 'bye.js',
        'sha256': sha256_hashes['bye.js']
    }],
    'generated_by': expected_generated_by
}, {
    '$schema': 'https://hydrilla.koszko.org/schemas/api_resource_description-1.schema.json',
    'source_name': 'hello',
    'source_copyright': [{
        'file': 'report.spdx',
        'sha256': sha256_hashes['report.spdx']
    }, {
        'file': 'LICENSES/CC0-1.0.txt',
        'sha256': 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': sha256_hashes['message.js']
    }],
    'generated_by': expected_generated_by
}]

expected_mapping = {
    '$schema': 'https://hydrilla.koszko.org/schemas/api_mapping_description-1.schema.json',
    'source_name': 'hello',
    'source_copyright': [{
        'file': 'report.spdx',
        'sha256': sha256_hashes['report.spdx']
    }, {
        'file': 'LICENSES/CC0-1.0.txt',
        'sha256': 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'
        }
    },
    'generated_by': expected_generated_by
}

expected_source_description = {
    '$schema': 'https://hydrilla.koszko.org/schemas/api_source_description-1.schema.json',
    'source_name': 'hello',
    'source_copyright': [{
        'file': 'report.spdx',
        'sha256': sha256_hashes['report.spdx']
    }, {
        'file': 'LICENSES/CC0-1.0.txt',
        'sha256': sha256_hashes['LICENSES/CC0-1.0.txt']
    }],
    'source_archives': {
        'zip': {
            'sha256': '!!!!value to fill during test!!!!',
        }
    },
    'upstream_url': 'https://git.koszko.org/hydrilla-source-package-example',
    'definitions': [{
        'type': 'resource',
        'identifier': 'helloapple',
        'long_name': 'Hello Apple',
        'version': [2021, 11, 10],
    }, {
        'type':       'resource',
        'identifier': 'hello-message',
        'long_name': 'Hello Message',
        'version':     [2021, 11, 10],
    }, {
        'type': 'mapping',
        'identifier': 'helloapple',
	'long_name': 'Hello Apple',
        'version': [2021, 11, 10],
    }],
    'generated_by': expected_generated_by
}

expected = [*expected_resources, expected_mapping, expected_source_description]

@pytest.fixture
def tmpdir() -> Iterable[str]:
    """
    Provide test case with a temporary directory that will be automatically
    deleted after the test.
    """
    with TemporaryDirectory() as tmpdir:
        yield Path(tmpdir)

def run_reuse(command, **kwargs):
    """
    Instead of running a 'reuse' command, check if 'mock_reuse_missing' file
    exists under root directory. If yes, raise FileNotFoundError as if 'reuse'
    command was missing. If not, check if 'README.txt.license' file exists
    in the requested directory and return zero if it does.
    """
    expected = ['reuse', '--root', '<root>',
                'lint' if 'lint' in command else 'spdx']

    root_path = Path(process_command(command, expected)['root'])

    if (root_path / 'mock_reuse_missing').exists():
        raise FileNotFoundError('dummy')

    is_reuse_compliant = (root_path / 'README.txt.license').exists()

    return MockedCompletedProcess(command, 1 - is_reuse_compliant,
                                  stdout=f'dummy {expected[-1]} output',
                                  text_output=kwargs.get('text'))

mocked_piggybacked_archives = [
    PurePosixPath('apt/something.deb'),
    PurePosixPath('apt/something.orig.tar.gz'),
    PurePosixPath('apt/something.debian.tar.xz'),
    PurePosixPath('othersystem/other-something.tar.gz')
]

@pytest.fixture
def mock_piggybacked_apt_system(monkeypatch):
    """Make local_apt.piggybacked_system() return a mocked result."""
    # We set 'td' to a temporary dir path further below.
    td = None

    class MockedPiggybacked:
        """Minimal mock of Piggybacked object."""
        package_license_files = [PurePosixPath('.apt-root/.../copyright')]
        package_must_depend = [{'identifier': 'apt-common-licenses'}]

        def resolve_file(path):
            """
            For each path that starts with '.apt-root' return a valid
            dummy file path.
            """
            if path.parts[0] != '.apt-root':
                return None

            (td / path.name).write_text(f'dummy {path.name}')

            return (td / path.name)

        def archive_files():
            """Yield some valid dummy file path tuples."""
            for desired_path in mocked_piggybacked_archives:
                real_path = td / desired_path.name
                real_path.write_text(f'dummy {desired_path.name}')

                yield desired_path, real_path

    @contextmanager
    def mocked_piggybacked_system(piggyback_def, piggyback_files):
        """Mock the execution of local_apt.piggybacked_system()."""
        assert piggyback_def == {
	    'system': 'apt',
	    'distribution': 'nabia',
	    'packages': ['somelib=1.0'],
	    'dependencies': False
        }
        if piggyback_files is not None:
            assert {str(path) for path in mocked_piggybacked_archives} == \
                {path.relative_to(piggyback_files).as_posix()
                 for path in piggyback_files.rglob('*') if path.is_file()}

        yield MockedPiggybacked

    monkeypatch.setattr(local_apt, 'piggybacked_system',
                        mocked_piggybacked_system)

    with TemporaryDirectory() as td:
        td = Path(td)
        yield

@pytest.fixture
def sample_source():
    """Prepare a directory with sample Haketilo source package."""
    with TemporaryDirectory() as td:
        sample_source = Path(td) / 'hello'
        for name, contents in src_files.items():
            path = sample_source / name
            path.parent.mkdir(parents=True, exist_ok=True)
            path.write_bytes(contents)

        yield sample_source

variant_makers = []
def variant_maker(function):
    """Decorate function by placing it in variant_makers array."""
    variant_makers.append(function)
    return function

@variant_maker
def sample_source_change_index_json(monkeypatch, sample_source):
    """
    Return a non-standard path for index.json. Ensure parent directories exist.
    """
    # Use a path under sample_source so that it gets auto-deleted after the
    # test. Use a file under .git because .git is ignored by REUSE.
    path = sample_source / '.git' / 'replacement.json'
    path.parent.mkdir()
    return path

@variant_maker
def sample_source_add_comments(monkeypatch, sample_source):
    """Add index.json comments that should be preserved."""
    for dictionary in (index_obj, expected_source_description):
        monkeypatch.setitem(dictionary, 'comment', 'index.json comment')

    for i, dicts in enumerate(zip(index_obj['definitions'], expected)):
        for dictionary in dicts:
            monkeypatch.setitem(dictionary, 'comment', 'index.json comment')

@variant_maker
def sample_source_remove_spdx(monkeypatch, sample_source):
    """Remove spdx report generation."""
    monkeypatch.delitem(index_obj, 'reuse_generate_spdx_report')

    for obj, key in [
            (index_obj, 'copyright'),
            *((definition, 'source_copyright') for definition in expected)
    ]:
        new_list = [r for r in obj[key] if r['file'] != 'report.spdx']
        monkeypatch.setitem(obj, key, new_list)

    monkeypatch.delitem(dist_files, 'report.spdx')

    # To verify that reuse does not get called now, make mocked subprocess.run()
    # raise an error if called.
    (sample_source / 'mock_reuse_missing').touch()

@variant_maker
def sample_source_remove_additional_files(monkeypatch, sample_source):
    """Use default value ([]) for 'additionall_files' property."""
    monkeypatch.delitem(index_obj, 'additional_files')

    for name in 'README.txt', 'README.txt.license', '.reuse/dep5':
        monkeypatch.delitem(src_files, name)

@variant_maker
def sample_source_remove_script(monkeypatch, sample_source):
    """Use default value ([]) for 'scripts' property in one of the resources."""
    monkeypatch.delitem(index_obj['definitions'][1], 'scripts')

    monkeypatch.setitem(expected_resources[1], 'scripts', [])

    for files in dist_files, src_files:
        monkeypatch.delitem(files, 'message.js')

@variant_maker
def sample_source_remove_payloads(monkeypatch, sample_source):
    """Use default value ({}) for 'payloads' property in mapping."""
    monkeypatch.delitem(index_obj['definitions'][2], 'payloads')

    monkeypatch.setitem(expected_mapping, 'payloads', {})

@variant_maker
def sample_source_remove_uuids(monkeypatch, sample_source):
    """Don't use UUIDs (they are optional)."""
    for definition in index_obj['definitions']:
        monkeypatch.delitem(definition, 'uuid')

    for description in expected:
        if 'uuid' in description:
            monkeypatch.delitem(description, 'uuid')

@variant_maker
def sample_source_add_extra_props(monkeypatch, sample_source):
    """Add some unrecognized properties that should be stripped."""
    to_process = [index_obj]
    while to_process:
        processed = to_process.pop()

        if type(processed) is list:
            to_process.extend(processed)
        elif type(processed) is dict and 'spurious_property' not in processed:
            to_process.extend(v for k, v in processed.items()
                              if k != 'payloads')
            monkeypatch.setitem(processed, 'spurious_property', 'some_value')

piggyback_archive_names = [
    'apt/something.deb',
    'apt/something.orig.tar.gz',
    'apt/something.debian.tar.xz',
    'othersystem/other-something.tar.gz'
]

@variant_maker
def sample_source_add_piggyback(monkeypatch, sample_source,
                                extra_build_args={}):
    """Add piggybacked foreign system packages."""
    old_build = build.Build
    new_build = lambda *a, **kwa: old_build(*a, **kwa, **extra_build_args)
    monkeypatch.setattr(build, 'Build', new_build)

    monkeypatch.setitem(index_obj, 'piggyback_on', {
	'system': 'apt',
	'distribution': 'nabia',
	'packages': ['somelib=1.0'],
	'dependencies': False
    })
    schema = 'https://hydrilla.koszko.org/schemas/package_source-2.schema.json'
    monkeypatch.setitem(index_obj, '$schema', schema)

    new_refs = {}
    for name in '.apt-root/.../copyright', '.apt-root/.../script.js':
        contents = f'dummy {PurePosixPath(name).name}'.encode()
        digest = sha256(contents).digest().hex()
        monkeypatch.setitem(dist_files, name, contents)
        monkeypatch.setitem(sha256_hashes, name, digest)
        new_refs[PurePosixPath(name).name] = {'file': name, 'sha256': digest}

    for obj in expected:
        new_list = [*obj['source_copyright'], new_refs['copyright']]
        monkeypatch.setitem(obj, 'source_copyright', new_list)

    for obj in expected_resources:
        new_list = [{'identifier': 'apt-common-licenses'}, *obj['dependencies']]
        monkeypatch.setitem(obj, 'dependencies', new_list)

    for obj in index_obj['definitions'][0], expected_resources[0]:
        new_list = [new_refs['script.js'], *obj['scripts']]
        monkeypatch.setitem(obj, 'scripts', new_list)

    for name in piggyback_archive_names:
        path = PurePosixPath('hello.foreign-packages') / name
        monkeypatch.setitem(extra_archive_files, str(path),
                            f'dummy {path.name}'.encode())

def prepare_foreign_packages_dir(path):
    """
    Put some dummy archive in the directory so that it can be passed to
    piggybacked_system().
    """
    for name in piggyback_archive_names:
        archive_path = path / name
        archive_path.parent.mkdir(parents=True, exist_ok=True)
        archive_path.write_text(f'dummy {archive_path.name}')

@variant_maker
def sample_source_add_piggyback_pass_archives(monkeypatch, sample_source):
    """
    Add piggybacked foreign system packages, use pre-downloaded foreign package
    archives (have Build() find them in their default directory).
    """
    # Dir next to 'sample_source' will also be gc'd by sample_source() fixture.
    foreign_packages_dir = sample_source.parent / 'arbitrary-name'

    prepare_foreign_packages_dir(foreign_packages_dir)

    sample_source_add_piggyback(monkeypatch, sample_source,
                                {'piggyback_files': foreign_packages_dir})

@variant_maker
def sample_source_add_piggyback_find_archives(monkeypatch, sample_source):
    """
    Add piggybacked foreign system packages, use pre-downloaded foreign package
    archives (specify their directory as argument to Build()).
    """
    # Dir next to 'sample_source' will also be gc'd by sample_source() fixture.
    foreign_packages_dir = sample_source.parent / 'hello.foreign-packages'

    prepare_foreign_packages_dir(foreign_packages_dir)

    sample_source_add_piggyback(monkeypatch, sample_source)

@variant_maker
def sample_source_add_piggyback_no_download(monkeypatch, sample_source,
                                            pass_directory_to_build=False):
    """
    Add piggybacked foreign system packages, use pre-downloaded foreign package
    archives.
    """
    # Use a dir next to 'sample_source'; have it gc'd by sample_source fixture.
    if pass_directory_to_build:
        foreign_packages_dir = sample_source.parent / 'arbitrary-name'
    else:
        foreign_packages_dir = sample_source.parent / 'hello.foreign-packages'

    prepare_foreign_packages_dir(foreign_packages_dir)

    sample_source_add_piggyback(monkeypatch, sample_source)

@pytest.fixture(params=[lambda m, s: None, *variant_makers])
def sample_source_make_variants(request, monkeypatch, sample_source,
                                mock_piggybacked_apt_system):
    """
    Prepare a directory with sample Haketilo source package in multiple slightly
    different versions (all correct). Return an index.json path that should be
    used when performing test build.
    """
    index_path = request.param(monkeypatch, sample_source) or Path('index.json')

    index_text = json.dumps(index_obj)

    (sample_source / index_path).write_text(index_text)

    monkeypatch.setitem(src_files, 'index.json', index_text.encode())

    return index_path

@pytest.mark.subprocess_run(build, run_reuse)
@pytest.mark.usefixtures('mock_subprocess_run')
def test_build(sample_source, sample_source_make_variants, tmpdir):
    """Build the sample source package and verify the produced files."""
    index_json_path = sample_source_make_variants

    # First, build the package
    build.Build(sample_source, index_json_path).write_package_files(tmpdir)

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

    # Verify files under 'file/'
    file_dir = tmpdir / 'file' / 'sha256'

    for name, contents in dist_files.items():
        dist_file_path = file_dir / sha256_hashes[name]
        assert dist_file_path.is_file()
        assert dist_file_path.read_bytes() == contents

    assert {p.name for p in file_dir.iterdir()} == \
        {sha256_hashes[name] for name in dist_files.keys()}

    # Verify files under 'resource/'
    resource_dir = tmpdir / 'resource'

    assert {rj['identifier'] for rj in expected_resources} == \
        {path.name for path in resource_dir.iterdir()}

    for resource_json in expected_resources:
        subdir = resource_dir / resource_json['identifier']
        assert ['2021.11.10'] == [path.name for path in subdir.iterdir()]

        assert json.loads((subdir / '2021.11.10').read_text()) == resource_json

        hydrilla_util.validator_for('api_resource_description-1.0.1.schema.json')\
                     .validate(resource_json)

    # Verify files under 'mapping/'
    mapping_dir = tmpdir / '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()]

    assert json.loads((subdir / '2021.11.10').read_text()) == expected_mapping

    hydrilla_util.validator_for('api_mapping_description-1.0.1.schema.json')\
                 .validate(expected_mapping)

    # Verify files under 'source/'
    source_dir = tmpdir / 'source'
    assert {'hello.json', 'hello.zip'} == \
        {path.name for path in source_dir.iterdir()}

    archive_files = {**dict((f'hello/{name}', contents)
                            for name, contents in src_files.items()),
                     **extra_archive_files}

    with ZipFile(source_dir / 'hello.zip', 'r') as archive:
        print(archive.namelist())
        assert len(archive.namelist()) == len(archive_files)

        for name, contents in archive_files.items():
            assert archive.read(name) == contents

    zip_ref = expected_source_description['source_archives']['zip']
    zip_contents = (source_dir / 'hello.zip').read_bytes()
    zip_ref['sha256'] = sha256(zip_contents).digest().hex()

    assert json.loads((source_dir / 'hello.json').read_text()) == \
        expected_source_description

    hydrilla_util.validator_for('api_source_description-1.0.1.schema.json')\
                 .validate(expected_source_description)

error_makers = []
def error_maker(function):
    """Decorate function by placing it in error_makers array."""
    error_makers.append(function)

@error_maker
def sample_source_error_missing_file(monkeypatch, sample_source):
    """
    Modify index.json to expect missing report.spdx file and cause an error.
    """
    monkeypatch.delitem(index_obj, 'reuse_generate_spdx_report')
    return FileNotFoundError

@error_maker
def sample_source_error_index_schema(monkeypatch, sample_source):
    """Modify index.json to be incompliant with the schema."""
    monkeypatch.delitem(index_obj, 'definitions')
    return ValidationError

@error_maker
def sample_source_error_bad_comment(monkeypatch, sample_source):
    """Modify index.json to have an invalid '/' in it."""
    return json.JSONDecodeError, json.dumps(index_obj) + '/something\n'

@error_maker
def sample_source_error_bad_json(monkeypatch, sample_source):
    """Modify index.json to not be valid json even after comment stripping."""
    return json.JSONDecodeError, json.dumps(index_obj) + '???/\n'

@error_maker
def sample_source_error_missing_reuse(monkeypatch, sample_source):
    """Cause mocked reuse process invocation to fail with FileNotFoundError."""
    (sample_source / 'mock_reuse_missing').touch()
    return build.ReuseError

@error_maker
def sample_source_error_missing_license(monkeypatch, sample_source):
    """Remove a file to make package REUSE-incompliant."""
    (sample_source / 'README.txt.license').unlink()
    return build.ReuseError

@error_maker
def sample_source_error_file_outside(monkeypatch, sample_source):
    """Make index.json illegally reference a file outside srcdir."""
    new_list = [*index_obj['copyright'], {'file': '../abc'}]
    monkeypatch.setitem(index_obj, 'copyright', new_list)
    return FileReferenceError

@error_maker
def sample_source_error_reference_itself(monkeypatch, sample_source):
    """Make index.json illegally reference index.json."""
    new_list = [*index_obj['copyright'], {'file': 'index.json'}]
    monkeypatch.setitem(index_obj, 'copyright', new_list)
    return FileReferenceError

@error_maker
def sample_source_error_report_excluded(monkeypatch, sample_source):
    """
    Make index.json require generation of report.spdx but don't include it among
    copyright files.
    """
    new_list = [file_ref for file_ref in index_obj['copyright']
                if file_ref['file'] != 'report.spdx']
    monkeypatch.setitem(index_obj, 'copyright', new_list)
    return FileReferenceError

@pytest.fixture(params=error_makers)
def sample_source_make_errors(request, monkeypatch, sample_source):
    """
    Prepare a directory with sample Haketilo source package in multiple slightly
    broken versions. Return an error type that should be raised when running
    test build.
    """
    index_text = None
    error_type = request.param(monkeypatch, sample_source)
    if type(error_type) is tuple:
        error_type, index_text = error_type

    index_text = index_text or json.dumps(index_obj)

    (sample_source / 'index.json').write_text(index_text)

    monkeypatch.setitem(src_files, 'index.json', index_text.encode())

    return error_type

@pytest.mark.subprocess_run(build, run_reuse)
@pytest.mark.usefixtures('mock_subprocess_run')
def test_build_error(tmpdir, sample_source, sample_source_make_errors):
    """Try building the sample source package and verify generated errors."""
    error_type = sample_source_make_errors

    dstdir = Path(tmpdir) / 'dstdir'
    tmpdir = Path(tmpdir) / 'example'

    dstdir.mkdir(exist_ok=True)
    tmpdir.mkdir(exist_ok=True)

    with pytest.raises(error_type):
        build.Build(sample_source, Path('index.json'))\
             .write_package_files(dstdir)