alpm-package 0.4.2

Library and command line tool for the creation of ALPM based packages
Documentation
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
//! Integration tests for `alpm-package`.

use std::{
    fs::{File, FileTimes, create_dir, create_dir_all, read},
    io::Write,
    os::unix::fs::symlink,
    path::{Path, PathBuf},
    time::SystemTime,
};

use alpm_compress::compression::{
    Bzip2CompressionLevel,
    CompressionSettings,
    GzipCompressionLevel,
    XzCompressionLevel,
    ZstdCompressionLevel,
    ZstdThreads,
};
use alpm_mtree::create_mtree_v2_from_input_dir;
use alpm_package::{
    Error,
    InputDir,
    MetadataEntry,
    OutputDir,
    Package,
    PackageCreationConfig,
    PackageEntry,
    PackageInput,
    PackageReader,
};
use alpm_types::{Blake2b512Checksum, INSTALL_SCRIPTLET_FILE_NAME, MetadataFileName};
use filetime::{FileTime, set_symlink_file_times};
use log::{LevelFilter, debug};
use rstest::rstest;
use simplelog::{ColorChoice, Config, TermLogger, TerminalMode};
use tar::EntryType;
use tempfile::TempDir;
use testresult::TestResult;

const VALID_BUILDINFO_V2_DATA: &str = r#"
format = 2
builddate = 1
builddir = /build
startdir = /startdir/
buildtool = devtools
buildtoolver = 1:1.2.1-1-any
buildenv = ccache
buildenv = color
installed = bar-1.2.3-1-any
installed = beh-2.2.3-4-any
options = lto
options = !strip
packager = John Doe <john@example.org>
pkgarch = any
pkgbase = example
pkgbuild_sha256sum = b5bb9d8014a0f9b1d61e21e796d78dccdf1352f23cd32812f4850b878ae4944c
pkgname = example
pkgver = 1:1.0.0-1
"#;

const VALID_PKGINFO_V2_DATA: &str = r#"
pkgname = example
pkgbase = example
xdata = pkgtype=pkg
pkgver = 1:1.0.0-1
pkgdesc = A project that does something
url = https://example.org/
builddate = 1
packager = John Doe <john@example.org>
size = 181849963
arch = any
license = GPL-3.0-or-later
license = LGPL-3.0-or-later
replaces = other-package>0.9.0-3
group = package-group
group = other-package-group
conflict = conflicting-package<1.0.0
conflict = other-conflicting-package<1.0.0
provides = some-component
provides = some-other-component=1:1.0.0-1
backup = etc/example/config.toml
backup = etc/example/other-config.txt
depend = glibc
depend = gcc-libs
optdepend = python: for special-python-script.py
optdepend = ruby: for special-ruby-script.rb
makedepend = cmake
makedepend = python-sphinx
checkdepend = extra-test-tool
checkdepend = other-extra-test-tool
"#;

const VALID_INSTALL_SCRIPTLET: &str = r#"
pre_install() {
    echo "Preparing to install package version $1"
}

post_install() {
    echo "Package version $1 installed"
}

pre_upgrade() {
    echo "Preparing to upgrade from version $2 to $1"
}

post_upgrade() {
    echo "Upgraded from version $2 to $1"
}

pre_remove() {
    echo "Preparing to remove package version $1"
}

post_remove() {
    echo "Package version $1 removed"
}
"#;

/// Initializes a global [`TermLogger`].
fn init_logger() {
    if TermLogger::init(
        LevelFilter::Debug,
        Config::default(),
        TerminalMode::Mixed,
        ColorChoice::Auto,
    )
    .is_err()
    {
        debug!("Not initializing another logger, as one is initialized already.");
    }
}

/// Returns a default [`FileTimes`] struct with all times set to UNIX_EPOCH.
fn default_filetimes() -> FileTimes {
    FileTimes::new()
        .set_accessed(SystemTime::UNIX_EPOCH)
        .set_modified(SystemTime::UNIX_EPOCH)
}

/// Returns a default [`FileTime`] struct with all times set to UNIX_EPOCH.
fn default_filetime() -> FileTime {
    FileTime::from_unix_time(0, 0)
}

