stuffr-cli 0.24.0

The stuffr command line interface
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
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
//! Differential tests: stuffr invoked as `compress` and `uncompress` with
//! `STUFFR_COMPRESS_FLAVOR=apple`, against Apple's own `/usr/bin/compress`
//! (file_cmds-487.0.1; `uncompress` is the same binary).
//!
//! Every row comes from the measurement record of 0.18.0 Task 1
//! (`compress-measurements.md`: the surface table, the `-b` parse table,
//! the validity table) and is compared against the live reference: exit
//! code, standard error, standard output and the working directory
//! afterwards (bytes, modes, times). Compressed bytes are promised
//! identical everywhere (no tier split: there is no C LZW), so nothing is
//! round-tripped.
//!
//! The deviations of `compat/compress/apple/mod.rs` are asserted in their
//! stated direction, with Apple's measured behaviour as the reference's
//! expected value (`deviations`).
//!
//! **macOS only.** The reference exists nowhere else; elsewhere one test
//! says so instead of the file compiling to nothing.

#[cfg(not(target_os = "macos"))]
#[test]
fn apple_compress_is_the_reference_and_exists_only_on_macos() {
    println!("skipped: the Apple compress suite needs /usr/bin/compress, which only macOS has");
}

#[cfg(target_os = "macos")]
mod compat_harness;

/// Cases, fixtures and the comparison every group shares.
#[cfg(target_os = "macos")]
mod common {
    pub use super::compat_harness::*;
    use std::path::Path;
    use std::process::{Command, Stdio};

    /// Where `compress` and `uncompress` live.
    pub const DIR: &str = "/usr/bin";

    /// stuffr's side runs as the Apple personality whatever the platform
    /// default; the reference ignores the variable.
    pub const FLAVOR: (&str, &str) = ("STUFFR_COMPRESS_FLAVOR", "apple");

    pub fn case<'a>(name: &'a str, args: &[&'a str], files: Vec<(&'a str, Vec<u8>)>) -> Case<'a> {
        Case {
            name,
            args: args.to_vec(),
            files,
            stdin: None,
            tty_stdin: false,
            tty_stdout: false,
            tty_ctl: false,
            tty_signal: None,
            env: vec![FLAVOR],
            compare_output_bytes: true,
            expect_stdout_plain: None,
            setup: None,
            roundtrip: Roundtrip::Exact,
            tz: None,
            unzip_flavour: None,
            zip_flavour: None,
            zip_tree: None,
        }
    }

    /// `case` with `stdin` piped in.
    pub fn piped<'a>(name: &'a str, args: &[&'a str], stdin: Vec<u8>) -> Case<'a> {
        let mut c = case(name, args, vec![]);
        c.stdin = Some(stdin);
        c
    }

    /// `c` with `setup` run in its directory before the program starts.
    pub fn set<'a>(mut c: Case<'a>, setup: fn(&Path)) -> Case<'a> {
        c.setup = Some(setup);
        c
    }

    /// One row: the reference and stuffr on the same case, every
    /// difference reported. False when the reference is absent.
    pub fn compare(c: &Case) -> bool {
        let Some(r) = reference(DIR, c) else {
            return false;
        };
        let s = stuffr_as(c);
        assert_same(c, &r, &s);
        true
    }

    pub fn group(name: &str, cases: &[Case]) {
        run_group(name, cases, Skipped::none(), &compare);
    }

    /// Apple's `compress -c` on `input`, standard output returned. The input
    /// is a file in a scratch directory and standard input is `/dev/null`:
    /// no pipe, no writer thread, no `/dev/stdin` reopen. Exit status,
    /// standard error and length are asserted and named in the panic.
    pub fn apple(args: &[&str], input: &[u8]) -> Vec<u8> {
        let dir = ScratchDir::unique("compress-fixture");
        std::fs::create_dir_all(&*dir).unwrap();
        std::fs::write(dir.join("in"), input).unwrap();
        let mut cmd = Command::new(Path::new(DIR).join("compress"));
        cmd.arg("-c")
            .args(args)
            .arg("in")
            .current_dir(&*dir)
            .stdin(Stdio::null())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        let out = spawn(&mut cmd)
            .expect("spawn /usr/bin/compress")
            .wait_with_output()
            .unwrap();
        assert!(
            out.status.success(),
            "/usr/bin/compress -c {args:?} ({} input bytes): {:?}, stderr {:?}, {} output bytes",
            input.len(),
            out.status,
            String::from_utf8_lossy(&out.stderr),
            out.stdout.len()
        );
        out.stdout
    }

    /// The inputs of the record: `f` (text, compresses), `a` and `b`
    /// (text), `r` (random, grows), and Apple's `.Z` of `f`, `a`, `b`.
    pub struct Fx {
        pub f: Vec<u8>,
        pub a: Vec<u8>,
        pub b: Vec<u8>,
        pub r: Vec<u8>,
        pub fz: Vec<u8>,
        pub az: Vec<u8>,
        pub bz: Vec<u8>,
    }

    /// [`Fx`] (built once for the run), or `None` (after
    /// `require_reference`'s note) without the reference.
    pub fn fixtures() -> Option<&'static Fx> {
        static FX: std::sync::OnceLock<Option<Fx>> = std::sync::OnceLock::new();
        FX.get_or_init(build_fixtures).as_ref()
    }

    fn build_fixtures() -> Option<Fx> {
        if !require_reference(&Path::new(DIR).join("compress")) {
            return None;
        }
        let f = text(50_000, 0x0018_0001);
        let a = text(30_000, 0x0018_0002);
        let b = text(30_000, 0x0018_0003);
        let r = random(4096, 0x0018_0004);
        let fz = apple(&[], &f);
        let az = apple(&[], &a);
        let bz = apple(&[], &b);
        assert!(
            fz.starts_with(&[0x1f, 0x9d, 0x90]),
            "Apple's .Z of f: {} bytes, starts {:02x?}",
            fz.len(),
            &fz[..fz.len().min(8)]
        );
        Some(Fx {
            f,
            a,
            b,
            r,
            fz,
            az,
            bz,
        })
    }

    /// A name only a test needs once, kept for the run.
    pub fn leak(s: String) -> &'static str {
        Box::leak(s.into_boxed_str())
    }
}

#[cfg(target_os = "macos")]
mod differential {
    use super::common::*;
    use std::path::Path;

    fn sym_fz_to_f(d: &Path) {
        std::os::unix::fs::symlink("f", d.join("f.Z")).unwrap();
    }
    fn hard_fz_to_f(d: &Path) {
        std::fs::hard_link(d.join("f"), d.join("f.Z")).unwrap();
    }
    fn hard_g_to_f(d: &Path) {
        std::fs::hard_link(d.join("f"), d.join("g")).unwrap();
    }
    fn sym_l_to_f(d: &Path) {
        std::os::unix::fs::symlink("f", d.join("l")).unwrap();
    }
    fn sym_f_to_fz(d: &Path) {
        std::os::unix::fs::symlink("f.Z", d.join("f")).unwrap();
    }
    fn sym_lz_to_fz(d: &Path) {
        std::os::unix::fs::symlink("f.Z", d.join("l.Z")).unwrap();
    }
    fn mkdir_dir(d: &Path) {
        std::fs::create_dir(d.join("dir")).unwrap();
    }
    fn mkdir_fz(d: &Path) {
        std::fs::create_dir(d.join("f.Z")).unwrap();
    }
    fn mode(p: &Path, m: u32) {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(p, std::fs::Permissions::from_mode(m)).unwrap();
    }
    fn f_000(d: &Path) {
        mode(&d.join("f"), 0o000);
    }
    fn fz_000(d: &Path) {
        mode(&d.join("f.Z"), 0o000);
    }
    fn ro_555(d: &Path) {
        mode(&d.join("ro"), 0o555);
    }
    fn f_640(d: &Path) {
        mode(&d.join("f"), 0o640);
    }
    fn f_4755(d: &Path) {
        mode(&d.join("f"), 0o4755);
    }
    fn fz_600(d: &Path) {
        mode(&d.join("f.Z"), 0o600);
    }

