zlayer-builder 0.12.4

Dockerfile parsing and buildah-based container image building
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
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
//! Buildah command generation and execution
//!
//! This module provides types and functions for converting Dockerfile instructions
//! into buildah commands and executing them.
//!
//! # Installation
//!
//! The [`BuildahInstaller`] type can be used to find existing buildah installations
//! or provide helpful installation instructions when buildah is not found.
//!
//! ```no_run
//! use zlayer_builder::buildah::{BuildahInstaller, BuildahExecutor};
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Ensure buildah is available
//! let installer = BuildahInstaller::new();
//! let installation = installer.ensure().await?;
//!
//! // Create executor using found installation
//! let executor = BuildahExecutor::with_path(installation.path);
//! # Ok(())
//! # }
//! ```

mod executor;
mod install;

pub use executor::*;
#[cfg(unix)]
pub use install::buildd as buildd_install;
#[cfg(unix)]
pub use install::buildd::{ensure_buildd_sidecar, InstallOutcome as SidecarInstallOutcome};
pub use install::{
    current_platform, install_instructions, is_platform_supported, BuildahInstallation,
    BuildahInstaller, InstallError,
};

use crate::backend::ImageOs;
use crate::dockerfile::{
    AddInstruction, CopyInstruction, EnvInstruction, ExposeInstruction, HealthcheckInstruction,
    Instruction, RunInstruction, RunNetwork, ShellOrExec,
};

use std::collections::HashMap;
use std::path::{Path, PathBuf};

/// Default shell used for `RUN <cmd>` (shell form) on Linux targets.
///
/// Matches the historical default used by Docker / buildah and keeps the
/// generated buildah command byte-identical to what we emitted before the
/// OS-aware translator landed.
const LINUX_DEFAULT_SHELL: &[&str] = &["/bin/sh", "-c"];

/// Default shell used for `RUN <cmd>` (shell form) on Windows targets.
///
/// Matches Docker's Windows default (`cmd /S /C`) used when no `SHELL`
/// instruction has overridden it.
const WINDOWS_DEFAULT_SHELL: &[&str] = &["cmd.exe", "/S", "/C"];

/// Return the default shell-form prefix for an OS when no `SHELL` instruction
/// has been seen.
fn default_shell_for(os: ImageOs) -> Vec<String> {
    let raw: &[&str] = match os {
        ImageOs::Linux => LINUX_DEFAULT_SHELL,
        ImageOs::Windows => WINDOWS_DEFAULT_SHELL,
    };
    raw.iter().map(|s| (*s).to_string()).collect()
}

/// A buildah command ready for execution
#[derive(Debug, Clone)]
pub struct BuildahCommand {
    /// The program to execute (typically "buildah")
    pub program: String,

    /// Command arguments
    pub args: Vec<String>,

    /// Optional environment variables for the command
    pub env: HashMap<String, String>,
}

impl BuildahCommand {
    /// Create a new buildah command
    #[must_use]
    pub fn new(subcommand: &str) -> Self {
        Self {
            program: "buildah".to_string(),
            args: vec![subcommand.to_string()],
            env: HashMap::new(),
        }
    }

    /// Add an argument
    #[must_use]
    pub fn arg(mut self, arg: impl Into<String>) -> Self {
        self.args.push(arg.into());
        self
    }

    /// Add multiple arguments
    #[must_use]
    pub fn args(mut self, args: impl IntoIterator<Item = impl Into<String>>) -> Self {
        self.args.extend(args.into_iter().map(Into::into));
        self
    }

    /// Add an optional argument (only added if value is Some)
    #[must_use]
    pub fn arg_opt(self, flag: &str, value: Option<impl Into<String>>) -> Self {
        if let Some(v) = value {
            self.arg(flag).arg(v)
        } else {
            self
        }
    }