/// Creates data files and directories below `path`.
fn create_data_files(path: impl AsRef<Path>) -> TestResult {
    let path = path.as_ref();

    // Create an arbitrary file at root of the package. (.ARBITRARY)
    let mut file = File::create(path.join(".ARBITRARY"))?;
    write!(file, "This is an arbitrary file.")?;
    file.set_times(default_filetimes())?;

    // Create dummy directory structure
    create_dir_all(path.join("foo/bar/baz"))?;
    create_dir_all(path.join("foo/bar/buh"))?;

    // Create dummy text file
    let mut file = File::create(path.join("foo/beh.txt"))?;
    write!(file, "test")?;
    file.set_times(default_filetimes())?;

    // Create relative symlink to actual text file
    let existing_target_link = path.join("foo/bar/baz/beh.txt");
    symlink("../../beh.txt", &existing_target_link)?;
    set_symlink_file_times(
        &existing_target_link,
        default_filetime(),
        default_filetime(),
    )?;

    // Create symlink to absolute, non-existing file.
    let non_existing_target_link = path.join("foo/bar/baz/buh.txt");
    symlink(
        "/tmp/something/very/unlikely/to/ever/exist/hopefully.txt",
        &non_existing_target_link,
    )?;
    set_symlink_file_times(
        &non_existing_target_link,
        default_filetime(),
        default_filetime(),
    )?;

    // Adjust file times of directory structure.
    for path in [
        path.join("foo/bar/baz"),
        path.join("foo/bar/buh"),
        path.join("foo/bar"),
        path.join("foo"),
    ] {
        let file = File::open(path)?;
        file.set_times(default_filetimes())?;
    }

    Ok(())
}

/// Creates a [BUILDINFO] file in `path`.
///
/// [BUILDINFO]: https://alpm.archlinux.page/specifications/BUILDINFO.5.html
fn create_build_info_file(path: impl AsRef<Path>) -> TestResult {
    let path = path.as_ref();
    let mut file = File::create(path.join(MetadataFileName::BuildInfo.as_ref()))?;
    write!(file, "{VALID_BUILDINFO_V2_DATA}")?;
    file.set_times(default_filetimes())?;

    Ok(())
}

/// Creates a [PKGINFO] file in `path`.
///
/// [PKGINFO]: https://alpm.archlinux.page/specifications/PKGINFO.5.html
fn create_package_info_file(path: impl AsRef<Path>) -> TestResult {
    let path = path.as_ref();
    let mut file = File::create(path.join(MetadataFileName::PackageInfo.as_ref()))?;
    write!(file, "{VALID_PKGINFO_V2_DATA}")?;
    file.set_times(default_filetimes())?;

    Ok(())
}

/// Creates an [alpm-install-scriptlet] file in `path`.
///
/// [alpm-install-scriptlet]: https://alpm.archlinux.page/specifications/alpm-install-scriptlet.5.html
fn create_install_scriptlet(path: impl AsRef<Path>) -> TestResult {
    let path = path.as_ref();
    let mut file = File::create(path.join(INSTALL_SCRIPTLET_FILE_NAME))?;
    write!(file, "{VALID_INSTALL_SCRIPTLET}")?;
    file.set_times(default_filetimes())?;

    Ok(())
}

/// Creates a [PKGINFO] file in `path`.
///
/// [PKGINFO]: https://alpm.archlinux.page/specifications/PKGINFO.5.html
fn create_mtree_file(path: impl AsRef<Path>) -> TestResult {
    let path = path.as_ref();
    let _mtree_file = create_mtree_v2_from_input_dir(path)?;
    let file = File::open(path.join(MetadataFileName::Mtree.as_ref()))?;
    file.set_times(default_filetimes())?;

    Ok(())
}

/// A config for creating an input directory.
struct InputDirConfig {
    build_info: bool,
    data_files: bool,
    mtree: bool,
    package_info: bool,
    scriptlet: bool,
}