    /// The usage, flags and version rows: every option Apple's getopt
    /// (`b:cdfv`) refuses prints `illegal option` with the full `argv[0]`
    /// and the usage line, exit 1; `--` and `-` behave as getopt says.
    #[test]
    fn flags_and_usage_match_apple() {
        let Some(x) = fixtures() else { return };
        let mut cases = Vec::new();
        for flag in [
            "-x",
            "-h",
            "--help",
            "-V",
            "--version",
            "-q",
            "-s",
            "-k",
            "-n",
            "-C",
            "-F",
            "-r",
            "-R",
            "-?",
            "-B",
            "-cx",
            "-bx",
        ] {
            cases.push(piped("compress", &[flag], x.f.clone()));
        }
        for flag in ["-x", "-h", "-V", "-r", "--"] {
            cases.push(piped("uncompress", &[flag], x.fz.clone()));
        }
        // `-d` before a bad option: the usage names `uncompress`.
        cases.push(piped("compress", &["-d", "-x"], x.fz.clone()));
        cases.push(piped("uncompress", &["-b"], x.fz.clone()));
        cases.push(case("compress", &["-k", "f"], vec![("f", x.f.clone())]));
        cases.push(case("compress", &["-q", "r"], vec![("r", x.r.clone())]));
        cases.push(case("compress", &["--", "-f"], vec![("-f", x.f.clone())]));
        cases.push(piped("compress", &["-"], x.f.clone()));
        cases.push(piped("uncompress", &["-"], x.fz.clone()));
        cases.push(case("compress", &["-V", "f"], vec![("f", x.f.clone())]));
        cases.push(case("compress", &["-cv", "f"], vec![("f", x.f.clone())]));
        cases.push(case(
            "compress",
            &["-vc", "--", "f"],
            vec![("f", x.f.clone())],
        ));
        // getopt stops at the first operand: `-c` after it is a file.
        cases.push(case("compress", &["f", "-c"], vec![("f", x.f.clone())]));
        group("flags", &cases);
    }

    /// Compressing and decompressing files, standard input and output.
    #[test]
    fn basics_match_apple() {
        let Some(x) = fixtures() else { return };
        let f = || vec![("f", x.f.clone())];
        let fz = || vec![("f.Z", x.fz.clone())];
        let ab = || vec![("a", x.a.clone()), ("b", x.b.clone())];
        let abz = || vec![("a.Z", x.az.clone()), ("b.Z", x.bz.clone())];
        let cases = vec![
            case("compress", &["f"], f()),
            case("compress", &["-v", "f"], f()),
            piped("compress", &["-v"], x.f.clone()),
            piped("compress", &[], x.f.clone()),
            case("compress", &["-c", "f"], f()),
            case("compress", &["-cv", "f"], f()),
            case("compress", &["-c", "a", "b"], ab()),
            case("compress", &["a", "b"], ab()),
            case("compress", &["-v", "a", "b"], ab()),
            case("compress", &["-d", "f.Z"], fz()),
            case("compress", &["-dc", "f.Z"], fz()),
            case("compress", &["-dv", "f.Z"], fz()),
            case("uncompress", &["f.Z"], fz()),
            case("uncompress", &["-v", "f.Z"], fz()),
            piped("uncompress", &[], x.fz.clone()),
            piped("uncompress", &["-v"], x.fz.clone()),
            piped("uncompress", &["-c"], x.fz.clone()),
            case("uncompress", &["-c", "f.Z"], fz()),
            case("uncompress", &["-c", "a.Z", "b.Z"], abz()),
            case("uncompress", &["a.Z", "b.Z"], abz()),
            case("uncompress", &["-v", "a.Z", "b.Z"], abz()),
            case("uncompress", &["f"], fz()),
            case(
                "uncompress",
                &["f"],
                vec![("f", x.f.clone()), ("f.Z", x.fz.clone())],
            ),
            case(
                "uncompress",
                &["-f", "f"],
                vec![("f", x.f.clone()), ("f.Z", x.fz.clone())],
            ),
            case("uncompress", &["-c", "f"], fz()),
            // `-` sets `cat` for the operands after it.
            {
                let mut c = case("compress", &["-", "a"], vec![("a", x.a.clone())]);
                c.stdin = Some(x.b.clone());
                c
            },
            {
                let mut c = case("compress", &["a", "-"], vec![("a", x.a.clone())]);
                c.stdin = Some(x.b.clone());
                c
            },
            {
                let mut c = case("uncompress", &["-", "a.Z"], vec![("a.Z", x.az.clone())]);
                c.stdin = Some(x.bz.clone());
                c
            },
            case("compress", &["-c", "-b", "12", "f"], f()),
            case("compress", &["-b", "12", "f"], f()),
        ];
        group("basics", &cases);
    }

    /// Review Focus 1 without a terminal: an existing output is refused
    /// (`already exists`, exit 1, both files kept) unless `-f`; `-c` never
    /// looks; a directory in the output's place.
    #[test]
    fn existing_outputs_match_apple() {
        let Some(x) = fixtures() else { return };
        let both = || vec![("f", x.f.clone()), ("f.Z", x.fz.clone())];
        let cases = vec![
            case("compress", &["f"], both()),
            case("compress", &["-v", "f"], both()),
            case("compress", &["-f", "f"], both()),
            case("compress", &["-c", "f"], both()),
            case("uncompress", &["f.Z"], both()),
            case("uncompress", &["-f", "f.Z"], both()),
            case("uncompress", &["-c", "f.Z"], both()),
            set(
                case("compress", &["-f", "f"], vec![("f", x.f.clone())]),
                mkdir_fz,
            ),
            set(case("compress", &["f"], vec![("f", x.f.clone())]), mkdir_fz),
            // A dangling link in the output's place: deviation A5
            // (`deviations::a5_*`).
            // Standard input is a terminal but standard error is not: no
            // question, the refusal.
            {
                let mut c = case("compress", &["f"], both());
                c.tty_stdin = true;
                c.stdin = Some(b"y\n".to_vec());
                c
            },
            {
                let mut c = case("uncompress", &["f.Z"], both());
                c.tty_stdin = true;
                c.stdin = Some(b"y\n".to_vec());
                c
            },
        ];
        group("existing outputs", &cases);
    }