    /// Add an environment variable for command execution
    #[must_use]
    pub fn env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.env.insert(key.into(), value.into());
        self
    }

    /// Convert to a command line string for display/logging
    #[must_use]
    pub fn to_command_string(&self) -> String {
        let mut parts = vec![self.program.clone()];
        parts.extend(self.args.iter().map(|a| {
            if a.contains(' ') || a.contains('"') {
                format!("\"{}\"", a.replace('"', "\\\""))
            } else {
                a.clone()
            }
        }));
        parts.join(" ")
    }

    // =========================================================================
    // Container Lifecycle Commands
    // =========================================================================

    /// Create a new working container from an image
    ///
    /// `buildah from <image>`
    #[must_use]
    pub fn from_image(image: &str) -> Self {
        Self::new("from").arg(image)
    }

    /// Create a new working container from an image with a specific name
    ///
    /// `buildah from --name <name> <image>`
    #[must_use]
    pub fn from_image_named(image: &str, name: &str) -> Self {
        Self::new("from").arg("--name").arg(name).arg(image)
    }

    /// Create a scratch container
    ///
    /// `buildah from scratch`
    #[must_use]
    pub fn from_scratch() -> Self {
        Self::new("from").arg("scratch")
    }

    /// Remove a working container
    ///
    /// `buildah rm <container>`
    #[must_use]
    pub fn rm(container: &str) -> Self {
        Self::new("rm").arg(container)
    }

    /// Commit a container to create an image
    ///
    /// `buildah commit <container> <image>`
    #[must_use]
    pub fn commit(container: &str, image_name: &str) -> Self {
        Self::new("commit").arg(container).arg(image_name)
    }

    /// Commit with additional options
    #[must_use]
    pub fn commit_with_opts(
        container: &str,
        image_name: &str,
        format: Option<&str>,
        squash: bool,
    ) -> Self {
        let mut cmd = Self::new("commit");

        if let Some(fmt) = format {
            cmd = cmd.arg("--format").arg(fmt);
        }

        if squash {
            cmd = cmd.arg("--squash");
        }

        cmd.arg(container).arg(image_name)
    }

    /// Tag an image with a new name
    ///
    /// `buildah tag <image> <new-name>`
    #[must_use]
    pub fn tag(image: &str, new_name: &str) -> Self {
        Self::new("tag").arg(image).arg(new_name)
    }

    /// Remove an image
    ///
    /// `buildah rmi <image>`
    #[must_use]
    pub fn rmi(image: &str) -> Self {
        Self::new("rmi").arg(image)
    }

    /// Pull an image from a registry into local storage.
    ///
    /// `buildah pull [--policy <policy>] <image>`
    ///
    /// `policy` controls when the registry is consulted: `"newer"` only pulls
    /// when the upstream is newer than the local copy, `"always"` forces a
    /// fresh pull, and `"never"` (or `"missing"`) is useful for offline builds.
    /// When `policy` is `None`, buildah's default policy applies.
    #[must_use]
    pub fn pull(image: &str, policy: Option<&str>) -> Self {
        let mut cmd = Self::new("pull");
        if let Some(p) = policy {
            cmd = cmd.arg("--policy").arg(p);
        }
        cmd.arg(image)
    }

    /// Push an image to a registry
    ///
    /// `buildah push <image>`
    #[must_use]
    pub fn push(image: &str) -> Self {
        Self::new("push").arg(image)
    }

    /// Push an image to a registry with options
    ///
    /// `buildah push [options] <image> [destination]`
    #[must_use]
    pub fn push_to(image: &str, destination: &str) -> Self {
        Self::new("push").arg(image).arg(destination)
    }

    /// Inspect an image or container
    ///
    /// `buildah inspect <name>`
    #[must_use]
    pub fn inspect(name: &str) -> Self {
        Self::new("inspect").arg(name)
    }

    /// Inspect an image or container with format
    ///
    /// `buildah inspect --format <format> <name>`
    #[must_use]
    pub fn inspect_format(name: &str, format: &str) -> Self {
        Self::new("inspect").arg("--format").arg(format).arg(name)
    }

    /// List images
    ///
    /// `buildah images`
    #[must_use]
    pub fn images() -> Self {
        Self::new("images")
    }

    /// List containers
    ///
    /// `buildah containers`
    #[must_use]
    pub fn containers() -> Self {
        Self::new("containers")
    }

    // =========================================================================
    // Run Commands
    // =========================================================================

    /// Run a command in the container (shell form) using the Linux default shell.
    ///
    /// `buildah run <container> -- /bin/sh -c "<command>"`
    ///
    /// For OS-aware translation (Windows targets, or honoring a `SHELL`
    /// override), prefer [`Self::run_shell_custom`] or the stateful
    /// [`DockerfileTranslator`].
    #[must_use]
    pub fn run_shell(container: &str, command: &str) -> Self {
        Self::run_shell_custom(container, LINUX_DEFAULT_SHELL, command)
    }

    /// Run a command in the container (shell form) using an explicit shell.
    ///
    /// `buildah run <container> -- <shell...> <command>`
    ///
    /// The `shell` slice is emitted verbatim before the command argument, e.g.
    /// `["cmd.exe", "/S", "/C"]` for Windows or `["/bin/bash", "-lc"]` for a
    /// bash-login override.
    #[must_use]
    pub fn run_shell_custom(
        container: &str,
        shell: impl IntoIterator<Item = impl AsRef<str>>,
        command: &str,
    ) -> Self {
        Self::run_shell_custom_with_net(container, shell, command, None)
    }

    /// Run a command in the container (shell form) using an explicit shell,
    /// optionally pinning the network mode.
    ///
    /// `buildah run [--net=<mode>] <container> -- <shell...> <command>`
    ///
    /// See [`Self::run_exec_with_net`] for the meaning of `net`.
    #[must_use]
    pub fn run_shell_custom_with_net(
        container: &str,
        shell: impl IntoIterator<Item = impl AsRef<str>>,
        command: &str,
        net: Option<RunNetwork>,
    ) -> Self {
        let mut cmd = Self::new("run");
        if let Some(mode) = net {
            match mode {
                RunNetwork::Host => cmd = cmd.arg("--net=host"),
                RunNetwork::None => cmd = cmd.arg("--net=none"),
                RunNetwork::Default => {}
            }
        }
        cmd = cmd.arg(container).arg("--");
        for s in shell {
            cmd = cmd.arg(s.as_ref().to_string());
        }
        cmd.arg(command)
    }

    /// Run a command in the container (shell form) using the OS default shell.
    ///
    /// Linux → `/bin/sh -c`, Windows → `cmd.exe /S /C`.
    #[must_use]
    pub fn run_shell_for_os(container: &str, command: &str, os: ImageOs) -> Self {
        let shell = default_shell_for(os);
        Self::run_shell_custom(container, &shell, command)
    }

    /// Run a command in the container (exec form)
    ///
    /// `buildah run <container> -- <args...>`
    #[must_use]
    pub fn run_exec(container: &str, args: &[String]) -> Self {
        Self::run_exec_with_net(container, args, None)
    }

    /// Run a command in the container (exec form), optionally pinning the
    /// network mode.
    ///
    /// `buildah run [--net=<mode>] <container> -- <args...>`
    ///
    /// The `--net=<mode>` flag MUST precede the container ID — buildah parses
    /// flags up to the first positional and then treats the rest as the
    /// command. When `net` is `None`, no `--net` flag is emitted and buildah
    /// uses its default rootless networking. Use `Some(RunNetwork::Host)`
    /// when the translator-level `host_network` override is on, so the
    /// emitted `buildah run` bypasses CNI/netavark just like the dedicated
    /// `RUN` instruction path does.
    #[must_use]
    pub fn run_exec_with_net(container: &str, args: &[String], net: Option<RunNetwork>) -> Self {
        let mut cmd = Self::new("run");
        if let Some(mode) = net {
            match mode {
                RunNetwork::Host => cmd = cmd.arg("--net=host"),
                RunNetwork::None => cmd = cmd.arg("--net=none"),
                RunNetwork::Default => {}
            }
        }
        cmd = cmd.arg(container).arg("--");
        for arg in args {
            cmd = cmd.arg(arg);
        }
        cmd
    }

    /// Run a command based on `ShellOrExec`
    #[must_use]
    pub fn run(container: &str, command: &ShellOrExec) -> Self {
        match command {
            ShellOrExec::Shell(s) => Self::run_shell(container, s),
            ShellOrExec::Exec(args) => Self::run_exec(container, args),
        }
    }

    /// Run a command with mount specifications from a `RunInstruction`.
    ///
    /// Buildah requires `--mount` arguments to appear BEFORE the container ID:
    /// `buildah run [--mount=...] <container> -- <command>`
    ///
    /// This method properly orders the arguments to ensure mounts are applied.
    ///
    /// Uses the Linux default shell (`/bin/sh -c`) for shell-form commands.
    /// For OS-aware translation use [`Self::run_with_mounts_shell`].
    #[must_use]
    pub fn run_with_mounts(container: &str, run: &RunInstruction) -> Self {
        Self::run_with_mounts_shell(container, run, LINUX_DEFAULT_SHELL)
    }

    /// Run a command with mount specifications, using an explicit shell for
    /// shell-form commands.
    ///
    /// Exec-form commands ignore `shell` and are emitted verbatim, matching
    /// Docker/Buildah semantics.
    #[must_use]
    pub fn run_with_mounts_shell(
        container: &str,
        run: &RunInstruction,
        shell: impl IntoIterator<Item = impl AsRef<str>>,
    ) -> Self {
        let mut cmd = Self::new("run");

        // Add --mount arguments BEFORE the container ID
        for mount in &run.mounts {
            cmd = cmd.arg(format!("--mount={}", mount.to_buildah_arg()));
        }

        // Add transient --env=K=V flags BEFORE the container ID. Sort by key
        // for deterministic ordering (HashMap iteration is non-deterministic).
        // These are scoped to this single `buildah run` invocation and are
        // intentionally NOT persisted into the image config.
        let mut env_keys: Vec<&String> = run.env.keys().collect();
        env_keys.sort();
        for key in env_keys {
            if let Some(value) = run.env.get(key) {
                cmd = cmd.arg(format!("--env={key}={value}"));
            }
        }

        // Add --net=<value> BEFORE the container ID when a network mode is set.
        // `RunNetwork::Default` is omitted (buildah's default is `private`),
        // matching Docker's BuildKit semantics where `--network` is only
        // emitted when the user opts out of the default. `--net` is the
        // canonical buildah spelling per the man page (both `--net` and
        // `--network` work, but `--net` is shorter and matches buildah's
        // own help output).
        if let Some(net) = run.network {
            match net {
                RunNetwork::Host => {
                    cmd = cmd.arg("--net=host");
                }
                RunNetwork::None => {
                    cmd = cmd.arg("--net=none");
                }
                RunNetwork::Default => {}
            }
        }

        // Now add container and the command
        cmd = cmd.arg(container).arg("--");

        match &run.command {
            ShellOrExec::Shell(s) => {
                for part in shell {
                    cmd = cmd.arg(part.as_ref().to_string());
                }
                cmd.arg(s)
            }
            ShellOrExec::Exec(args) => {
                for arg in args {
                    cmd = cmd.arg(arg);
                }
                cmd
            }
        }
    }

    // =========================================================================
    // Copy/Add Commands
    // =========================================================================

    /// Copy files into the container
    ///
    /// `buildah copy <container> <src...> <dest>`
    #[must_use]
    pub fn copy(container: &str, sources: &[String], dest: &str) -> Self {
        let mut cmd = Self::new("copy").arg(container);
        for src in sources {
            cmd = cmd.arg(src);
        }
        cmd.arg(dest)
    }

    /// Copy files from another container/image
    ///
    /// `buildah copy --from=<source> <container> <src...> <dest>`
    #[must_use]
    pub fn copy_from(container: &str, from: &str, sources: &[String], dest: &str) -> Self {
        let mut cmd = Self::new("copy").arg("--from").arg(from).arg(container);
        for src in sources {
            cmd = cmd.arg(src);
        }
        cmd.arg(dest)
    }

    /// Materialize a directory in the container rootfs without running a
    /// process inside the container.
    ///
    /// Emits `buildah copy <container> <empty_src>/. <dest>` where
    /// `empty_src` is an empty host directory. `buildah copy` creates
    /// `<dest>` (and its parents) in the rootfs as part of the copy, and
    /// because the source has no entries nothing is actually copied —
    /// the net effect is `mkdir -p <dest>` on the rootfs, but executed by
    /// buildah's filesystem code, not by a shell inside the container.
    ///
    /// This is the shell-free equivalent of
    /// `BuildahCommand::run_exec_with_net(ctr, &["mkdir","-p", dir], …)`,
    /// usable on distroless / scratch / any base image that lacks
    /// `mkdir` / `/bin/sh`. Callers MUST keep the host source directory
    /// alive (and empty) for the lifetime of this command's execution.
    #[must_use]
    pub fn copy_empty_dir(container: &str, empty_src: &Path, dest: &str) -> Self {
        // `<empty_src>/.` (note trailing `/.`) tells buildah to copy the
        // contents of the source, not the directory entry itself. With no
        // entries this is a no-op materialization that still creates the
        // destination path in the rootfs.
        let mut src = empty_src.to_string_lossy().into_owned();
        if !src.ends_with('/') {
            src.push('/');
        }
        src.push('.');
        Self::new("copy").arg(container).arg(src).arg(dest)
    }

    /// Copy with all options from `CopyInstruction`
    #[must_use]
    pub fn copy_instruction(container: &str, copy: &CopyInstruction) -> Self {
        let mut cmd = Self::new("copy");

        if let Some(ref from) = copy.from {
            cmd = cmd.arg("--from").arg(from);
        }

        if let Some(ref chown) = copy.chown {
            cmd = cmd.arg("--chown").arg(chown);
        }

        if let Some(ref chmod) = copy.chmod {
            cmd = cmd.arg("--chmod").arg(chmod);
        }

        cmd = cmd.arg(container);

        for src in &copy.sources {
            cmd = cmd.arg(src);
        }

        cmd.arg(&copy.destination)
    }

    /// Add files (like copy but with URL support and extraction)
    #[must_use]
    pub fn add(container: &str, sources: &[String], dest: &str) -> Self {
        let mut cmd = Self::new("add").arg(container);
        for src in sources {
            cmd = cmd.arg(src);
        }
        cmd.arg(dest)
    }

    /// Add with all options from `AddInstruction`
    #[must_use]
    pub fn add_instruction(container: &str, add: &AddInstruction) -> Self {
        let mut cmd = Self::new("add");

        if let Some(ref chown) = add.chown {
            cmd = cmd.arg("--chown").arg(chown);
        }

        if let Some(ref chmod) = add.chmod {
            cmd = cmd.arg("--chmod").arg(chmod);
        }

        cmd = cmd.arg(container);

        for src in &add.sources {
            cmd = cmd.arg(src);
        }

        cmd.arg(&add.destination)
    }

    // =========================================================================
    // Config Commands
    // =========================================================================

    /// Set an environment variable
    ///
    /// `buildah config --env KEY=VALUE <container>`
    #[must_use]
    pub fn config_env(container: &str, key: &str, value: &str) -> Self {
        Self::new("config")
            .arg("--env")
            .arg(format!("{key}={value}"))
            .arg(container)
    }

    /// Set multiple environment variables
    #[must_use]
    pub fn config_envs(container: &str, env: &EnvInstruction) -> Vec<Self> {
        env.vars
            .iter()
            .map(|(k, v)| Self::config_env(container, k, v))
            .collect()
    }

    /// Set the working directory
    ///
    /// `buildah config --workingdir <dir> <container>`
    #[must_use]
    pub fn config_workdir(container: &str, dir: &str) -> Self {
        Self::new("config")
            .arg("--workingdir")
            .arg(dir)
            .arg(container)
    }

    /// Expose a port
    ///
    /// `buildah config --port <port>/<proto> <container>`
    #[must_use]
    pub fn config_expose(container: &str, expose: &ExposeInstruction) -> Self {
        let port_spec = format!(
            "{}/{}",
            expose.port,
            match expose.protocol {
                crate::dockerfile::ExposeProtocol::Tcp => "tcp",
                crate::dockerfile::ExposeProtocol::Udp => "udp",
            }
        );
        Self::new("config")
            .arg("--port")
            .arg(port_spec)
            .arg(container)
    }

    /// Set the entrypoint (shell form)
    ///
    /// `buildah config --entrypoint '<command>' <container>`
    #[must_use]
    pub fn config_entrypoint_shell(container: &str, command: &str) -> Self {
        Self::new("config")
            .arg("--entrypoint")
            .arg(format!(
                "[\"/bin/sh\", \"-c\", \"{}\"]",
                escape_json_string(command)
            ))
            .arg(container)
    }

    /// Set the entrypoint (exec form)
    ///
    /// `buildah config --entrypoint '["exe", "arg1"]' <container>`
    #[must_use]
    pub fn config_entrypoint_exec(container: &str, args: &[String]) -> Self {
        let json_array = format!(
            "[{}]",
            args.iter()
                .map(|a| format!("\"{}\"", escape_json_string(a)))
                .collect::<Vec<_>>()
                .join(", ")
        );
        Self::new("config")
            .arg("--entrypoint")
            .arg(json_array)
            .arg(container)
    }

    /// Set the entrypoint based on `ShellOrExec`
    #[must_use]
    pub fn config_entrypoint(container: &str, command: &ShellOrExec) -> Self {
        match command {
            ShellOrExec::Shell(s) => Self::config_entrypoint_shell(container, s),
            ShellOrExec::Exec(args) => Self::config_entrypoint_exec(container, args),
        }
    }

    /// Set the default command (shell form)
    #[must_use]
    pub fn config_cmd_shell(container: &str, command: &str) -> Self {
        Self::new("config")
            .arg("--cmd")
            .arg(format!("/bin/sh -c \"{}\"", escape_json_string(command)))
            .arg(container)
    }

    /// Set the default command (exec form)
    #[must_use]
    pub fn config_cmd_exec(container: &str, args: &[String]) -> Self {
        let json_array = format!(
            "[{}]",
            args.iter()
                .map(|a| format!("\"{}\"", escape_json_string(a)))
                .collect::<Vec<_>>()
                .join(", ")
        );
        Self::new("config")
            .arg("--cmd")
            .arg(json_array)
            .arg(container)
    }

    /// Set the default command based on `ShellOrExec`
    #[must_use]
    pub fn config_cmd(container: &str, command: &ShellOrExec) -> Self {
        match command {
            ShellOrExec::Shell(s) => Self::config_cmd_shell(container, s),
            ShellOrExec::Exec(args) => Self::config_cmd_exec(container, args),
        }
    }

    /// Set the user
    ///
    /// `buildah config --user <user> <container>`
    #[must_use]
    pub fn config_user(container: &str, user: &str) -> Self {
        Self::new("config").arg("--user").arg(user).arg(container)
    }

    /// Set a label
    ///
    /// `buildah config --label KEY=VALUE <container>`
    #[must_use]
    pub fn config_label(container: &str, key: &str, value: &str) -> Self {
        Self::new("config")
            .arg("--label")
            .arg(format!("{key}={value}"))
            .arg(container)
    }

    /// Set multiple labels
    #[must_use]
    pub fn config_labels(container: &str, labels: &HashMap<String, String>) -> Vec<Self> {
        labels
            .iter()
            .map(|(k, v)| Self::config_label(container, k, v))
            .collect()
    }

    /// Set volumes
    ///
    /// `buildah config --volume <path> <container>`
    #[must_use]
    pub fn config_volume(container: &str, path: &str) -> Self {
        Self::new("config").arg("--volume").arg(path).arg(container)
    }

    /// Set the stop signal
    ///
    /// `buildah config --stop-signal <signal> <container>`
    #[must_use]
    pub fn config_stopsignal(container: &str, signal: &str) -> Self {
        Self::new("config")
            .arg("--stop-signal")
            .arg(signal)
            .arg(container)
    }

    /// Set the shell
    ///
    /// `buildah config --shell '["shell", "args"]' <container>`
    #[must_use]
    pub fn config_shell(container: &str, shell: &[String]) -> Self {
        let json_array = format!(
            "[{}]",
            shell
                .iter()
                .map(|a| format!("\"{}\"", escape_json_string(a)))
                .collect::<Vec<_>>()
                .join(", ")
        );
        Self::new("config")
            .arg("--shell")
            .arg(json_array)
            .arg(container)
    }

    /// Set healthcheck
    #[must_use]
    pub fn config_healthcheck(container: &str, healthcheck: &HealthcheckInstruction) -> Self {
        match healthcheck {
            HealthcheckInstruction::None => Self::new("config")
                .arg("--healthcheck")
                .arg("NONE")
                .arg(container),
            HealthcheckInstruction::Check {
                command,
                interval,
                timeout,
                start_period,
                retries,
                ..
            } => {
                let mut cmd = Self::new("config");

                let cmd_str = match command {
                    ShellOrExec::Shell(s) => format!("CMD {s}"),
                    ShellOrExec::Exec(args) => {
                        format!(
                            "CMD [{}]",
                            args.iter()
                                .map(|a| format!("\"{}\"", escape_json_string(a)))
                                .collect::<Vec<_>>()
                                .join(", ")
                        )
                    }
                };

                cmd = cmd.arg("--healthcheck").arg(cmd_str);

                if let Some(i) = interval {
                    cmd = cmd
                        .arg("--healthcheck-interval")
                        .arg(format!("{}s", i.as_secs()));
                }

                if let Some(t) = timeout {
                    cmd = cmd
                        .arg("--healthcheck-timeout")
                        .arg(format!("{}s", t.as_secs()));
                }

                if let Some(sp) = start_period {
                    cmd = cmd
                        .arg("--healthcheck-start-period")
                        .arg(format!("{}s", sp.as_secs()));
                }

                if let Some(r) = retries {
                    cmd = cmd.arg("--healthcheck-retries").arg(r.to_string());
                }

                cmd.arg(container)
            }
        }
    }

    // =========================================================================
    // Manifest Commands
    // =========================================================================

    /// Create a new manifest list.
    ///
    /// `buildah manifest create <name>`
    #[must_use]
    pub fn manifest_create(name: &str) -> Self {
        Self::new("manifest").arg("create").arg(name)
    }

    /// Add an image to a manifest list.
    ///
    /// `buildah manifest add <list> <image>`
    #[must_use]
    pub fn manifest_add(list: &str, image: &str) -> Self {
        Self::new("manifest").arg("add").arg(list).arg(image)
    }

    /// Push a manifest list and all referenced images.
    ///
    /// `buildah manifest push --all <list> <destination>`
    #[must_use]
    pub fn manifest_push(list: &str, destination: &str) -> Self {
        Self::new("manifest")
            .arg("push")
            .arg("--all")
            .arg(list)
            .arg(destination)
    }

    /// Remove a manifest list.
    ///
    /// `buildah manifest rm <list>`
    #[must_use]
    pub fn manifest_rm(list: &str) -> Self {
        Self::new("manifest").arg("rm").arg(list)
    }

    // =========================================================================
    // Convert Instruction to Commands
    // =========================================================================

    /// Convert a Dockerfile instruction to buildah command(s) using the Linux
    /// default shell (`/bin/sh -c`) and POSIX `mkdir -p` semantics.
    ///
    /// This is a convenience wrapper around [`DockerfileTranslator`] with
    /// `target_os = ImageOs::Linux` and no `SHELL` override. It preserves the
    /// historical byte-for-byte behavior for every call site that existed
    /// before OS-aware translation landed in Phase L-3.
    ///
    /// For Windows targets or when a Dockerfile-level `SHELL` instruction
    /// needs to be honored across subsequent `RUN`s, construct a
    /// [`DockerfileTranslator`] explicitly and call
    /// [`DockerfileTranslator::translate`].
    ///
    /// Some instructions map to multiple buildah commands (e.g., multiple
    /// ENV vars, or WORKDIR emitting both `mkdir` and `config --workingdir`).
    #[must_use]
    pub fn from_instruction(container: &str, instruction: &Instruction) -> Vec<Self> {
        DockerfileTranslator::new(ImageOs::Linux).translate(container, instruction)
    }
}

