varve-core 0.45.0

Layer manifests, resolution, the core store, and verification wiring for varve
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
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
//! Zephyr modules a project ADDS to its own build (REQ-ZMODEXPORT-001).
//!
//! varve carries the blocks a project adds, never Zephyr itself. A
//! `zephyr-module` payload is the module's tree as a gzip-tar, stored exactly
//! as published; `varve export-zephyr-modules` lays each one under
//! `<out>/<module-name>/` and writes a CMake script appending them to
//! `ZEPHYR_EXTRA_MODULES`, so the team's own `west.yml` is never edited.
//!
//! **No Python, anywhere in this file** (clause 5). `zephyr/module.yml` is
//! parsed in Rust. A module that declares `package-managers: pip` adds a
//! Python dependency to every build that uses it, so the deposit refuses it
//! unless the layer records an opt-in naming exactly the requirement files.
//!
//! The decisions live HERE, in a module the `--lib` mutation gate can reach,
//! and not in the CLI: a check that only a system test exercises is a check
//! the gate cannot defend.

use crate::sdkexport::{Member, MemberBody};
use std::collections::BTreeSet;
use std::path::Path;

/// Where Zephyr looks for a module's description.
pub const MODULE_YML: &str = "zephyr/module.yml";

/// The CMake script an export writes beside the module directories.
pub const CMAKE_FILE: &str = "zephyr-extra-modules.cmake";

/// Written by an export into every module directory it creates. It is what
/// says "varve wrote this directory", which is the only licence to replace or
/// remove one: the export stamp lists everything under `--out`, a user's own
/// files included, so a stamp alone proves nothing about who wrote a directory.
pub const MODULE_MARKER: &str = ".varve-zephyr-module";

/// Signed annotation recording the operator's opt-in to a module's pip
/// requirement files, comma-separated (REQ-ZMODEXPORT-001 clause 5).
pub const ANN_ALLOW_PIP: &str = "eu.pulseengine.varve.zephyr.allow-pip";

/// What `zephyr/module.yml` says that varve acts on. Everything else in the
/// file is Zephyr's business and is ignored.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ModuleYml {
    /// `name:` — absent means the payload's own name is used.
    pub name: Option<String>,
    /// `build: depends:` — modules that must be present.
    pub depends: Vec<String>,
    /// `Some(requirement files)` when `package-managers: pip` is declared AT
    /// ALL, even with no files or a null value; `None` when it is not.
    pub pip: Option<Vec<String>>,
}

#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum ZephyrModuleError {
    #[error("zephyr-module '{payload}' cannot be opened as a tar archive: {detail}")]
    Archive { payload: String, detail: String },
    #[error(
        "zephyr-module '{payload}' has no {MODULE_YML} — Zephyr finds a module through that file, \
         so a tree without one is not a module and would install, verify and then be ignored by \
         every build. {members} member(s) were read; the first: {saw:?}"
    )]
    NoModuleYml {
        payload: String,
        members: usize,
        saw: Vec<String>,
    },
    #[error("zephyr-module '{payload}': {MODULE_YML} does not parse: {detail}")]
    BadModuleYml { payload: String, detail: String },
    #[error(
        "zephyr-module '{payload}': {MODULE_YML} carries the YAML tag {tag}. Zephyr's own \
         reader (PyYAML) treats a key tagged `!!merge` as a merge key and varve's reader does \
         not, so a tagged node can hide `package-managers: pip`, `build: depends` or `name` \
         from every check varve makes while Zephyr still honours them. Real module.yml files \
         use no tags; remove it."
    )]
    TaggedModuleYml { payload: String, tag: String },
    #[error(
        "zephyr-module '{payload}' declares `package-managers: pip` (requirement files: {files}), \
         which adds a Python dependency to every build that uses it. varve distributes the blocks \
         a project adds and needs no Python (REQ-ZMODEXPORT-001 clause 5), so this is refused \
         unless the layer records an explicit opt-in naming exactly those files: \
         allow-pip = [{quoted}]"
    )]
    PipWithoutOptIn {
        payload: String,
        files: String,
        quoted: String,
    },
    #[error(
        "zephyr-module '{payload}' declares pip requirement files [{declared}] and its allow-pip \
         opt-in names [{allowed}] (opt-in missing: [{missing}]; opt-in names but the module does \
         not declare: [{extra}]). The opt-in must name EXACTLY the declared files — a different \
         list is a different Python dependency than the one the operator accepted."
    )]
    PipOptInMismatch {
        payload: String,
        declared: String,
        allowed: String,
        missing: String,
        extra: String,
    },
    #[error(
        "zephyr-module '{payload}' carries allow-pip = [{allowed}] and its {MODULE_YML} declares \
         no `package-managers: pip` — an opt-in for a dependency that does not exist would be \
         signed, read by nothing, and believed. Drop it."
    )]
    PipOptInUnneeded { payload: String, allowed: String },
    #[error("allow-pip names {entry:?}, which is not a usable requirement-file path ({why})")]
    BadPipEntry { entry: String, why: &'static str },
    #[error(
        "zephyr-module '{payload}' names itself {name:?}, which is not a plain directory name \
         ({why}) — the export writes the module to <out>/<name>/, and a name that is a path \
         would write somewhere else"
    )]
    BadName {
        payload: String,
        name: String,
        why: &'static str,
    },
    #[error(
        "zephyr-module '{payload}' contains the symlink {member:?} — a module tree is laid out \
         as stored and a link in it could point anywhere on the consumer's machine, so it is \
         refused"
    )]
    Symlink { payload: String, member: String },
    #[error(
        "zephyr-module '{payload}' lists the path {path:?} twice, or twice differing only in \
         case (compared lowercased, as a case-insensitive file system such as macOS's would). \
         One would overwrite the other at export, and which one survives is not what was \
         parsed or checked."
    )]
    DuplicateMember { payload: String, path: String },
    #[error(
        "zephyr-module '{payload}' carries {member:?} beside the {MODULE_YML} varve parsed. \
         Compared ignoring case it names the same file Zephyr reads for the module \
         description, so on a case-insensitive file system it could replace the checked \
         description with an unchecked one (a pip dependency nobody opted into). Remove it."
    )]
    ModuleYmlCollision { payload: String, member: String },
    #[error(
        "{path} exists and was not written by varve export-zephyr-modules; remove it or choose \
         another --out. varve replaces a module directory only when this --out carries a \
         zephyr-module export stamp listing files under it, so a checkout of your own is \
         never deleted."
    )]
    NotWrittenByVarve { path: String },
    #[error("zephyr-module '{payload}': member {member:?} is not a usable path: {detail}")]
    UnsafeMember {
        payload: String,
        member: String,
        detail: String,
    },
    #[error(
        "payloads '{first}' and '{second}' are both the Zephyr module '{name}' (compared \
         ignoring case, as a case-insensitive file system would) — they would write into one \
         directory"
    )]
    DuplicateModule {
        name: String,
        first: String,
        second: String,
    },
    #[error(
        "no zephyr-module named {asked:?} in this pin. It carries: {carried}. \
         `varve inspect` lists every payload."
    )]
    UnknownSelect { asked: String, carried: String },
    #[error(
        "zephyr module '{module}' declares `build: depends: [{dependency}]`, and '{dependency}' \
         is not among the modules this export carries ({carried}). An export with an incomplete \
         dependency closure builds only until Zephyr looks for the missing module. Add it to the \
         layer (and do not narrow --select past it); a module the workspace's own west manifest \
         already provides is not recognised yet (REQ-ZMODEXPORT-002)."
    )]
    IncompleteClosure {
        module: String,
        dependency: String,
        carried: String,
    },
    #[error(
        "the path {path:?} cannot be written into a CMake list safely: {why}. Export to a \
         directory whose absolute path avoids it."
    )]
    UnsafeCmakePath { path: String, why: &'static str },
    #[error("cannot write {path}: {detail}")]
    Io { path: String, detail: String },
}

/// A module's tree as stored, with the wrapper directory (if any) stripped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModuleTree {
    pub yml: ModuleYml,
    /// Root-relative, normalised, no archive-root entries.
    pub members: Vec<Member>,
}

/// The slice of `zephyr/module.yml` varve reads, as the YAML spells it.
#[derive(serde::Deserialize)]
struct RawYml {
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    build: Option<RawBuild>,
    #[serde(default, rename = "package-managers")]
    package_managers: Option<RawPackageManagers>,
}

#[derive(serde::Deserialize)]
struct RawBuild {
    #[serde(default)]
    depends: Option<Vec<String>>,
}

#[derive(serde::Deserialize)]
struct RawPackageManagers {
    // Only called when the key is PRESENT, which is the point: `pip:` with a
    // null value still declares pip, and `Option<RawPip>` alone could not
    // tell it from no `pip` key at all.
    #[serde(default, deserialize_with = "pip_declared")]
    pip: Option<Vec<String>>,
}

#[derive(serde::Deserialize)]
struct RawPip {
    #[serde(default, rename = "requirement-files")]
    files: Option<Vec<String>>,
}

fn pip_declared<'de, D>(d: D) -> Result<Option<Vec<String>>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let pip = <Option<RawPip> as serde::Deserialize>::deserialize(d)?;
    Ok(Some(pip.and_then(|p| p.files).unwrap_or_default()))
}

/// Parse `zephyr/module.yml`.
///
/// In Rust, by `serde-saphyr` (pure safe Rust, `unsafe` forbidden, with
/// resource budgets against hostile input). Nothing here shells out, so no
/// Python is needed to deposit or export a module (clause 5).
pub fn parse_module_yml(payload: &str, text: &str) -> Result<ModuleYml, ZephyrModuleError> {
    // Decided here, not left to the YAML library: serde-saphyr 2.0.0 reads an
    // empty document as "all defaults" and 2.1.0 refuses it, so the answer
    // would change with a version bump. A module.yml that says nothing is not
    // a description of a module.
    if text.trim().is_empty() {
        return Err(ZephyrModuleError::BadModuleYml {
            payload: payload.to_string(),
            detail: "the file is empty".to_string(),
        });
    }
    refuse_yaml_tags(payload, text)?;
    let raw: RawYml =
        serde_saphyr::from_str(text).map_err(|e| ZephyrModuleError::BadModuleYml {
            payload: payload.to_string(),
            detail: e.to_string(),
        })?;
    Ok(ModuleYml {
        name: raw.name,
        depends: raw.build.and_then(|b| b.depends).unwrap_or_default(),
        pip: raw.package_managers.and_then(|p| p.pip),
    })
}

/// Refuse ANY explicit YAML tag, found by the YAML parser's own events and not
/// by scanning text, so a `!` inside a quoted string, a comment or a block
/// scalar is correctly not one.
///
/// Why this exists: PyYAML, which reads this file for Zephyr, honours a tag
/// serde-saphyr ignores (`!!merge` on any key makes it a merge key), so a tagged
/// node can carry `package-managers: pip` past varve's check while Zephyr still
/// installs it. Refusing every tag closes that whole class, not one spelling.
/// A scan error is left for the typed parse below to report.
fn refuse_yaml_tags(payload: &str, text: &str) -> Result<(), ZephyrModuleError> {
    for item in serde_saphyr::granit_parser::Parser::new_from_str(text) {
        let Ok((event, _)) = item else { break };
        if let Some(tag) = event.tag() {
            return Err(ZephyrModuleError::TaggedModuleYml {
                payload: payload.to_string(),
                tag: format!("{}{}", tag.handle(), tag.suffix()),
            });
        }
    }
    Ok(())
}

/// The one top-level directory every member sits under, if there is one.
fn wrapper_of(members: &[Member]) -> Option<String> {
    let top = members.first()?.path.split('/').next()?.to_string();
    let prefix = format!("{top}/");
    members
        .iter()
        .all(|m| m.path.starts_with(&prefix) || (m.path == top && m.body == MemberBody::Dir))
        .then_some(top)
}