/// Prepares the input directory with all necessary files for package creation.
fn prepare_input_dir(path: impl AsRef<Path>, config: &InputDirConfig) -> TestResult {
    let path = path.as_ref();

    // Create package data files.
    if config.data_files {
        create_data_files(path)?;
    }
    if config.scriptlet {
        create_install_scriptlet(path)?;
    }
    // Create metadata files.
    if config.build_info {
        create_build_info_file(path)?;
    }
    if config.package_info {
        create_package_info_file(path)?;
    }
    if config.mtree {
        create_mtree_file(path)?;
    }

    Ok(())
}

/// Creates a package from an input directory and returns it and its hash digest.
///
/// Creates an `input` and `output` directory in `path` (the caller has to ensure that those are
/// unique!).
/// Uses `input_dir_config` to configure the input directory and `compression` during the creation
/// of the package.
fn package_digest(
    path: impl AsRef<Path>,
    input: &str,
    output: &str,
    compression: CompressionSettings,
    input_dir_config: &InputDirConfig,
) -> TestResult<(PathBuf, Blake2b512Checksum)> {
    let path = path.as_ref();
    debug!("Creating package digest while using test dir {path:?}");

    // Create the input and output directories.
    let input_dir_path = path.join(input);
    create_dir(&input_dir_path)?;
    let input_dir = InputDir::new(input_dir_path)?;
    let output_dir = OutputDir::new(path.join(output))?;

    // Prepare the input directory based on InputDirConfig.
    prepare_input_dir(&input_dir, input_dir_config)?;

    // Create PackageInput
    let package_input: PackageInput = input_dir.try_into()?;
    let config = PackageCreationConfig::new(package_input, output_dir, compression)?;

    // Create package file.
    let package = Package::try_from(&config)?;
    let buf = read(package.to_path_buf())?;

    Ok((
        package.to_path_buf(),
        Blake2b512Checksum::calculate_from(buf),
    ))
}

/// Check that [alpm-package] files can be created reproducibly from input directories.
///
/// This test assumes, that all files in the input directory have the same timestamps and contain
/// the same data when re-creating a package.
///
/// [alpm-package]: https://alpm.archlinux.page/specifications/alpm-package.7.html
#[rstest]
#[case::bzip2_compression_all_files(
    CompressionSettings::Bzip2 { compression_level: Bzip2CompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: true },
)]
#[case::bzip2_compression_no_scriptlet(
    CompressionSettings::Bzip2 { compression_level: Bzip2CompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: false },
)]
#[case::bzip2_compression_no_data_files(
    CompressionSettings::Bzip2 { compression_level: Bzip2CompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: false, mtree: true, package_info: true, scriptlet: false },
)]
#[case::gzip_compression_all_files(
    CompressionSettings::Gzip { compression_level: GzipCompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: true },
)]
#[case::gzip_compression_no_scriptlet(
    CompressionSettings::Gzip { compression_level: GzipCompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: false },
)]
#[case::gzip_compression_no_data_files(
    CompressionSettings::Gzip { compression_level: GzipCompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: false, mtree: true, package_info: true, scriptlet: false },
)]
#[case::xz_compression_all_files(
    CompressionSettings::Xz { compression_level: XzCompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: true },
)]
#[case::xz_compression_no_scriptlet(
    CompressionSettings::Xz { compression_level: XzCompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: false },
)]
#[case::xz_compression_no_data_files(
    CompressionSettings::Xz { compression_level: XzCompressionLevel::default() },
    InputDirConfig { build_info: true, data_files: false, mtree: true, package_info: true, scriptlet: false },
)]
#[case::zstd_compression_all_files(
    CompressionSettings::Zstd { compression_level: ZstdCompressionLevel::default(), threads: ZstdThreads::all() },
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: true },
)]
#[case::zstd_compression_no_scriptlet(
    CompressionSettings::Zstd { compression_level: ZstdCompressionLevel::default(), threads: ZstdThreads::all() },
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: false },
)]
#[case::zstd_compression_no_data_files(
    CompressionSettings::Zstd { compression_level: ZstdCompressionLevel::default(), threads: ZstdThreads::all() },
    InputDirConfig { build_info: true, data_files: false, mtree: true, package_info: true, scriptlet: false },
)]
#[case::no_compression_all_files(
    CompressionSettings::None,
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: true },
)]
#[case::no_compression_no_scriptlet(
    CompressionSettings::None,
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: false },
)]
#[case::no_compression_no_data_files(
    CompressionSettings::None,
    InputDirConfig { build_info: true, data_files: false, mtree: true, package_info: true, scriptlet: false },
)]
fn create_package_from_input(
    #[case] compression: CompressionSettings,
    #[case] input_dir_config: InputDirConfig,
) -> TestResult {
    init_logger();

    // Create a common temporary dir.
    let temp_dir = TempDir::new()?;
    let test_dir = temp_dir.path();

    let (package_a, package_digest_a) = package_digest(
        test_dir,
        "input1",
        "output1",
        compression.clone(),
        &input_dir_config,
    )?;
    debug!("Created package A: {package_a:?}");

    let (package_b, package_digest_b) = package_digest(
        test_dir,
        "input2",
        "output2",
        compression.clone(),
        &input_dir_config,
    )?;
    debug!("Created package B: {package_b:?}");

    assert_eq!(package_digest_a, package_digest_b);
    Ok(())
}