/// Stateful translator from [`Instruction`] to [`BuildahCommand`] sequences.
///
/// Tracks the target OS and the most recent `SHELL` instruction so that
/// shell-form `RUN` / `CMD` / `ENTRYPOINT` use the correct shell for the
/// target platform:
///
/// - **Linux, no SHELL override** — `RUN cmd` → `buildah run -- /bin/sh -c "cmd"`
/// - **Windows, no SHELL override** — `RUN cmd` → `buildah run -- cmd.exe /S /C "cmd"`
/// - **Any OS with `SHELL ["pwsh", "-Command"]`** — subsequent `RUN cmd` uses
///   `buildah run -- pwsh -Command "cmd"`
///
/// The translator is stateful because Dockerfile `SHELL` instructions persist
/// across subsequent `RUN`/`CMD`/`ENTRYPOINT` translations until another
/// `SHELL` replaces them. Callers that translate a multi-instruction stage
/// should reuse a single translator instance across the full instruction
/// stream.
///
/// This translator is designed to be shared between the buildah backend and
/// the Phase L-4 HCS (Windows host compute service) backend, so neither needs
/// to re-implement the shell-form / workdir branching.
#[derive(Debug, Clone)]
pub struct DockerfileTranslator {
    target_os: ImageOs,
    /// Most recent `SHELL` instruction override, if any. When `None` the
    /// translator falls back to [`default_shell_for`] for the target OS.
    shell_override: Option<Vec<String>>,
    /// When `true`, every emitted `buildah run` for a `RUN` instruction will
    /// carry `--net=host` regardless of any per-instruction `network` value.
    /// Mirrors Docker's `docker build --network=host` flag and bypasses
    /// buildah's CNI/netavark plumbing entirely (the container shares the
    /// host's network namespace).
    host_network: bool,
    /// Path to an empty host directory the translator can point `buildah
    /// copy` at to materialize a `WORKDIR` directory in the container
    /// rootfs without running a process inside the container. Required for
    /// images whose base lacks a shell (`gcr.io/distroless/*`, `scratch`).
    /// When `None`, [`Self::translate_workdir`] falls back to the legacy
    /// `buildah run -- mkdir -p <dir>` path, which only works on bases
    /// that ship `mkdir`. Production callers (the `BuildahBackend`) MUST
    /// set this; tests and doc snippets can leave it unset.
    empty_src_dir: Option<PathBuf>,
}

impl DockerfileTranslator {
    /// Create a new translator for a given target OS, with no `SHELL` override.
    #[must_use]
    pub fn new(target_os: ImageOs) -> Self {
        Self {
            target_os,
            shell_override: None,
            host_network: false,
            empty_src_dir: None,
        }
    }

    /// Point `WORKDIR` translation at an empty host directory used as the
    /// source for `buildah copy`, which materializes the workdir in the
    /// container rootfs without executing a process inside the container.
    ///
    /// Without this, `WORKDIR` falls back to `buildah run -- mkdir -p <dir>`,
    /// which fails on distroless / scratch images that lack a shell.
    /// Production callers (the `BuildahBackend`'s build loop) should create
    /// a `TempDir` for the lifetime of the build and pass its path here.
    /// The translator does NOT own the directory's lifecycle — callers
    /// must keep it alive (and empty) until every translated WORKDIR
    /// `buildah copy` command has finished executing.
    #[must_use]
    pub fn with_empty_src_dir(mut self, dir: PathBuf) -> Self {
        self.empty_src_dir = Some(dir);
        self
    }

    /// Force every translated `RUN` instruction to use host networking.
    ///
    /// When `on` is `true`, the translator overrides any per-instruction
    /// `network` value (including `None`) with [`RunNetwork::Host`] before
    /// emitting the buildah command. This mirrors the effect of Docker's
    /// `docker build --network=host` flag and bypasses buildah's CNI /
    /// netavark plumbing entirely. When `on` is `false` (the default),
    /// per-instruction `network` values are passed through unchanged.
    #[must_use]
    pub fn with_host_network(mut self, on: bool) -> Self {
        self.host_network = on;
        self
    }

    /// Return the target OS this translator emits commands for.
    #[must_use]
    pub fn target_os(&self) -> ImageOs {
        self.target_os
    }

    /// Return the current shell-form prefix: the `SHELL` override if one was
    /// applied, else the OS default (`/bin/sh -c` on Linux, `cmd.exe /S /C` on
    /// Windows).
    #[must_use]
    pub fn active_shell(&self) -> Vec<String> {
        match &self.shell_override {
            Some(s) => s.clone(),
            None => default_shell_for(self.target_os),
        }
    }

    /// Replace the translator's `SHELL` override, matching the effect of a
    /// Dockerfile `SHELL ["…"]` instruction on subsequent RUN/CMD/ENTRYPOINT
    /// shell-form commands.
    pub fn set_shell_override(&mut self, shell: Vec<String>) {
        self.shell_override = Some(shell);
    }