/// Open a module archive: validate every member, find `zephyr/module.yml`
/// (at the root, else inside a single wrapper directory) and parse it.
pub fn open(payload: &str, archive: &[u8]) -> Result<ModuleTree, ZephyrModuleError> {
    let raw = crate::sdkexport::read_members(archive).map_err(|e| ZephyrModuleError::Archive {
        payload: payload.to_string(),
        detail: e.to_string(),
    })?;
    let mut members: Vec<Member> = Vec::with_capacity(raw.len());
    let mut seen = BTreeSet::new();
    for m in raw {
        // `git archive` leads with a pax global header recording the commit.
        // It is metadata about the archive, not a member of the module.
        if m.path == "pax_global_header" && matches!(m.body, MemberBody::File { .. }) {
            continue;
        }
        // `tar -C dir .` writes the root itself first.
        if m.body == MemberBody::Dir && crate::sdkexport::names_archive_root(&m.path) {
            continue;
        }
        let path = crate::sdkexport::safe_member_path(&m.path).map_err(|e| {
            ZephyrModuleError::UnsafeMember {
                payload: payload.to_string(),
                member: m.path.clone(),
                detail: e.to_string(),
            }
        })?;
        if matches!(m.body, MemberBody::Symlink { .. }) {
            return Err(ZephyrModuleError::Symlink {
                payload: payload.to_string(),
                member: path,
            });
        }
        // The name an export marks its own directories with is not the
        // module's to use: a tree carrying it would be indistinguishable from
        // one varve wrote.
        if path.to_lowercase() == MODULE_MARKER {
            return Err(ZephyrModuleError::UnsafeMember {
                payload: payload.to_string(),
                member: m.path.clone(),
                detail: "that name is reserved for varve's own marker".to_string(),
            });
        }
        // Compared LOWERCASED: macOS and Windows file systems are
        // case-insensitive, so `zephyr/Module.yml` and `zephyr/module.yml` are
        // one file there, and which of the two a tar reader writes last wins.
        // That is how an unchecked description could replace the one parsed
        // (and pip-checked). Unicode normalisation is NOT handled: it is not
        // needed for the module-description names, which are ASCII, and the
        // `create_new` write at export refuses any collision this misses.
        if !seen.insert(path.to_lowercase()) {
            return Err(ZephyrModuleError::DuplicateMember {
                payload: payload.to_string(),
                path,
            });
        }
        members.push(Member { path, body: m.body });
    }

    // The root first, so a module that is nothing BUT `zephyr/module.yml`
    // (one top-level directory, `zephyr`) is not mistaken for a wrapper.
    let is_yml = |m: &&Member, prefix: &str| {
        m.path == format!("{prefix}{MODULE_YML}") && matches!(m.body, MemberBody::File { .. })
    };
    let (strip, found) = match members.iter().find(|m| is_yml(m, "")) {
        Some(m) => (None, Some(m)),
        None => match wrapper_of(&members) {
            Some(w) => {
                let prefix = format!("{w}/");
                (Some(w), members.iter().find(|m| is_yml(m, &prefix)))
            }
            None => (None, None),
        },
    };
    let Some(Member {
        body: MemberBody::File { bytes, .. },
        ..
    }) = found
    else {
        return Err(ZephyrModuleError::NoModuleYml {
            payload: payload.to_string(),
            members: members.len(),
            saw: members.iter().take(8).map(|m| m.path.clone()).collect(),
        });
    };
    let text = std::str::from_utf8(bytes).map_err(|e| ZephyrModuleError::BadModuleYml {
        payload: payload.to_string(),
        detail: format!("not UTF-8: {e}"),
    })?;
    let yml = parse_module_yml(payload, text)?;

    if let Some(w) = strip {
        let prefix = format!("{w}/");
        members = members
            .into_iter()
            .filter_map(|m| {
                m.path.strip_prefix(&prefix).map(|p| Member {
                    path: p.to_string(),
                    body: m.body,
                })
            })
            .collect();
    }
    // Nothing else may fold onto the description varve parsed (see above): the
    // case-folded duplicate check catches `Module.yml`; this catches the
    // sibling spelling Zephyr also looks for.
    for m in &members {
        let lower = m.path.to_lowercase();
        if m.path != MODULE_YML && (lower == MODULE_YML || lower == "zephyr/module.yaml") {
            return Err(ZephyrModuleError::ModuleYmlCollision {
                payload: payload.to_string(),
                member: m.path.clone(),
            });
        }
    }
    Ok(ModuleTree { yml, members })
}

/// Why `name` cannot be one directory component of the export, if it cannot.
fn plain_component_fault(name: &str) -> Option<&'static str> {
    if name.is_empty() {
        Some("empty")
    } else if name.contains('/') || name.contains('\\') {
        Some("it contains a path separator")
    } else if name.chars().any(char::is_control) {
        Some("it contains a control character")
    } else if name.starts_with('.') {
        Some("it starts with '.', which names the current or parent directory or hides itself")
    } else if name.starts_with('-') {
        Some("it starts with '-', which a command line would read as a flag")
    } else if name == CMAKE_FILE {
        Some("it is the name of the CMake script written beside the modules")
    } else {
        None
    }
}

/// The directory name this module is exported under: `module.yml`'s `name:`,
/// else the payload's own name. Refused unless a plain directory component.
pub fn module_name(payload: &str, yml: &ModuleYml) -> Result<String, ZephyrModuleError> {
    let name = yml.name.clone().unwrap_or_else(|| payload.to_string());
    match plain_component_fault(&name) {
        Some(why) => Err(ZephyrModuleError::BadName {
            payload: payload.to_string(),
            name,
            why,
        }),
        None => Ok(name),
    }
}

/// `allow-pip` entries are signed into an annotation comma-separated, so one
/// containing a comma, a control character or nothing at all cannot round-trip.
pub fn check_allow_pip_entries(allow: &[String]) -> Result<(), ZephyrModuleError> {
    for entry in allow {
        let why = if entry.is_empty() {
            Some("empty")
        } else if entry.contains(',') {
            Some("it contains a comma, which separates entries in the signed annotation")
        } else if entry.chars().any(char::is_control) {
            Some("it contains a control character")
        } else {
            None
        };
        if let Some(why) = why {
            return Err(ZephyrModuleError::BadPipEntry {
                entry: entry.clone(),
                why,
            });
        }
    }
    Ok(())
}

fn join(items: impl IntoIterator<Item = impl AsRef<str>>) -> String {
    items
        .into_iter()
        .map(|s| s.as_ref().to_string())
        .collect::<Vec<_>>()
        .join(", ")
}

/// The pip rule (clause 5): declared requires an opt-in naming EXACTLY the
/// declared files; an opt-in with nothing declared is refused as inert.
pub fn check_pip(
    payload: &str,
    yml: &ModuleYml,
    allow: Option<&[String]>,
) -> Result<(), ZephyrModuleError> {
    let allowed: BTreeSet<&str> = allow
        .unwrap_or_default()
        .iter()
        .map(String::as_str)
        .collect();
    let Some(declared_files) = &yml.pip else {
        return match allow {
            None => Ok(()),
            Some(_) => Err(ZephyrModuleError::PipOptInUnneeded {
                payload: payload.to_string(),
                allowed: join(&allowed),
            }),
        };
    };
    let declared: BTreeSet<&str> = declared_files.iter().map(String::as_str).collect();
    let Some(_) = allow else {
        return Err(ZephyrModuleError::PipWithoutOptIn {
            payload: payload.to_string(),
            files: if declared.is_empty() {
                "none listed".to_string()
            } else {
                join(&declared)
            },
            quoted: join(declared.iter().map(|f| format!("{f:?}"))),
        });
    };
    if declared != allowed {
        return Err(ZephyrModuleError::PipOptInMismatch {
            payload: payload.to_string(),
            declared: join(&declared),
            allowed: join(&allowed),
            missing: join(declared.difference(&allowed)),
            extra: join(allowed.difference(&declared)),
        });
    }
    Ok(())
}

/// Everything the deposit checks about one `zephyr-module` payload
/// (clauses 1 and 5).
pub fn check_deposit(
    payload: &str,
    archive: &[u8],
    allow_pip: Option<&[String]>,
) -> Result<(), ZephyrModuleError> {
    let tree = open(payload, archive)?;
    module_name(payload, &tree.yml)?;
    if let Some(allow) = allow_pip {
        check_allow_pip_entries(allow)?;
    }
    check_pip(payload, &tree.yml, allow_pip)
}

/// The opt-in as the signed annotation spells it, and back.
pub fn allow_pip_to_annotation(allow: &[String]) -> String {
    allow
        .iter()
        .map(String::as_str)
        .collect::<BTreeSet<_>>()
        .into_iter()
        .collect::<Vec<_>>()
        .join(",")
}

pub fn allow_pip_from_annotation(raw: &str) -> Vec<String> {
    raw.split(',')
        .filter(|s| !s.is_empty())
        .map(str::to_string)
        .collect()
}

/// One module ready to export.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExportModule {
    pub payload: String,
    pub name: String,
    pub tree: ModuleTree,
    /// THIS payload's own signed opt-in. Carried here and never looked up by
    /// name: two payloads of a composition can share a payload name.
    pub allow_pip: Option<Vec<String>>,
}

/// One verified `zephyr-module` payload as an export receives it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ModulePayload {
    pub name: String,
    pub bytes: Vec<u8>,
    /// The layer's SIGNED `allow-pip` annotation, decoded; `None` where the
    /// entry carries none.
    pub allow_pip: Option<Vec<String>>,
}

impl From<(String, Vec<u8>)> for ModulePayload {
    fn from((name, bytes): (String, Vec<u8>)) -> Self {
        ModulePayload {
            name,
            bytes,
            allow_pip: None,
        }
    }
}

/// Open, name, (optionally) select and de-duplicate the modules of an export,
/// re-check the pip rule against each one's signed opt-in, then require a
/// complete `build: depends` closure over what is exported.
///
/// The pip rule is enforced HERE as well as at deposit: a layer signed by a
/// varve that predates the deposit rule carries pip modules with no opt-in, and
/// the export is the last place that can refuse them.
pub fn prepare<P: Into<ModulePayload> + Clone>(
    payloads: &[P],
    select: Option<&str>,
) -> Result<Vec<ExportModule>, ZephyrModuleError> {
    let mut modules = Vec::with_capacity(payloads.len());
    for p in payloads {
        let ModulePayload {
            name: payload,
            bytes,
            allow_pip,
        } = p.clone().into();
        let tree = open(&payload, &bytes)?;
        let name = module_name(&payload, &tree.yml)?;
        modules.push(ExportModule {
            payload,
            name,
            tree,
            allow_pip,
        });
    }
    modules.sort_by(|a, b| (&a.name, &a.payload).cmp(&(&b.name, &b.payload)));
    if let Some(asked) = select {
        let carried = join(
            modules
                .iter()
                .map(|m| format!("{} (payload {})", m.name, m.payload)),
        );
        modules.retain(|m| m.name == asked || m.payload == asked);
        if modules.is_empty() {
            return Err(ZephyrModuleError::UnknownSelect {
                asked: asked.to_string(),
                carried,
            });
        }
    }
    for m in &modules {
        check_pip(&m.payload, &m.tree.yml, m.allow_pip.as_deref())?;
    }
    for (i, m) in modules.iter().enumerate() {
        if let Some(first) = modules[..i]
            .iter()
            .find(|f| f.name.to_lowercase() == m.name.to_lowercase())
        {
            return Err(ZephyrModuleError::DuplicateModule {
                name: m.name.clone(),
                first: first.payload.clone(),
                second: m.payload.clone(),
            });
        }
    }
    check_closure(&modules)?;
    Ok(modules)
}