    /// Review Focus 4: neither tool refuses a terminal. Compressed data is
    /// written to a terminal standard output, and a terminal standard
    /// input is read (an immediate end of file).
    #[test]
    fn terminals_match_apple() {
        let Some(x) = fixtures() else { return };
        let tty_out = |mut c: Case<'static>| {
            c.tty_stdout = true;
            c
        };
        let tty_in = |mut c: Case<'static>| {
            c.tty_stdin = true;
            c
        };
        let cases = vec![
            tty_out(piped("compress", &[], x.f.clone())),
            tty_out(piped("uncompress", &[], x.fz.clone())),
            tty_out(case("compress", &["-c", "f"], vec![("f", x.f.clone())])),
            tty_out(case(
                "uncompress",
                &["-c", "f.Z"],
                vec![("f.Z", x.fz.clone())],
            )),
            tty_in(case("compress", &[], vec![])),
            tty_in(case("uncompress", &[], vec![])),
            tty_in(case("compress", &["-c"], vec![])),
            // A pipe is read as it comes.
            piped("compress", &["-c", "-"], x.a.clone()),
        ];
        run_group("terminals", &cases, Skipped::none(), &|c: &Case| {
            let Some(mut r) = reference(DIR, c) else {
                return false;
            };
            let mut s = stuffr_as(c);
            if c.tty_stdout {
                r.stdout = without_terminal_crs(&r.stdout);
                s.stdout = without_terminal_crs(&s.stdout);
            }
            assert_same(c, &r, &s);
            true
        });
    }

    /// What a terminal standard output shows, without the carriage returns
    /// its driver puts before each newline (ONLCR). macOS's driver, its
    /// output queue full in the middle of that, sometimes repeats or drops
    /// the `\r`: 20 of 200 runs of `/usr/bin/uncompress` alone under load
    /// showed `\r\r\n` (the record's "tty saw" counts also varied by a few
    /// bytes run to run). The program's bytes are compared exactly; only
    /// the terminal's additions are left out.
    fn without_terminal_crs(b: &[u8]) -> Vec<u8> {
        let mut out = Vec::with_capacity(b.len());
        for &c in b {
            if c == b'\n' {
                while out.last() == Some(&b'\r') {
                    out.pop();
                }
            }
            out.push(c);
        }
        out
    }

    /// Review Focus 2: an output not smaller than its input is removed,
    /// exit 2 (the message only under `-v`), unless `-f`; not checked for
    /// standard output; an empty input writes nothing at all.
    #[test]
    fn no_saving_matches_apple() {
        let Some(x) = fixtures() else { return };
        let r = || vec![("r", x.r.clone())];
        let e = || vec![("e", Vec::new())];
        let one = || vec![("one", b"x".to_vec())];
        let cases = vec![
            case("compress", &["r"], r()),
            case("compress", &["-v", "r"], r()),
            case("compress", &["-f", "r"], r()),
            case("compress", &["-f", "-v", "r"], r()),
            piped("compress", &[], x.r.clone()),
            piped("compress", &["-v"], x.r.clone()),
            case("compress", &["-c", "r"], r()),
            case("compress", &["e"], e()),
            case("compress", &["-v", "e"], e()),
            case("compress", &["-f", "e"], e()),
            case("compress", &["-f", "-v", "e"], e()),
            case("compress", &["-c", "e"], e()),
            piped("compress", &[], Vec::new()),
            case("compress", &["-f", "-v", "one"], one()),
            case("compress", &["one"], one()),
            case(
                "compress",
                &["r", "f"],
                vec![("r", x.r.clone()), ("f", x.f.clone())],
            ),
            case(
                "compress",
                &["f", "r"],
                vec![("r", x.r.clone()), ("f", x.f.clone())],
            ),
            case(
                "compress",
                &["-v", "r", "f"],
                vec![("r", x.r.clone()), ("f", x.f.clone())],
            ),
            case(
                "uncompress",
                &["-v", "r.Z"],
                vec![("r.Z", apple(&[], &x.r))],
            ),
        ];
        group("no saving", &cases);
    }

    /// Links where Apple's own behaviour keeps the data (the `-f` cases
    /// where it does not are `deviations::a2_*`).
    #[test]
    fn links_match_apple() {
        let Some(x) = fixtures() else { return };
        let f = || vec![("f", x.f.clone())];
        let fz = || vec![("f.Z", x.fz.clone())];
        let cases = vec![
            set(case("compress", &["f"], f()), sym_fz_to_f),
            set(case("compress", &["f"], f()), hard_fz_to_f),
            set(case("compress", &["f"], f()), hard_g_to_f),
            set(case("compress", &["-f", "f"], f()), hard_g_to_f),
            set(case("compress", &["l"], f()), sym_l_to_f),
            set(case("compress", &["-c", "l"], f()), sym_l_to_f),
            set(case("uncompress", &["f.Z"], fz()), sym_f_to_fz),
            set(case("uncompress", &["l.Z"], fz()), sym_lz_to_fz),
            set(case("uncompress", &["-c", "f.Z"], fz()), sym_f_to_fz),
        ];
        group("links", &cases);
    }

    /// Missing operands, directories, names with and without `.Z`, devices.
    #[test]
    fn odd_operands_match_apple() {
        let Some(x) = fixtures() else { return };
        let long = leak("n".repeat(1100));
        let long_z = leak(format!("{}.Z", "n".repeat(1100)));
        let cases = vec![
            case("compress", &["nope"], vec![]),
            case("uncompress", &["nope"], vec![]),
            case("uncompress", &["nope.Z"], vec![]),
            case("uncompress", &["-c", "nope"], vec![]),
            case("uncompress", &["f"], vec![("f", x.f.clone())]),
            case("uncompress", &["-v", "f"], vec![("f", x.f.clone())]),
            case("uncompress", &["-c", "f"], vec![("f", x.f.clone())]),
            case("compress", &["f.Z"], vec![("f.Z", x.fz.clone())]),
            case("compress", &["-c", "f.Z"], vec![("f.Z", x.fz.clone())]),
            case("compress", &["a.Z.Z"], vec![]),
            case("compress", &["x.Zz"], vec![("x.Zz", x.a.clone())]),
            set(case("compress", &["dir"], vec![]), mkdir_dir),
            set(case("compress", &["-v", "dir"], vec![]), mkdir_dir),
            set(case("compress", &["-c", "dir"], vec![]), mkdir_dir),
            set(case("compress", &["-r", "dir"], vec![]), mkdir_dir),
            set(case("uncompress", &["dir"], vec![]), mkdir_dir),
            set(case("uncompress", &["-r", "dir"], vec![]), mkdir_dir),
            case("compress", &["-c", "/dev/null"], vec![]),
            case("compress", &["/dev/null"], vec![]),
            case("uncompress", &["-c", "/dev/null"], vec![]),
            case("uncompress", &[".Z"], vec![(".Z", x.fz.clone())]),
            case("compress", &[long], vec![]),
            case("uncompress", &[long], vec![]),
            case("uncompress", &[long_z], vec![]),
        ];
        group("odd operands", &cases);
    }

    /// Review Focus 3: a non-`.Z`, cut-short, damaged or odd `.Z` given to
    /// `uncompress`. The valid-but-refused cases are `deviations::a1_*` and
    /// `deviations::a3_*`.
    #[test]
    fn bad_input_matches_apple() {
        let Some(x) = fixtures() else { return };
        let half = x.fz[..x.fz.len() / 2].to_vec();
        let mut b17 = x.fz.clone();
        b17[2] = 0x91;
        let mut junk = x.fz.clone();
        junk.extend_from_slice(&[0xff, 0xff, 0xff]);
        let mut cat = x.az.clone();
        cat.extend_from_slice(&x.bz);
        let mut noblock = x.fz.clone();
        noblock[2] &= 0x7f;
        let mut flip = x.fz.clone();
        flip[100] ^= 0xff;
        // The first code is never checked: 0x1ff is written as its low
        // byte; then a code past the next free entry.
        let first_wide = vec![0x1f, 0x9d, 0x90, 0xff, 0x03, 0x00];
        let cases = vec![
            case(
                "uncompress",
                &["bad.Z"],
                vec![("bad", x.f.clone()), ("bad.Z", x.f.clone())],
            ),
            piped("uncompress", &[], x.f.clone()),
            case(
                "uncompress",
                &["-c", "bad.Z"],
                vec![("bad", x.f.clone()), ("bad.Z", x.f.clone())],
            ),
            case("uncompress", &["bad.Z"], vec![("bad.Z", x.f.clone())]),
            case("uncompress", &["-f", "bad.Z"], vec![("bad.Z", x.f.clone())]),
            case("uncompress", &["trunc.Z"], vec![("trunc.Z", half.clone())]),
            case(
                "uncompress",
                &["-c", "trunc.Z"],
                vec![("trunc.Z", half.clone())],
            ),
            piped("uncompress", &[], half.clone()),
            piped("uncompress", &[], x.fz[..x.fz.len() - 1].to_vec()),
            piped("uncompress", &[], vec![0x1f, 0x9d]),
            piped("uncompress", &[], vec![0x1f]),
            piped("uncompress", &[], vec![0x1f, 0x9d, 0x90, 0x41]),
            case("uncompress", &["e.Z"], vec![("e.Z", Vec::new())]),
            case("uncompress", &["-f", "e.Z"], vec![("e.Z", Vec::new())]),
            piped("uncompress", &[], Vec::new()),
            case("uncompress", &["b17.Z"], vec![("b17.Z", b17.clone())]),
            piped("uncompress", &[], b17.clone()),
            piped("uncompress", &[], junk.clone()),
            case("uncompress", &["junk.Z"], vec![("junk.Z", junk.clone())]),
            piped("uncompress", &[], cat.clone()),
            case("uncompress", &["cat.Z"], vec![("cat.Z", cat.clone())]),
            case("uncompress", &["-c", "cat.Z"], vec![("cat.Z", cat.clone())]),
            case("uncompress", &["-f", "cat.Z"], vec![("cat.Z", cat.clone())]),
            piped("uncompress", &[], noblock.clone()),
            piped("uncompress", &[], flip.clone()),
            piped("uncompress", &[], first_wide.clone()),
            case("uncompress", &["w.Z"], vec![("w.Z", first_wide.clone())]),
        ];
        group("bad input", &cases);
    }

    /// Several operands, one failing: the run goes on, and the exit code is
    /// the last one set (an error 1, "no saving" 2; success lowers neither).
    #[test]
    fn several_operands_match_apple() {
        let Some(x) = fixtures() else { return };
        let three = || {
            vec![
                ("a.Z", x.az.clone()),
                ("b.Z", x.bz.clone()),
                ("bad.Z", x.f.clone()),
            ]
        };
        let mut with_bad = three();
        with_bad.push(("bad", x.f.clone()));
        let mut c_with_bad = three();
        c_with_bad.push(("bad", x.f.clone()));
        let fr = || vec![("f", x.f.clone()), ("r", x.r.clone())];
        let cases = vec![
            case("uncompress", &["a.Z", "bad.Z", "b.Z"], three()),
            case("uncompress", &["a.Z", "bad.Z", "b.Z"], with_bad),
            case("uncompress", &["-c", "a.Z", "bad.Z", "b.Z"], c_with_bad),
            case(
                "compress",
                &["a", "nope", "b"],
                vec![("a", x.a.clone()), ("b", x.b.clone())],
            ),
            case(
                "compress",
                &["-v", "a", "nope", "b"],
                vec![("a", x.a.clone()), ("b", x.b.clone())],
            ),
            case(
                "uncompress",
                &["a.Z", "nope.Z", "b.Z"],
                vec![("a.Z", x.az.clone()), ("b.Z", x.bz.clone())],
            ),
            case("compress", &["r", "nope"], fr()),
            case("compress", &["nope", "r"], fr()),
            case("compress", &["-v", "f", "r", "nope"], fr()),
            case("compress", &["-v", "r", "f"], fr()),
            case("compress", &["r", "f"], fr()),
            case("compress", &["f", "r"], fr()),
        ];
        group("several operands", &cases);
    }

    /// Permission errors, and the input's mode on the output.
    #[test]
    fn permissions_match_apple() {
        let Some(x) = fixtures() else { return };
        let cases = vec![
            set(case("compress", &["f"], vec![("f", x.f.clone())]), f_000),
            set(
                case("uncompress", &["f.Z"], vec![("f.Z", x.fz.clone())]),
                fz_000,
            ),
            set(
                case("compress", &["ro/f"], vec![("ro/f", x.f.clone())]),
                ro_555,
            ),
            set(
                case("uncompress", &["ro/g.Z"], vec![("ro/g.Z", x.fz.clone())]),
                ro_555,
            ),
            set(case("compress", &["f"], vec![("f", x.f.clone())]), f_640),
            set(case("compress", &["f"], vec![("f", x.f.clone())]), f_4755),
            set(
                case("uncompress", &["f.Z"], vec![("f.Z", x.fz.clone())]),
                fz_600,
            ),
        ];
        group("permissions", &cases);
    }

    /// The `-b` parse table: `strtol` in base 10 (leading blanks and a
    /// sign accepted, trailing junk refused, the value truncated to an
    /// `int`), `zopen`'s range check (`0..=16`, 0 meaning 16) as
    /// `Invalid argument` naming the output (or, decompressing, the input),
    /// and getopt's handling of a missing value. Apple's `-b 1` to `-b 8`
    /// bytes are reproduced (ruling E1).
    #[test]
    fn bit_counts_parse_as_apple() {
        let Some(x) = fixtures() else { return };
        let mut cases = Vec::new();
        for v in [
            "-1",
            "0",
            "1",
            "2",
            "7",
            "8",
            "9",
            "12",
            "16",
            "17",
            "100",
            "abc",
            "",
            "12x",
            " 12",
            "+12",
            " ",
            "+",
            "-0",
            "4294967312",
            "99999999999999999999",
            "-99999999999999999999",
        ] {
            cases.push(piped("compress", &["-b", v], x.f.clone()));
        }
        for a in ["-b9", "-b17", "-babc", "-b-1", "-b0", "-cb12", "-b"] {
            cases.push(piped("compress", &[a], x.f.clone()));
        }
        cases.push(piped("compress", &["-b", "9", "-b", "12"], x.f.clone()));
        cases.push(case("compress", &["f", "-b"], vec![("f", x.f.clone())]));
        cases.push(case(
            "compress",
            &["-b", "17", "f"],
            vec![("f", x.f.clone())],
        ));
        cases.push(case(
            "compress",
            &["-b", "-1", "f"],
            vec![("f", x.f.clone())],
        ));
        cases.push(case(
            "compress",
            &["-b", "8", "f"],
            vec![("f", x.f.clone())],
        ));
        cases.push(case("compress", &["-b", "17", "nope"], vec![]));
        cases.push(piped("uncompress", &["-b", "17"], x.fz.clone()));
        cases.push(piped("uncompress", &["-b", "9"], x.fz.clone()));
        cases.push(piped("uncompress", &["-b", "abc"], x.fz.clone()));
        cases.push(case(
            "uncompress",
            &["-b", "17", "f.Z"],
            vec![("f.Z", x.fz.clone())],
        ));
        cases.push(case("uncompress", &["-b", "17", "nope.Z"], vec![]));
        group("bit counts", &cases);
    }

    /// The validity table: at every `-b` from 9 to 16 Apple's stream is
    /// valid, so its bytes are promised, on text, random and all-zero
    /// input, large enough to fill the table and reach the ratio checks
    /// and their CLEAR codes.
    #[test]
    fn every_bit_count_writes_apples_bytes() {
        let Some(_) = fixtures() else { return };
        let inputs = [
            text(1 << 20, 0x0018_0010),
            random(1 << 20, 0x0018_0011),
            vec![0u8; 1 << 20],
        ];
        let mut cases = Vec::new();
        for bits in ["9", "10", "11", "12", "13", "14", "15", "16"] {
            for input in &inputs {
                cases.push(piped("compress", &["-b", bits], input.clone()));
            }
        }
        // And a file, so the output's name, mode and times are compared.
        cases.push(case(
            "compress",
            &["-b", "11", "t"],
            vec![("t", inputs[0].clone())],
        ));
        group("every bit count", &cases);
    }
}