/// Ensures that [`PackageInput::from_input_dir`] fails on missing metadata files.
#[rstest]
#[case::no_build_info(
    InputDirConfig { build_info: false, data_files: false, mtree: true, package_info: true, scriptlet: false },
    PathBuf::from(MetadataFileName::BuildInfo.as_ref()),
)]
#[case::no_package_info(
    InputDirConfig { build_info: true, data_files: false, mtree: true, package_info: false, scriptlet: false },
    PathBuf::from(MetadataFileName::PackageInfo.as_ref()),
)]
#[case::no_mtree(
    InputDirConfig { build_info: true, data_files: false, mtree: false, package_info: true, scriptlet: false },
    PathBuf::from(MetadataFileName::Mtree.as_ref()),
)]
fn package_input_fails_on_missing_metadata(
    #[case] config: InputDirConfig,
    #[case] expected: PathBuf,
) -> TestResult {
    init_logger();

    // Create a common temporary dir.
    let temp_dir = TempDir::new()?;
    let input_dir = InputDir::new(temp_dir.path().to_path_buf())?;

    prepare_input_dir(&input_dir, &config)?;

    if let Err(error) = PackageInput::try_from(input_dir) {
        match error {
            alpm_package::Error::Input(alpm_package::input::Error::FileIsMissing {
                path,
                input_dir: _,
            }) => assert_eq!(path, expected),
            _ => panic!("Did not return the correct error variant"),
        }
    } else {
        panic!("Should have returned an error but succeeded");
    }

    Ok(())
}

/// Ensures that [`PackageInput`] has the correct amount of files depending on whether data files
/// and scriptlet are present.
#[rstest]
#[case::all_files(
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: true },
)]
#[case::no_scriptlet(
    InputDirConfig { build_info: true, data_files: true, mtree: true, package_info: true, scriptlet: false },
)]
#[case::no_data_files_no_scriptlet(
    InputDirConfig { build_info: true, data_files: false, mtree: true, package_info: true, scriptlet: false },
)]
fn test_package_input_methods(#[case] config: InputDirConfig) -> TestResult {
    init_logger();

    // Create a common temporary dir.
    let temp_dir = TempDir::new()?;
    let input_dir = InputDir::new(temp_dir.path().to_path_buf())?;

    prepare_input_dir(&input_dir, &config)?;
    let package_input: PackageInput = input_dir.try_into()?;

    // Ensure that all metadata can be retrieved.
    let _mtree = package_input.mtree()?;
    let _build_info = package_input.build_info();
    let _package_info = package_input.package_info();

    if config.scriptlet {
        assert!(package_input.install_scriptlet().is_some());
        if !config.data_files {
            // Only metadata files and the scriptlet are present
            assert!(package_input.relative_paths().len() == 4)
        } else {
            assert!(package_input.relative_paths().len() > 4)
        }
    } else {
        assert!(package_input.install_scriptlet().is_none());
        if !config.data_files {
            // Only metadata files are present
            assert!(package_input.relative_paths().len() == 3)
        } else {
            assert!(package_input.relative_paths().len() > 3)
        }
    }

    Ok(())
}