/// Every `build: depends` of every module must itself be exported.
pub fn check_closure(modules: &[ExportModule]) -> Result<(), ZephyrModuleError> {
    let names: BTreeSet<&str> = modules.iter().map(|m| m.name.as_str()).collect();
    for m in modules {
        for dep in &m.tree.yml.depends {
            if !names.contains(dep.as_str()) {
                return Err(ZephyrModuleError::IncompleteClosure {
                    module: m.name.clone(),
                    dependency: dep.clone(),
                    carried: join(&names),
                });
            }
        }
    }
    Ok(())
}

/// A path as a CMake quoted argument.
///
/// `;` is refused rather than escaped: `ZEPHYR_EXTRA_MODULES` is a LIST, and a
/// list element containing `;` is read back as two.
pub fn cmake_quote(path: &str) -> Result<String, ZephyrModuleError> {
    let unsafe_path = |why| ZephyrModuleError::UnsafeCmakePath {
        path: path.to_string(),
        why,
    };
    let mut out = String::with_capacity(path.len() + 2);
    out.push('"');
    for c in path.chars() {
        match c {
            ';' => return Err(unsafe_path("a ';' would split the module list in two")),
            '[' | ']' => return Err(unsafe_path("a bracket can open a CMake bracket argument")),
            '\\' => return Err(unsafe_path("a backslash is an escape in CMake")),
            c if c.is_control() => return Err(unsafe_path("it contains a control character")),
            '"' | '$' | '@' => {
                out.push('\\');
                out.push(c);
            }
            c => out.push(c),
        }
    }
    out.push('"');
    Ok(out)
}

/// Header values land in `#` comments; a line break would end the comment and
/// start CMake code.
fn comment_safe(s: &str) -> String {
    s.chars()
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect()
}

/// The script appending each `(name, absolute dir)` to `ZEPHYR_EXTRA_MODULES`.
///
/// It works both ways Zephyr users reach for. `include()` runs it as ordinary
/// CMake, where `list(APPEND)` is enough. `west build ... -- -C script` runs it
/// as an INITIAL CACHE script, where a plain variable never reaches the
/// project: only a cache entry does, so the list is also written to the cache.
pub fn cmake_script(
    layer: &str,
    manifest_digest: &str,
    dirs: &[(String, String)],
) -> Result<String, ZephyrModuleError> {
    let mut s = String::new();
    s.push_str("# Generated by `varve export-zephyr-modules`. Do not edit; re-run the command.\n");
    s.push_str(&format!("# Layer: {}\n", comment_safe(layer)));
    s.push_str(&format!(
        "# Manifest digest: {}\n",
        comment_safe(manifest_digest)
    ));
    s.push_str(
        "#\n\
         # Appends the verified Zephyr modules below to ZEPHYR_EXTRA_MODULES. Your west.yml is\n\
         # never touched. Use it either way:\n\
         #   west build ... -- -C <this file>\n\
         #   include(<this file>)      # in a plain CMake build\n\n",
    );
    for (name, dir) in dirs {
        s.push_str(&format!("# {}\n", comment_safe(name)));
        s.push_str(&format!(
            "list(APPEND ZEPHYR_EXTRA_MODULES {})\n",
            cmake_quote(dir)?
        ));
    }
    s.push_str(
        "list(REMOVE_DUPLICATES ZEPHYR_EXTRA_MODULES)\n\
         set(ZEPHYR_EXTRA_MODULES \"${ZEPHYR_EXTRA_MODULES}\" CACHE STRING\n\
         \x20   \"Zephyr modules added by varve\" FORCE)\n",
    );
    Ok(s)
}

/// What an export wrote.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ExportReport {
    /// (module name, absolute directory), in export order.
    pub modules: Vec<(String, String)>,
    pub files: usize,
    pub cmake: String,
    /// Module directories of a previous export that this one no longer
    /// carries, removed so the stamp never vouches for a leftover.
    pub removed: Vec<String>,
}

fn io_err(path: &Path, e: impl std::fmt::Display) -> ZephyrModuleError {
    ZephyrModuleError::Io {
        path: path.display().to_string(),
        detail: e.to_string(),
    }
}

/// Does `out` carry a stamp that says a zephyr-module export is what is here?
fn ours_stamp(out: &Path) -> bool {
    crate::exportstamp::read_stamp(out)
        .is_ok_and(|st| st.kind == crate::kind::PayloadKind::ZephyrModule.as_str())
}

/// Is `dir` a real directory varve marked as the module `name`?
fn is_marked_dir(dir: &Path, name: &str) -> bool {
    let is_dir = std::fs::symlink_metadata(dir).is_ok_and(|md| md.is_dir());
    let marker = dir.join(MODULE_MARKER);
    let marked = std::fs::symlink_metadata(&marker).is_ok_and(|md| md.is_file())
        && std::fs::read_to_string(&marker)
            .is_ok_and(|text| text.lines().next() == Some(&format!("module: {name}")));
    is_dir && marked
}

fn not_ours(path: &Path) -> ZephyrModuleError {
    ZephyrModuleError::NotWrittenByVarve {
        path: path.display().to_string(),
    }
}

/// Write each module under `out/<name>/` and the CMake script beside them.
///
/// `out` must be absolute: the script names the module directories, and a
/// relative path there would resolve against wherever the build runs.
///
/// Everything is decided BEFORE anything is written or removed: every path
/// CMake must carry, every destination, the script and the stamp. A refusal
/// therefore leaves neither a half export nor a deleted directory behind.
///
/// A directory is replaced or removed only if it carries varve's own marker
/// ([`MODULE_MARKER`]) AND `out` carries a zephyr-module stamp. Neither alone
/// is enough: the stamp lists whatever was under `out`, including a user's own
/// files, and a marker without our stamp is not in an export directory of ours.
///
/// Known and deferred (REQ-ZMODEXPORT-002): the script persists the list in the
/// CMake cache with `FORCE`, so a LATER build in the same build tree keeps the
/// modules after the script is no longer passed with `-C`.
pub fn export(
    modules: &[ExportModule],
    out: &Path,
    layer: &str,
    manifest_digest: &str,
) -> Result<ExportReport, ZephyrModuleError> {
    if !out.is_absolute() {
        return Err(io_err(
            out,
            "the export directory must be absolute: the CMake script names it",
        ));
    }
    let ours = ours_stamp(out);
    let dirs: Vec<(String, String)> = modules
        .iter()
        .map(|m| (m.name.clone(), out.join(&m.name).display().to_string()))
        .collect();
    // Quoting is validated here, not after the trees are on disk.
    let script_text = cmake_script(layer, manifest_digest, &dirs)?;

    for m in modules {
        let dir = out.join(&m.name);
        // Nothing there (or nothing readable): `create_dir_all` below reports
        // any real failure with its own error. Anything there must be varve's.
        if std::fs::symlink_metadata(&dir).is_ok() && !(ours && is_marked_dir(&dir, &m.name)) {
            return Err(not_ours(&dir));
        }
    }
    // The script and the stamp are replaced only when they are ours: never
    // followed through a symlink, never overwritten when somebody else wrote
    // them. (The stamp is written by the caller, after this returns.)
    let script = out.join(CMAKE_FILE);
    let stamp_file = out.join(crate::exportstamp::STAMP_FILE);
    for path in [&script, &stamp_file] {
        match std::fs::symlink_metadata(path) {
            Err(_) => {}
            Ok(md) if md.is_file() && ours => {}
            Ok(_) => return Err(not_ours(path)),
        }
    }
    // Leftovers: directories a previous export of OURS made for modules this
    // export no longer carries. The stamp must never vouch for one.
    let mut leftovers = Vec::new();
    if ours && let Ok(entries) = std::fs::read_dir(out) {
        for entry in entries.flatten() {
            let name = entry.file_name().to_string_lossy().into_owned();
            if !modules.iter().any(|m| m.name == name) && is_marked_dir(&entry.path(), &name) {
                leftovers.push(name);
            }
        }
        leftovers.sort();
    }

    let mut report = ExportReport::default();
    for name in &leftovers {
        let dir = out.join(name);
        std::fs::remove_dir_all(&dir).map_err(|e| io_err(&dir, e))?;
        report.removed.push(name.clone());
    }
    for m in modules {
        let dir = out.join(&m.name);
        if std::fs::symlink_metadata(&dir).is_ok() {
            std::fs::remove_dir_all(&dir).map_err(|e| io_err(&dir, e))?;
        }
        std::fs::create_dir_all(&dir).map_err(|e| io_err(&dir, e))?;
        create_file(
            &dir.join(MODULE_MARKER),
            format!("module: {}\nlayer: {layer}\n", m.name).as_bytes(),
        )?;
        for member in &m.tree.members {
            let dest = dir.join(&member.path);
            match &member.body {
                MemberBody::Dir => std::fs::create_dir_all(&dest).map_err(|e| io_err(&dest, e))?,
                MemberBody::File { mode, bytes } => {
                    if let Some(parent) = dest.parent() {
                        std::fs::create_dir_all(parent).map_err(|e| io_err(parent, e))?;
                    }
                    create_file(&dest, bytes)?;
                    set_mode(&dest, *mode)?;
                    report.files += 1;
                }
                MemberBody::Symlink { .. } => {
                    return Err(ZephyrModuleError::Symlink {
                        payload: m.payload.clone(),
                        member: member.path.clone(),
                    });
                }
            }
        }
    }
    report.modules = dirs;
    report.cmake = script_text;
    if std::fs::symlink_metadata(&script).is_ok() {
        std::fs::remove_file(&script).map_err(|e| io_err(&script, e))?;
    }
    create_file(&script, report.cmake.as_bytes())?;
    Ok(report)
}

/// `create_new`: a file is CREATED, never overwritten and never written
/// through a link. The directory is fresh, so a collision means two members
/// landed on one path, and the later must not win.
fn create_file(path: &Path, bytes: &[u8]) -> Result<(), ZephyrModuleError> {
    use std::io::Write;
    std::fs::OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(path)
        .and_then(|mut f| f.write_all(bytes))
        .map_err(|e| io_err(path, e))
}

/// Executable stays executable, everything else is plain; setuid/setgid and
/// group/other write bits from an archive are never honoured.
#[cfg(unix)]
fn set_mode(path: &Path, mode: u32) -> Result<(), ZephyrModuleError> {
    use std::os::unix::fs::PermissionsExt;
    let mode = if mode & 0o111 != 0 { 0o755 } else { 0o644 };
    std::fs::set_permissions(path, std::fs::Permissions::from_mode(mode))
        .map_err(|e| io_err(path, e))
}