/// The deviations of `compat/compress/apple/mod.rs`, each asserted in its
/// stated direction: the reference's measured behaviour on one side, the
/// correct one on stuffr's.
#[cfg(target_os = "macos")]
mod deviations {
    use super::common::*;
    use std::path::Path;

    fn sym_fz_to_f(d: &Path) {
        std::os::unix::fs::symlink("f", d.join("f.Z")).unwrap();
    }
    fn hard_fz_to_f(d: &Path) {
        std::fs::hard_link(d.join("f"), d.join("f.Z")).unwrap();
    }
    fn sym_f_to_fz(d: &Path) {
        std::os::unix::fs::symlink("f.Z", d.join("f")).unwrap();
    }
    fn hard_f_to_fz(d: &Path) {
        std::fs::hard_link(d.join("f.Z"), d.join("f")).unwrap();
    }

    /// Both sides of `c`, or `None` without the reference.
    fn both(c: &Case) -> Option<(Run, Run)> {
        let r = reference(DIR, c)?;
        Some((r, stuffr_as(c)))
    }

    fn names(run: &Run) -> Vec<&str> {
        run.tree.keys().map(String::as_str).collect()
    }

    /// A1 (with ruling E1): a valid stream whose header asks for 1 to 11
    /// bits. Apple's own `-b 1`, `-b 8`, `-b 9`, `-b 10` and `-b 11` output
    /// is refused by its `uncompress` (`Inappropriate file type or
    /// format`, exit 1, nothing written); stuffr decodes it.
    #[test]
    fn a1_low_maxbits_streams_decode() {
        let Some(x) = fixtures() else { return };
        for bits in ["1", "8", "9", "10", "11"] {
            let z = apple(&["-b", bits], &x.f);
            assert_eq!(z[2], 0x80 | bits.parse::<u8>().unwrap(), "-b {bits}");
            let c = piped("uncompress", &[], z.clone());
            let Some((r, s)) = both(&c) else { return };
            assert_eq!(
                (r.code, r.stderr.as_str(), r.stdout.len()),
                (
                    1,
                    "uncompress: /dev/stdin: Inappropriate file type or format\n",
                    0
                ),
                "reference, -b {bits} on standard input"
            );
            assert_eq!((s.code, s.stderr.as_str()), (0, ""), "stuffr, -b {bits}");
            assert!(s.stdout == x.f, "stuffr decodes -b {bits}");

            let c = case("uncompress", &["x.Z"], vec![("x.Z", z.clone())]);
            let Some((r, s)) = both(&c) else { return };
            assert_eq!(
                (r.code, r.stderr.as_str(), names(&r)),
                (
                    1,
                    "uncompress: x.Z: Inappropriate file type or format\n",
                    vec!["x.Z"]
                ),
                "reference, -b {bits} file"
            );
            assert_eq!(
                (s.code, s.stderr.as_str(), names(&s)),
                (0, "", vec!["x"]),
                "stuffr, -b {bits} file"
            );
            let (bytes, mode, mtime) = &s.tree["x"];
            assert!(*bytes == x.f, "-b {bits}: x holds the input");
            assert_eq!((*mode, *mtime), (0o644, INPUT_MTIME as i64), "-b {bits}");
        }
    }