/// Ensures that [`PackageCreationConfig::new`] fails on overlapping input and output directories.
///
/// This includes that the output directory may not be a subdirectory of the input directory and
/// vice versa.
#[test]
fn package_creation_config_new_fails() -> TestResult {
    init_logger();

    let config = InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: false,
    };

    // Create a common temporary dir.
    let temp_dir = TempDir::new()?;
    let path_equal = temp_dir.path().join("equal-input");
    create_dir_all(&path_equal)?;
    let input_dir = InputDir::new(path_equal)?;

    prepare_input_dir(&input_dir, &config)?;
    let package_input: PackageInput = input_dir.clone().try_into()?;

    // Create an input and output at the same location.
    let output_dir = OutputDir::new(input_dir.to_path_buf())?;
    match PackageCreationConfig::new(package_input.clone(), output_dir, CompressionSettings::None) {
        Err(error) => assert!(matches!(
            error,
            alpm_package::Error::InputDirIsOutputDir { path: _ }
        )),
        Ok(_) => panic!("Succeeded, but should have failed"),
    }

    // Set the output to a sudirectory of the input directory.
    let output_dir = OutputDir::new(input_dir.join("output-in-input"))?;
    match PackageCreationConfig::new(package_input.clone(), output_dir, CompressionSettings::None) {
        Err(error) => assert!(matches!(
            error,
            alpm_package::Error::OutputDirInInputDir {
                input_path: _,
                output_path: _
            }
        )),
        Ok(_) => panic!("Succeeded, but should have failed"),
    }

    // Set the input to a subdirectory of the output directory.
    let output_dir = OutputDir::new(temp_dir.path().to_path_buf())?;
    match PackageCreationConfig::new(package_input, output_dir, CompressionSettings::None) {
        Err(error) => assert!(matches!(
            error,
            alpm_package::Error::InputDirInOutputDir {
                input_path: _,
                output_path: _
            }
        )),
        Ok(_) => panic!("Succeeded, but should have failed"),
    }

    Ok(())
}

/// Helper function to create a package from an input directory config and
/// optional compression settings.
fn create_package(
    temp_dir: &TempDir,
    config: &InputDirConfig,
    compression: CompressionSettings,
) -> TestResult<Package> {
    let input_dir_path = temp_dir.path().join("input");
    let output_dir_path = temp_dir.path().join("output");

    create_dir(&input_dir_path)?;
    let input_dir = InputDir::new(input_dir_path)?;
    prepare_input_dir(&input_dir, config)?;

    let package_input: PackageInput = input_dir.try_into()?;
    let output_dir = OutputDir::new(output_dir_path)?;
    let config = PackageCreationConfig::new(package_input, output_dir, compression)?;
    Ok(Package::try_from(&config)?)
}