    /// Translate a single instruction into zero or more [`BuildahCommand`]s.
    ///
    /// Stateful: `SHELL` instructions update the translator's shell override,
    /// so subsequent `RUN` / `CMD` / `ENTRYPOINT` shell-form translations pick
    /// up the new shell. `WORKDIR` emits an OS-appropriate pre-mkdir followed
    /// by `buildah config --workingdir`.
    #[allow(clippy::too_many_lines)]
    pub fn translate(&mut self, container: &str, instruction: &Instruction) -> Vec<BuildahCommand> {
        match instruction {
            Instruction::Run(run) => {
                let shell = self.active_shell();
                // Apply the translator-level host_network override: when set,
                // every emitted RUN gets `--net=host` regardless of any
                // per-instruction network value. We clone only when we
                // actually need to mutate so the no-override path stays
                // allocation-free.
                let effective_run: std::borrow::Cow<'_, RunInstruction> = if self.host_network {
                    let mut owned = run.clone();
                    owned.network = Some(RunNetwork::Host);
                    std::borrow::Cow::Owned(owned)
                } else {
                    std::borrow::Cow::Borrowed(run)
                };
                let run_ref: &RunInstruction = &effective_run;

                // Route through run_with_mounts_shell whenever mounts, env,
                // or a network mode are present, since the simple factories
                // don't emit those pre-container flags.
                let needs_pre_container_flags = !run_ref.mounts.is_empty()
                    || !run_ref.env.is_empty()
                    || run_ref.network.is_some();

                if needs_pre_container_flags {
                    vec![BuildahCommand::run_with_mounts_shell(
                        container, run_ref, &shell,
                    )]
                } else {
                    match &run_ref.command {
                        ShellOrExec::Shell(s) => {
                            vec![BuildahCommand::run_shell_custom(container, &shell, s)]
                        }
                        ShellOrExec::Exec(args) => vec![BuildahCommand::run_exec(container, args)],
                    }
                }
            }

            Instruction::Copy(copy) => {
                vec![BuildahCommand::copy_instruction(container, copy)]
            }

            Instruction::Add(add) => {
                vec![BuildahCommand::add_instruction(container, add)]
            }

            Instruction::Env(env) => BuildahCommand::config_envs(container, env),

            Instruction::Workdir(dir) => self.translate_workdir(container, dir),

            Instruction::Expose(expose) => {
                vec![BuildahCommand::config_expose(container, expose)]
            }

            Instruction::Label(labels) => BuildahCommand::config_labels(container, labels),

            Instruction::User(user) => {
                vec![BuildahCommand::config_user(container, user)]
            }

            Instruction::Entrypoint(cmd) => {
                vec![BuildahCommand::config_entrypoint(container, cmd)]
            }

            Instruction::Cmd(cmd) => {
                vec![BuildahCommand::config_cmd(container, cmd)]
            }

            Instruction::Volume(paths) => paths
                .iter()
                .map(|p| BuildahCommand::config_volume(container, p))
                .collect(),

            Instruction::Shell(shell) => {
                // SHELL instruction: update the translator's shell override
                // AND emit the metadata config --shell so committed images
                // record the user-declared shell. Both matter: the override
                // changes how we translate subsequent RUN/CMD/ENTRYPOINT
                // shell-form instructions in THIS build; the metadata is what
                // tools like `docker inspect` show on the resulting image.
                self.set_shell_override(shell.clone());
                vec![BuildahCommand::config_shell(container, shell)]
            }

            Instruction::Arg(_) => {
                // ARG is handled during variable expansion, not as a buildah command
                vec![]
            }

            Instruction::Stopsignal(signal) => {
                vec![BuildahCommand::config_stopsignal(container, signal)]
            }

            Instruction::Healthcheck(hc) => {
                vec![BuildahCommand::config_healthcheck(container, hc)]
            }

            Instruction::Onbuild(_) => {
                // ONBUILD would need special handling
                tracing::warn!("ONBUILD instruction not supported in buildah conversion");
                vec![]
            }
        }
    }

    /// Emit the commands needed to realise a `WORKDIR <dir>` instruction for
    /// the target OS.
    ///
    /// # Linux
    ///
    /// Emits `mkdir -p <dir>` followed by `buildah config --workingdir`. This
    /// matches Docker's WORKDIR semantics: the directory must exist in the
    /// rootfs before a process can `chdir()` into it, and `buildah config
    /// --workingdir` alone is metadata-only.
    ///
    /// # Windows
    ///
    /// Emits `cmd /S /C "if not exist <dir> mkdir <dir>"` before the
    /// metadata write. Windows `mkdir` (unlike `mkdir -p`) errors out when the
    /// directory exists, so we guard with `if not exist` to stay idempotent
    /// across repeated WORKDIR instructions in the same Dockerfile.
    fn translate_workdir(&self, container: &str, dir: &str) -> Vec<BuildahCommand> {
        // Mirror `Instruction::Run`: when the translator was constructed with
        // `with_host_network(true)`, every emitted `buildah run` MUST carry
        // `--net=host` so the build bypasses buildah's rootless CNI/netavark
        // plumbing entirely. Without this, a WORKDIR — which runs `mkdir -p`
        // through `buildah run` — gets routed through netavark even though
        // the user opted out, and dies on the first instruction of the first
        // stage when the host's netavark config is broken.
        let net = self.host_network.then_some(RunNetwork::Host);
        match self.target_os {
            ImageOs::Linux => {
                // Prefer the shell-free `buildah copy` path when the caller
                // supplied an empty source directory. `buildah copy <ctr>
                // <empty>/. <dir>` materializes `<dir>` in the rootfs
                // without running anything inside the container, so this
                // works on distroless / scratch / any base lacking
                // `/bin/sh` / `mkdir`. Falls back to the legacy
                // `buildah run -- mkdir -p` path when no source dir is
                // configured (tests, docs, callers that haven't migrated).
                if let Some(empty_src) = self.empty_src_dir.as_deref() {
                    vec![
                        BuildahCommand::copy_empty_dir(container, empty_src, dir),
                        BuildahCommand::config_workdir(container, dir),
                    ]
                } else {
                    vec![
                        BuildahCommand::run_exec_with_net(
                            container,
                            &["mkdir".to_string(), "-p".to_string(), dir.to_string()],
                            net,
                        ),
                        BuildahCommand::config_workdir(container, dir),
                    ]
                }
            }
            ImageOs::Windows => {
                // Quote the path so paths with spaces (e.g. `C:\Program Files\app`)
                // survive cmd.exe parsing. `cmd /S /C` strips the outer quotes
                // before executing, so double-quote the full command and escape
                // any inner quotes.
                let guarded = format!(r#"if not exist "{dir}" mkdir "{dir}""#);
                vec![
                    BuildahCommand::run_shell_custom_with_net(
                        container,
                        WINDOWS_DEFAULT_SHELL,
                        &guarded,
                        net,
                    ),
                    BuildahCommand::config_workdir(container, dir),
                ]
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Linux-package-manager → Chocolatey translation helpers
//
// These were originally housed in `crate::windows_builder` and are used to
// rewrite Linux package-manager invocations (`apt-get install`, `apk add`,
// `yum/dnf install`) inside `RUN` instructions into a Chocolatey
// (`choco install -y …`) equivalent when the target OS is Windows. Moving
// them here lets both the production `HcsBackend` and the in-process
// `WindowsBuilder` test harness flow through one translator instead of
// duplicating the logic.
//
// The free helpers below are kept `pub(crate)` so the existing
// `windows_builder` test module (and any other in-crate caller) can keep
// exercising them directly without going through `DockerfileTranslator`.
// ---------------------------------------------------------------------------

/// Which Linux package manager an install sub-command was issued against.
//
// The whole apt→choco helper surface is gated on `windows || test` so
// non-Windows production builds don't warn about dead code — every call
// site lives in either the Windows-only `windows_builder` production
// path or a `#[cfg(test)]` unit test.
#[cfg(any(target_os = "windows", test))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DetectedPmKind {
    /// `apt-get install -y …` or `apt install -y …`.
    Apt,
    /// `apk add [--no-cache] …`.
    Apk,
    /// `yum install -y …` or `dnf install -y …`.
    YumOrDnf,
}

/// One sub-command parsed out of a shell-form RUN string.
#[cfg(any(target_os = "windows", test))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum ShellSubcommand {
    /// The literal text of a non-install sub-command, kept verbatim.
    Verbatim(String),
    /// The literal text of an `apt-get update` / `apk update` /
    /// `dnf check-update` style sync command. Surfaced as a distinct
    /// variant so we can elide it (no Chocolatey equivalent) instead of
    /// passing it through verbatim and breaking the shell.
    PackageManagerSync,
    /// A detected install invocation: kind + the package list.
    Install {
        kind: DetectedPmKind,
        packages: Vec<String>,
    },
}

/// Detect whether a single shell sub-command is an `apt-get install`,
/// `apk add`, `yum install`, or `dnf install` invocation. Returns
/// `Some((kind, packages))` if so. Flag-only arguments (starting with
/// `-`) are stripped; bare positional args are treated as package names.
#[cfg(any(target_os = "windows", test))]
pub(crate) fn detect_install_in_subcommand(
    subcommand: &str,
) -> Option<(DetectedPmKind, Vec<String>)> {
    let tokens: Vec<&str> = subcommand.split_whitespace().collect();
    if tokens.is_empty() {
        return None;
    }
    // Drop `sudo` if present so `sudo apt-get install -y curl` is
    // recognised the same as the bareword form.
    let (kind, after_verb_idx) = match tokens[0] {
        "sudo" if tokens.len() >= 2 => detect_pm_verb(&tokens[1..]).map(|(k, n)| (k, n + 1))?,
        _ => detect_pm_verb(&tokens)?,
    };
    let args = &tokens[after_verb_idx..];
    let mut packages = Vec::new();
    for arg in args {
        if arg.starts_with('-') {
            continue;
        }
        packages.push((*arg).to_string());
    }
    if packages.is_empty() {
        return None;
    }
    Some((kind, packages))
}

/// Recognise the `<pm> <verb>` prefix of a sub-command. Returns
/// `(kind, tokens_consumed)` on success — the caller then walks
/// `tokens[tokens_consumed..]` for the package list.
#[cfg(any(target_os = "windows", test))]
fn detect_pm_verb(tokens: &[&str]) -> Option<(DetectedPmKind, usize)> {
    match (tokens.first().copied(), tokens.get(1).copied()) {
        (Some("apt-get" | "apt"), Some("install")) => Some((DetectedPmKind::Apt, 2)),
        (Some("apk"), Some("add")) => Some((DetectedPmKind::Apk, 2)),
        (Some("yum" | "dnf"), Some("install")) => Some((DetectedPmKind::YumOrDnf, 2)),
        _ => None,
    }
}

/// Recognise package-manager-sync invocations (`apt-get update`,
/// `apk update`, `dnf check-update`, etc.) so we can elide them in the
/// rewritten command — Chocolatey resolves package metadata on every
/// install and has no separate sync step.
#[cfg(any(target_os = "windows", test))]
pub(crate) fn is_package_manager_sync(subcommand: &str) -> bool {
    let tokens: Vec<&str> = subcommand.split_whitespace().collect();
    let stripped: &[&str] = if tokens.first().copied() == Some("sudo") {
        &tokens[1..]
    } else {
        &tokens
    };
    matches!(
        (stripped.first().copied(), stripped.get(1).copied()),
        (Some("apt-get" | "apt" | "apk"), Some("update"))
            | (
                Some("yum" | "dnf"),
                Some("check-update" | "update" | "makecache")
            )
    )
}

/// Split a shell-form RUN command on `&&` and `;` boundaries, preserving
/// each sub-command's text. Quoted regions are NOT honoured (Docker
/// shell-form is itself a string passed to `cmd /c` which doesn't
/// preserve nested shell quoting either; matching that lenient
/// behaviour avoids a regex dep and keeps the implementation simple).
#[cfg(any(target_os = "windows", test))]
pub(crate) fn split_shell_subcommands(raw: &str) -> Vec<String> {
    let mut out = Vec::new();
    let mut current = String::new();
    let mut chars = raw.chars().peekable();
    while let Some(c) = chars.next() {
        match c {
            '&' if chars.peek() == Some(&'&') => {
                chars.next();
                if !current.trim().is_empty() {
                    out.push(current.trim().to_string());
                }
                current.clear();
            }
            ';' => {
                if !current.trim().is_empty() {
                    out.push(current.trim().to_string());
                }
                current.clear();
            }
            other => current.push(other),
        }
    }
    if !current.trim().is_empty() {
        out.push(current.trim().to_string());
    }
    out
}

/// Re-join a list of [`ShellSubcommand`]s back into a single
/// `cmd /c`-compatible string, eliding sync sub-commands and rewriting
/// install sub-commands as `choco install -y …`.
#[cfg(any(target_os = "windows", test))]
pub(crate) fn rejoin_subcommands(parts: &[ShellSubcommand]) -> String {
    let mut emitted: Vec<String> = Vec::new();
    for part in parts {
        match part {
            ShellSubcommand::Verbatim(s) => emitted.push(s.clone()),
            ShellSubcommand::PackageManagerSync => {
                // Eliding: no equivalent in Chocolatey.
            }
            ShellSubcommand::Install { packages, .. } => {
                if packages.is_empty() {
                    continue;
                }
                let mut joined = String::from("choco install -y");
                for pkg in packages {
                    joined.push(' ');
                    joined.push_str(pkg);
                }
                emitted.push(joined);
            }
        }
    }
    emitted.join(" && ")
}

/// Wrap a shell command body in `cmd /c "…"` so HCS's `CreateProcess`
/// invokes the Windows command interpreter. Embedded double quotes are
/// backslash-escaped per the NT `CommandLineToArgvW` convention. An
/// empty body still produces a well-formed (no-op) command.
#[cfg(any(target_os = "windows", test))]
pub(crate) fn wrap_in_cmd(body: &str) -> String {
    if body.is_empty() {
        return "cmd /c \"\"".to_string();
    }
    let escaped = body.replace('"', "\\\"");
    format!("cmd /c \"{escaped}\"")
}

/// Return `true` if `linux_pkg` is the Linux name of the language toolchain
/// indicated by `toolchain_language`. Used to drop packages that are
/// already provisioned directly into the rootfs via
/// `crate::windows_toolchain`, so we don't re-install (a possibly
/// conflicting) Chocolatey copy on top.
///
/// Match is case-insensitive on the toolchain language and exact on the
/// package name (lower-cased). The mapping mirrors what users typically
/// write in Linux Dockerfiles for each language:
///
/// | toolchain language | matching Linux package names      |
/// |--------------------|-----------------------------------|
/// | `go`               | `golang`, `go`                    |
/// | `node`             | `nodejs`, `node`                  |
/// | `python`           | `python3`, `python`               |
/// | `rust`             | `rust`, `rustc`, `cargo`          |
/// | `deno`             | `deno`                            |
/// | `bun`              | `bun`                             |
#[cfg(any(target_os = "windows", test))]
fn package_matches_toolchain(linux_pkg: &str, toolchain_language: &str) -> bool {
    let pkg = linux_pkg.to_ascii_lowercase();
    match toolchain_language.to_ascii_lowercase().as_str() {
        "go" => matches!(pkg.as_str(), "golang" | "go"),
        "node" => matches!(pkg.as_str(), "nodejs" | "node"),
        "python" => matches!(pkg.as_str(), "python3" | "python"),
        "rust" => matches!(pkg.as_str(), "rust" | "rustc" | "cargo"),
        "deno" => pkg == "deno",
        "bun" => pkg == "bun",
        _ => false,
    }
}

#[cfg(any(target_os = "windows", test))]
impl DockerfileTranslator {
    /// Translate a `RUN` command (shell- or exec-form) for the
    /// translator's target OS.
    ///
    /// - **Linux**: returns the command verbatim — `(joined_string, [])` —
    ///   because Linux RUNs are handed straight to `/bin/sh -c` by the
    ///   buildah backend and need no rewriting.
    /// - **Windows**: exec-form is passed through (joined with spaces);
    ///   shell-form is forwarded to [`translate_shell_command`] which
    ///   detects apt/apk/yum/dnf invocations and rewrites them as
    ///   `choco install -y …` against the Chocolatey package map for
    ///   `source_distro`.
    ///
    /// If `provisioned_toolchain_language` is `Some(lang)`, any package
    /// matching that language ([`package_matches_toolchain`]) is **dropped**
    /// from the install list — the toolchain is already on `PATH` via
    /// `crate::windows_toolchain` and re-installing via Chocolatey would
    /// either fail or shadow the provisioned binary.
    ///
    /// Returns `(translated_command, skipped_packages)` where
    /// `skipped_packages` enumerates Linux package names whose Chocolatey
    /// mapping is the `__skip__` sentinel (no Windows equivalent — the
    /// caller typically logs them for the user).
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::BuildError::ChocoResolutionFailed`] if any
    /// Linux package detected in the install list has no mapping in the
    /// Chocolatey package map for `source_distro`. Returns whatever error
    /// `resolve_chocolatey_packages` surfaces for cache setup failures.
    pub(crate) async fn translate_run_command(
        &self,
        cmd: &ShellOrExec,
        source_distro: &str,
        provisioned_toolchain_language: Option<&str>,
    ) -> Result<(String, Vec<String>), crate::error::BuildError> {
        match self.target_os {
            ImageOs::Linux => match cmd {
                ShellOrExec::Shell(s) => Ok((s.clone(), Vec::new())),
                ShellOrExec::Exec(args) => Ok((args.join(" "), Vec::new())),
            },
            ImageOs::Windows => match cmd {
                ShellOrExec::Exec(args) => Ok((args.join(" "), Vec::new())),
                ShellOrExec::Shell(raw) => {
                    self.translate_shell_command(raw, source_distro, provisioned_toolchain_language)
                        .await
                }
            },
        }
    }

    /// Translate a shell-form RUN command. Behaviour matches
    /// [`Self::translate_run_command`] — Linux returns the input verbatim,
    /// Windows rewrites Linux package-manager invocations to Chocolatey.
    ///
    /// See [`Self::translate_run_command`] for the meaning of
    /// `provisioned_toolchain_language`.
    ///
    /// # Errors
    ///
    /// See [`Self::translate_run_command`].
    pub(crate) async fn translate_shell_command(
        &self,
        raw: &str,
        source_distro: &str,
        provisioned_toolchain_language: Option<&str>,
    ) -> Result<(String, Vec<String>), crate::error::BuildError> {
        if matches!(self.target_os, ImageOs::Linux) {
            return Ok((raw.to_string(), Vec::new()));
        }

        let subcommands = split_shell_subcommands(raw);
        if subcommands.is_empty() {
            // Empty RUN — defer to the underlying shell which will be a
            // no-op. `cmd /c` accepts an empty argument list and exits 0.
            return Ok((wrap_in_cmd(""), Vec::new()));
        }

        let mut classified: Vec<ShellSubcommand> = Vec::with_capacity(subcommands.len());
        let mut all_packages: Vec<String> = Vec::new();
        for sub in &subcommands {
            if is_package_manager_sync(sub) {
                classified.push(ShellSubcommand::PackageManagerSync);
                continue;
            }
            if let Some((kind, mut packages)) = detect_install_in_subcommand(sub) {
                // Drop packages already covered by the provisioned
                // toolchain — re-installing via Chocolatey would either
                // fail (version conflict) or shadow the provisioned
                // binary that the rest of the build relies on.
                if let Some(lang) = provisioned_toolchain_language {
                    packages.retain(|p| !package_matches_toolchain(p, lang));
                }
                if packages.is_empty() {
                    // Every package in this sub-command was the
                    // toolchain; nothing left to install, so elide the
                    // sub-command entirely.
                    continue;
                }
                all_packages.extend(packages.iter().cloned());
                classified.push(ShellSubcommand::Install { kind, packages });
                continue;
            }
            classified.push(ShellSubcommand::Verbatim(sub.clone()));
        }

        if all_packages.is_empty() {
            // No install was detected — pass the original shell command
            // through `cmd /c` unchanged. We re-join from the classified
            // parts so an `apt-get update`-only RUN still elides correctly.
            let rejoined = rejoin_subcommands(&classified);
            return Ok((wrap_in_cmd(&rejoined), Vec::new()));
        }

        // Bulk-resolve every package across every install sub-command in
        // one go so duplicate shard fetches are coalesced inside the
        // resolver.
        let resolved = crate::windows_image_resolver::resolve_chocolatey_packages(
            &all_packages,
            source_distro,
        )
        .await?;

        let mut lookup: HashMap<String, (Option<String>, bool)> = HashMap::new();
        for (linux, choco, skipped) in resolved {
            lookup.insert(linux, (choco, skipped));
        }

        let mut skipped_out: Vec<String> = Vec::new();
        for part in &mut classified {
            if let ShellSubcommand::Install { kind: _, packages } = part {
                let mut rewritten: Vec<String> = Vec::new();
                for pkg in packages.iter() {
                    match lookup.get(pkg) {
                        Some((Some(choco), false)) => rewritten.push(choco.clone()),
                        Some((_, true)) => skipped_out.push(pkg.clone()),
                        Some((None, false)) | None => {
                            // No mapping — surface as an error so the user
                            // gets a precise diagnostic instead of a
                            // silently-broken image.
                            return Err(crate::error::BuildError::ChocoResolutionFailed {
                                package: pkg.clone(),
                                source_distro: source_distro.to_string(),
                            });
                        }
                    }
                }
                *packages = rewritten;
            }
        }

        Ok((wrap_in_cmd(&rejoin_subcommands(&classified)), skipped_out))
    }
}

/// Escape a string for use in JSON
fn escape_json_string(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
        .replace('\r', "\\r")
        .replace('\t', "\\t")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dockerfile::RunInstruction;

    #[test]
    fn test_from_image() {
        let cmd = BuildahCommand::from_image("alpine:3.18");
        assert_eq!(cmd.program, "buildah");
        assert_eq!(cmd.args, vec!["from", "alpine:3.18"]);
    }

    #[test]
    fn test_pull_no_policy() {
        let cmd = BuildahCommand::pull("ghcr.io/astral-sh/uv:0.5.0", None);
        assert_eq!(cmd.program, "buildah");
        assert_eq!(cmd.args, vec!["pull", "ghcr.io/astral-sh/uv:0.5.0"]);
    }

    #[test]
    fn test_pull_with_policy() {
        let cmd = BuildahCommand::pull("ghcr.io/astral-sh/uv:0.5.0", Some("newer"));
        assert_eq!(
            cmd.args,
            vec!["pull", "--policy", "newer", "ghcr.io/astral-sh/uv:0.5.0"]
        );
    }

    #[test]
    fn test_run_shell() {
        let cmd = BuildahCommand::run_shell("container-1", "apt-get update");
        assert_eq!(
            cmd.args,
            vec![
                "run",
                "container-1",
                "--",
                "/bin/sh",
                "-c",
                "apt-get update"
            ]
        );
    }

    #[test]
    fn test_run_exec() {
        let args = vec!["echo".to_string(), "hello".to_string()];
        let cmd = BuildahCommand::run_exec("container-1", &args);
        assert_eq!(cmd.args, vec!["run", "container-1", "--", "echo", "hello"]);
    }

    #[test]
    fn test_copy() {
        let sources = vec!["src/".to_string(), "Cargo.toml".to_string()];
        let cmd = BuildahCommand::copy("container-1", &sources, "/app/");
        assert_eq!(
            cmd.args,
            vec!["copy", "container-1", "src/", "Cargo.toml", "/app/"]
        );
    }

    #[test]
    fn test_copy_from() {
        let sources = vec!["/app".to_string()];
        let cmd = BuildahCommand::copy_from("container-1", "builder", &sources, "/app");
        assert_eq!(
            cmd.args,
            vec!["copy", "--from", "builder", "container-1", "/app", "/app"]
        );
    }

    #[test]
    fn test_copy_from_external_image_reference_is_preserved() {
        // `COPY --from=ghcr.io/astral-sh/uv:0.5.0 /uv /usr/local/bin/uv`
        // — when the buildah backend hands an external image reference to
        // the translator, the registry-qualified ref must reach buildah
        // verbatim. Buildah resolves it against the local image store; the
        // backend is responsible for pulling the image first.
        use crate::dockerfile::CopyInstruction;

        let copy = CopyInstruction {
            sources: vec!["/uv".to_string()],
            destination: "/usr/local/bin/uv".to_string(),
            from: Some("ghcr.io/astral-sh/uv:0.5.0".to_string()),
            chown: None,
            chmod: None,
            link: false,
            exclude: Vec::new(),
        };
        let instruction = Instruction::Copy(copy);
        let cmds = BuildahCommand::from_instruction("container-1", &instruction);

        assert_eq!(
            cmds.len(),
            1,
            "COPY translates to a single buildah copy command"
        );
        assert_eq!(
            cmds[0].args,
            vec![
                "copy",
                "--from",
                "ghcr.io/astral-sh/uv:0.5.0",
                "container-1",
                "/uv",
                "/usr/local/bin/uv",
            ],
            "external image reference must be passed through to buildah unchanged",
        );
    }

    #[test]
    fn test_config_env() {
        let cmd = BuildahCommand::config_env("container-1", "PATH", "/usr/local/bin");
        assert_eq!(
            cmd.args,
            vec!["config", "--env", "PATH=/usr/local/bin", "container-1"]
        );
    }

    #[test]
    fn test_config_workdir() {
        let cmd = BuildahCommand::config_workdir("container-1", "/app");
        assert_eq!(
            cmd.args,
            vec!["config", "--workingdir", "/app", "container-1"]
        );
    }

    #[test]
    fn test_config_entrypoint_exec() {
        let args = vec!["/app".to_string(), "--config".to_string()];
        let cmd = BuildahCommand::config_entrypoint_exec("container-1", &args);
        assert!(cmd.args.contains(&"--entrypoint".to_string()));
        assert!(cmd
            .args
            .iter()
            .any(|a| a.contains('[') && a.contains("/app")));
    }

    #[test]
    fn test_commit() {
        let cmd = BuildahCommand::commit("container-1", "myimage:latest");
        assert_eq!(cmd.args, vec!["commit", "container-1", "myimage:latest"]);
    }

    #[test]
    fn test_to_command_string() {
        let cmd = BuildahCommand::config_env("container-1", "VAR", "value with spaces");
        let s = cmd.to_command_string();
        assert!(s.starts_with("buildah config"));
        assert!(s.contains("VAR=value with spaces"));
    }

    #[test]
    fn test_from_instruction_run() {
        let instruction = Instruction::Run(RunInstruction {
            command: ShellOrExec::Shell("echo hello".to_string()),
            mounts: vec![],
            network: None,
            security: None,
            env: HashMap::new(),
        });

        let cmds = BuildahCommand::from_instruction("container-1", &instruction);
        assert_eq!(cmds.len(), 1);
        assert!(cmds[0].args.contains(&"run".to_string()));
    }

    #[test]
    fn test_from_instruction_workdir_creates_and_configures() {
        // WORKDIR must both create the dir in the rootfs (like Docker) AND
        // update image metadata. Emitting only `config --workingdir` leaves
        // containers chdir-ing to a missing directory at init time.
        let instruction = Instruction::Workdir("/workspace".to_string());
        let cmds = BuildahCommand::from_instruction("container-1", &instruction);

        assert_eq!(cmds.len(), 2, "WORKDIR should emit mkdir + config");

        let run_args = &cmds[0].args;
        assert_eq!(run_args[0], "run");
        assert_eq!(run_args[1], "container-1");
        assert_eq!(run_args[2], "--");
        assert_eq!(run_args[3], "mkdir");
        assert_eq!(run_args[4], "-p");
        assert_eq!(run_args[5], "/workspace");

        assert_eq!(
            cmds[1].args,
            vec!["config", "--workingdir", "/workspace", "container-1"]
        );
    }

    #[test]
    fn test_from_instruction_env_multiple() {
        let mut vars = HashMap::new();
        vars.insert("FOO".to_string(), "bar".to_string());
        vars.insert("BAZ".to_string(), "qux".to_string());

        let instruction = Instruction::Env(EnvInstruction { vars });
        let cmds = BuildahCommand::from_instruction("container-1", &instruction);

        // Should produce two config commands (one per env var)
        assert_eq!(cmds.len(), 2);
        for cmd in &cmds {
            assert!(cmd.args.contains(&"config".to_string()));
            assert!(cmd.args.contains(&"--env".to_string()));
        }
    }

    #[test]
    fn test_escape_json_string() {
        assert_eq!(escape_json_string("hello"), "hello");
        assert_eq!(escape_json_string("hello \"world\""), "hello \\\"world\\\"");
        assert_eq!(escape_json_string("line1\nline2"), "line1\\nline2");
    }

    #[test]
    fn test_run_with_mounts_cache() {
        use crate::dockerfile::{CacheSharing, RunMount};

        let run = RunInstruction {
            command: ShellOrExec::Shell("apt-get update".to_string()),
            mounts: vec![RunMount::Cache {
                target: "/var/cache/apt".to_string(),
                id: Some("apt-cache".to_string()),
                sharing: CacheSharing::Shared,
                readonly: false,
            }],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmd = BuildahCommand::run_with_mounts("container-1", &run);

        // Verify --mount comes BEFORE container ID
        let mount_idx = cmd
            .args
            .iter()
            .position(|a| a.starts_with("--mount="))
            .expect("should have --mount arg");
        let container_idx = cmd
            .args
            .iter()
            .position(|a| a == "container-1")
            .expect("should have container id");

        assert!(
            mount_idx < container_idx,
            "--mount should come before container ID"
        );

        // Verify mount argument content
        assert!(cmd.args[mount_idx].contains("type=cache"));
        assert!(cmd.args[mount_idx].contains("target=/var/cache/apt"));
        assert!(cmd.args[mount_idx].contains("id=apt-cache"));
        assert!(cmd.args[mount_idx].contains("sharing=shared"));
    }

    #[test]
    fn test_run_with_multiple_mounts() {
        use crate::dockerfile::{CacheSharing, RunMount};

        let run = RunInstruction {
            command: ShellOrExec::Shell("cargo build".to_string()),
            mounts: vec![
                RunMount::Cache {
                    target: "/usr/local/cargo/registry".to_string(),
                    id: Some("cargo-registry".to_string()),
                    sharing: CacheSharing::Shared,
                    readonly: false,
                },
                RunMount::Cache {
                    target: "/app/target".to_string(),
                    id: Some("cargo-target".to_string()),
                    sharing: CacheSharing::Locked,
                    readonly: false,
                },
            ],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmd = BuildahCommand::run_with_mounts("container-1", &run);

        // Count --mount arguments
        let mount_count = cmd
            .args
            .iter()
            .filter(|a| a.starts_with("--mount="))
            .count();
        assert_eq!(mount_count, 2, "should have 2 mount arguments");

        // Verify all mounts come before container ID
        let container_idx = cmd
            .args
            .iter()
            .position(|a| a == "container-1")
            .expect("should have container id");

        for (idx, arg) in cmd.args.iter().enumerate() {
            if arg.starts_with("--mount=") {
                assert!(
                    idx < container_idx,
                    "--mount at index {idx} should come before container ID at {container_idx}",
                );
            }
        }
    }

    #[test]
    fn test_from_instruction_run_with_mounts() {
        use crate::dockerfile::{CacheSharing, RunMount};

        let instruction = Instruction::Run(RunInstruction {
            command: ShellOrExec::Shell("npm install".to_string()),
            mounts: vec![RunMount::Cache {
                target: "/root/.npm".to_string(),
                id: Some("npm-cache".to_string()),
                sharing: CacheSharing::Shared,
                readonly: false,
            }],
            network: None,
            security: None,
            env: HashMap::new(),
        });

        let cmds = BuildahCommand::from_instruction("container-1", &instruction);
        assert_eq!(cmds.len(), 1);

        let cmd = &cmds[0];
        assert!(
            cmd.args.iter().any(|a| a.starts_with("--mount=")),
            "should include --mount argument"
        );
    }

    #[test]
    fn test_run_with_mounts_exec_form() {
        use crate::dockerfile::{CacheSharing, RunMount};

        let run = RunInstruction {
            command: ShellOrExec::Exec(vec![
                "pip".to_string(),
                "install".to_string(),
                "-r".to_string(),
                "requirements.txt".to_string(),
            ]),
            mounts: vec![RunMount::Cache {
                target: "/root/.cache/pip".to_string(),
                id: Some("pip-cache".to_string()),
                sharing: CacheSharing::Shared,
                readonly: false,
            }],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmd = BuildahCommand::run_with_mounts("container-1", &run);

        // Should have mount, container, --, and then the exec args
        assert!(cmd.args.contains(&"--".to_string()));
        assert!(cmd.args.contains(&"pip".to_string()));
        assert!(cmd.args.contains(&"install".to_string()));
    }

    #[test]
    fn test_run_with_mounts_emits_env_flags_sorted() {
        // RunInstruction with `env` must produce `--env=K=V` args BEFORE the
        // container ID, sorted by key for determinism. Env is intentionally
        // scoped to this single buildah-run invocation (not baked into the
        // image config via `buildah config --env`).
        let mut env = HashMap::new();
        env.insert("B".to_string(), "2".to_string());
        env.insert("A".to_string(), "1".to_string());

        let run = RunInstruction {
            command: ShellOrExec::Shell("env".to_string()),
            mounts: vec![],
            network: None,
            security: None,
            env,
        };

        let cmd = BuildahCommand::run_with_mounts("container-1", &run);

        // Confirm both --env flags appear, in sorted (A then B) order.
        let env_positions: Vec<(usize, &String)> = cmd
            .args
            .iter()
            .enumerate()
            .filter(|(_, a)| a.starts_with("--env="))
            .collect();
        assert_eq!(
            env_positions.len(),
            2,
            "expected 2 --env args, got {env_positions:?}"
        );
        assert_eq!(env_positions[0].1, "--env=A=1");
        assert_eq!(env_positions[1].1, "--env=B=2");

        // Both --env flags must come BEFORE the container ID.
        let container_idx = cmd
            .args
            .iter()
            .position(|a| a == "container-1")
            .expect("container ID present");
        for (idx, _) in &env_positions {
            assert!(
                *idx < container_idx,
                "--env at {idx} must precede container ID at {container_idx}"
            );
        }

        // And BEFORE the `--` separator.
        let sep_idx = cmd
            .args
            .iter()
            .position(|a| a == "--")
            .expect("-- separator present");
        for (idx, _) in &env_positions {
            assert!(
                *idx < sep_idx,
                "--env at {idx} must precede `--` at {sep_idx}"
            );
        }
    }

    #[test]
    fn test_translator_routes_env_only_run_through_mounts_path() {
        // A RunInstruction with env but no mounts must still flow through
        // run_with_mounts_shell so the --env= flags get emitted. The plain
        // run_shell / run_exec factories don't know about env.
        let mut env = HashMap::new();
        env.insert("FOO".to_string(), "bar".to_string());

        let run = RunInstruction {
            command: ShellOrExec::Shell("echo $FOO".to_string()),
            mounts: vec![],
            network: None,
            security: None,
            env,
        };

        let cmds = DockerfileTranslator::new(ImageOs::Linux)
            .translate("container-1", &Instruction::Run(run));
        assert_eq!(cmds.len(), 1);

        let cmd = &cmds[0];
        assert!(
            cmd.args.iter().any(|a| a == "--env=FOO=bar"),
            "expected --env=FOO=bar in args: {:?}",
            cmd.args
        );
    }

    // ---------------------------------------------------------------------
    // Host-network plumbing
    //
    // These tests pin the `--host-network` CLI flag's end-to-end behavior:
    // a `RunInstruction` with `network: Some(RunNetwork::Host)` MUST emit
    // `--net=host` BEFORE the container ID on the buildah command, and the
    // translator-level `with_host_network(true)` MUST force-set host
    // networking on every RUN regardless of any per-instruction value.
    // ---------------------------------------------------------------------

    #[test]
    fn test_run_with_mounts_emits_net_host_before_container() {
        let run = RunInstruction {
            command: ShellOrExec::Shell("apt-get update".to_string()),
            mounts: vec![],
            network: Some(crate::dockerfile::RunNetwork::Host),
            security: None,
            env: HashMap::new(),
        };

        let cmd = BuildahCommand::run_with_mounts("container-1", &run);

        let net_idx = cmd
            .args
            .iter()
            .position(|a| a == "--net=host")
            .expect("expected --net=host arg");
        let container_idx = cmd
            .args
            .iter()
            .position(|a| a == "container-1")
            .expect("container id present");
        let sep_idx = cmd
            .args
            .iter()
            .position(|a| a == "--")
            .expect("-- separator present");
        assert!(
            net_idx < container_idx,
            "--net=host (idx {net_idx}) must precede container ID (idx {container_idx})"
        );
        assert!(
            net_idx < sep_idx,
            "--net=host (idx {net_idx}) must precede `--` (idx {sep_idx})"
        );
    }

    #[test]
    fn test_run_with_mounts_emits_net_none() {
        let run = RunInstruction {
            command: ShellOrExec::Shell("hostname".to_string()),
            mounts: vec![],
            network: Some(crate::dockerfile::RunNetwork::None),
            security: None,
            env: HashMap::new(),
        };

        let cmd = BuildahCommand::run_with_mounts("container-1", &run);
        assert!(
            cmd.args.iter().any(|a| a == "--net=none"),
            "expected --net=none in args, got: {:?}",
            cmd.args
        );
    }

    #[test]
    fn test_run_with_mounts_default_network_omits_net_flag() {
        let run = RunInstruction {
            command: ShellOrExec::Shell("true".to_string()),
            mounts: vec![],
            network: Some(crate::dockerfile::RunNetwork::Default),
            security: None,
            env: HashMap::new(),
        };

        let cmd = BuildahCommand::run_with_mounts("container-1", &run);
        assert!(
            !cmd.args.iter().any(|a| a.starts_with("--net")),
            "RunNetwork::Default must NOT emit any --net flag, got: {:?}",
            cmd.args
        );
    }

    #[test]
    fn test_run_with_mounts_no_network_field_omits_net_flag() {
        let run = RunInstruction {
            command: ShellOrExec::Shell("true".to_string()),
            mounts: vec![],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmd = BuildahCommand::run_with_mounts("container-1", &run);
        assert!(
            !cmd.args.iter().any(|a| a.starts_with("--net")),
            "network=None must NOT emit any --net flag, got: {:?}",
            cmd.args
        );
    }

    #[test]
    fn test_translator_host_network_forces_net_host_on_run_with_none_network() {
        // Load-bearing assertion for the `zlayer --host-network` CLI flag:
        // host_network=true must emit --net=host even when the Dockerfile
        // RUN does NOT specify a per-instruction --network.
        let run = RunInstruction {
            command: ShellOrExec::Shell("apt-get update".to_string()),
            mounts: vec![],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmds = DockerfileTranslator::new(ImageOs::Linux)
            .with_host_network(true)
            .translate("c1", &Instruction::Run(run));

        assert_eq!(cmds.len(), 1, "expected exactly one buildah command");
        let cmd = &cmds[0];
        assert!(
            cmd.args.iter().any(|a| a == "--net=host"),
            "expected --net=host in args (host_network=true should force it even when run.network is None), got: {:?}",
            cmd.args
        );
    }

    #[test]
    fn test_translator_host_network_overrides_per_instruction_network_none() {
        // `RUN --network=none` + CLI `--host-network` -> host wins.
        let run = RunInstruction {
            command: ShellOrExec::Shell("apt-get install -y curl".to_string()),
            mounts: vec![],
            network: Some(crate::dockerfile::RunNetwork::None),
            security: None,
            env: HashMap::new(),
        };

        let cmds = DockerfileTranslator::new(ImageOs::Linux)
            .with_host_network(true)
            .translate("c1", &Instruction::Run(run));

        assert_eq!(cmds.len(), 1);
        let cmd = &cmds[0];
        assert!(
            cmd.args.iter().any(|a| a == "--net=host"),
            "host_network=true must override RunNetwork::None, got: {:?}",
            cmd.args
        );
        assert!(
            !cmd.args.iter().any(|a| a == "--net=none"),
            "host_network=true must REPLACE (not append to) RunNetwork::None, got: {:?}",
            cmd.args
        );
    }

    #[test]
    fn test_translator_host_network_routes_bare_run_through_mounts_path() {
        // Bare RUN (no mounts, env, or network) must still flow through
        // run_with_mounts_shell when host_network=true — the simple
        // factories don't emit --net=host.
        let run = RunInstruction {
            command: ShellOrExec::Shell("echo hi".to_string()),
            mounts: vec![],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmds = DockerfileTranslator::new(ImageOs::Linux)
            .with_host_network(true)
            .translate("c1", &Instruction::Run(run));

        assert_eq!(cmds.len(), 1);
        let cmd = &cmds[0];
        assert!(
            cmd.args.iter().any(|a| a == "--net=host"),
            "bare RUN with host_network=true must emit --net=host, got: {:?}",
            cmd.args
        );
    }

    #[test]
    fn test_translator_host_network_routes_env_only_run_with_net_host() {
        // env-only RUN + host_network=true must produce BOTH --env=... and
        // --net=host, both before the container ID.
        let mut env = HashMap::new();
        env.insert("FOO".to_string(), "bar".to_string());

        let run = RunInstruction {
            command: ShellOrExec::Shell("echo $FOO".to_string()),
            mounts: vec![],
            network: None,
            security: None,
            env,
        };

        let cmds = DockerfileTranslator::new(ImageOs::Linux)
            .with_host_network(true)
            .translate("c1", &Instruction::Run(run));

        assert_eq!(cmds.len(), 1);
        let cmd = &cmds[0];

        let env_idx = cmd
            .args
            .iter()
            .position(|a| a == "--env=FOO=bar")
            .expect("--env=FOO=bar present");
        let net_idx = cmd
            .args
            .iter()
            .position(|a| a == "--net=host")
            .expect("--net=host present");
        let container_idx = cmd
            .args
            .iter()
            .position(|a| a == "c1")
            .expect("container id present");
        assert!(env_idx < container_idx);
        assert!(net_idx < container_idx);
    }

    #[test]
    fn test_translator_host_network_routes_mount_only_run_with_net_host() {
        use crate::dockerfile::{CacheSharing, RunMount};

        let run = RunInstruction {
            command: ShellOrExec::Shell("npm install".to_string()),
            mounts: vec![RunMount::Cache {
                target: "/root/.npm".to_string(),
                id: Some("npm-cache".to_string()),
                sharing: CacheSharing::Shared,
                readonly: false,
            }],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmds = DockerfileTranslator::new(ImageOs::Linux)
            .with_host_network(true)
            .translate("c1", &Instruction::Run(run));

        assert_eq!(cmds.len(), 1);
        let cmd = &cmds[0];
        assert!(
            cmd.args.iter().any(|a| a.starts_with("--mount=")),
            "--mount must be present"
        );
        assert!(
            cmd.args.iter().any(|a| a == "--net=host"),
            "--net=host must be present alongside --mount when host_network=true"
        );
    }

    #[test]
    fn test_translator_host_network_default_off_does_not_emit_net_flag() {
        // Sanity: default translator (host_network not set) must not emit
        // --net=... on a vanilla RUN. We're only adding a flag, never
        // silently changing existing behavior.
        let run = RunInstruction {
            command: ShellOrExec::Shell("true".to_string()),
            mounts: vec![],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmds =
            DockerfileTranslator::new(ImageOs::Linux).translate("c1", &Instruction::Run(run));
        assert_eq!(cmds.len(), 1);
        let cmd = &cmds[0];
        assert!(
            !cmd.args.iter().any(|a| a.starts_with("--net")),
            "default translator (host_network=false) must NOT emit --net flag, got: {:?}",
            cmd.args
        );
    }

    #[test]
    fn test_manifest_create() {
        let cmd = BuildahCommand::manifest_create("myapp:latest");
        assert_eq!(cmd.program, "buildah");
        assert_eq!(cmd.args, vec!["manifest", "create", "myapp:latest"]);
    }

    #[test]
    fn test_manifest_add() {
        let cmd = BuildahCommand::manifest_add("myapp:latest", "myapp-amd64:latest");
        assert_eq!(
            cmd.args,
            vec!["manifest", "add", "myapp:latest", "myapp-amd64:latest"]
        );
    }

    #[test]
    fn test_manifest_push() {
        let cmd =
            BuildahCommand::manifest_push("myapp:latest", "docker://registry.example.com/myapp");
        assert_eq!(
            cmd.args,
            vec![
                "manifest",
                "push",
                "--all",
                "myapp:latest",
                "docker://registry.example.com/myapp"
            ]
        );
    }

    #[test]
    fn test_manifest_rm() {
        let cmd = BuildahCommand::manifest_rm("myapp:latest");
        assert_eq!(cmd.args, vec!["manifest", "rm", "myapp:latest"]);
    }

    // -------------------------------------------------------------------------
    // L-3: OS-aware translation tests
    // -------------------------------------------------------------------------

    #[test]
    fn test_run_shell_for_os_linux() {
        let cmd = BuildahCommand::run_shell_for_os("c1", "echo hello", ImageOs::Linux);
        assert_eq!(
            cmd.args,
            vec!["run", "c1", "--", "/bin/sh", "-c", "echo hello"]
        );
    }

    #[test]
    fn test_run_shell_for_os_windows() {
        let cmd = BuildahCommand::run_shell_for_os("c1", "echo hello", ImageOs::Windows);
        assert_eq!(
            cmd.args,
            vec!["run", "c1", "--", "cmd.exe", "/S", "/C", "echo hello"]
        );
    }

    #[test]
    fn test_run_shell_custom_powershell() {
        let shell = ["powershell", "-Command"];
        let cmd = BuildahCommand::run_shell_custom("c1", shell, "Get-Process");
        assert_eq!(
            cmd.args,
            vec!["run", "c1", "--", "powershell", "-Command", "Get-Process"]
        );
    }

    #[test]
    fn test_translator_linux_run_shell_default() {
        let mut t = DockerfileTranslator::new(ImageOs::Linux);
        let instr = Instruction::Run(RunInstruction::shell("apt-get update"));
        let cmds = t.translate("c1", &instr);
        assert_eq!(cmds.len(), 1);
        assert_eq!(
            cmds[0].args,
            vec!["run", "c1", "--", "/bin/sh", "-c", "apt-get update"]
        );
    }

    #[test]
    fn test_translator_windows_run_shell_default() {
        let mut t = DockerfileTranslator::new(ImageOs::Windows);
        let instr = Instruction::Run(RunInstruction::shell("dir C:\\"));
        let cmds = t.translate("c1", &instr);
        assert_eq!(cmds.len(), 1);
        assert_eq!(
            cmds[0].args,
            vec!["run", "c1", "--", "cmd.exe", "/S", "/C", "dir C:\\"]
        );
    }

    #[test]
    fn test_translator_shell_override_linux_bash() {
        // SHELL ["/bin/bash", "-lc"] then RUN cmd → uses bash
        let mut t = DockerfileTranslator::new(ImageOs::Linux);

        let shell_instr = Instruction::Shell(vec!["/bin/bash".to_string(), "-lc".to_string()]);
        let shell_cmds = t.translate("c1", &shell_instr);
        // SHELL emits the metadata config --shell
        assert_eq!(shell_cmds.len(), 1);
        assert!(shell_cmds[0].args.contains(&"--shell".to_string()));

        let run_instr = Instruction::Run(RunInstruction::shell("set -e; echo $SHELL"));
        let run_cmds = t.translate("c1", &run_instr);
        assert_eq!(run_cmds.len(), 1);
        assert_eq!(
            run_cmds[0].args,
            vec!["run", "c1", "--", "/bin/bash", "-lc", "set -e; echo $SHELL"]
        );
    }

    #[test]
    fn test_translator_shell_override_windows_powershell() {
        // SHELL ["powershell", "-Command"] then RUN cmd on Windows → uses
        // powershell, not the default cmd.exe /S /C.
        let mut t = DockerfileTranslator::new(ImageOs::Windows);

        let shell_instr =
            Instruction::Shell(vec!["powershell".to_string(), "-Command".to_string()]);
        t.translate("c1", &shell_instr);

        let run_instr = Instruction::Run(RunInstruction::shell("Get-Process"));
        let run_cmds = t.translate("c1", &run_instr);
        assert_eq!(run_cmds.len(), 1);
        assert_eq!(
            run_cmds[0].args,
            vec!["run", "c1", "--", "powershell", "-Command", "Get-Process"]
        );
    }

    #[test]
    fn test_translator_shell_override_persists_across_runs() {
        // Two RUNs after a single SHELL should both use the overridden shell.
        let mut t = DockerfileTranslator::new(ImageOs::Linux);
        t.translate(
            "c1",
            &Instruction::Shell(vec!["/bin/bash".to_string(), "-c".to_string()]),
        );

        for _ in 0..2 {
            let cmds = t.translate("c1", &Instruction::Run(RunInstruction::shell("echo hi")));
            assert_eq!(
                cmds[0].args,
                vec!["run", "c1", "--", "/bin/bash", "-c", "echo hi"]
            );
        }
    }

    #[test]
    fn test_translator_exec_form_ignores_shell_override() {
        // Exec-form RUN must not be wrapped with the shell prefix, even
        // when a SHELL override is active — matches Docker semantics.
        let mut t = DockerfileTranslator::new(ImageOs::Windows);
        t.translate(
            "c1",
            &Instruction::Shell(vec!["powershell".to_string(), "-Command".to_string()]),
        );

        let run = Instruction::Run(RunInstruction::exec(vec![
            "myapp.exe".to_string(),
            "--flag".to_string(),
        ]));
        let cmds = t.translate("c1", &run);
        assert_eq!(cmds[0].args, vec!["run", "c1", "--", "myapp.exe", "--flag"]);
    }

    #[test]
    fn test_translator_workdir_linux() {
        let mut t = DockerfileTranslator::new(ImageOs::Linux);
        let cmds = t.translate("c1", &Instruction::Workdir("/app".to_string()));
        assert_eq!(cmds.len(), 2);
        assert_eq!(cmds[0].args, vec!["run", "c1", "--", "mkdir", "-p", "/app"]);
        assert_eq!(cmds[1].args, vec!["config", "--workingdir", "/app", "c1"]);
    }

    #[test]
    fn test_translator_workdir_linux_with_empty_src_dir() {
        // When configured with an empty source directory (the production
        // BuildahBackend path), WORKDIR translates to `buildah copy
        // <empty>/. <dir>` instead of `buildah run -- mkdir -p <dir>`.
        // That's the shell-free path that works on distroless / scratch /
        // any base lacking `/bin/sh`.
        let empty = std::path::PathBuf::from("/tmp/zlayer-empty-test");
        let mut t = DockerfileTranslator::new(ImageOs::Linux).with_empty_src_dir(empty);
        let cmds = t.translate("c1", &Instruction::Workdir("/app".to_string()));
        assert_eq!(cmds.len(), 2);
        assert_eq!(
            cmds[0].args,
            vec!["copy", "c1", "/tmp/zlayer-empty-test/.", "/app"],
            "WORKDIR with empty_src_dir must emit `buildah copy <empty>/. <dir>` to materialize the dir without running a shell",
        );
        assert_eq!(cmds[1].args, vec!["config", "--workingdir", "/app", "c1"]);
    }

    #[test]
    fn test_translator_workdir_linux_with_empty_src_dir_ignores_host_network() {
        // `buildah copy` never executes anything inside the container, so
        // `--net=host` is irrelevant to it. The host_network flag should
        // affect RUN translations only, not the COPY-based WORKDIR path.
        let empty = std::path::PathBuf::from("/var/tmp/empty");
        let mut t = DockerfileTranslator::new(ImageOs::Linux)
            .with_host_network(true)
            .with_empty_src_dir(empty);
        let cmds = t.translate("c1", &Instruction::Workdir("/app".to_string()));
        assert_eq!(cmds.len(), 2);
        assert!(
            !cmds[0].args.iter().any(|a| a.starts_with("--net")),
            "buildah copy must never carry --net flags, got: {:?}",
            cmds[0].args
        );
        assert_eq!(cmds[0].args[0], "copy");
        assert_eq!(cmds[1].args, vec!["config", "--workingdir", "/app", "c1"]);
    }

    #[test]
    fn test_translator_workdir_windows() {
        let mut t = DockerfileTranslator::new(ImageOs::Windows);
        let cmds = t.translate("c1", &Instruction::Workdir("C:\\app".to_string()));
        assert_eq!(cmds.len(), 2);
        // Pre-mkdir guarded with `if not exist` so a repeated WORKDIR in the
        // same Dockerfile doesn't cause Windows mkdir to error on existence.
        assert_eq!(
            cmds[0].args,
            vec![
                "run",
                "c1",
                "--",
                "cmd.exe",
                "/S",
                "/C",
                r#"if not exist "C:\app" mkdir "C:\app""#
            ]
        );
        assert_eq!(
            cmds[1].args,
            vec!["config", "--workingdir", "C:\\app", "c1"]
        );
    }

    #[test]
    fn test_translator_workdir_host_network_linux_emits_net_host() {
        // Regression: when the translator is constructed with
        // `with_host_network(true)` (i.e. the user passed `--host-network`),
        // WORKDIR's `mkdir -p` MUST carry `--net=host` just like every
        // RUN does. Before the fix, only `Instruction::Run` consulted
        // `self.host_network`, so a `WORKDIR /app` would route through
        // buildah's default rootless networking → netavark → die on the
        // very first instruction of stage 1 if the host's netavark
        // config was broken.
        let mut t = DockerfileTranslator::new(ImageOs::Linux).with_host_network(true);
        let cmds = t.translate("c1", &Instruction::Workdir("/app".to_string()));
        assert_eq!(cmds.len(), 2);
        assert_eq!(
            cmds[0].args,
            vec!["run", "--net=host", "c1", "--", "mkdir", "-p", "/app"],
            "WORKDIR mkdir with host_network=true must emit --net=host BEFORE the container ID",
        );
        // The `config --workingdir` metadata write is unaffected by
        // host_network — `buildah config` doesn't run a container.
        assert_eq!(cmds[1].args, vec!["config", "--workingdir", "/app", "c1"]);
    }

    #[test]
    fn test_translator_workdir_no_host_network_omits_net_flag() {
        // Pin the default-path behavior: without `--host-network`,
        // WORKDIR's mkdir must NOT carry any `--net` flag. Mirrors the
        // existing `test_translator_workdir_linux` but makes the
        // host_network=false branch explicit so a future refactor that
        // accidentally always emits `--net=host` is caught immediately.
        let mut t = DockerfileTranslator::new(ImageOs::Linux).with_host_network(false);
        let cmds = t.translate("c1", &Instruction::Workdir("/app".to_string()));
        assert_eq!(cmds.len(), 2);
        assert!(
            !cmds[0].args.iter().any(|a| a.starts_with("--net")),
            "WORKDIR with host_network=false must NOT emit any --net flag, got: {:?}",
            cmds[0].args
        );
        assert_eq!(cmds[0].args, vec!["run", "c1", "--", "mkdir", "-p", "/app"]);
    }

    #[test]
    fn test_translator_workdir_host_network_windows_emits_net_host() {
        // Symmetric coverage for the Windows guarded-mkdir path. Windows
        // builds dispatch through the HCS backend in practice (not buildah),
        // so this is belt-and-suspenders: if someone ever wires the buildah
        // translator into a Windows build, host_network must still be
        // honored on the guarded `if not exist ... mkdir` invocation.
        let mut t = DockerfileTranslator::new(ImageOs::Windows).with_host_network(true);
        let cmds = t.translate("c1", &Instruction::Workdir("C:\\app".to_string()));
        assert_eq!(cmds.len(), 2);
        let net_idx = cmds[0]
            .args
            .iter()
            .position(|a| a == "--net=host")
            .expect("expected --net=host on Windows WORKDIR with host_network=true");
        let container_idx = cmds[0]
            .args
            .iter()
            .position(|a| a == "c1")
            .expect("container ID present");
        let sep_idx = cmds[0]
            .args
            .iter()
            .position(|a| a == "--")
            .expect("`--` separator present");
        assert!(
            net_idx < container_idx && container_idx < sep_idx,
            "argument order must be: run --net=host <container> -- ... (got {:?})",
            cmds[0].args
        );
    }

    #[test]
    fn test_translator_workdir_windows_path_with_spaces() {
        // Paths containing spaces (e.g. `C:\Program Files\app`) must be quoted
        // for cmd.exe, which is why the mkdir command we emit wraps the path
        // in double-quotes.
        let mut t = DockerfileTranslator::new(ImageOs::Windows);
        let cmds = t.translate(
            "c1",
            &Instruction::Workdir("C:\\Program Files\\app".to_string()),
        );
        assert_eq!(cmds.len(), 2);
        let mkdir_cmd = &cmds[0].args[6];
        assert_eq!(
            mkdir_cmd,
            r#"if not exist "C:\Program Files\app" mkdir "C:\Program Files\app""#
        );
    }

    #[test]
    fn test_from_instruction_preserves_linux_byte_identical_output() {
        // Backward-compat guarantee: the legacy `from_instruction` entrypoint
        // must emit the exact same byte-for-byte commands as it did before
        // the translator refactor. The existing Linux callers (buildah backend)
        // rely on this.
        let run = Instruction::Run(RunInstruction::shell("echo hello"));
        let legacy = BuildahCommand::from_instruction("c1", &run);
        let via_translator = DockerfileTranslator::new(ImageOs::Linux).translate("c1", &run);
        assert_eq!(legacy.len(), via_translator.len());
        for (a, b) in legacy.iter().zip(via_translator.iter()) {
            assert_eq!(a.args, b.args);
            assert_eq!(a.program, b.program);
        }

        // WORKDIR must still emit mkdir -p + config --workingdir
        let workdir = Instruction::Workdir("/workspace".to_string());
        let legacy = BuildahCommand::from_instruction("c1", &workdir);
        assert_eq!(legacy.len(), 2);
        assert_eq!(
            legacy[0].args,
            vec!["run", "c1", "--", "mkdir", "-p", "/workspace"]
        );
        assert_eq!(
            legacy[1].args,
            vec!["config", "--workingdir", "/workspace", "c1"]
        );
    }

    #[test]
    fn test_translator_active_shell_reflects_override() {
        let mut t = DockerfileTranslator::new(ImageOs::Linux);
        assert_eq!(t.active_shell(), vec!["/bin/sh", "-c"]);

        t.set_shell_override(vec!["/bin/bash".to_string(), "-lc".to_string()]);
        assert_eq!(t.active_shell(), vec!["/bin/bash", "-lc"]);
    }

    #[test]
    fn test_translator_target_os_accessor() {
        assert_eq!(
            DockerfileTranslator::new(ImageOs::Linux).target_os(),
            ImageOs::Linux
        );
        assert_eq!(
            DockerfileTranslator::new(ImageOs::Windows).target_os(),
            ImageOs::Windows
        );
    }

    #[test]
    fn test_translator_windows_run_with_mounts_uses_cmd_exe() {
        use crate::dockerfile::{CacheSharing, RunMount};

        let mut t = DockerfileTranslator::new(ImageOs::Windows);
        let run = RunInstruction {
            command: ShellOrExec::Shell("echo cached".to_string()),
            mounts: vec![RunMount::Cache {
                target: "C:\\cache".to_string(),
                id: Some("win-cache".to_string()),
                sharing: CacheSharing::Shared,
                readonly: false,
            }],
            network: None,
            security: None,
            env: HashMap::new(),
        };

        let cmds = t.translate("c1", &Instruction::Run(run));
        assert_eq!(cmds.len(), 1);

        // --mount= must precede container ID
        let mount_idx = cmds[0]
            .args
            .iter()
            .position(|a| a.starts_with("--mount="))
            .expect("mount arg present");
        let container_idx = cmds[0]
            .args
            .iter()
            .position(|a| a == "c1")
            .expect("container ID present");
        assert!(mount_idx < container_idx);

        // Shell form must use cmd.exe /S /C, not /bin/sh -c
        assert!(cmds[0].args.iter().any(|a| a == "cmd.exe"));
        assert!(cmds[0].args.iter().any(|a| a == "/S"));
        assert!(cmds[0].args.iter().any(|a| a == "/C"));
        assert!(!cmds[0].args.iter().any(|a| a == "/bin/sh"));
    }

    // -----------------------------------------------------------------
    // apt → choco translation (moved from `windows_builder::tests`)
    // -----------------------------------------------------------------

    use crate::windows_image_resolver::{ChocoMapMetadata, ChocoMapShard};

    /// Tests that mutate `XDG_CACHE_HOME` / `LOCALAPPDATA` must run
    /// serially or the resolver picks up another test's fixture dir
    /// because `cargo test` runs unit tests in parallel by default.
    static CACHE_ENV_GUARD: std::sync::Mutex<()> = std::sync::Mutex::new(());

    /// Write a single-shard package map under `cache_root` so the
    /// resolver's disk cache hits without going through the network.
    fn write_shard_fixture(
        cache_root: &std::path::Path,
        distro: &str,
        shard: &str,
        mappings: &[(&str, &str)],
    ) {
        let fixture = ChocoMapShard {
            metadata: ChocoMapMetadata {
                generated_at: "2026-05-21T00:00:00Z".to_string(),
                source: "chocolatey.org".to_string(),
                distro: distro.to_string(),
                shard: shard.to_string(),
                total_mappings: mappings.len() as u64,
            },
            mappings: mappings
                .iter()
                .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
                .collect(),
        };
        let shard_dir = cache_root.join("package-maps-choco-v1").join(distro);
        std::fs::create_dir_all(&shard_dir).unwrap();
        std::fs::write(
            shard_dir.join(format!("{shard}.json")),
            serde_json::to_string(&fixture).unwrap(),
        )
        .unwrap();
    }

    /// Override the platform cache dir for the duration of a test by
    /// pointing `XDG_CACHE_HOME` (Linux/macOS) and `LOCALAPPDATA`
    /// (Windows) at a fresh tempdir. Returns the mutex guard (so the
    /// env is held exclusively for the test's lifetime), the tempdir
    /// (kept alive until drop), and the cache root.
    fn redirect_cache_dir() -> (
        std::sync::MutexGuard<'static, ()>,
        tempfile::TempDir,
        std::path::PathBuf,
    ) {
        // `lock()` may report poisoning if a prior test panicked while
        // holding it; the env vars themselves are safe to re-set so we
        // unwrap-or-into-inner to keep the test useful even after a
        // sibling failure.
        let guard = CACHE_ENV_GUARD
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        let tmp = tempfile::tempdir().unwrap();
        let cache_root = tmp.path().to_path_buf();
        // `dirs::cache_dir()` ignores `XDG_CACHE_HOME` on macOS/Windows, so set
        // the explicit override the resolver honors on every platform. The
        // XDG/LOCALAPPDATA sets are kept for any other code paths that read the
        // platform cache dir directly.
        std::env::set_var("ZLAYER_PACKAGE_MAP_CACHE_DIR", &cache_root);
        std::env::set_var("XDG_CACHE_HOME", &cache_root);
        std::env::set_var("LOCALAPPDATA", &cache_root);
        (guard, tmp, cache_root)
    }

    fn block_on<F: std::future::Future>(fut: F) -> F::Output {
        tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("runtime")
            .block_on(fut)
    }

    #[test]
    fn detect_apt_install_in_run() {
        let parts = split_shell_subcommands("apt-get update && apt-get install -y curl git");
        assert_eq!(parts.len(), 2);
        assert!(is_package_manager_sync(&parts[0]));
        let detected = detect_install_in_subcommand(&parts[1])
            .expect("install sub-command must be recognised");
        assert_eq!(detected.0, DetectedPmKind::Apt);
        assert_eq!(detected.1, vec!["curl".to_string(), "git".to_string()]);
    }

    #[test]
    fn detect_yum_install_in_run() {
        let detected = detect_install_in_subcommand("yum install -y httpd")
            .expect("yum install -y httpd must be recognised");
        assert_eq!(detected.0, DetectedPmKind::YumOrDnf);
        assert_eq!(detected.1, vec!["httpd".to_string()]);

        let detected = detect_install_in_subcommand("dnf install -y nginx php-fpm")
            .expect("dnf install -y must be recognised");
        assert_eq!(detected.0, DetectedPmKind::YumOrDnf);
        assert_eq!(detected.1, vec!["nginx".to_string(), "php-fpm".to_string()]);
    }

    #[test]
    fn detect_apk_install_in_run() {
        let detected = detect_install_in_subcommand("apk add --no-cache nodejs npm")
            .expect("apk add must be recognised");
        assert_eq!(detected.0, DetectedPmKind::Apk);
        assert_eq!(detected.1, vec!["nodejs".to_string(), "npm".to_string()]);
    }

    #[test]
    fn detect_no_install_returns_none() {
        assert!(detect_install_in_subcommand("echo hello").is_none());
        assert!(detect_install_in_subcommand("ls /tmp").is_none());
        assert!(detect_install_in_subcommand("apt-getinstall -y curl").is_none());
        assert!(detect_install_in_subcommand("apt-get install -y").is_none());
        let parts = split_shell_subcommands("echo hello && ls /tmp");
        assert_eq!(parts.len(), 2);
        for p in &parts {
            assert!(detect_install_in_subcommand(p).is_none());
            assert!(!is_package_manager_sync(p));
        }
    }

    #[test]
    fn split_shell_subcommands_honours_and_and_semicolon() {
        let parts = split_shell_subcommands("a && b ; c");
        assert_eq!(
            parts,
            vec!["a".to_string(), "b".to_string(), "c".to_string()]
        );
    }

    #[test]
    fn split_shell_subcommands_drops_empty_segments() {
        let parts = split_shell_subcommands(" && a && ; b ;");
        assert_eq!(parts, vec!["a".to_string(), "b".to_string()]);
    }

    #[test]
    fn is_package_manager_sync_matches_common_variants() {
        assert!(is_package_manager_sync("apt-get update"));
        assert!(is_package_manager_sync("apt update"));
        assert!(is_package_manager_sync("apk update"));
        assert!(is_package_manager_sync("yum check-update"));
        assert!(is_package_manager_sync("dnf makecache"));
        assert!(is_package_manager_sync("sudo apt-get update"));
        assert!(!is_package_manager_sync("apt-get install -y curl"));
        assert!(!is_package_manager_sync("echo hello"));
    }

    #[test]
    fn rejoin_emits_choco_install_for_install_subcommand() {
        let parts = vec![
            ShellSubcommand::Verbatim("echo before".to_string()),
            ShellSubcommand::PackageManagerSync,
            ShellSubcommand::Install {
                kind: DetectedPmKind::Apt,
                packages: vec!["curl".to_string(), "git".to_string()],
            },
            ShellSubcommand::Verbatim("echo after".to_string()),
        ];
        let out = rejoin_subcommands(&parts);
        assert_eq!(
            out,
            "echo before && choco install -y curl git && echo after"
        );
    }

    #[test]
    fn wrap_in_cmd_escapes_embedded_quotes() {
        let wrapped = wrap_in_cmd(r#"echo "hello""#);
        assert!(wrapped.starts_with("cmd /c \""));
        assert!(wrapped.contains(r#"\"hello\""#));
        assert!(wrapped.ends_with('"'));
    }

    #[test]
    fn translate_run_apt_to_choco_with_in_memory_shard() {
        // Build the shape the resolver writes to disk so the cache-hit
        // path runs without any network.
        let (_guard, _tmp, cache_root) = redirect_cache_dir();
        write_shard_fixture(
            &cache_root,
            "debian-12",
            "c",
            &[("curl", "curl"), ("linux-headers-generic", "__skip__")],
        );
        write_shard_fixture(
            &cache_root,
            "debian-12",
            "l",
            &[("linux-headers-generic", "__skip__")],
        );

        let translator = DockerfileTranslator::new(ImageOs::Windows);
        let (rewritten, skipped) = block_on(translator.translate_shell_command(
            "apt-get install -y curl linux-headers-generic",
            "debian-12",
            None,
        ))
        .expect("translate succeeds when every package resolves");
        assert!(
            rewritten.contains("choco install -y curl"),
            "rewritten command must include curl: {rewritten}"
        );
        assert!(
            !rewritten.contains("linux-headers-generic"),
            "skipped package must NOT appear in rewritten command: {rewritten}"
        );
        assert_eq!(skipped, vec!["linux-headers-generic".to_string()]);
    }

    // -----------------------------------------------------------------
    // Provisioned-toolchain skip behaviour (new in the move to
    // `DockerfileTranslator`).
    // -----------------------------------------------------------------

    #[test]
    fn translate_shell_command_skips_provisioned_toolchain() {
        // With a Go toolchain provisioned, an `apt-get install -y golang
        // git` must drop `golang` from the choco install and only emit
        // `git`. We seed the `g` shard with a `git → git` mapping so the
        // resolver hits without network.
        let (_guard, _tmp, cache_root) = redirect_cache_dir();
        write_shard_fixture(&cache_root, "debian-12", "g", &[("git", "git")]);

        let translator = DockerfileTranslator::new(ImageOs::Windows);
        let (rewritten, skipped) = block_on(translator.translate_shell_command(
            "apt-get install -y golang git",
            "debian-12",
            Some("go"),
        ))
        .expect("translate succeeds when remaining package resolves");
        assert!(
            !rewritten.contains("golang"),
            "provisioned-toolchain package must NOT appear in choco install: {rewritten}"
        );
        assert!(
            rewritten.contains("choco install -y git"),
            "non-toolchain package must still be installed: {rewritten}"
        );
        assert!(
            skipped.is_empty(),
            "toolchain drops are not reported as resolver-skipped: {skipped:?}"
        );
    }

    #[test]
    fn translate_shell_command_keeps_unrelated_pkg_with_toolchain() {
        // With a Go toolchain provisioned, an `apt-get install -y curl`
        // must still produce `choco install -y curl` — the toolchain
        // skip is only for packages that match the language.
        let (_guard, _tmp, cache_root) = redirect_cache_dir();
        write_shard_fixture(&cache_root, "debian-12", "c", &[("curl", "curl")]);

        let translator = DockerfileTranslator::new(ImageOs::Windows);
        let (rewritten, skipped) = block_on(translator.translate_shell_command(
            "apt-get install -y curl",
            "debian-12",
            Some("go"),
        ))
        .expect("translate succeeds");
        assert!(
            rewritten.contains("choco install -y curl"),
            "curl must still be installed: {rewritten}"
        );
        assert!(skipped.is_empty(), "no resolver-skipped: {skipped:?}");
    }

    #[test]
    fn translate_run_command_linux_is_passthrough() {
        // On Linux the translator must pass shell- and exec-form RUNs
        // through verbatim; no apt→choco rewriting happens because the
        // Linux backend hands the command straight to `/bin/sh -c`.
        let translator = DockerfileTranslator::new(ImageOs::Linux);
        let (shell_out, skipped) = block_on(translator.translate_run_command(
            &ShellOrExec::Shell("apt-get install -y curl".to_string()),
            "debian-12",
            None,
        ))
        .expect("Linux passthrough never fails");
        assert_eq!(shell_out, "apt-get install -y curl");
        assert!(skipped.is_empty());

        let (exec_out, skipped) = block_on(translator.translate_run_command(
            &ShellOrExec::Exec(vec!["echo".to_string(), "hi".to_string()]),
            "debian-12",
            None,
        ))
        .expect("Linux passthrough never fails");
        assert_eq!(exec_out, "echo hi");
        assert!(skipped.is_empty());
    }

    #[test]
    fn translate_run_command_windows_exec_is_passthrough() {
        // Exec-form on Windows joins with spaces and skips choco
        // detection — the caller has already chosen the absolute path
        // to the binary they want to invoke.
        let translator = DockerfileTranslator::new(ImageOs::Windows);
        let (out, skipped) = block_on(translator.translate_run_command(
            &ShellOrExec::Exec(vec![
                "C:\\app\\bin\\srv.exe".to_string(),
                "--port".to_string(),
                "80".to_string(),
            ]),
            "debian-12",
            None,
        ))
        .expect("exec-form passthrough never fails");
        assert_eq!(out, "C:\\app\\bin\\srv.exe --port 80");
        assert!(skipped.is_empty());
    }

    #[test]
    fn translate_shell_command_no_toolchain_installs_all() {
        // With no toolchain provisioned, `apt-get install -y golang git`
        // installs both packages via Chocolatey. `golang` resolves to
        // `golang` in the seeded shard (Chocolatey actually ships a
        // `golang` package, but the precise mapping is irrelevant here —
        // what matters is that it isn't dropped).
        let (_guard, _tmp, cache_root) = redirect_cache_dir();
        write_shard_fixture(
            &cache_root,
            "debian-12",
            "g",
            &[("golang", "golang"), ("git", "git")],
        );

        let translator = DockerfileTranslator::new(ImageOs::Windows);
        let (rewritten, skipped) = block_on(translator.translate_shell_command(
            "apt-get install -y golang git",
            "debian-12",
            None,
        ))
        .expect("translate succeeds");
        assert!(
            rewritten.contains("choco install -y golang git"),
            "both packages must be installed: {rewritten}"
        );
        assert!(skipped.is_empty(), "no resolver-skipped: {skipped:?}");
    }
}