    /// A2: `compress -f f` when `f.Z` is a link to `f`, and `uncompress -f
    /// f.Z` when `f` is a link to `f.Z`. Apple opens the output for
    /// writing over the input and loses the data (exit 0 for compress,
    /// `Invalid argument` exit 1 for uncompress); stuffr writes the output
    /// under a temporary name and puts it in place when complete, so the
    /// data survives, compressed or decompressed, with the same silence
    /// and exit 0.
    #[test]
    fn a2_an_output_linked_to_the_input_keeps_the_data() {
        let Some(x) = fixtures() else { return };
        for (setup, what) in [
            (sym_fz_to_f as fn(&Path), "f.Z -> f"),
            (hard_fz_to_f, "f.Z hard link of f"),
        ] {
            let mut c = case("compress", &["-f", "f"], vec![("f", x.f.clone())]);
            c.setup = Some(setup);
            let Some((r, s)) = both(&c) else { return };
            assert_eq!((r.code, r.stderr.as_str()), (0, ""), "reference, {what}");
            assert_eq!(names(&r), ["f.Z"], "reference, {what}");
            assert!(r.tree["f.Z"].0 != x.fz, "reference kept the data? {what}");
            assert_eq!(
                (s.code, s.stderr.as_str(), names(&s)),
                (0, "", vec!["f.Z"]),
                "stuffr, {what}"
            );
            assert!(s.tree["f.Z"].0 == x.fz, "stuffr's f.Z, {what}");
            assert_eq!(s.tree["f.Z"].2, INPUT_MTIME as i64, "{what}");
        }
        for (setup, what) in [
            (sym_f_to_fz as fn(&Path), "f -> f.Z"),
            (hard_f_to_fz, "f hard link of f.Z"),
        ] {
            let mut c = case("uncompress", &["-f", "f.Z"], vec![("f.Z", x.fz.clone())]);
            c.setup = Some(setup);
            let Some((r, s)) = both(&c) else { return };
            assert_eq!(
                (r.code, r.stderr.as_str()),
                (1, "uncompress: f.Z: Invalid argument\n"),
                "reference, {what}"
            );
            assert!(
                r.tree.get("f.Z").is_none_or(|e| e.0 != x.fz),
                "reference kept the data? {what}"
            );
            assert_eq!(
                (s.code, s.stderr.as_str(), names(&s)),
                (0, "", vec!["f"]),
                "stuffr, {what}"
            );
            assert!(s.tree["f"].0 == x.f, "stuffr's f, {what}");
        }
    }