// TODO: This could be a matrix test when the new version of `rstest` is out.
//
// This is avoided now due to generated test names being too long.
//
// See these links:
//
// - <https://github.com/la10736/rstest/issues/320>
// - <https://gitlab.archlinux.org/archlinux/alpm/alpm/-/merge_requests/255#note_284079.
#[rstest]
#[case::all_files_bzip2(
    CompressionSettings::Bzip2 { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: true,
        mtree: true,
        package_info: true,
        scriptlet: true,
    }
)]
#[case::no_data_files_bzip2(
    CompressionSettings::Bzip2 { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: true,
    }
)]
#[case::no_scriptlet_bzip2(
    CompressionSettings::Bzip2 { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: true,
        mtree: true,
        package_info: true,
        scriptlet: false,
    }
)]
#[case::no_data_files_no_scriptlet_bzip2(
    CompressionSettings::Bzip2 { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: false,
    }
)]
#[case::all_files_gzip(
    CompressionSettings::Gzip { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: true,
        mtree: true,
        package_info: true,
        scriptlet: true,
    }
)]
#[case::no_data_files_gzip(
    CompressionSettings::Gzip { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: true,
    }
)]
#[case::no_scriptlet_gzip(
    CompressionSettings::Gzip { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: true,
        mtree: true,
        package_info: true,
        scriptlet: false,
    }
)]
#[case::no_data_files_no_scriptlet_gzip(
    CompressionSettings::Gzip { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: false,
    }
)]
#[case::all_files_xz(
    CompressionSettings::Xz { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: true,
        mtree: true,
        package_info: true,
        scriptlet: true,
    }
)]
#[case::no_data_files_xz(
    CompressionSettings::Xz { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: true,
    }
)]
#[case::no_scriptlet_xz(
    CompressionSettings::Xz { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: true,
        mtree: true,
        package_info: true,
        scriptlet: false,
    }
)]
#[case::no_data_files_no_scriptlet_xz(
    CompressionSettings::Xz { compression_level: Default::default() },
    InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: false,
    }
)]
#[case::all_files_zstd(
    CompressionSettings::Zstd {
        compression_level: Default::default(),
        threads: ZstdThreads::all(),
    },
    InputDirConfig {
        build_info: true,
        data_files: true,
        mtree: true,
        package_info: true,
        scriptlet: true,
    }
)]
#[case::no_data_files_zstd(
    CompressionSettings::Zstd {
        compression_level: Default::default(),
        threads: ZstdThreads::all(),
    },
    InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: true,
    }
)]
#[case::no_scriptlet_zstd(
    CompressionSettings::Zstd {
        compression_level: Default::default(),
        threads: ZstdThreads::all(),
    },
    InputDirConfig {
        build_info: true,
        data_files: true,
        mtree: true,
        package_info: true,
        scriptlet: false,
    }
)]
#[case::no_data_files_no_scriptlet_zstd(
    CompressionSettings::Zstd {
        compression_level: Default::default(),
        threads: ZstdThreads::all(),
    },
    InputDirConfig {
        build_info: true,
        data_files: false,
        mtree: true,
        package_info: true,
        scriptlet: false,
    }
)]
fn read_package_contents(
    #[case] compression: CompressionSettings,
    #[case] config_flags: InputDirConfig,
) -> TestResult {
    use std::io::Read;

    init_logger();

    // Create the package.
    let temp_dir = TempDir::new()?;
    let package = create_package(&temp_dir, &config_flags, compression)?;
    // Ensure that individually reading all metadata files works as expected.
    let pkginfo = package.read_pkginfo()?;
    let pkgname = match &pkginfo {
        alpm_pkginfo::PackageInfo::V1(v) => &v.pkgname,
        alpm_pkginfo::PackageInfo::V2(v) => &v.pkgname,
    };
    assert_eq!(pkgname.to_string(), "example");

    let buildinfo = package.read_buildinfo()?;
    let pkgbase = match &buildinfo {
        alpm_buildinfo::BuildInfo::V1(v) => &v.pkgbase,
        alpm_buildinfo::BuildInfo::V2(v) => &v.pkgbase,
    };
    assert_eq!(pkgbase.to_string(), "example");

    let mtree = package.read_mtree()?;
    let paths = match &mtree {
        alpm_mtree::Mtree::V1(paths) | alpm_mtree::Mtree::V2(paths) => paths,
    }
    .iter()
    .map(|p| p.to_path_buf())
    .collect::<Vec<_>>();
    assert!(paths.contains(&PathBuf::from("./.BUILDINFO")));
    if config_flags.data_files {
        assert!(paths.contains(&PathBuf::from("./foo/bar/baz/buh.txt")));
        assert!(paths.contains(&PathBuf::from("./foo/bar/baz/beh.txt")));
    }

    // Ensure all the metadata files can be read as expected.
    let mut reader: PackageReader = package.clone().try_into()?;
    let metadata = reader.metadata()?;
    assert_eq!(buildinfo, metadata.buildinfo);
    assert_eq!(pkginfo, metadata.pkginfo);
    assert_eq!(mtree, metadata.mtree);

    // Ensure that the scriptlet is there, if it is added to the package.
    if config_flags.scriptlet {
        let install_scriptlet = package.read_install_scriptlet()?;
        assert_eq!(Some(VALID_INSTALL_SCRIPTLET), install_scriptlet.as_deref());
    } else {
        assert!(package.read_install_scriptlet()?.is_none());
    }

    // Make sure that all data files are included, if added to the package.
    // Otherwise ensure, that no data files are present.
    if config_flags.data_files {
        // Ensure that the data files are present.
        let mut reader: PackageReader = package.clone().try_into()?;
        let data_files = reader.data_entries()?;
        assert_eq!(
            vec![
                PathBuf::from(".ARBITRARY"),
                PathBuf::from("foo/bar/baz/beh.txt"),
                PathBuf::from("foo/bar/baz/buh.txt"),
                PathBuf::from("foo/beh.txt"),
            ],
            data_files
                .flatten()
                .filter(|entry| entry.is_file() || entry.is_symlink())
                .map(|entry| entry.path().to_path_buf())
                .collect::<Vec<_>>()
        );

        // Ensure that the directory structure is correct.
        let mut reader: PackageReader = package.clone().try_into()?;
        let data_files = reader.data_entries()?;
        assert_eq!(
            vec![
                PathBuf::from("foo"),
                PathBuf::from("foo/bar"),
                PathBuf::from("foo/bar/baz"),
                PathBuf::from("foo/bar/buh"),
            ],
            data_files
                .flatten()
                .filter(|entry| entry.is_dir())
                .map(|entry| entry.path().to_path_buf())
                .collect::<Vec<_>>()
        );

        // Ensure that the arbitrary file can be read correctly.
        let mut reader: PackageReader = package.clone().try_into()?;
        let mut entry = match reader.read_data_entry(".ARBITRARY")? {
            Some(entry) => entry,
            None => panic!("Expected .ARBITRARY entry, but found none"),
        };
        let content = entry.content()?;
        let content = String::from_utf8(content)?;
        assert_eq!("This is an arbitrary file.", content);

        // Read the first 4 bytes of the file using `std::io::Read`.
        let mut reader: PackageReader = package.clone().try_into()?;
        let mut entry = match reader.read_data_entry(".ARBITRARY")? {
            Some(entry) => entry,
            None => panic!("Expected .ARBITRARY entry, but found none"),
        };
        let mut buffer = vec![0; 4];
        entry.read_exact(&mut buffer)?;
        assert_eq!(b"This", buffer.as_slice());

        // Ensure that the debug output contains the path of the entry.
        assert!(format!("{entry:?}").contains(&entry.path().to_string_lossy().to_string()));

        // Ensure the permission bits
        assert_eq!(0o644, entry.permissions()?);

        // Get the raw entry and assert various properties.
        assert_eq!(26, entry.raw().header().size()?);
        assert_eq!(0o100644, entry.raw().header().mode()?);
        assert_eq!(EntryType::Regular, entry.raw().header().entry_type());
    } else {
        let mut reader: PackageReader = package.clone().try_into()?;
        assert_eq!(0, reader.data_entries()?.count());
    }

    // Ensure that the debug output of the reader contains the expected type.
    let reader_debug = format!("{reader:?}");
    assert!(reader_debug.contains("Archive<CompressionDecoder>"));

    Ok(())
}