#[cfg(not(unix))]
fn set_mode(_path: &Path, _mode: u32) -> Result<(), ZephyrModuleError> {
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;

    // ------------------------------------------------------------ fixtures

    /// A gzip-tar of `files` (path, content). Directories are implied.
    fn archive(files: &[(&str, &str)]) -> Vec<u8> {
        let mut tar = tar::Builder::new(Vec::new());
        for (path, body) in files {
            let mut h = tar::Header::new_gnu();
            h.set_size(body.len() as u64);
            h.set_mode(0o644);
            h.set_entry_type(tar::EntryType::Regular);
            tar.append_data(&mut h, path, body.as_bytes()).unwrap();
        }
        let raw = tar.into_inner().unwrap();
        let mut gz = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::fast());
        gz.write_all(&raw).unwrap();
        gz.finish().unwrap()
    }

    fn module(name: &str, depends: &[&str]) -> Vec<u8> {
        let mut yml = format!("name: {name}\nbuild:\n  cmake: .\n");
        if !depends.is_empty() {
            yml.push_str("  depends:\n");
            for d in depends {
                yml.push_str(&format!("    - {d}\n"));
            }
        }
        archive(&[
            ("zephyr/module.yml", &yml),
            ("CMakeLists.txt", "# module\n"),
            ("src/lib.c", "int x;\n"),
        ])
    }

    fn pip_module(files: &[&str]) -> Vec<u8> {
        let mut yml = String::from("name: py\npackage-managers:\n  pip:\n    requirement-files:\n");
        for f in files {
            yml.push_str(&format!("      - {f}\n"));
        }
        archive(&[("zephyr/module.yml", &yml)])
    }

    fn strings(v: &[&str]) -> Vec<String> {
        v.iter().map(|s| s.to_string()).collect()
    }

    // ------------------------------------------------------- module.yml parser

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_parser_reads_name_depends_and_ignores_the_rest() {
        let y = parse_module_yml(
            "p",
            "name: gale\nbuild:\n  cmake: .\n  kconfig: Kconfig\n  depends:\n    - hal_a\n    - hal_b\n  settings:\n    board_root: .\ntests:\n  - tests\n",
        )
        .unwrap();
        assert_eq!(y.name.as_deref(), Some("gale"));
        assert_eq!(y.depends, strings(&["hal_a", "hal_b"]));
        assert_eq!(y.pip, None, "no package-managers means no pip");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_parser_reads_flow_style_comments_and_quotes() {
        let y = parse_module_yml(
            "p",
            "# a module\nname: \"quoted name\"  # trailing\nbuild: {depends: [a, 'b']}\n",
        )
        .unwrap();
        assert_eq!(y.name.as_deref(), Some("quoted name"));
        assert_eq!(y.depends, strings(&["a", "b"]));
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_module_with_no_name_and_no_depends_parses_to_defaults() {
        let y = parse_module_yml("p", "build:\n  cmake: .\n").unwrap();
        assert_eq!(y, ModuleYml::default());
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn pip_is_seen_with_files_without_files_and_with_a_null_value() {
        let with = parse_module_yml(
            "p",
            "package-managers:\n  pip:\n    requirement-files:\n      - a.txt\n      - b/c.txt\n",
        )
        .unwrap();
        assert_eq!(with.pip, Some(strings(&["a.txt", "b/c.txt"])));
        let bare = parse_module_yml("p", "package-managers:\n  pip: {}\n").unwrap();
        assert_eq!(bare.pip, Some(vec![]));
        let null = parse_module_yml("p", "package-managers:\n  pip:\n").unwrap();
        assert_eq!(null.pip, Some(vec![]), "a null `pip:` still declares pip");
        let other = parse_module_yml("p", "package-managers:\n  npm: {}\n").unwrap();
        assert_eq!(other.pip, None, "only pip is a Python dependency");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_malformed_module_yml_is_refused_naming_the_payload() {
        for bad in [
            "name: [unclosed\n",
            "build:\n  depends: not-a-list\n",
            "name:\n  - a\n  - b\n",
            "",
            "  \n\t\n",
            "\t\t: : :\n",
        ] {
            let e = parse_module_yml("gale", bad).expect_err(bad);
            assert!(
                matches!(&e, ZephyrModuleError::BadModuleYml { payload, .. } if payload == "gale"),
                "{bad:?} -> {e:?}"
            );
            assert!(e.to_string().contains("gale"), "{e}");
        }
    }

    // ---------------------------------------------------------- names

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_name_comes_from_module_yml_else_the_payload() {
        let named = ModuleYml {
            name: Some("from-yml".into()),
            ..Default::default()
        };
        assert_eq!(module_name("payload", &named).unwrap(), "from-yml");
        assert_eq!(
            module_name("payload", &ModuleYml::default()).unwrap(),
            "payload"
        );
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_name_that_is_not_a_plain_directory_component_is_refused() {
        for bad in [
            "",
            ".",
            "..",
            "a/b",
            "a\\b",
            "../x",
            "/abs",
            ".hidden",
            "-dash",
            "nul\0byte",
            "new\nline",
            "tab\tname",
            CMAKE_FILE,
        ] {
            let y = ModuleYml {
                name: Some(bad.into()),
                ..Default::default()
            };
            let e = module_name("p", &y).expect_err(bad);
            assert!(matches!(e, ZephyrModuleError::BadName { .. }), "{bad:?}");
        }
        // The payload name is judged by the same rule.
        let e = module_name("../evil", &ModuleYml::default()).expect_err("payload name");
        assert!(matches!(e, ZephyrModuleError::BadName { .. }));
        for ok in ["gale", "hal_stm32", "my.module", "a b", "x-1_2"] {
            let y = ModuleYml {
                name: Some(ok.into()),
                ..Default::default()
            };
            assert_eq!(module_name("p", &y).unwrap(), ok);
        }
    }

    // --------------------------------------------------------- open

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_tree_with_a_module_yml_opens() {
        let t = open("gale", &module("gale", &["hal"])).unwrap();
        assert_eq!(t.yml.name.as_deref(), Some("gale"));
        assert_eq!(t.yml.depends, strings(&["hal"]));
        let paths: Vec<&str> = t.members.iter().map(|m| m.path.as_str()).collect();
        assert!(paths.contains(&"zephyr/module.yml"), "{paths:?}");
        assert!(paths.contains(&"src/lib.c"), "{paths:?}");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_tree_without_a_module_yml_is_refused() {
        let a = archive(&[("CMakeLists.txt", "x"), ("src/a.c", "y")]);
        let e = open("nomod", &a).expect_err("no module.yml");
        assert!(
            matches!(&e, ZephyrModuleError::NoModuleYml { payload, members: 2, .. } if payload == "nomod"),
            "{e:?}"
        );
        let msg = e.to_string();
        assert!(
            msg.contains("zephyr/module.yml") && msg.contains("nomod"),
            "{msg}"
        );
        // A module.yml in the WRONG place (not under zephyr/) is not one.
        let a = archive(&[("module.yml", "name: x\n"), ("zephyr/other", "y")]);
        assert!(matches!(
            open("p", &a),
            Err(ZephyrModuleError::NoModuleYml { .. })
        ));
        // Nor is a directory called module.yml.
        let a = archive(&[("zephyr/module.yml/inner", "name: x\n")]);
        assert!(matches!(
            open("p", &a),
            Err(ZephyrModuleError::NoModuleYml { .. })
        ));
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_malformed_module_yml_refuses_the_open() {
        let a = archive(&[("zephyr/module.yml", "name: [unclosed\n")]);
        assert!(matches!(
            open("p", &a),
            Err(ZephyrModuleError::BadModuleYml { .. })
        ));
        let a = archive(&[("zephyr/module.yml", "\u{ff}")]);
        let bad = {
            // invalid UTF-8 bytes, not merely a non-ASCII char
            let mut tar = tar::Builder::new(Vec::new());
            let mut h = tar::Header::new_gnu();
            h.set_size(2);
            h.set_mode(0o644);
            h.set_entry_type(tar::EntryType::Regular);
            tar.append_data(&mut h, "zephyr/module.yml", &[0xff, 0xfe][..])
                .unwrap();
            tar.into_inner().unwrap()
        };
        drop(a);
        let e = open("p", &bad).expect_err("not utf-8");
        assert!(matches!(e, ZephyrModuleError::BadModuleYml { .. }), "{e:?}");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_wrapper_directory_is_stripped_and_the_tree_is_stored_below_it() {
        let a = archive(&[
            ("gale-1.0/zephyr/module.yml", "name: gale\n"),
            ("gale-1.0/CMakeLists.txt", "x"),
        ]);
        let t = open("gale", &a).unwrap();
        let mut paths: Vec<&str> = t.members.iter().map(|m| m.path.as_str()).collect();
        paths.sort();
        assert_eq!(paths, vec!["CMakeLists.txt", "zephyr/module.yml"]);
    }

    fn dir_entry(tar: &mut tar::Builder<Vec<u8>>, path: &str) {
        let mut d = tar::Header::new_gnu();
        d.set_size(0);
        d.set_mode(0o755);
        d.set_entry_type(tar::EntryType::Directory);
        tar.append_data(&mut d, path, &[][..]).unwrap();
    }

    fn file_entry(tar: &mut tar::Builder<Vec<u8>>, path: &str, body: &str) {
        let mut h = tar::Header::new_gnu();
        h.set_size(body.len() as u64);
        h.set_mode(0o644);
        h.set_entry_type(tar::EntryType::Regular);
        tar.append_data(&mut h, path, body.as_bytes()).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_wrapper_with_its_own_directory_entry_and_a_dot_root_entry_opens() {
        let mut tar = tar::Builder::new(Vec::new());
        dir_entry(&mut tar, "./");
        dir_entry(&mut tar, "./w/");
        file_entry(&mut tar, "./w/zephyr/module.yml", "name: w\n");
        file_entry(&mut tar, "./w/a.c", "x");
        let t = open("p", &tar.into_inner().unwrap()).unwrap();
        let mut paths: Vec<&str> = t.members.iter().map(|m| m.path.as_str()).collect();
        paths.sort();
        assert_eq!(paths, vec!["a.c", "zephyr/module.yml"]);
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn two_top_level_directories_are_not_a_wrapper() {
        let mut tar = tar::Builder::new(Vec::new());
        file_entry(&mut tar, "w/zephyr/module.yml", "name: w\n");
        dir_entry(&mut tar, "other/");
        let e = open("p", &tar.into_inner().unwrap()).expect_err("no wrapper");
        assert!(matches!(e, ZephyrModuleError::NoModuleYml { .. }), "{e:?}");
        let mut tar = tar::Builder::new(Vec::new());
        file_entry(&mut tar, "w/zephyr/module.yml", "name: w\n");
        file_entry(&mut tar, "other/x", "y");
        assert!(matches!(
            open("p", &tar.into_inner().unwrap()),
            Err(ZephyrModuleError::NoModuleYml { .. })
        ));
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_tree_that_is_only_zephyr_module_yml_is_not_mistaken_for_a_wrapper() {
        let a = archive(&[("zephyr/module.yml", "name: tiny\n")]);
        let t = open("p", &a).unwrap();
        assert_eq!(t.members.len(), 1);
        assert_eq!(t.members[0].path, "zephyr/module.yml");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn dot_slash_paths_and_a_git_archive_header_do_not_hide_the_module_yml() {
        let a = archive(&[
            ("pax_global_header", "52 comment=abc\n"),
            ("./zephyr/module.yml", "name: dotted\n"),
            ("./README", "r"),
        ]);
        let t = open("p", &a).unwrap();
        assert_eq!(t.yml.name.as_deref(), Some("dotted"));
        assert!(t.members.iter().all(|m| m.path != "pax_global_header"));
        assert!(t.members.iter().any(|m| m.path == "README"));
    }

    /// A tar whose member path is written RAW, because `tar::Builder` itself
    /// refuses to write a `..`.
    fn raw_path_tar(entries: &[(&str, &str)]) -> Vec<u8> {
        let mut out = Vec::new();
        for (path, body) in entries {
            let mut h = tar::Header::new_gnu();
            h.set_size(body.len() as u64);
            h.set_mode(0o644);
            h.set_entry_type(tar::EntryType::Regular);
            h.as_gnu_mut().unwrap().name[..path.len()].copy_from_slice(path.as_bytes());
            h.set_cksum();
            out.extend_from_slice(h.as_bytes());
            out.extend_from_slice(body.as_bytes());
            out.resize(out.len().div_ceil(512) * 512, 0);
        }
        out.extend_from_slice(&[0u8; 1024]);
        out
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_member_that_escapes_the_tree_is_refused() {
        for evil in ["../evil", "a/../../evil", "/abs/evil"] {
            let a = raw_path_tar(&[("zephyr/module.yml", "name: x\n"), (evil, "y")]);
            let e = open("p", &a).expect_err(evil);
            assert!(
                matches!(&e, ZephyrModuleError::UnsafeMember { member, .. } if member == evil),
                "{evil}: {e:?}"
            );
        }
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_symlink_in_the_tree_is_refused() {
        let mut tar = tar::Builder::new(Vec::new());
        let mut h = tar::Header::new_gnu();
        h.set_size(10);
        h.set_mode(0o644);
        h.set_entry_type(tar::EntryType::Regular);
        tar.append_data(&mut h, "zephyr/module.yml", &b"name: s\n\n\n\n"[..10])
            .unwrap();
        let mut l = tar::Header::new_gnu();
        l.set_size(0);
        l.set_entry_type(tar::EntryType::Symlink);
        tar.append_link(&mut l, "link", "/etc/passwd").unwrap();
        let e = open("p", &tar.into_inner().unwrap()).expect_err("symlink");
        assert!(
            matches!(&e, ZephyrModuleError::Symlink { member, .. } if member == "link"),
            "{e:?}"
        );
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_path_listed_twice_is_refused() {
        let a = archive(&[
            ("zephyr/module.yml", "name: x\n"),
            ("a.c", "1"),
            ("a.c", "2"),
        ]);
        let e = open("p", &a).expect_err("duplicate");
        assert!(
            matches!(&e, ZephyrModuleError::DuplicateMember { path, .. } if path == "a.c"),
            "{e:?}"
        );
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn something_that_is_not_an_archive_is_refused() {
        let e = open("p", b"<html>404</html>").expect_err("html");
        assert!(
            matches!(
                e,
                ZephyrModuleError::Archive { .. } | ZephyrModuleError::NoModuleYml { .. }
            ),
            "{e:?}"
        );
    }

    // ------------------------------------------------------ pip, clause 5

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn pip_without_an_opt_in_is_refused_naming_the_files() {
        let e = check_deposit("py", &pip_module(&["scripts/requirements.txt"]), None)
            .expect_err("pip, no opt-in");
        assert!(
            matches!(e, ZephyrModuleError::PipWithoutOptIn { .. }),
            "{e:?}"
        );
        let msg = e.to_string();
        assert!(msg.contains("scripts/requirements.txt"), "{msg}");
        assert!(msg.contains("allow-pip"), "{msg}");
        assert!(msg.contains("py"), "{msg}");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn pip_with_the_matching_opt_in_is_accepted() {
        let allow = strings(&["scripts/requirements.txt"]);
        check_deposit(
            "py",
            &pip_module(&["scripts/requirements.txt"]),
            Some(&allow),
        )
        .unwrap();
        // Order and repetition do not matter; the SET is what is accepted.
        let allow = strings(&["b.txt", "a.txt", "a.txt"]);
        check_deposit("py", &pip_module(&["a.txt", "b.txt"]), Some(&allow)).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn pip_with_a_mismatched_opt_in_is_refused_both_ways() {
        // The opt-in names a DIFFERENT file than the one declared.
        let allow = strings(&["other.txt"]);
        let e = check_deposit(
            "py",
            &pip_module(&["scripts/requirements.txt"]),
            Some(&allow),
        )
        .expect_err("wrong file");
        match &e {
            ZephyrModuleError::PipOptInMismatch { missing, extra, .. } => {
                assert_eq!(missing, "scripts/requirements.txt");
                assert_eq!(extra, "other.txt");
            }
            other => panic!("{other:?}"),
        }
        // The opt-in covers only SOME of the declared files.
        let allow = strings(&["a.txt"]);
        let e = check_deposit("py", &pip_module(&["a.txt", "b.txt"]), Some(&allow))
            .expect_err("subset");
        match &e {
            ZephyrModuleError::PipOptInMismatch { missing, extra, .. } => {
                assert_eq!(missing, "b.txt");
                assert_eq!(extra, "");
            }
            other => panic!("{other:?}"),
        }
        // The opt-in names MORE than the module declares.
        let allow = strings(&["a.txt", "extra.txt"]);
        let e = check_deposit("py", &pip_module(&["a.txt"]), Some(&allow)).expect_err("superset");
        match &e {
            ZephyrModuleError::PipOptInMismatch { missing, extra, .. } => {
                assert_eq!(missing, "");
                assert_eq!(extra, "extra.txt");
            }
            other => panic!("{other:?}"),
        }
        assert!(e.to_string().contains("EXACTLY"), "{e}");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn bare_pip_needs_an_explicit_empty_opt_in() {
        let a = archive(&[("zephyr/module.yml", "package-managers:\n  pip: {}\n")]);
        let e = check_deposit("p", &a, None).expect_err("bare pip, no opt-in");
        assert!(matches!(e, ZephyrModuleError::PipWithoutOptIn { .. }));
        assert!(e.to_string().contains("none listed"), "{e}");
        check_deposit("p", &a, Some(&[])).unwrap();
        let allow = strings(&["x.txt"]);
        assert!(matches!(
            check_deposit("p", &a, Some(&allow)),
            Err(ZephyrModuleError::PipOptInMismatch { .. })
        ));
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn an_opt_in_for_a_module_that_declares_no_pip_is_refused() {
        let allow = strings(&["scripts/requirements.txt"]);
        let e = check_deposit("p", &module("p", &[]), Some(&allow)).expect_err("inert");
        assert!(
            matches!(e, ZephyrModuleError::PipOptInUnneeded { .. }),
            "{e:?}"
        );
        // Even an EMPTY opt-in is inert where nothing is declared.
        assert!(matches!(
            check_deposit("p", &module("p", &[]), Some(&[])),
            Err(ZephyrModuleError::PipOptInUnneeded { .. })
        ));
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_module_without_pip_needs_no_opt_in() {
        check_deposit("p", &module("p", &["x"]), None).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_deposit_check_runs_all_of_it() {
        // no module.yml
        assert!(matches!(
            check_deposit("p", &archive(&[("a", "b")]), None),
            Err(ZephyrModuleError::NoModuleYml { .. })
        ));
        // a bad NAME in a good tree
        let a = archive(&[("zephyr/module.yml", "name: ../x\n")]);
        assert!(matches!(
            check_deposit("p", &a, None),
            Err(ZephyrModuleError::BadName { .. })
        ));
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn allow_pip_entries_must_survive_the_annotation_encoding() {
        check_allow_pip_entries(&strings(&["a.txt", "dir/b.txt"])).unwrap();
        check_allow_pip_entries(&[]).unwrap();
        for bad in ["", "a,b", "new\nline", "tab\there", "nul\0"] {
            let e = check_allow_pip_entries(&strings(&[bad])).expect_err(bad);
            assert!(
                matches!(e, ZephyrModuleError::BadPipEntry { .. }),
                "{bad:?}"
            );
        }
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_annotation_round_trips_sorted_and_deduplicated() {
        let raw = allow_pip_to_annotation(&strings(&["b.txt", "a.txt", "b.txt"]));
        assert_eq!(raw, "a.txt,b.txt");
        assert_eq!(
            allow_pip_from_annotation(&raw),
            strings(&["a.txt", "b.txt"])
        );
        assert_eq!(allow_pip_to_annotation(&[]), "");
        assert_eq!(allow_pip_from_annotation(""), Vec::<String>::new());
    }

    // ------------------------------------------------------ prepare / closure

    fn payloads(v: &[(&str, Vec<u8>)]) -> Vec<(String, Vec<u8>)> {
        v.iter().map(|(n, b)| (n.to_string(), b.clone())).collect()
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn prepare_names_modules_from_module_yml_and_keeps_the_payload_name() {
        let m = prepare(
            &payloads(&[
                ("pay-a", module("name-a", &[])),
                ("b", archive(&[("zephyr/module.yml", "build: {}\n")])),
            ]),
            None,
        )
        .unwrap();
        assert_eq!(m.len(), 2);
        // Sorted by module name, so an export is the same whatever order the
        // composition handed the payloads over in.
        assert_eq!((m[0].payload.as_str(), m[0].name.as_str()), ("b", "b"));
        assert_eq!(
            (m[1].payload.as_str(), m[1].name.as_str()),
            ("pay-a", "name-a")
        );
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn two_payloads_that_are_one_module_name_are_refused() {
        let e = prepare(
            &payloads(&[("one", module("same", &[])), ("two", module("same", &[]))]),
            None,
        )
        .expect_err("duplicate name");
        assert!(
            matches!(e, ZephyrModuleError::DuplicateModule { .. }),
            "{e:?}"
        );
        // Case-insensitively: macOS and Windows would merge the directories.
        let e = prepare(
            &payloads(&[("one", module("Gale", &[])), ("two", module("gale", &[]))]),
            None,
        )
        .expect_err("case-folded duplicate");
        assert!(
            matches!(e, ZephyrModuleError::DuplicateModule { .. }),
            "{e:?}"
        );
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn select_picks_by_module_name_or_payload_name_and_refuses_an_unknown_one() {
        let all = payloads(&[("pa", module("alpha", &[])), ("pb", module("beta", &[]))]);
        let by_module = prepare(&all, Some("beta")).unwrap();
        assert_eq!(by_module.len(), 1);
        assert_eq!(by_module[0].name, "beta");
        let by_payload = prepare(&all, Some("pa")).unwrap();
        assert_eq!(by_payload.len(), 1);
        assert_eq!(by_payload[0].name, "alpha");
        let e = prepare(&all, Some("gamma")).expect_err("unknown");
        match &e {
            ZephyrModuleError::UnknownSelect { asked, carried } => {
                assert_eq!(asked, "gamma");
                assert!(
                    carried.contains("alpha") && carried.contains("beta"),
                    "{carried}"
                );
            }
            other => panic!("{other:?}"),
        }
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_dependency_outside_the_exported_set_is_refused_naming_both() {
        let e = prepare(&payloads(&[("app", module("app", &["missing_mod"]))]), None)
            .expect_err("incomplete closure");
        match &e {
            ZephyrModuleError::IncompleteClosure {
                module, dependency, ..
            } => {
                assert_eq!(module, "app");
                assert_eq!(dependency, "missing_mod");
            }
            other => panic!("{other:?}"),
        }
        let msg = e.to_string();
        assert!(msg.contains("app") && msg.contains("missing_mod"), "{msg}");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_complete_closure_passes_in_any_order() {
        let m = prepare(
            &payloads(&[
                ("app", module("app", &["lib_a", "lib_b"])),
                ("lib_b", module("lib_b", &["lib_a"])),
                ("lib_a", module("lib_a", &[])),
            ]),
            None,
        )
        .unwrap();
        assert_eq!(m.len(), 3);
        check_closure(&m).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn narrowing_select_past_a_dependency_breaks_the_closure() {
        let all = payloads(&[
            ("app", module("app", &["lib"])),
            ("lib", module("lib", &[])),
        ]);
        assert_eq!(prepare(&all, Some("lib")).unwrap().len(), 1);
        let e = prepare(&all, Some("app")).expect_err("lib not selected");
        assert!(
            matches!(&e, ZephyrModuleError::IncompleteClosure { dependency, .. } if dependency == "lib"),
            "{e:?}"
        );
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_dependency_is_matched_by_module_name_not_by_payload_name() {
        // The payload is called 'p-lib' but the module is 'lib'.
        let m = prepare(
            &payloads(&[
                ("app", module("app", &["lib"])),
                ("p-lib", module("lib", &[])),
            ]),
            None,
        )
        .unwrap();
        assert_eq!(m.len(), 2);
        let e = prepare(
            &payloads(&[
                ("app", module("app", &["p-lib"])),
                ("p-lib", module("lib", &[])),
            ]),
            None,
        )
        .expect_err("depends names the payload, not the module");
        assert!(matches!(e, ZephyrModuleError::IncompleteClosure { .. }));
    }

    // ------------------------------------------------------------- cmake

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn cmake_quoting_escapes_what_cmake_would_interpret() {
        assert_eq!(cmake_quote("/plain/path").unwrap(), "\"/plain/path\"");
        assert_eq!(cmake_quote("/with space/x").unwrap(), "\"/with space/x\"");
        assert_eq!(cmake_quote("/q\"uote").unwrap(), "\"/q\\\"uote\"");
        assert_eq!(cmake_quote("/d$ollar").unwrap(), "\"/d\\$ollar\"");
        assert_eq!(cmake_quote("/${HOME}").unwrap(), "\"/\\${HOME}\"");
        assert_eq!(cmake_quote("/at@sign").unwrap(), "\"/at\\@sign\"");
        assert_eq!(cmake_quote("/hash#x").unwrap(), "\"/hash#x\"");
        assert_eq!(cmake_quote("/paren(s)").unwrap(), "\"/paren(s)\"");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn cmake_refuses_paths_it_cannot_carry_in_a_list() {
        // `;` splits a CMake list: one path would become two modules.
        for (bad, why) in [
            ("/a;b", "semicolon"),
            ("/a[b", "open bracket"),
            ("/a]b", "close bracket"),
            ("/a\\b", "backslash"),
            ("/a\nb", "newline"),
            ("/a\rb", "carriage return"),
            ("/a\0b", "NUL"),
            ("/a\u{7}b", "control"),
        ] {
            let e = cmake_quote(bad).expect_err(why);
            assert!(
                matches!(e, ZephyrModuleError::UnsafeCmakePath { .. }),
                "{why}"
            );
        }
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_script_appends_each_module_and_names_the_layer_and_digest() {
        let s = cmake_script(
            "2026.11.0",
            "sha256:abc123",
            &[
                ("gale".into(), "/out dir/gale".into()),
                ("hal".into(), "/out dir/hal".into()),
            ],
        )
        .unwrap();
        assert!(s.contains("2026.11.0"), "{s}");
        assert!(s.contains("sha256:abc123"), "{s}");
        assert!(
            s.lines()
                .any(|l| l == "list(APPEND ZEPHYR_EXTRA_MODULES \"/out dir/gale\")"),
            "{s}"
        );
        assert!(
            s.lines()
                .any(|l| l == "list(APPEND ZEPHYR_EXTRA_MODULES \"/out dir/hal\")"),
            "{s}"
        );
        // Usable under `-C` as well as `include()`: a plain variable set in a
        // -C script does not reach the project, so the list is also cached.
        assert!(s.contains("CACHE"), "{s}");
        assert!(s.lines().next().unwrap().starts_with('#'), "{s}");
        assert!(s.contains("west.yml is\n# never touched"), "{s}");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_newline_in_the_header_values_cannot_inject_cmake() {
        let s = cmake_script(
            "evil\nmessage(FATAL_ERROR \"pwned\")",
            "sha256:x\r\nmessage(FATAL_ERROR \"pwned2\")",
            &[("m".into(), "/m".into())],
        )
        .unwrap();
        for l in s.lines() {
            assert!(
                l.starts_with('#')
                    || l.is_empty()
                    || l.starts_with("list(")
                    || l.starts_with("set(")
                    || l.starts_with("  "),
                "an injected line survived: {l:?}"
            );
        }
        assert!(!s.contains('\r'));
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_script_refuses_an_unquotable_directory() {
        let e = cmake_script("l", "d", &[("m".into(), "/a;b".into())]).expect_err("semicolon");
        assert!(matches!(e, ZephyrModuleError::UnsafeCmakePath { .. }));
    }

    // ------------------------------------------------------------ export

    fn temp(tag: &str) -> std::path::PathBuf {
        let d = std::env::temp_dir().join(format!(
            "varve-zmod-{tag}-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&d).unwrap();
        d
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn export_writes_each_tree_as_stored_and_the_cmake_beside_them() {
        let out = temp("export");
        let mods = prepare(
            &payloads(&[
                ("gale", module("gale", &["hal"])),
                ("hal", module("hal", &[])),
            ]),
            None,
        )
        .unwrap();
        let r = export(&mods, &out, "2026.11.0", "sha256:dd").unwrap();
        assert_eq!(r.modules.len(), 2);
        assert_eq!(
            std::fs::read_to_string(out.join("gale/zephyr/module.yml")).unwrap(),
            "name: gale\nbuild:\n  cmake: .\n  depends:\n    - hal\n"
        );
        assert_eq!(
            std::fs::read_to_string(out.join("hal/src/lib.c")).unwrap(),
            "int x;\n"
        );
        assert!(out.join("gale/CMakeLists.txt").is_file());
        let cmake = std::fs::read_to_string(out.join(CMAKE_FILE)).unwrap();
        assert_eq!(cmake, r.cmake);
        assert!(cmake.contains(&format!(
            "list(APPEND ZEPHYR_EXTRA_MODULES \"{}\")",
            out.join("gale").display()
        )));
        assert!(cmake.contains(&format!(
            "list(APPEND ZEPHYR_EXTRA_MODULES \"{}\")",
            out.join("hal").display()
        )));
        // 3 files per module; the cmake file is not a module file.
        assert_eq!(r.files, 6);
        // No west.yml is created.
        assert!(!out.join("west.yml").exists());
        std::fs::remove_dir_all(&out).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[cfg(unix)]
    #[test]
    fn export_keeps_the_exec_bit_and_drops_setuid() {
        use std::os::unix::fs::PermissionsExt;
        let mut tar = tar::Builder::new(Vec::new());
        for (path, mode, body) in [
            ("zephyr/module.yml", 0o644u32, "name: x\n"),
            ("scripts/run.sh", 0o4755, "#!/bin/sh\n"),
            ("data.txt", 0o600, "d"),
        ] {
            let mut h = tar::Header::new_gnu();
            h.set_size(body.len() as u64);
            h.set_mode(mode);
            h.set_entry_type(tar::EntryType::Regular);
            tar.append_data(&mut h, path, body.as_bytes()).unwrap();
        }
        let a = tar.into_inner().unwrap();
        let out = temp("mode");
        let mods = prepare(&[("x".to_string(), a)], None).unwrap();
        export(&mods, &out, "l", "d").unwrap();
        let mode = |p: &str| std::fs::metadata(out.join(p)).unwrap().permissions().mode() & 0o7777;
        assert_eq!(mode("x/scripts/run.sh"), 0o755);
        assert_eq!(mode("x/data.txt"), 0o644);
        std::fs::remove_dir_all(&out).unwrap();
    }

    /// What the CLI does after `export`: stamp the directory, which is the
    /// record that varve wrote what is in it.
    fn stamp(out: &Path, kind: &str) {
        let st = crate::exportstamp::stamp_for(out, "l", "sha256:d", kind).unwrap();
        crate::exportstamp::write_stamp(out, &st).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_second_export_over_a_first_replaces_cleanly_rather_than_merging() {
        let out = temp("stale");
        let mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        export(&mods, &out, "l", "d").unwrap();
        stamp(&out, "zephyr-module");
        std::fs::write(out.join("x/stale.txt"), "old").unwrap();
        export(&mods, &out, "l2", "d2").unwrap();
        assert!(!out.join("x/stale.txt").exists(), "a stale file survived");
        assert!(out.join("x/zephyr/module.yml").is_file());
        std::fs::remove_dir_all(&out).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_directory_varve_did_not_write_is_refused_and_survives() {
        let out = temp("foreign");
        std::fs::create_dir_all(out.join("gale")).unwrap();
        std::fs::write(out.join("gale/MARKER"), "my own checkout").unwrap();
        let mods = prepare(
            &payloads(&[
                ("alpha", module("alpha", &[])),
                ("gale", module("gale", &[])),
            ]),
            None,
        )
        .unwrap();
        let e = export(&mods, &out, "l", "d").expect_err("not ours");
        assert!(
            matches!(&e, ZephyrModuleError::NotWrittenByVarve { path } if path.ends_with("gale")),
            "{e:?}"
        );
        let msg = e.to_string();
        assert!(
            msg.contains("was not written by varve export-zephyr-modules")
                && msg.contains("choose another --out"),
            "{msg}"
        );
        assert_eq!(
            std::fs::read_to_string(out.join("gale/MARKER")).unwrap(),
            "my own checkout"
        );
        // Refused BEFORE anything is written: alpha sorts first and is not there.
        assert!(!out.join("alpha").exists());
        assert!(!out.join(CMAKE_FILE).exists());
        std::fs::remove_dir_all(&out).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn only_a_marked_directory_under_a_zephyr_stamp_licenses_a_replace() {
        let mods = prepare(&[("gale".to_string(), module("gale", &[]))], None).unwrap();
        let refused = |out: &Path, what: &str| {
            let e = export(&mods, out, "l", "d").expect_err(what);
            assert!(
                matches!(e, ZephyrModuleError::NotWrittenByVarve { .. }),
                "{what}: {e:?}"
            );
        };
        // Another export kind's stamp, over a directory varve marked.
        let out = temp("otherkind");
        export(&mods, &out, "l", "d").unwrap();
        stamp(&out, "cargo");
        refused(&out, "a cargo stamp is not ours");
        assert!(out.join("gale/zephyr/module.yml").is_file());
        std::fs::remove_dir_all(&out).unwrap();
        // A marked directory and NO stamp at all.
        let out = temp("nostamp");
        export(&mods, &out, "l", "d").unwrap();
        refused(&out, "no stamp");
        std::fs::remove_dir_all(&out).unwrap();
        // A zephyr stamp over a directory with no marker.
        let out = temp("nomarker");
        export(&mods, &out, "l", "d").unwrap();
        std::fs::remove_file(out.join("gale").join(MODULE_MARKER)).unwrap();
        stamp(&out, "zephyr-module");
        std::fs::write(out.join("gale/MARKER"), "mine").unwrap();
        refused(&out, "no marker");
        assert!(out.join("gale/MARKER").is_file());
        std::fs::remove_dir_all(&out).unwrap();
        // A marker naming a DIFFERENT module is not this one's.
        let out = temp("wrongmarker");
        export(&mods, &out, "l", "d").unwrap();
        std::fs::write(
            out.join("gale").join(MODULE_MARKER),
            "module: other\nlayer: l\n",
        )
        .unwrap();
        stamp(&out, "zephyr-module");
        refused(&out, "marker names another module");
        std::fs::remove_dir_all(&out).unwrap();
        // A malformed stamp.
        let out = temp("badstamp");
        export(&mods, &out, "l", "d").unwrap();
        std::fs::write(out.join(crate::exportstamp::STAMP_FILE), "{nope").unwrap();
        refused(&out, "malformed stamp");
        std::fs::remove_dir_all(&out).unwrap();
    }

    /// The reviewer's reproduction. The stamp lists EVERYTHING under `--out`,
    /// the user's files included, so a stamp listing `hal/…` proves nothing
    /// about who wrote `hal/`. Only varve's own marker does.
    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_stamp_that_swept_up_the_users_directory_does_not_license_deleting_it() {
        let out = temp("sweep");
        std::fs::create_dir_all(out.join("hal")).unwrap();
        std::fs::write(out.join("hal/MARKER"), "my own hal").unwrap();
        // 2. export {gale}: the stamp (scan_tree) now lists hal/MARKER too.
        let gale = prepare(&[("gale".to_string(), module("gale", &[]))], None).unwrap();
        export(&gale, &out, "l", "d").unwrap();
        stamp(&out, "zephyr-module");
        let st = crate::exportstamp::read_stamp(&out).unwrap();
        assert!(st.files.iter().any(|f| f.path == "hal/MARKER"));
        // 3. export {gale, hal}: hal/ is NOT varve's.
        let both = prepare(
            &payloads(&[("gale", module("gale", &[])), ("hal", module("hal", &[]))]),
            None,
        )
        .unwrap();
        let e = export(&both, &out, "l", "d").expect_err("hal is the user's");
        assert!(
            matches!(&e, ZephyrModuleError::NotWrittenByVarve { path } if path.ends_with("hal")),
            "{e:?}"
        );
        assert_eq!(
            std::fs::read_to_string(out.join("hal/MARKER")).unwrap(),
            "my own hal"
        );
        std::fs::remove_dir_all(&out).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_tree_may_not_carry_varves_own_marker_name() {
        for name in [MODULE_MARKER, ".VARVE-ZEPHYR-MODULE"] {
            let a = archive(&[("zephyr/module.yml", "name: x\n"), (name, "module: x\n")]);
            let e = open("p", &a).expect_err(name);
            assert!(matches!(e, ZephyrModuleError::UnsafeMember { .. }), "{e:?}");
        }
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn every_module_directory_carries_the_marker_naming_module_and_layer() {
        let out = temp("marker");
        let mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        export(&mods, &out, "2026.11.0", "sha256:d").unwrap();
        assert_eq!(
            std::fs::read_to_string(out.join("x").join(MODULE_MARKER)).unwrap(),
            "module: x\nlayer: 2026.11.0\n"
        );
        std::fs::remove_dir_all(&out).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_module_dropped_from_a_new_export_is_removed_and_the_users_directories_are_not() {
        let out = temp("leftover");
        let ab = prepare(
            &payloads(&[("a", module("a", &[])), ("b", module("b", &[]))]),
            None,
        )
        .unwrap();
        export(&ab, &out, "l", "d").unwrap();
        stamp(&out, "zephyr-module");
        std::fs::create_dir_all(out.join("mine")).unwrap();
        std::fs::write(out.join("mine/KEEP"), "k").unwrap();
        std::fs::write(out.join("note.txt"), "n").unwrap();
        let a = prepare(&[("a".to_string(), module("a", &[]))], None).unwrap();
        let r = export(&a, &out, "l2", "d2").unwrap();
        assert!(!out.join("b").exists(), "a leftover module survived");
        assert!(out.join("a/zephyr/module.yml").is_file());
        assert!(
            out.join("mine/KEEP").is_file(),
            "a user directory was touched"
        );
        assert!(out.join("note.txt").is_file());
        assert_eq!(r.removed, vec!["b".to_string()]);
        std::fs::remove_dir_all(&out).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn leftovers_are_removed_only_under_our_stamp() {
        for kind in ["cargo", ""] {
            let out = temp("leftover-foreign");
            let ab = prepare(
                &payloads(&[("a", module("a", &[])), ("b", module("b", &[]))]),
                None,
            )
            .unwrap();
            export(&ab, &out, "l", "d").unwrap();
            if !kind.is_empty() {
                stamp(&out, kind);
            }
            let a = prepare(&[("a".to_string(), module("a", &[]))], None).unwrap();
            // `a` is refused here (no zephyr stamp), and b must survive too.
            let _ = export(&a, &out, "l", "d");
            assert!(out.join("b/zephyr/module.yml").is_file(), "{kind:?}");
            std::fs::remove_dir_all(&out).unwrap();
        }
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[cfg(unix)]
    #[test]
    fn the_script_and_the_stamp_are_never_written_through_a_link_or_over_a_foreign_file() {
        let mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        // The script is a symlink.
        let out = temp("script-link");
        let victim = temp("victim");
        std::os::unix::fs::symlink(victim.join("target"), out.join(CMAKE_FILE)).unwrap();
        let e = export(&mods, &out, "l", "d").expect_err("symlinked script");
        assert!(
            matches!(e, ZephyrModuleError::NotWrittenByVarve { .. }),
            "{e:?}"
        );
        assert!(!victim.join("target").exists(), "written through the link");
        assert!(
            !out.join("x").exists(),
            "refused before any tree is written"
        );
        std::fs::remove_dir_all(&out).unwrap();
        // The script is somebody's regular file, with no stamp of ours.
        let out = temp("script-file");
        std::fs::write(out.join(CMAKE_FILE), "their own cmake").unwrap();
        let e = export(&mods, &out, "l", "d").expect_err("foreign script");
        assert!(
            matches!(e, ZephyrModuleError::NotWrittenByVarve { .. }),
            "{e:?}"
        );
        assert_eq!(
            std::fs::read_to_string(out.join(CMAKE_FILE)).unwrap(),
            "their own cmake"
        );
        std::fs::remove_dir_all(&out).unwrap();
        // The stamp is a symlink, or another export's.
        let out = temp("stamp-link");
        std::os::unix::fs::symlink(
            victim.join("stamp"),
            out.join(crate::exportstamp::STAMP_FILE),
        )
        .unwrap();
        let e = export(&mods, &out, "l", "d").expect_err("symlinked stamp");
        assert!(
            matches!(e, ZephyrModuleError::NotWrittenByVarve { .. }),
            "{e:?}"
        );
        assert!(!victim.join("stamp").exists());
        std::fs::remove_dir_all(&out).unwrap();
        let out = temp("stamp-other");
        stamp(&out, "cargo");
        let e = export(&mods, &out, "l", "d").expect_err("another export's stamp");
        assert!(
            matches!(e, ZephyrModuleError::NotWrittenByVarve { .. }),
            "{e:?}"
        );
        assert!(!out.join("x").exists());
        std::fs::remove_dir_all(&out).unwrap();
        std::fs::remove_dir_all(&victim).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn our_own_script_is_replaced_on_a_re_export() {
        let out = temp("script-ours");
        let mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        export(&mods, &out, "l1", "d").unwrap();
        stamp(&out, "zephyr-module");
        export(&mods, &out, "l2", "d").unwrap();
        let script = std::fs::read_to_string(out.join(CMAKE_FILE)).unwrap();
        assert!(script.contains("# Layer: l2"), "{script}");
        std::fs::remove_dir_all(&out).unwrap();
    }

    /// Nothing is written until everything is known to be writable: a path
    /// CMake cannot carry must not leave the trees behind.
    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn an_unquotable_out_directory_writes_no_tree_at_all() {
        let base = temp("halfexport");
        let out = base.join("o[ut");
        std::fs::create_dir_all(&out).unwrap();
        let mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        let e = export(&mods, &out, "l", "d").expect_err("bracket in the path");
        assert!(
            matches!(e, ZephyrModuleError::UnsafeCmakePath { .. }),
            "{e:?}"
        );
        assert!(
            std::fs::read_dir(&out).unwrap().next().is_none(),
            "something was written"
        );
        std::fs::remove_dir_all(&base).unwrap();
    }

    // -------------------------------------------- pip, re-checked at export

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn export_rechecks_pip_against_the_signed_opt_in() {
        let pip = |allow: Option<Vec<String>>| {
            vec![ModulePayload {
                name: "py".into(),
                bytes: pip_module(&["scripts/requirements.txt"]),
                allow_pip: allow,
            }]
        };
        // A layer signed before the deposit rule existed: no annotation.
        let e = prepare(&pip(None), None).expect_err("no opt-in");
        assert!(
            matches!(e, ZephyrModuleError::PipWithoutOptIn { .. }),
            "{e:?}"
        );
        let e = prepare(&pip(Some(strings(&["other.txt"]))), None).expect_err("wrong opt-in");
        assert!(
            matches!(e, ZephyrModuleError::PipOptInMismatch { .. }),
            "{e:?}"
        );
        let ok = prepare(&pip(Some(strings(&["scripts/requirements.txt"]))), None).unwrap();
        assert_eq!(ok.len(), 1);
        // An unselected pip module does not block selecting another.
        let two = vec![
            ModulePayload {
                name: "py".into(),
                bytes: pip_module(&["a.txt"]),
                allow_pip: None,
            },
            ModulePayload {
                name: "plain".into(),
                bytes: module("plain", &[]),
                allow_pip: None,
            },
        ];
        assert_eq!(prepare(&two, Some("plain")).unwrap().len(), 1);
        assert!(prepare(&two, Some("py")).is_err());
    }

    /// Two layers of a composition can carry payloads with the SAME payload name
    /// (different versions both export). Each module is judged on ITS OWN signed
    /// opt-in: B declares the same pip files as A and has none, and must not be
    /// passed on A's.
    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_pip_module_cannot_borrow_the_opt_in_of_a_payload_with_the_same_name() {
        let pip_named = |module_name: &str| {
            archive(&[(
                "zephyr/module.yml",
                &format!(
                    "name: {module_name}\npackage-managers:\n  pip:\n    requirement-files:\n      - r.txt\n"
                ),
            )])
        };
        let a = ModulePayload {
            name: "py".into(),
            bytes: pip_named("py_a"),
            allow_pip: Some(strings(&["r.txt"])),
        };
        let b = ModulePayload {
            name: "py".into(),
            bytes: pip_named("py_b"),
            allow_pip: None,
        };
        for order in [vec![a.clone(), b.clone()], vec![b.clone(), a.clone()]] {
            let e = prepare(&order, None).expect_err("B has no opt-in of its own");
            assert!(
                matches!(&e, ZephyrModuleError::PipWithoutOptIn { .. }),
                "{e:?}"
            );
        }
        // A alone is fine; so is B with an opt-in of its own.
        prepare(std::slice::from_ref(&a), None).unwrap();
        let b_ok = ModulePayload {
            allow_pip: Some(strings(&["r.txt"])),
            ..b.clone()
        };
        assert_eq!(prepare(&[a.clone(), b_ok], None).unwrap().len(), 2);
        // The SAME module name from two payloads is refused outright.
        let dup = ModulePayload {
            bytes: pip_named("py_a"),
            allow_pip: Some(strings(&["r.txt"])),
            ..b
        };
        let e = prepare(&[a, dup], None).expect_err("one module name twice");
        assert!(
            matches!(e, ZephyrModuleError::DuplicateModule { .. }),
            "{e:?}"
        );
    }

    // ------------------------------------------- YAML tags (the merge bypass)

    const MERGE_SPELLINGS: &[&str] = &[
        "name: gale\npackage-managers:\n  !!merge x: {pip: {requirement-files: [scripts/requirements.txt]}}\n",
        "name: gale\npackage-managers:\n  !<tag:yaml.org,2002:merge> x: {pip: {requirement-files: [r.txt]}}\n",
        "name: gale\npackage-managers:\n  !!merge \"zz\": {pip: {requirement-files: [r.txt]}}\n",
        "name: gale\n!!merge x: {package-managers: {pip: {requirement-files: [r.txt]}}}\n",
        "name: gale\n!!merge x: {build: {depends: [ghost]}}\n",
        "name: gale\nbuild:\n  !!merge x: {depends: [ghost]}\n",
        "!!merge x: {name: other}\n",
        "name: !!str gale\n",
        "name: gale\nbuild:\n  depends: !!seq [a]\n",
        "name: gale\nbuild: !local {cmake: .}\n",
    ];

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn any_yaml_tag_in_module_yml_is_refused_at_every_depth() {
        for yml in MERGE_SPELLINGS {
            let e = parse_module_yml("gale", yml).expect_err(yml);
            assert!(
                matches!(&e, ZephyrModuleError::TaggedModuleYml { payload, .. } if payload == "gale"),
                "{yml:?} -> {e:?}"
            );
            // At deposit, at the archive root and inside a wrapper directory.
            for (path, other) in [
                ("zephyr/module.yml", "CMakeLists.txt"),
                (
                    "wrapper-1.0/zephyr/module.yml",
                    "wrapper-1.0/CMakeLists.txt",
                ),
            ] {
                let a = archive(&[(path, yml), (other, "x")]);
                let e = check_deposit("gale", &a, None).expect_err(yml);
                assert!(
                    matches!(e, ZephyrModuleError::TaggedModuleYml { .. }),
                    "{path} {yml:?}: {e:?}"
                );
            }
        }
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_tag_refusal_names_the_tag() {
        let e = parse_module_yml("gale", MERGE_SPELLINGS[0]).unwrap_err();
        let msg = e.to_string();
        assert!(msg.contains("merge") && msg.contains("gale"), "{msg}");
        let e = parse_module_yml("gale", "name: !local gale\n").unwrap_err();
        assert!(e.to_string().contains("local"), "{e}");
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_bang_that_is_not_a_tag_is_not_refused() {
        for yml in [
            "name: \"a!!merge b\"\n",
            "name: 'x !!merge y'\nbuild:\n  depends: ['!z', \"!!w\"]\n",
            "# !!merge x: {pip: {}}\nname: gale  # !!str trailing\n",
            "name: gale\ndescription: |\n  !!merge x: {pip: {}}\n  text\nbuild:\n  cmake: .\n",
            "name: gale\ndescription: >\n  !<tag:yaml.org,2002:merge> folded\n",
            "name: gale!\n",
            "name: wow!!\n",
        ] {
            let y = parse_module_yml("gale", yml).unwrap_or_else(|e| panic!("{yml:?}: {e}"));
            assert_eq!(y.pip, None, "{yml:?}");
        }
        // A normal module.yml passes.
        let y =
            parse_module_yml("gale", "name: gale\nbuild:\n  cmake: .\n  depends: [a]\n").unwrap();
        assert_eq!(y.depends, strings(&["a"]));
    }

    /// The untagged merge key `<<` is understood by both parsers, so it must
    /// not hide pip either.
    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn the_plain_merge_key_cannot_hide_pip_either() {
        let y = parse_module_yml(
            "p",
            "x: &a {pip: {requirement-files: [r.txt]}}\npackage-managers:\n  <<: *a\n",
        );
        // Refusing it is as safe as seeing through it.
        if let Ok(y) = y {
            assert_eq!(y.pip, Some(strings(&["r.txt"])));
        }
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[cfg(unix)]
    #[test]
    fn export_refuses_a_symlinked_module_directory_even_if_stamped() {
        let out = temp("link");
        let elsewhere = temp("elsewhere");
        let mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        export(&mods, &out, "l", "d").unwrap();
        stamp(&out, "zephyr-module");
        std::fs::remove_dir_all(out.join("x")).unwrap();
        std::os::unix::fs::symlink(&elsewhere, out.join("x")).unwrap();
        let e = export(&mods, &out, "l", "d").expect_err("symlink at destination");
        assert!(
            matches!(e, ZephyrModuleError::NotWrittenByVarve { .. }),
            "{e:?}"
        );
        assert!(
            std::fs::read_dir(&elsewhere).unwrap().next().is_none(),
            "something was written through the link"
        );
        std::fs::remove_dir_all(&out).unwrap();
        std::fs::remove_dir_all(&elsewhere).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn export_refuses_a_module_dir_that_is_a_file() {
        let out = temp("file");
        std::fs::write(out.join("x"), "a file").unwrap();
        let mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        assert!(matches!(
            export(&mods, &out, "l", "d"),
            Err(ZephyrModuleError::NotWrittenByVarve { .. })
        ));
        assert_eq!(std::fs::read_to_string(out.join("x")).unwrap(), "a file");
        std::fs::remove_dir_all(&out).unwrap();
    }

    /// Second guard: files are created, never overwritten. Two members that
    /// land on one path (which a case-insensitive file system turns two
    /// differently-cased names into) must fail rather than let the later win.
    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn export_never_overwrites_a_file_it_wrote_a_moment_ago() {
        let mut mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        mods[0].tree.members.push(Member {
            path: "zephyr/module.yml".into(),
            body: MemberBody::File {
                mode: 0o644,
                bytes: b"name: overwritten\n".to_vec(),
            },
        });
        let out = temp("nooverwrite");
        let e = export(&mods, &out, "l", "d").expect_err("second write must fail");
        assert!(matches!(e, ZephyrModuleError::Io { .. }), "{e:?}");
        assert_eq!(
            std::fs::read_to_string(out.join("x/zephyr/module.yml")).unwrap(),
            "name: x\nbuild:\n  cmake: .\n",
            "the first write must be the one that stands"
        );
        std::fs::remove_dir_all(&out).unwrap();
    }

    // ------------------------------------------- case-insensitive collisions

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn members_that_differ_only_in_case_are_refused_at_open() {
        for twin in [
            "zephyr/Module.yml",
            "ZEPHYR/module.yml",
            "Zephyr/MODULE.YML",
        ] {
            let a = archive(&[
                ("zephyr/module.yml", "name: good\n"),
                (twin, "package-managers:\n  pip: {}\n"),
            ]);
            let e = open("p", &a).expect_err(twin);
            assert!(
                matches!(&e, ZephyrModuleError::DuplicateMember { .. }),
                "{twin}: {e:?}"
            );
            assert!(e.to_string().contains("case"), "{e}");
        }
        // Any member, not only module.yml: two files one file system merges.
        let a = archive(&[
            ("zephyr/module.yml", "name: x\n"),
            ("src/A.c", "1"),
            ("src/a.c", "2"),
        ]);
        assert!(matches!(
            open("p", &a),
            Err(ZephyrModuleError::DuplicateMember { .. })
        ));
        // Distinct names are still fine.
        let a = archive(&[
            ("zephyr/module.yml", "name: x\n"),
            ("src/a.c", "1"),
            ("src/b.c", "2"),
        ]);
        open("p", &a).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn a_second_module_description_beside_the_parsed_one_is_refused() {
        for twin in ["zephyr/module.yaml", "Zephyr/Module.yaml"] {
            let a = archive(&[("zephyr/module.yml", "name: x\n"), (twin, "name: y\n")]);
            let e = open("p", &a).expect_err(twin);
            assert!(
                matches!(&e, ZephyrModuleError::ModuleYmlCollision { member, .. } if member == twin),
                "{twin}: {e:?}"
            );
        }
        // Also inside a wrapper directory.
        let a = archive(&[
            ("w/zephyr/module.yml", "name: x\n"),
            ("w/zephyr/module.yaml", "name: y\n"),
        ]);
        assert!(matches!(
            open("p", &a),
            Err(ZephyrModuleError::ModuleYmlCollision { .. })
        ));
        // The parsed member itself is not its own collision.
        open("p", &archive(&[("zephyr/module.yml", "name: x\n")])).unwrap();
        // A file that merely sits beside it is fine.
        open(
            "p",
            &archive(&[("zephyr/module.yml", "name: x\n"), ("zephyr/Kconfig", "")]),
        )
        .unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn export_needs_an_absolute_directory_and_refuses_a_symlink_member() {
        let mods = prepare(&[("x".to_string(), module("x", &[]))], None).unwrap();
        let e = export(&mods, Path::new("relative/out"), "l", "d").expect_err("relative");
        assert!(matches!(e, ZephyrModuleError::Io { .. }));
        // `open` never yields a link, but the type allows one: refused anyway.
        let mut with_link = mods.clone();
        with_link[0].tree.members.push(Member {
            path: "link".into(),
            body: MemberBody::Symlink {
                target: "/etc".into(),
            },
        });
        let out = temp("symlink-member");
        let e = export(&with_link, &out, "l", "d").expect_err("link member");
        assert!(matches!(e, ZephyrModuleError::Symlink { .. }), "{e:?}");
        std::fs::remove_dir_all(&out).unwrap();
    }

    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn empty_directories_in_the_tree_are_kept() {
        let mut tar = tar::Builder::new(Vec::new());
        let mut h = tar::Header::new_gnu();
        h.set_size(8);
        h.set_mode(0o644);
        h.set_entry_type(tar::EntryType::Regular);
        tar.append_data(&mut h, "zephyr/module.yml", &b"name: e\n\n"[..8])
            .unwrap();
        let mut d = tar::Header::new_gnu();
        d.set_size(0);
        d.set_mode(0o755);
        d.set_entry_type(tar::EntryType::Directory);
        tar.append_data(&mut d, "boards/", &[][..]).unwrap();
        let out = temp("emptydir");
        let mods = prepare(&[("e".to_string(), tar.into_inner().unwrap())], None).unwrap();
        export(&mods, &out, "l", "d").unwrap();
        assert!(out.join("e/boards").is_dir());
        std::fs::remove_dir_all(&out).unwrap();
    }

    // ----------------------------------------------- the script, run by CMake

    /// Does the generated script do what it says when real CMake runs it?
    /// Three ways in: `include()`, `-C`, and `-C` after a user's own `-D`.
    // rivet: verifies REQ-ZMODEXPORT-001
    #[test]
    fn real_cmake_appends_both_modules_via_include_and_via_dash_c() {
        let Some(cmake) = which_cmake() else {
            eprintln!(
                "SKIPPED real_cmake_appends_both_modules_via_include_and_via_dash_c: \
                 `cmake` is not on PATH"
            );
            return;
        };
        let out = temp("cmake dir with space");
        let mods = prepare(
            &payloads(&[("gale", module("gale", &[])), ("hal", module("hal", &[]))]),
            None,
        )
        .unwrap();
        export(&mods, &out, "2026.11.0", "sha256:dd").unwrap();
        let script = out.join(CMAKE_FILE);
        let gale = out.join("gale");
        let hal = out.join("hal");

        let project = temp("project");
        std::fs::write(
            project.join("CMakeLists.txt"),
            format!(
                "cmake_minimum_required(VERSION 3.20)\nproject(probe NONE)\n\
                 if(PROBE_INCLUDE)\n  include(\"{}\")\nendif()\n\
                 message(STATUS \"ZEM=[${{ZEPHYR_EXTRA_MODULES}}]\")\n",
                script.display()
            ),
        )
        .unwrap();

        let run = |args: &[String]| -> String {
            let build = temp("build");
            let o = std::process::Command::new(&cmake)
                .args(["-S"])
                .arg(&project)
                .arg("-B")
                .arg(&build)
                .args(args)
                .output()
                .unwrap();
            let text = String::from_utf8_lossy(&o.stdout).into_owned()
                + &String::from_utf8_lossy(&o.stderr);
            assert!(o.status.success(), "cmake failed: {text}");
            let line = text
                .lines()
                .find(|l| l.contains("ZEM=["))
                .unwrap_or_else(|| panic!("no probe line: {text}"))
                .to_string();
            std::fs::remove_dir_all(&build).ok();
            line
        };

        // 1. include()
        let line = run(&["-DPROBE_INCLUDE=1".to_string()]);
        assert!(line.contains(&gale.display().to_string()), "{line}");
        assert!(line.contains(&hal.display().to_string()), "{line}");
        // 2. west's `-- -C script`
        let line = run(&["-C".to_string(), script.display().to_string()]);
        assert!(line.contains(&gale.display().to_string()), "-C: {line}");
        assert!(line.contains(&hal.display().to_string()), "-C: {line}");
        // 3. -C keeps what the user already passed with -D
        let line = run(&[
            "-DZEPHYR_EXTRA_MODULES=/users/own".to_string(),
            "-C".to_string(),
            script.display().to_string(),
        ]);
        assert!(
            line.contains("/users/own"),
            "-D then -C lost the user's: {line}"
        );
        assert!(line.contains(&gale.display().to_string()), "{line}");
        std::fs::remove_dir_all(&out).ok();
        std::fs::remove_dir_all(&project).ok();
    }

    fn which_cmake() -> Option<std::path::PathBuf> {
        let path = std::env::var_os("PATH")?;
        std::env::split_paths(&path)
            .map(|d| d.join("cmake"))
            .find(|p| p.is_file())
    }
}