    /// A3: the header-only stream `1f 9d 90` (what ncompress writes for an
    /// empty input) is a valid stream of nothing. Apple refuses it with a
    /// stale `errno` (`No such file or directory` from the output's
    /// `stat`, or `Undefined error: 0`), exit 1; stuffr decodes it to an
    /// empty output.
    #[test]
    fn a3_a_header_only_stream_decodes_to_nothing() {
        let Some(_) = fixtures() else { return };
        let hdr = vec![0x1f, 0x9d, 0x90];
        let rows: [(Case, &str, &[&str], &[&str]); 4] = [
            (
                case("uncompress", &["hdr.Z"], vec![("hdr.Z", hdr.clone())]),
                "uncompress: hdr.Z: No such file or directory\n",
                &["hdr.Z"],
                &["hdr"],
            ),
            (
                case("uncompress", &["-f", "hdr.Z"], vec![("hdr.Z", hdr.clone())]),
                "uncompress: hdr.Z: No such file or directory\n",
                &["hdr.Z"],
                &["hdr"],
            ),
            (
                case("uncompress", &["-c", "hdr.Z"], vec![("hdr.Z", hdr.clone())]),
                "uncompress: hdr.Z: Undefined error: 0\n",
                &["hdr.Z"],
                &["hdr.Z"],
            ),
            (
                piped("uncompress", &[], hdr.clone()),
                "uncompress: /dev/stdin: Undefined error: 0\n",
                &[],
                &[],
            ),
        ];
        for (c, ref_err, ref_files, our_files) in rows {
            let Some((r, s)) = both(&c) else { return };
            assert_eq!(
                (r.code, r.stderr.as_str(), names(&r).as_slice()),
                (1, ref_err, ref_files),
                "reference, {:?}",
                c.args
            );
            assert_eq!(
                (
                    s.code,
                    s.stderr.as_str(),
                    names(&s).as_slice(),
                    s.stdout.len()
                ),
                (0, "", our_files, 0),
                "stuffr, {:?}",
                c.args
            );
            if let Some((bytes, _, mtime)) = s.tree.get("hdr") {
                assert!(bytes.is_empty());
                assert_eq!(*mtime, INPUT_MTIME as i64);
            }
        }
    }
    fn live_out(d: &Path, name: &str) {
        std::fs::write(d.join("t"), b"old target").unwrap();
        std::os::unix::fs::symlink("t", d.join(name)).unwrap();
    }
    fn live_fz(d: &Path) {
        live_out(d, "f.Z");
    }
    fn live_f(d: &Path) {
        live_out(d, "f");
    }
    fn dangling_fz(d: &Path) {
        std::os::unix::fs::symlink("t", d.join("f.Z")).unwrap();
    }
    fn dangling_f(d: &Path) {
        std::os::unix::fs::symlink("t", d.join("f")).unwrap();
    }

    /// A5: a symlink at the output name, live or dangling. Apple follows
    /// it: without `-f` a dangling one looks absent and the output is
    /// written to the file it names (`t` created), with `-f` a live one is
    /// truncated through. stuffr treats it as an existing file: refused
    /// without `-f` (`already exists`, exit 1, link kept, `t` as it was or
    /// absent); with `-f` the link is replaced by a regular file holding
    /// the output and `t` is untouched.
    #[test]
    fn a5_a_symlink_at_the_output_name_is_never_written_through() {
        let Some(x) = fixtures() else { return };
        type Row<'a> = (
            &'a str,
            &'a str,
            (&'a str, Vec<u8>),
            &'a str,
            fn(&Path),
            bool,
        );
        let rows: [Row; 4] = [
            (
                "compress",
                "f",
                ("f", x.f.clone()),
                "f.Z",
                dangling_fz,
                false,
            ),
            ("compress", "f", ("f", x.f.clone()), "f.Z", live_fz, true),
            (
                "uncompress",
                "f.Z",
                ("f.Z", x.fz.clone()),
                "f",
                dangling_f,
                false,
            ),
            (
                "uncompress",
                "f.Z",
                ("f.Z", x.fz.clone()),
                "f",
                live_f,
                true,
            ),
        ];
        for (tool, operand, input, out, setup, live) in rows {
            let what = format!("{tool} {operand}, {out} live {live}");
            let want = if tool == "compress" { &x.fz } else { &x.f };
            let mut c = case(tool, &[operand], vec![input.clone()]);
            c.setup = Some(setup);
            let Some((r, s)) = both(&c) else { return };
            if !live {
                // The reference wrote through the dangling link.
                assert_eq!(r.code, 0, "reference, {what}: {}", r.stderr);
                assert!(
                    r.tree.get("t").is_some_and(|t| t.0 == *want),
                    "reference, {what}"
                );
            }
            assert_eq!(
                (s.code, s.stderr.as_str()),
                (1, format!("{tool}: {out} already exists\n").as_str()),
                "stuffr, {what}"
            );
            assert_eq!(s.tree[out].0, b"t", "stuffr, {what}: link replaced");
            assert!(s.tree.contains_key(input.0), "stuffr, {what}: input gone");
            if live {
                assert_eq!(s.tree["t"].0, b"old target", "stuffr, {what}");
            } else {
                assert!(!s.tree.contains_key("t"), "stuffr, {what}: t created");
            }

            let mut c = case(tool, &["-f", operand], vec![input.clone()]);
            c.setup = Some(setup);
            let Some((r, s)) = both(&c) else { return };
            if live {
                assert_eq!(r.tree["t"].0, *want, "reference -f, {what}");
            }
            assert_eq!((s.code, s.stderr.as_str()), (0, ""), "stuffr -f, {what}");
            assert_eq!(s.tree[out].0, *want, "stuffr -f, {what}");
            assert!(!s.tree.contains_key(input.0), "stuffr -f, {what}");
            if live {
                assert_eq!(s.tree["t"].0, b"old target", "stuffr -f, {what}");
            } else {
                assert!(!s.tree.contains_key("t"), "stuffr -f, {what}: t created");
            }
        }
    }
}

/// Review Focus 1 at a terminal: Apple asks `overwrite NAME? ` on standard
/// error only when standard error is a terminal, reads the answer from
/// standard input (`rpmatch` of its first byte), and on a no prints the
/// refusal too. The harness cannot make standard error a terminal, so these
/// rows run both programs on a pty of their own (echo off, so the tty shows
/// only what the program writes) and compare the exit code, what the
/// terminal saw and the directory.
#[cfg(target_os = "macos")]
mod prompts {
    use super::common::*;
    use std::io::Read;
    use std::os::fd::{AsRawFd, FromRawFd, OwnedFd};
    use std::os::unix::process::{CommandExt, ExitStatusExt};
    use std::path::{Path, PathBuf};
    use std::process::{Command, Stdio};
    use std::time::{Duration, Instant};

    struct Seen {
        code: i32,
        tty: Vec<u8>,
        files: Vec<(String, Vec<u8>)>,
    }