// Ensure that the metadata iterator short-circuits after finding all metadata files.
#[test]
fn package_metadata_iterator() -> TestResult {
    init_logger();

    let temp_dir = TempDir::new()?;
    let package = create_package(
        &temp_dir,
        &InputDirConfig {
            build_info: true,
            data_files: true,
            mtree: true,
            package_info: true,
            scriptlet: true,
        },
        CompressionSettings::Xz {
            compression_level: Default::default(),
        },
    )?;

    let mut reader: PackageReader = package.clone().try_into()?;
    let mut metadata_entries = reader.metadata_entries()?;

    assert!(matches!(
        metadata_entries.next(),
        Some(Ok(MetadataEntry::BuildInfo(_)))
    ));
    assert!(matches!(
        metadata_entries.next(),
        Some(Ok(MetadataEntry::Mtree(_)))
    ));
    assert!(matches!(
        metadata_entries.next(),
        Some(Ok(MetadataEntry::PackageInfo(_)))
    ));
    // Make sure that the iterator no longer returns any values.
    // This should not advance the iterator, as all metadata files have been found.
    assert!(metadata_entries.next().is_none());

    // Now test that the short-circuit of the iterator actually worked.
    // Get the underlying raw Entries iterator and make sure that this non-filtering iterator still
    // produces data files.
    let mut inner_iter = metadata_entries.into_inner().into_inner();
    assert!(
        inner_iter.next().is_some(),
        "Expected the non-filtering iterator to still return a value."
    );

    Ok(())
}

// Small helper function to assert that an entry is a metadata entry and then returns
// that entry.
fn assert_metadata_entry(entry: Option<Result<PackageEntry, Error>>) -> TestResult<MetadataEntry> {
    assert!(
        entry.is_some(),
        "Expected package entry to be of some value."
    );
    match entry.unwrap()? {
        PackageEntry::Metadata(entry) => Ok(*entry),
        PackageEntry::InstallScriptlet(_) => {
            panic!("Expected Metadata entry, got install scriptlet.")
        }
    }
}

// Ensure that the package entry iterator short-circuits after finding all known entries.
#[test]
fn package_entry_iterator() -> TestResult {
    init_logger();

    let temp_dir = TempDir::new()?;
    let package = create_package(
        &temp_dir,
        &InputDirConfig {
            build_info: true,
            data_files: true,
            mtree: true,
            package_info: true,
            scriptlet: true,
        },
        CompressionSettings::Xz {
            compression_level: Default::default(),
        },
    )?;

    let mut reader: PackageReader = package.clone().try_into()?;
    let mut entries = reader.entries()?;

    assert!(matches!(
        assert_metadata_entry(entries.next())?,
        MetadataEntry::BuildInfo(_)
    ));
    assert!(matches!(
        entries.next(),
        Some(Ok(PackageEntry::InstallScriptlet(_)))
    ));
    assert!(matches!(
        assert_metadata_entry(entries.next())?,
        MetadataEntry::Mtree(_)
    ));
    assert!(matches!(
        assert_metadata_entry(entries.next())?,
        MetadataEntry::PackageInfo(_)
    ));
    // Make sure that the iterator no longer returns any values.
    // This should not advance the iterator, as all metadata files have been found.
    assert!(entries.next().is_none());

    // Now test that the short-circuit of the iterator actually worked.
    // Get the underlying raw Entries iterator and make sure that this non-filtering iterator still
    // produces data files.
    let mut inner_iter = entries.into_inner();
    assert!(
        inner_iter.next().is_some(),
        "Expected the non-filtering iterator to still return a value."
    );

    Ok(())
}

// Ensure that the package entry iterator short-circuits even if install scriptlet is missing.
#[test]
fn package_entry_iterator_without_scriptlet() -> TestResult {
    init_logger();

    let temp_dir = TempDir::new()?;
    let package = create_package(
        &temp_dir,
        &InputDirConfig {
            build_info: true,
            data_files: true,
            mtree: true,
            package_info: true,
            scriptlet: false, // No install scriptlet
        },
        CompressionSettings::Xz {
            compression_level: Default::default(),
        },
    )?;

    let mut reader: PackageReader = package.clone().try_into()?;
    let mut entries = reader.entries()?;

    assert!(matches!(
        assert_metadata_entry(entries.next())?,
        MetadataEntry::BuildInfo(_)
    ));
    assert!(matches!(
        assert_metadata_entry(entries.next())?,
        MetadataEntry::Mtree(_)
    ));
    assert!(matches!(
        assert_metadata_entry(entries.next())?,
        MetadataEntry::PackageInfo(_)
    ));

    // Make sure that the iterator no longer returns any values.
    // This should not advance the inner iterator only once, as all package files except the
    // `.INSTALL` scriptlet have been found and the next non-package file results in a
    // short-circuit.
    assert!(entries.next().is_none());

    // Now test that the short-circuit of the iterator actually worked.
    // Get the underlying raw Entries iterator and make sure that this non-filtering iterator still
    // produces data files.
    let mut inner_iter = entries.into_inner();
    assert!(
        inner_iter.next().is_some(),
        "Expected the non-filtering iterator to still return a value."
    );

    Ok(())
}