    /// `exe` as `name` with `args` over `files`; standard error on a pty,
    /// standard input on it too (`answer` typed ahead) or `/dev/null`.
    fn run(
        exe: &Path,
        name: &str,
        args: &[&str],
        files: &[(&str, &[u8])],
        answer: Option<&[u8]>,
    ) -> Seen {
        let scratch = ScratchDir::unique("compress-pty");
        let work = scratch.join("work");
        std::fs::create_dir_all(&work).unwrap();
        for (n, b) in files {
            std::fs::write(work.join(n), b).unwrap();
        }
        let guard = spawn_lock();
        let (mut m, mut s) = (0, 0);
        // SAFETY: openpty writes the two descriptors it opens.
        let rc = unsafe {
            libc::openpty(
                &mut m,
                &mut s,
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                std::ptr::null_mut(),
            )
        };
        assert_eq!(rc, 0, "openpty");
        // SAFETY: both descriptors were just opened and are ours alone.
        let (master, slave) = unsafe { (OwnedFd::from_raw_fd(m), OwnedFd::from_raw_fd(s)) };
        for fd in [m, s] {
            // SAFETY: fcntl on descriptors we own.
            unsafe { libc::fcntl(fd, libc::F_SETFD, libc::FD_CLOEXEC) };
        }
        // Echo off: the terminal shows only what the program writes.
        // SAFETY: termios is plain data filled by tcgetattr.
        let mut t: libc::termios = unsafe { std::mem::zeroed() };
        // SAFETY: tcgetattr/tcsetattr on the slave we own.
        unsafe {
            assert_eq!(libc::tcgetattr(slave.as_raw_fd(), &mut t), 0);
            t.c_lflag &= !libc::ECHO;
            assert_eq!(libc::tcsetattr(slave.as_raw_fd(), libc::TCSANOW, &t), 0);
        }
        let mut cmd = Command::new(exe);
        cmd.arg0(name)
            .args(args)
            .current_dir(&work)
            .env_clear()
            .env("PATH", "/usr/bin:/bin")
            .env("LANG", "C")
            .env(FLAVOR.0, FLAVOR.1)
            .stdout(Stdio::null())
            .stderr(Stdio::from(slave.try_clone().unwrap()));
        if answer.is_some() {
            cmd.stdin(Stdio::from(slave.try_clone().unwrap()));
        } else {
            cmd.stdin(Stdio::null());
        }
        let mut child = cmd.spawn().expect("spawn");
        drop(cmd);
        drop(guard);
        let mut master = std::fs::File::from(master);
        if let Some(a) = answer {
            use std::io::Write;
            master.write_all(a).unwrap();
        }
        let deadline = Instant::now() + Duration::from_secs(30);
        let status = loop {
            if let Some(st) = child.try_wait().unwrap() {
                break st;
            }
            assert!(Instant::now() < deadline, "{exe:?} {args:?} timed out");
            std::thread::sleep(Duration::from_millis(5));
        };
        // Everything the child wrote is queued; our slave copy keeps the
        // master from reading a hang-up first.
        // SAFETY: fcntl on the master we own.
        unsafe {
            let fl = libc::fcntl(master.as_raw_fd(), libc::F_GETFL);
            libc::fcntl(master.as_raw_fd(), libc::F_SETFL, fl | libc::O_NONBLOCK);
        }
        let mut tty = Vec::new();
        let mut buf = [0u8; 4096];
        while let Ok(n) = master.read(&mut buf) {
            if n == 0 {
                break;
            }
            tty.extend_from_slice(&buf[..n]);
        }
        drop(slave);
        let mut names: Vec<_> = std::fs::read_dir(&work)
            .unwrap()
            .map(|e| e.unwrap().file_name().into_string().unwrap())
            .collect();
        names.sort();
        let files = names
            .into_iter()
            .map(|n| {
                let b = std::fs::read(work.join(&n)).unwrap();
                (n, b)
            })
            .collect();
        let code = status
            .code()
            .unwrap_or_else(|| -status.signal().unwrap_or(0));
        Seen { code, tty, files }
    }

    fn ours(name: &str) -> (ScratchDir, PathBuf) {
        let d = ScratchDir::unique("compress-bin");
        std::fs::create_dir_all(&*d).unwrap();
        let link = d.join(name);
        std::os::unix::fs::symlink(env!("CARGO_BIN_EXE_stuffr"), &link).unwrap();
        (d, link)
    }

    #[test]
    fn the_overwrite_question_matches_apple() {
        let Some(x) = fixtures() else { return };
        let both: [(&str, &[u8]); 2] = [("f", &x.f), ("f.Z", &x.fz)];
        // Name, arguments, what is typed (`None`: standard input is
        // `/dev/null`), what the terminal shows.
        type Row<'a> = (&'a str, &'a [&'a str], Option<&'a [u8]>, &'a str);
        let rows: [Row; 10] = [
            ("compress", &["f"], Some(b"y\n"), "overwrite f.Z? "),
            (
                "compress",
                &["f"],
                Some(b"n\n"),
                "overwrite f.Z? compress: f.Z already exists\r\n",
            ),
            ("compress", &["f"], Some(b"yes\n"), "overwrite f.Z? "),
            (
                "compress",
                &["f"],
                Some(b"\n"),
                "overwrite f.Z? compress: f.Z already exists\r\n",
            ),
            (
                "compress",
                &["f"],
                None,
                "overwrite f.Z? compress: f.Z already exists\r\n",
            ),
            ("uncompress", &["f.Z"], Some(b"y\n"), "overwrite f? "),
            (
                "uncompress",
                &["f.Z"],
                Some(b"n\n"),
                "overwrite f? uncompress: f already exists\r\n",
            ),
            ("uncompress", &["f.Z"], Some(b"Yes\n"), "overwrite f? "),
            (
                "uncompress",
                &["f.Z"],
                Some(b"\n"),
                "overwrite f? uncompress: f already exists\r\n",
            ),
            (
                "uncompress",
                &["f.Z"],
                None,
                "overwrite f? uncompress: f already exists\r\n",
            ),
        ];
        for (name, args, answer, seen) in rows {
            let r = run(&Path::new(DIR).join(name), name, args, &both, answer);
            let (_keep, link) = ours(name);
            let s = run(&link, name, args, &both, answer);
            let why = format!("{name} {args:?} answering {answer:?}");
            assert_eq!(String::from_utf8_lossy(&r.tty), seen, "reference, {why}");
            assert_eq!(
                (s.code, String::from_utf8_lossy(&s.tty)),
                (r.code, String::from_utf8_lossy(&r.tty)),
                "{why}"
            );
            assert!(s.files == r.files, "{why}: the directories differ");
        }
    }
}

/// Review Focus 5 (ruling E3): Apple's compress installs no signal handler,
/// so INT, TERM, HUP and QUIT kill it by their default action and leave the
/// partial output beside the intact input; an INT ignored at start stays
/// ignored and the run finishes.
#[cfg(target_os = "macos")]
mod interrupts {
    use super::common::*;
    use super::compat_harness::interrupt::*;
    use std::collections::HashMap;
    use std::path::{Path, PathBuf};
    use std::sync::{Mutex, OnceLock};

    const ENV: &[(&str, &str)] = &[FLAVOR];

    /// Apple's `.Z` of `big_input(mib)`, made once per size.
    fn big_z(mib: usize) -> Vec<u8> {
        static CACHE: OnceLock<Mutex<HashMap<usize, Vec<u8>>>> = OnceLock::new();
        let cache = CACHE.get_or_init(Mutex::default);
        let mut map = cache.lock().unwrap_or_else(|e| e.into_inner());
        map.entry(mib)
            .or_insert_with(|| {
                big_input_through(&format!("{DIR}/compress"), &["-c"], mib)
                    .expect("the reference is present")
            })
            .clone()
    }

    fn exe(mine: bool, name: &str) -> PathBuf {
        if mine {
            PathBuf::from(env!("CARGO_BIN_EXE_stuffr"))
        } else {
            Path::new(DIR).join(name)
        }
    }

    #[test]
    fn a_signal_leaves_the_partial_output_and_kills() {
        let _serial = interrupt_lock();
        if !require_reference(&Path::new(DIR).join("compress")) {
            return;
        }
        type Make<'a> = &'a dyn Fn(usize) -> Vec<u8>;
        let plain: Make = &big_input;
        let packed: Make = &big_z;
        let rows: [(&str, &str, &str, Make); 2] = [
            ("compress", "big", "big.Z", plain),
            ("uncompress", "big.Z", "big", packed),
        ];
        for sig in [libc::SIGINT, libc::SIGTERM, libc::SIGHUP, libc::SIGQUIT] {
            for (name, input, partial, make) in rows {
                let mut seen = Vec::new();
                for mine in [false, true] {
                    let dir = ScratchDir::unique("compress-intr");
                    let named = |n: &str| n == partial;
                    let ready = has_data(&dir, &named);
                    let Some(r) = interrupt_with_input_env(
                        &exe(mine, name),
                        name,
                        &[input],
                        &dir,
                        ENV,
                        input,
                        make,
                        Inherit::Default,
                        sig,
                        &ready,
                    ) else {
                        continue;
                    };
                    let why = format!("mine={mine} {name} signal {sig}");
                    assert!(killed_by(&r, sig), "{why}: {:?}", r.status);
                    let mut want = vec![input.to_string(), partial.to_string()];
                    want.sort();
                    assert_eq!(r.left, want, "{why}");
                    // The input is intact: the bytes of the attempt that ran
                    // (64 MiB, or the retry's 256).
                    let got = std::fs::read(dir.join(input)).unwrap();
                    assert!(
                        [64, 256].iter().any(|&m| make(m) == got),
                        "{why}: the input changed"
                    );
                    assert!(r.stderr.is_empty(), "{why}: {:?}", r.stderr);
                    seen.push(r.left);
                }
                if seen.len() == 2 {
                    assert_eq!(seen[0], seen[1], "{name} signal {sig}");
                }
            }
        }
    }

    /// Deviation A2 under a signal: `compress -f big` with `big.Z` a link to
    /// `big` writes a temporary (`.stuffr-compress-*.tmp`); INT, TERM and
    /// HUP remove it and kill the run, leaving the input and the link as
    /// they were. stuffr only: Apple has already emptied `big` by then.
    #[test]
    fn a2_temporary_is_removed_on_a_signal() {
        let _serial = interrupt_lock();
        for (sig, hard) in [
            (libc::SIGINT, true),
            (libc::SIGTERM, true),
            (libc::SIGHUP, true),
            (libc::SIGINT, false),
        ] {
            let why = format!("signal {sig}, hard link {hard}");
            // A run that ends before the signal can land is a miss: retry
            // with a larger input, fail only when every size misses.
            let mut hit = None;
            let mut hung = false;
            for mib in [64, 256] {
                let data = big_input(mib);
                let dir = ScratchDir::unique("compress-intr-a2");
                std::fs::create_dir_all(&*dir).unwrap();
                std::fs::write(dir.join("big"), &data).unwrap();
                if hard {
                    std::fs::hard_link(dir.join("big"), dir.join("big.Z")).unwrap();
                } else {
                    std::os::unix::fs::symlink("big", dir.join("big.Z")).unwrap();
                }
                let r = {
                    let named = |n: &str| n.starts_with(".stuffr-compress-");
                    let ready = has_data(&dir, &named);
                    interrupt_env(
                        &exe(true, "compress"),
                        "compress",
                        &["-f", "big"],
                        &dir,
                        ENV,
                        Inherit::Default,
                        sig,
                        &ready,
                    )
                };
                hung |= take_reference_hung();
                if let Some(r) = r {
                    hit = Some((r, data, dir));
                    break;
                }
            }
            let Some((r, data, dir)) = hit else {
                if hung {
                    eprintln!(
                        "note: the compress reference hung after the signal; comparison skipped"
                    );
                    continue;
                }
                panic!("{why}: no run reached the signal at 64 and 256 MiB");
            };
            assert!(killed_by(&r, sig), "{why}: {:?}", r.status);
            assert_eq!(r.left, ["big", "big.Z"], "{why}: the temporary was left");
            assert!(
                std::fs::read(dir.join("big")).unwrap() == data,
                "{why}: big changed"
            );
            if hard {
                use std::os::unix::fs::MetadataExt;
                let (a, b) = (
                    std::fs::metadata(dir.join("big")).unwrap(),
                    std::fs::metadata(dir.join("big.Z")).unwrap(),
                );
                assert_eq!((a.dev(), a.ino()), (b.dev(), b.ino()), "{why}");
            } else {
                let t = std::fs::read_link(dir.join("big.Z")).unwrap();
                assert_eq!(t, Path::new("big"), "{why}");
            }
        }
    }

    /// `compress -c` killed by SIGTERM leaves only its input.
    #[test]
    fn a_signal_to_a_filter_kills_it() {
        let _serial = interrupt_lock();
        if !require_reference(&Path::new(DIR).join("compress")) {
            return;
        }
        for mine in [false, true] {
            // A run that finished before the signal (no result, or exit 0
            // after "ready") is a miss: retry with a larger input.
            let mut hit = None;
            let mut hung = false;
            for mib in [64, 256] {
                let dir = ScratchDir::unique("compress-intr-c");
                std::fs::create_dir_all(&*dir).unwrap();
                std::fs::write(dir.join("big"), big_input(mib)).unwrap();
                // Ready once compressed bytes arrive: the run is under way.
                let r = interrupt_once_stdout_has_data(
                    &exe(mine, "compress"),
                    "compress",
                    &["-c", "big"],
                    &dir,
                    ENV,
                    Inherit::Default,
                    libc::SIGTERM,
                );
                hung |= take_reference_hung();
                if let Some(r) = r.filter(|r| !r.status.success()) {
                    hit = Some(r);
                    break;
                }
            }
            let Some(r) = hit else {
                if hung {
                    eprintln!(
                        "note: the compress reference hung after the signal; comparison skipped"
                    );
                    continue;
                }
                panic!("mine={mine}: compress -c: no run reached the signal at 64 and 256 MiB");
            };
            assert!(killed_by(&r, libc::SIGTERM), "mine={mine}: {:?}", r.status);
            assert_eq!(r.left, ["big"], "mine={mine}");
        }
    }

    /// An INT ignored at start stays ignored: the run finishes, exit 0
    /// (on text, which compresses: random input would end in "no saving").
    #[test]
    fn an_ignored_interrupt_stays_ignored() {
        let _serial = interrupt_lock();
        if !require_reference(&Path::new(DIR).join("compress")) {
            return;
        }
        let block = text(4 << 20, 0x0018_0020);
        for mine in [false, true] {
            // A run that ends before the signal lands is a miss: retry with
            // a larger input, fail only when every size misses.
            let mut hit = None;
            let mut hung = false;
            for mib in [64, 256] {
                let big: Vec<u8> = block.iter().copied().cycle().take(mib << 20).collect();
                let dir = ScratchDir::unique("compress-intr-ign");
                std::fs::create_dir_all(&*dir).unwrap();
                std::fs::write(dir.join("big"), &big).unwrap();
                let ready = has_data(&dir, &|n: &str| n == "big.Z");
                let r = interrupt_env(
                    &exe(mine, "compress"),
                    "compress",
                    &["big"],
                    &dir,
                    ENV,
                    Inherit::IgnoreInt,
                    libc::SIGINT,
                    &ready,
                );
                hung |= take_reference_hung();
                if let Some(r) = r {
                    hit = Some(r);
                    break;
                }
            }
            let Some(r) = hit else {
                if hung {
                    eprintln!(
                        "note: the compress reference hung after the signal; comparison skipped"
                    );
                    continue;
                }
                panic!("mine={mine}: compress: no run reached the signal at 64 and 256 MiB");
            };
            assert!(r.status.success(), "mine={mine}: {:?}", r.status);
            assert_eq!(r.left, ["big.Z"], "mine={mine}");
        }
    }
}