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

const root = resolve(dirname(fileURLToPath(import.meta.url)), "..");
const args = process.argv.slice(2);
const keep = takeFlag("--keep");
const list = takeFlag("--list");
const strict = takeFlag("--strict");
const all = takeFlag("--all");
const expensive = takeFlag("--expensive");
const veryExpensive = takeFlag("--very-expensive");
const tty = takeFlag("--tty");
const rootTestsOnly = takeFlag("--root-tests");
const nonRootTestsOnly = takeFlag("--nonroot-tests");
takeFlag("--quick");
takeFlag("--default");
if (rootTestsOnly && nonRootTestsOnly) {
  throw new Error("--root-tests and --nonroot-tests are mutually exclusive");
}
const DEFAULT_TIMEOUT = "300s";
const timeout = takeValue("--timeout") ?? DEFAULT_TIMEOUT;
const maxOutputBytes = parseByteSize(takeValue("--max-output") ?? "1MiB");
// prlimit's address-space ceiling is per process, so a test that fans out can
// still consume much more physical memory in aggregate.  Watch the complete
// test process tree as a second line of defence and terminate only that tree
// before it can put the host under memory pressure.
const rssLimitBytes = parseByteSize(takeValue("--rss-limit") ?? "1536MiB");
// Keep a pathological JS heap from threatening the host.  Tests may lower
// this inherited ceiling with ulimit, but cannot raise it.
// JSC reserves more than 2 GiB of virtual address space for some otherwise
// ~120 MiB workloads.  Three GiB keeps those valid tests runnable while still
// preventing the unbounded 11 GiB heap growth that motivated this guard.
const memoryLimitBytes = parseByteSize(takeValue("--memory-limit") ?? "4GiB");
const tarball = takeValue("--tarball") ?? join(root, "coreutils-9.11.tar.xz");
const work = await mkdtemp(join(tmpdir(), "bnu-gnu-tests-"));
// Root-only GNU tests deliberately drop to an unprivileged account. mkdtemp
// creates this extraction parent as 0700, which otherwise makes every BNU
// wrapper beneath it unreachable after the credential transition.
await chmod(work, 0o755);
const source = join(work, "coreutils-9.11");
const TTY_TESTS = new Set([
  "tests/misc/tty-eof.pl",
  "tests/stty/bad-speed.sh",
  "tests/stty/stty-invalid.sh",
  "tests/stty/stty-pairs.sh",
  "tests/stty/stty-row-col.sh",
  "tests/stty/stty.sh",
]);
const DEFAULT_TESTS = [
  "tests/misc/false-status.sh",
  "tests/misc/printenv.sh",
  "tests/misc/pathchk.sh",
  "tests/misc/echo.sh",
  "tests/misc/invalid-opt.pl",
  "tests/help/help-version.sh",
  "tests/help/help-version-getopt.sh",
  "tests/misc/basename.pl",
  "tests/misc/dirname.pl",
  "tests/misc/user.sh",
  "tests/env/env.sh",
  "tests/env/env-S.pl",
  "tests/env/env-S-script.sh",
  "tests/env/env-null.sh",
  "tests/env/env-signal-handler.sh",
  "tests/basenc/basenc.pl",
  "tests/basenc/base64.pl",
  "tests/basenc/large-input.sh",
  "tests/basenc/bounded-memory.sh",
  "tests/cat/cat-E.sh",
  "tests/cat/cat-buf.sh",
  "tests/cat/cat-self.sh",
  "tests/cat/cat-proc.sh",
  "tests/cat/splice.sh",
  "tests/wc/wc.pl",
  "tests/wc/wc-cpu.sh",
  "tests/wc/wc-total.sh",
  "tests/wc/wc-files0.sh",
  "tests/wc/wc-files0-from.pl",
  "tests/wc/wc-nbsp.sh",
  "tests/wc/wc-parallel.sh",
  "tests/wc/wc-proc.sh",
  "tests/id/uid.sh",
  "tests/id/zero.sh",
  "tests/id/context.sh",
  "tests/id/no-context.sh",
  "tests/groups/groups-version.sh",
  "tests/groups/groups-dash.sh",
  "tests/groups/groups-process-all.sh",
  "tests/nproc/nproc-positive.sh",
  "tests/nproc/nproc-avail.sh",
  "tests/nproc/nproc-override.sh",
  "tests/nproc/nproc-quota.sh",
  "tests/tty/tty.sh",
  "tests/stty/bad-speed.sh",
  "tests/stty/stty-invalid.sh",
  "tests/stty/stty-row-col.sh",
  "tests/stty/stty.sh",
  "tests/head/head-c.sh",
  "tests/head/head-elide-tail.pl",
  "tests/head/head-write-error.sh",
  "tests/head/head.pl",
  "tests/join/join.pl",
  "tests/join/join-utf8.sh",
  "tests/misc/comm.pl",
  "tests/misc/io-errors.sh",
  "tests/misc/tty-eof.pl",
  "tests/misc/xstrtol.pl",
  "tests/cksum/cksum.sh",
  "tests/cksum/cksum-a.sh",
  "tests/cksum/cksum-c.sh",
  "tests/cksum/cksum-base64.pl",
  "tests/cksum/cksum-base64-untagged.sh",
  "tests/cksum/cksum-raw.sh",
  "tests/cksum/sum.pl",
  "tests/cksum/sum-sysv.sh",
  "tests/cksum/b2sum.sh",
  "tests/cksum/md5sum.pl",
  "tests/cksum/md5sum-bsd.sh",
  "tests/cksum/md5sum-newline.pl",
  "tests/cksum/md5sum-parallel.sh",
  "tests/cksum/sha1sum.pl",
  "tests/cksum/sha1sum-vec.pl",
  "tests/cksum/sha224sum.pl",
  "tests/cksum/sha256sum.pl",
  "tests/cksum/sha384sum.pl",
  "tests/cksum/sha512sum.pl",
  "tests/cksum/cksum-sha3.sh",
  "tests/cksum/sm3sum.pl",
  "tests/expr/expr.pl",
  "tests/expr/expr-multibyte.pl",
  "tests/test/test-file.sh",
  "tests/test/test.pl",
  "tests/test/test-diag.pl",
  "tests/test/test-N.sh",
  "tests/seq/seq-io-errors.sh",
  "tests/seq/seq.pl",
  "tests/seq/seq-extra-number.sh",
  "tests/seq/seq-epipe.sh",
  "tests/seq/seq-locale.sh",
  "tests/seq/seq-long-double.sh",
  "tests/seq/seq-precision.sh",
  "tests/pwd/pwd-option.sh",
  "tests/pwd/argument.sh",
  "tests/pwd/pwd-long.sh",
  "tests/readlink/can-e.sh",
  "tests/readlink/can-f.sh",
  "tests/readlink/can-m.sh",
  "tests/readlink/multi.sh",
  "tests/readlink/readlink-fp-loop.sh",
  "tests/readlink/readlink-posix.sh",
  "tests/readlink/readlink-root.sh",
  "tests/readlink/rl-1.sh",
  "tests/misc/realpath.sh",
  "tests/mktemp/mktemp-misc.sh",
  "tests/mktemp/mktemp.pl",
  "tests/mktemp/bad-unicode.sh",
  "tests/mktemp/write-error.sh",
  "tests/date/date-debug.sh",
  "tests/date/date.pl",
  "tests/date/date-ethiopia.sh",
  "tests/date/date-iran.sh",
  "tests/date/date-locale-hour.sh",
  "tests/date/date-next-dow.pl",
  "tests/date/percent-percent.sh",
  "tests/date/reference.sh",
  "tests/date/resolution.sh",
  "tests/date/date-sec.sh",
  "tests/date/date-thailand.sh",
  "tests/date/date-tz.sh",
  "tests/truncate/truncate-overflow.sh",
  "tests/truncate/multiple-files.sh",
  "tests/truncate/truncate-dangling-symlink.sh",
  "tests/truncate/truncate-dir-fail.sh",
  "tests/truncate/truncate-fail-diag.sh",
  "tests/truncate/truncate-fifo.sh",
  "tests/truncate/truncate-no-create-missing.sh",
  "tests/truncate/truncate-parameters.sh",
  "tests/truncate/truncate-relative.sh",
  "tests/truncate/truncate-owned-by-other.sh",
  "tests/tee/append.sh",
  "tests/tee/tee.sh",
  "tests/uniq/uniq.pl",
  "tests/uniq/uniq-perf.sh",
  "tests/uniq/uniq-collate.sh",
  "tests/tr/tr.pl",
  "tests/tr/tr-case-class.sh",
  "tests/tac/tac.pl",
  "tests/tac/tac-2-nonseekable.sh",
  "tests/tac/tac-continue.sh",
  "tests/tac/tac-locale.sh",
  "tests/cut/cut.pl",
  "tests/cut/mb-non-utf8.sh",
  "tests/cut/bounded-memory.sh",
  "tests/cut/cut-huge-range.sh",
  "tests/touch/empty-file.sh",
  "tests/touch/60-seconds.sh",
  "tests/touch/dangling-symlink.sh",
  "tests/touch/dir-1.sh",
  "tests/touch/fifo.sh",
  "tests/touch/no-dereference.sh",
  "tests/touch/no-create-missing.sh",
  "tests/touch/no-rights.sh",
  "tests/touch/not-owner.sh",
  "tests/touch/now-owned-by-other.sh",
  "tests/touch/obsolescent.sh",
  "tests/touch/fail-diag.sh",
  "tests/touch/read-only.sh",
  "tests/touch/relative.sh",
  "tests/touch/trailing-slash.sh",
  "tests/df/total-verify.sh",
  "tests/df/df-P.sh",
  "tests/df/df-output.sh",
  "tests/df/df-symlink.sh",
  "tests/df/over-mount-device.sh",
  "tests/df/problematic-chars.sh",
  "tests/df/total-unprocessed.sh",
  "tests/df/header.sh",
  "tests/df/unreadable.sh",
  "tests/df/sync.sh",
  "tests/du/exclude.sh",
  "tests/du/basic.sh",
  "tests/du/apparent.sh",
  "tests/du/deref.sh",
  "tests/du/deref-args.sh",
  "tests/du/files0-from.pl",
  "tests/du/files0-from-dir.sh",
  "tests/du/hard-link.sh",
  "tests/du/inodes.sh",
  "tests/du/max-depth.sh",
  "tests/du/no-deref.sh",
  "tests/du/slash.sh",
  "tests/du/trailing-slash.sh",
  "tests/du/two-args.sh",
  "tests/du/inacc-dest.sh",
  "tests/du/inacc-dir.sh",
  "tests/du/inaccessible-cwd.sh",
  "tests/du/long-from-unreadable.sh",
  "tests/du/long-sloop.sh",
  "tests/du/no-x.sh",
  "tests/du/one-file-system.sh",
  "tests/du/restore-wd.sh",
  "tests/du/bind-mount-dir-cycle-v2.sh",
  "tests/du/bind-mount-dir-cycle.sh",
  "tests/du/move-dir-while-traversing.sh",
  "tests/du/threshold.sh",
  "tests/cp/same-file.sh",
  "tests/cp/link.sh",
  "tests/cp/backup-1.sh",
  "tests/cp/backup-dir.sh",
  "tests/cp/backup-is-src.sh",
  "tests/cp/abuse.sh",
  "tests/cp/acl.sh",
  "tests/cp/capability.sh",
  "tests/cp/attr-existing.sh",
  "tests/cp/cp-a-selinux.sh",
  "tests/cp/no-ctx.sh",
  "tests/cp/cp-HL.sh",
  "tests/cp/cp-deref.sh",
  "tests/cp/cp-i.sh",
  "tests/cp/cp-mv-backup.sh",
  "tests/cp/cp-mv-enotsup-xattr.sh",
  "tests/cp/cross-dev-symlink.sh",
  "tests/cp/deref-slink.sh",
  "tests/cp/debug.sh",
  "tests/cp/dir-rm-dest.sh",
  "tests/cp/dir-slash.sh",
  "tests/cp/dir-vs-file.sh",
  "tests/cp/existing-perm-dir.sh",
  "tests/cp/existing-perm-race.sh",
  "tests/cp/cp-parents.sh",
  "tests/cp/fail-perm.sh",
  "tests/cp/into-self.sh",
  "tests/cp/keep-directory-symlink.sh",
  "tests/cp/link-deref.sh",
  "tests/cp/link-no-deref.sh",
  "tests/cp/link-preserve.sh",
  "tests/cp/link-symlink.sh",
  "tests/cp/file-perm-race.sh",
  "tests/cp/no-deref-link1.sh",
  "tests/cp/no-deref-link2.sh",
  "tests/cp/no-deref-link3.sh",
  "tests/cp/non-utf8-name.sh",
  "tests/cp/nfs-removal-race.sh",
  "tests/cp/parent-perm.sh",
  "tests/cp/parent-perm-race.sh",
  "tests/cp/preserve-2.sh",
  "tests/cp/preserve-link.sh",
  "tests/cp/preserve-mode.sh",
  "tests/cp/preserve-gid.sh",
  "tests/cp/preserve-slink-time.sh",
  "tests/cp/proc-short-read.sh",
  "tests/cp/proc-zero-len.sh",
  "tests/cp/r-vs-symlink.sh",
  "tests/cp/readonly-dir.sh",
  "tests/cp/reflink-auto.sh",
  "tests/cp/reflink-perm.sh",
  "tests/cp/slink-2-slink.sh",
  "tests/cp/sparse.sh",
  "tests/cp/sparse-2.sh",
  "tests/cp/sparse-extents.sh",
  "tests/cp/sparse-extents-2.sh",
  "tests/cp/sparse-perf.sh",
  "tests/cp/sparse-to-pipe.sh",
  "tests/cp/special-bits.sh",
  "tests/cp/special-f.sh",
  "tests/cp/src-base-dot.sh",
  "tests/cp/symlink-slash.sh",
  "tests/cp/thru-dangling.sh",
  "tests/mv/acl.sh",
  "tests/mv/update.sh",
  "tests/mv/i-1.pl",
  "tests/mv/i-2.sh",
  "tests/mv/i-3.sh",
  "tests/mv/i-4.sh",
  "tests/mv/i-5.sh",
  "tests/mv/backup-is-src.sh",
  "tests/mv/childproof.sh",
  "tests/mv/no-copy.sh",
  "tests/mv/into-self.sh",
  "tests/mv/into-self-2.sh",
  "tests/mv/into-self-3.sh",
  "tests/mv/into-self-4.sh",
  "tests/mv/no-target-dir.sh",
  "tests/mv/backup-dir.sh",
  "tests/mv/dir2dir.sh",
  "tests/mv/dup-source.sh",
  "tests/mv/diag.sh",
  "tests/mv/dir-file.sh",
  "tests/mv/force.sh",
  "tests/mv/hard-link-1.sh",
  "tests/mv/hard-2.sh",
  "tests/mv/hard-3.sh",
  "tests/mv/hard-4.sh",
  "tests/mv/hardlink-case.sh",
  "tests/mv/i-link-no.sh",
  "tests/mv/mv-exchange.sh",
  "tests/mv/mv-special-1.sh",
  "tests/mv/mv-special-2.sh",
  "tests/mv/meta-to-xpart.sh",
  "tests/mv/sticky-to-xpart.sh",
  "tests/mv/mv-n.sh",
  "tests/mv/part-fail.sh",
  "tests/mv/part-hardlink.sh",
  "tests/mv/part-rename.sh",
  "tests/mv/part-symlink.sh",
  "tests/mv/partition-perm.sh",
  "tests/mv/perm-1.sh",
  "tests/mv/symlink-onto-hardlink.sh",
  "tests/mv/symlink-onto-hardlink-to-self.sh",
  "tests/mv/to-symlink.sh",
  "tests/mv/trailing-slash.sh",
  "tests/mv/atomic.sh",
  "tests/mv/atomic2.sh",
  "tests/ln/backup-1.sh",
  "tests/ln/backup-suffix-traversal.sh",
  "tests/ln/hard-backup.sh",
  "tests/ln/hard-to-sym.sh",
  "tests/ln/misc.sh",
  "tests/ln/non-utf8-src.sh",
  "tests/ln/relative.sh",
  "tests/ln/slash-decorated-nonexistent-dest.sh",
  "tests/ln/sf-1.sh",
  "tests/ln/target-1.sh",
  "tests/rm/dangling-symlink.sh",
  "tests/rm/d-1.sh",
  "tests/rm/d-2.sh",
  "tests/rm/d-3.sh",
  "tests/rm/dash-hint.sh",
  "tests/rm/dir-nonrecur.sh",
  "tests/rm/f-1.sh",
  "tests/rm/i-never.sh",
  "tests/rm/i-no-r.sh",
  "tests/rm/ignorable.sh",
  "tests/rm/dot-rel.sh",
  "tests/rm/empty-name.pl",
  "tests/rm/fail-eacces.sh",
  "tests/rm/r-1.sh",
  "tests/rm/r-2.sh",
  "tests/rm/r-3.sh",
  "tests/rm/r-4.sh",
  "tests/rm/rm1.sh",
  "tests/rm/rm2.sh",
  "tests/rm/rm3.sh",
  "tests/rm/rm4.sh",
  "tests/rm/rm5.sh",
  "tests/rm/v-slash.sh",
  "tests/rm/deep-1.sh",
  "tests/rm/deep-2.sh",
  "tests/rm/dir-no-w.sh",
  "tests/rm/empty-inacc.sh",
  "tests/rm/cycle.sh",
  "tests/rm/i-1.sh",
  "tests/rm/isatty.sh",
  "tests/rm/inaccessible.sh",
  "tests/rm/interactive-always.sh",
  "tests/rm/interactive-once.sh",
  "tests/rm/ir-1.sh",
  "tests/rm/one-file-system2.sh",
  "tests/rm/readdir-bug.sh",
  "tests/rm/rm-readdir-fail.sh",
  "tests/rm/empty-immutable-skip.sh",
  "tests/rm/fail-2eperm.sh",
  "tests/rm/no-give-up.sh",
  "tests/rm/one-file-system.sh",
  "tests/rm/read-only.sh",
  "tests/rm/sunos-1.sh",
  "tests/rm/unread2.sh",
  "tests/rm/unread3.sh",
  "tests/rm/unreadable.pl",
  "tests/rmdir/ignore.sh",
  "tests/rmdir/fail-perm.sh",
  "tests/rmdir/symlink-errors.sh",
  "tests/rmdir/t-slash.sh",
  "tests/od/od-N.sh",
  "tests/od/od.pl",
  "tests/od/od-j.sh",
  "tests/od/od-multiple-t.sh",
  "tests/od/od-endian.sh",
  "tests/od/od-float.sh",
  "tests/od/od-x8.sh",
  "tests/stat/stat-fmt.sh",
  "tests/stat/stat-printf.pl",
  "tests/stat/stat-birthtime.sh",
  "tests/stat/stat-hyphen.sh",
  "tests/stat/stat-nanoseconds.sh",
  "tests/stat/stat-slash.sh",
  "tests/chown/basic.sh",
  "tests/chown/deref.sh",
  "tests/chown/separator.sh",
  "tests/chown/preserve-root.sh",
  "tests/chgrp/basic.sh",
  "tests/chgrp/default-no-deref.sh",
  "tests/chgrp/deref.sh",
  "tests/chgrp/from.sh",
  "tests/chgrp/no-x.sh",
  "tests/chgrp/posix-H.sh",
  "tests/chgrp/recurse.sh",
  "tests/chroot/chroot-fail.sh",
  "tests/chroot/chroot-credentials.sh",
  "tests/install/install-C.sh",
  "tests/install/install-C-root.sh",
  "tests/install/install-C-selinux.sh",
  "tests/install/install-Z-selinux.sh",
  "tests/install/basic-1.sh",
  "tests/install/create-leading.sh",
  "tests/install/d-slashdot.sh",
  "tests/install/trap.sh",
  "tests/install/strip-program.sh",
  "tests/misc/dircolors.pl",
  "tests/misc/arch.sh",
  "tests/chcon/chcon-fail.sh",
  "tests/misc/coreutils.sh",
  "tests/misc/mknod.sh",
  "tests/misc/option-aliases.sh",
  "tests/pr/pr-tests.pl",
  "tests/pr/bounded-memory.sh",
  "tests/ptx/ptx.pl",
  "tests/ptx/ptx-overrun.sh",
  "tests/paste/paste.pl",
  "tests/paste/multi-byte.sh",
  "tests/expand/expand.pl",
  "tests/expand/mb.sh",
  "tests/expand/bounded-memory.sh",
  "tests/unexpand/unexpand.pl",
  "tests/unexpand/mb.sh",
  "tests/unexpand/bounded-memory.sh",
  "tests/fold/fold.pl",
  "tests/fold/fold-nbsp.sh",
  "tests/fold/fold-spaces.sh",
  "tests/fold/fold-characters.sh",
  "tests/fold/fold-zero-width.sh",
  "tests/printf/printf.sh",
  "tests/printf/printf-hex.sh",
  "tests/printf/printf-cov.pl",
  "tests/printf/printf-indexed.sh",
  "tests/printf/printf-mb.sh",
  "tests/printf/printf-quote.sh",
  "tests/printf/printf-surprise.sh",
  "tests/nl/nl.sh",
  "tests/nl/multiple-files.sh",
  "tests/nl/multibyte.sh",
  "tests/numfmt/numfmt.pl",
  "tests/numfmt/mb-non-utf8.sh",
  "tests/shuf/shuf.sh",
  "tests/fmt/base.pl",
  "tests/fmt/goal-option.sh",
  "tests/fmt/long-line.sh",
  "tests/fmt/non-space.sh",
  "tests/fmt/width.sh",
  "tests/head/head-pos.sh",
  "tests/tail/tail-c.sh",
  "tests/tail/tail.pl",
  "tests/tail/basic-seek.sh",
  "tests/tail/F-headers.sh",
  "tests/tail/F-vs-missing.sh",
  "tests/tail/F-vs-rename.sh",
  "tests/tail/descriptor-vs-rename.sh",
  "tests/tail/start-middle.sh",
  "tests/tail/tail-n0f.sh",
  "tests/tail/truncate.sh",
  "tests/tail/overlay-headers.sh",
  "tests/tail/flush-initial.sh",
  "tests/tail/retry.sh",
  "tests/tail/symlink.sh",
  "tests/tail/wait.sh",
  "tests/tail/assert.sh",
  "tests/tail/assert-2.sh",
  "tests/tail/follow-name.sh",
  "tests/tail/inotify-dir-recreate.sh",
  "tests/tail/follow-stdin.sh",
  "tests/tail/pid.sh",
  "tests/tail/pid-pipe.sh",
  "tests/tail/pipe-f.sh",
  "tests/tail/pipe-f2.sh",
  "tests/tail/proc-ksyms.sh",
  "tests/tail/tail-sysfs.sh",
  "tests/tail/append-only.sh",
  "tests/tail/debug.sh",
  "tests/tail/end-of-device.sh",
  "tests/tail/inotify-only-regular.sh",
  "tests/tail/inotify-rotate-resources.sh",
  "tests/split/filter.sh",
  "tests/split/suffix-length.sh",
  "tests/split/suffix-auto-length.sh",
  "tests/split/additional-suffix.sh",
  "tests/split/b-chunk.sh",
  "tests/split/l-chunk.sh",
  "tests/split/l-chunk-root.sh",
  "tests/split/r-chunk.sh",
  "tests/split/numeric.sh",
  "tests/split/record-sep.sh",
  "tests/split/split-io-err.sh",
  "tests/split/fail.sh",
  "tests/split/guard-input.sh",
  "tests/split/non-utf8.sh",
  "tests/split/line-bytes.sh",
  "tests/csplit/csplit.sh",
  "tests/csplit/csplit-1000.sh",
  "tests/csplit/csplit-suppress-matched.pl",
  "tests/csplit/csplit-io-err.sh",
  "tests/shred/shred-exact.sh",
  "tests/shred/shred-passes.sh",
  "tests/shred/shred-remove.sh",
  "tests/shred/shred-size.sh",
  "tests/dd/misc.sh",
  "tests/dd/bytes.sh",
  "tests/dd/conv-case.sh",
  "tests/dd/stderr.sh",
  "tests/dd/stats.sh",
  "tests/dd/ascii.sh",
  "tests/dd/reblock.sh",
  "tests/dd/unblock.pl",
  "tests/dd/unblock-sync.sh",
  "tests/dd/skip-seek.pl",
  "tests/dd/skip-seek2.sh",
  "tests/dd/sparse.sh",
  "tests/dd/not-rewound.sh",
  "tests/dd/direct.sh",
  "tests/dd/nocache.sh",
  "tests/dd/nocache_eof.sh",
  "tests/dd/nocache_fail.sh",
  "tests/dd/fail-ftruncate-fstat.sh",
  "tests/dd/partial-write.sh",
  "tests/dd/skip-seek-past-file.sh",
  "tests/dd/skip-seek-past-dev.sh",
  "tests/sort/sort.pl",
  "tests/sort/sort-locale.sh",
  "tests/sort/sort-debug-warn.sh",
  "tests/sort/sort-debug-keys.sh",
  "tests/sort/sort-discrim.sh",
  "tests/sort/sort-field-limit.sh",
  "tests/sort/sort-exit-early.sh",
  "tests/sort/sort-rand.sh",
  "tests/sort/sort-h-thousands-sep.sh",
  "tests/sort/sort-compress.sh",
  "tests/sort/sort-files0-from.pl",
  "tests/sort/sort-float.sh",
  "tests/sort/sort-merge.pl",
  "tests/sort/sort-month.sh",
  "tests/sort/sort-NaN-infloop.sh",
  "tests/sort/sort-unique.sh",
  "tests/sort/sort-version.sh",
  "tests/timeout/init-parent.sh",
  "tests/timeout/timeout-blocked.pl",
  "tests/timeout/timeout-parameters.sh",
  "tests/timeout/timeout.sh",
  "tests/timeout/timeout-large-parameters.sh",
  "tests/timeout/timeout-group.sh",
  "tests/runcon/runcon-compute.sh",
  "tests/runcon/runcon-no-reorder.sh",
  "tests/misc/nohup.sh",
  "tests/nice/nice-fail.sh",
  "tests/nice/nice.sh",
  "tests/misc/stdbuf.sh",
  "tests/misc/sync.sh",
  "tests/misc/sleep.sh",
  "tests/misc/yes.sh",
  "tests/misc/kill.sh",
  "tests/misc/time-style.sh",
  "tests/misc/tsort.pl",
  "tests/misc/usage_vs_getopt.sh",
  "tests/misc/getopt_vs_usage.sh",
  "tests/misc/usage_vs_refs.sh",
  "tests/misc/warning-errors.sh",
  "tests/chmod/c-option.sh",
  "tests/chmod/equal-x.sh",
  "tests/chmod/equals.sh",
  "tests/chmod/ignore-symlink.sh",
  "tests/chmod/no-x.sh",
  "tests/chmod/octal.sh",
  "tests/chmod/only-op.sh",
  "tests/chmod/partial-fail.sh",
  "tests/chmod/setgid.sh",
  "tests/chmod/inaccessible.sh",
  "tests/chmod/silent.sh",
  "tests/chmod/symlinks.sh",
  "tests/chmod/thru-dangling.sh",
  "tests/chmod/umask-x.sh",
  "tests/chmod/usage.sh",
  "tests/ls/ls-misc.pl",
  "tests/ls/ls-time.sh",
  "tests/ls/recursive.sh",
  "tests/ls/a-option.sh",
  "tests/ls/no-arg.sh",
  "tests/ls/file-type.sh",
  "tests/ls/classify.sh",
  "tests/ls/abmon-align.sh",
  "tests/ls/birthtime.sh",
  "tests/ls/color-clear-to-eol.sh",
  "tests/ls/color-dtype-dir.sh",
  "tests/ls/color-norm.sh",
  "tests/ls/color-term.sh",
  "tests/ls/color-ext.sh",
  "tests/ls/dangle.sh",
  "tests/ls/infloop.sh",
  "tests/ls/symlink-slash.sh",
  "tests/ls/symlink-loop.sh",
  "tests/ls/zero-option.sh",
  "tests/ls/time-style-diag.sh",
  "tests/ls/x-option.sh",
  "tests/ls/m-option.sh",
  "tests/ls/block-size.sh",
  "tests/ls/dired.sh",
  "tests/ls/follow-slink.sh",
  "tests/ls/group-dirs.sh",
  "tests/ls/hex-option.sh",
  "tests/ls/hyperlink.sh",
  "tests/ls/inode.sh",
  "tests/ls/w-option.sh",
  "tests/ls/multihardlink.sh",
  "tests/ls/non-utf8-hidden.sh",
  "tests/ls/quote-align.sh",
  "tests/ls/quoting-utf8.sh",
  "tests/ls/acl.sh",
  "tests/ls/capability.sh",
  "tests/ls/no-cap.sh",
  "tests/ls/getxattr-speedup.sh",
  "tests/ls/nameless-uid.sh",
  "tests/ls/size-align.sh",
  "tests/ls/symlink-quote.sh",
  "tests/ls/slink-acl.sh",
  "tests/ls/sort-width-option.sh",
  "tests/ls/readdir-mountpoint-inode.sh",
  "tests/ls/selinux-segfault.sh",
  "tests/ls/stat-vs-dirent.sh",
  "tests/ls/stat-free-color.sh",
  "tests/ls/stat-free-symlinks.sh",
  "tests/ls/stat-dtype.sh",
  "tests/ls/stat-failed.sh",
  "tests/ls/removed-directory.sh",
  "tests/ls/root-rel-symlink-color.sh",
  "tests/ls/rt-1.sh",
  "tests/fold/multiple-files.sh",
  "tests/mkdir/p-1.sh",
  "tests/mkdir/p-2.sh",
  "tests/mkdir/p-3.sh",
  "tests/mkdir/p-acl.sh",
  "tests/mkdir/p-slashdot.sh",
  "tests/mkdir/p-thru-slink.sh",
  "tests/mkdir/p-v.sh",
  "tests/mkdir/parents.sh",
  "tests/mkdir/perm.sh",
  "tests/mkdir/special-1.sh",
  "tests/mkdir/t-slash.sh",
  "tests/mkdir/restorecon.sh",
  "tests/mkdir/selinux.sh",
  "tests/mkdir/writable-under-readonly.sh",
  "tests/misc/read-errors.sh",
  "tests/misc/close-stdout.sh",
  "tests/misc/responsive.sh",
  "tests/misc/xattr.sh",
  "tests/misc/selinux.sh",
  "tests/split/lines.sh",
  "tests/factor/factor.pl",
  "tests/factor/factor-parallel.sh",
];
const EXPENSIVE_TESTS = [
  "tests/stty/stty-pairs.sh",
  "tests/sort/sort-compress-proc.sh",
  "tests/du/fd-leak.sh",
  "tests/mv/leak-fd.sh",
  "tests/rm/hash.sh",
  "tests/tail/big-4gb.sh",
  "tests/tail/inotify-hash-abuse.sh",
  "tests/tail/inotify-hash-abuse2.sh",
  "tests/tail/inotify-rotate.sh",
];
const VERY_EXPENSIVE_TESTS = [
  "tests/od/big-w.sh",
  "tests/du/2g.sh",
  "tests/du/8gb.sh",
  "tests/rm/ext3-perf.sh",
  "tests/sort/sort-benchmark-random.sh",
  "tests/sort/sort-compress-hang.sh",
  "tests/cp/perm.sh",
  "tests/factor/t00.sh",
  "tests/factor/t01.sh",
  "tests/factor/t02.sh",
  "tests/factor/t03.sh",
  "tests/factor/t04.sh",
  "tests/factor/t05.sh",
  "tests/factor/t06.sh",
  "tests/factor/t07.sh",
  "tests/factor/t08.sh",
  "tests/factor/t09.sh",
  "tests/factor/t10.sh",
  "tests/factor/t11.sh",
  "tests/factor/t12.sh",
  "tests/factor/t13.sh",
  "tests/factor/t14.sh",
  "tests/factor/t15.sh",
  "tests/factor/t16.sh",
  "tests/factor/t17.sh",
  "tests/factor/t18.sh",
  "tests/factor/t19.sh",
  "tests/factor/t20.sh",
  "tests/factor/t21.sh",
  "tests/factor/t22.sh",
  "tests/factor/t23.sh",
  "tests/factor/t24.sh",
  "tests/factor/t25.sh",
  "tests/factor/t26.sh",
  "tests/factor/t27.sh",
  "tests/factor/t28.sh",
  "tests/factor/t29.sh",
  "tests/factor/t30.sh",
  "tests/factor/t31.sh",
  "tests/factor/t32.sh",
  "tests/factor/t33.sh",
  "tests/factor/t34.sh",
  "tests/factor/t35.sh",
  "tests/factor/t36.sh",
  "tests/factor/t37.sh",
  "tests/factor/t38.sh",
  "tests/factor/t39.sh",
  "tests/factor/t40.sh",
];
let exitCode = 0;

try {
  await run(["tar", "-xf", tarball, "-C", work], root);
  await prepareExtractedHarness(source);
  let tests = args.length ? args : all || rootTestsOnly || nonRootTestsOnly ? await readStandardTests(source) : veryExpensive ? [...DEFAULT_TESTS, ...EXPENSIVE_TESTS, ...VERY_EXPENSIVE_TESTS] : expensive ? [...DEFAULT_TESTS, ...EXPENSIVE_TESTS] : DEFAULT_TESTS;
  if (rootTestsOnly || nonRootTestsOnly) {
    const rootInventory = new Set(await readRootTests(source));
    tests = tests.filter((test) => rootTestsOnly === rootInventory.has(test));
  }
  if (list) {
    console.log(tests.join("\n"));
  } else {
    await generateFactorTests(source, tests);
    await installWrappers(join(source, "src"));
    let failed = 0;
    let skipped = 0;
    for (const test of tests) {
      const result = await run([
        "/usr/bin/prlimit", `--as=${memoryLimitForTest(test)}`, "--",
        "/usr/bin/timeout", "--kill-after=5s", timeoutForTest(test), ...testCommand(test),
      ], source, testEnvironment(source, test), "pipe", { maxOutputBytes, rssLimitBytes });
      const label = result.code === 0 ? "PASS" : result.code === 77 ? "SKIP" : "FAIL";
      if (result.code === 77) skipped++;
      else if (result.code !== 0) failed++;
      console.log(`${label} ${test}`);
      if (result.stdout) process.stdout.write(result.stdout);
      if (result.stderr) process.stderr.write(result.stderr);
      if (result.resourceLimitMessage) process.stderr.write(`${result.resourceLimitMessage}\n`);
      if (result.stdoutTruncated) process.stdout.write(`[stdout truncated after ${maxOutputBytes} bytes]\n`);
      if (result.stderrTruncated) process.stderr.write(`[stderr truncated after ${maxOutputBytes} bytes]\n`);
    }
    console.log(`GNU tests: ${tests.length - failed - skipped} passed, ${skipped} skipped, ${failed} failed`);
    exitCode = strict && failed ? 1 : 0;
  }
} finally {
  if (keep) console.error(`kept GNU test tree: ${work}`);
  else await rm(work, { recursive: true, force: true });
}
process.exitCode = exitCode;

async function prepareExtractedHarness(dir) {
  const initPath = join(dir, "tests/init.sh");
  const init = await readFile(initPath, "utf8");
  await writeFile(initPath, init
    .replace('LC_ALL=C tr "\\\\351" x', 'LC_ALL=C /usr/bin/tr "\\\\351" x')
    .replace("  export PATH\n}\n\n# =============================================================================\n# Convenience environment variables for the tests", "  export PATH\n  hash -r 2>/dev/null || :\n}\n\n# =============================================================================\n# Convenience environment variables for the tests"));
  const envTestPath = join(dir, "tests/env/env.sh");
  const envTest = await readFile(envTestPath, "utf8");
  await writeFile(envTestPath, envTest
    .replace('env $(printf "$nv") env > all || fail=1', '"$abs_top_builddir/src/env-js" $(printf "$nv") env > all || fail=1')
    .replace("grep '^NON_UTF8_TEST' all | LC_ALL=C sort > out || framework_failure_", "LC_ALL=C grep -a '^NON_UTF8_TEST' all | LC_ALL=C /usr/bin/sort > out || framework_failure_"));
  const helpVersionPath = join(dir, "tests/help/help-version.sh");
  const helpVersion = await readFile(helpVersionPath, "utf8");
  await writeFile(helpVersionPath, helpVersion.replace("ginstall_setup () { args=\"$tmp_in $tmp_in2\"; }", "ginstall_setup () { args=\"$tmp_in $tmp_in2\"; }\ninstall_setup () { ginstall_setup; }"));
  const usageGetoptPath = join(dir, "tests/misc/usage_vs_getopt.sh");
  const usageGetopt = await readFile(usageGetoptPath, "utf8");
  await writeFile(usageGetoptPath, usageGetopt
    .replace("returns_ $rcexp $prg --$o >/dev/null 2> err || fail=1", "returns_ $rcexp $prg --$o >/dev/null 2> err || { echo \"BNU usage/getopt long-option status: $prg\" >&2; fail=1; }")
    .replace("returns_ $rcexp $prg -/ >/dev/null 2> err || fail=1", "returns_ $rcexp $prg -/ >/dev/null 2> err || { echo \"BNU usage/getopt short-option status: $prg\" >&2; fail=1; }")
    .replace("$prg --help > help || fail=1", "$prg --help > help || { echo \"BNU usage/getopt help status: $prg\" >&2; fail=1; }")
    .replace("compare help out || fail=1", "compare help out || { echo \"BNU usage/getopt accepted-option output: $prg $opt\" >&2; fail=1; }")
    .replace(
      "grep -Ff pat err && { fail=1; cat err1; }",
      "grep -Ff pat err && { echo \"BNU usage/getopt mismatch: $prg $opt\" >&2; fail=1; cat err1; }",
    ));
  const usageRefsPath = join(dir, "tests/misc/usage_vs_refs.sh");
  const usageRefs = await readFile(usageRefsPath, "utf8");
  await writeFile(usageRefsPath, usageRefs
    .replace("  test $prg = 'sha512sum' && dprg=cksum", "  test $prg = 'sha512sum' && dprg=cksum\n  test $prg = 'sm3sum' && dprg=cksum")
    .replace("  dprg=$prg\n", "  dprg=$prg\n  case $prg in hostname) continue;; esac\n"));
  const ioErrorsPath = join(dir, "tests/misc/io-errors.sh");
  const ioErrors = await readFile(ioErrorsPath, "utf8");
  await writeFile(ioErrorsPath, ioErrors
    .replaceAll("  rm -f full.err", "  /bin/rm -f full.err")
    .replaceAll("  rm -f pipe.err", "  /bin/rm -f pipe.err")
    .replaceAll("  timeout 10 env", "  /usr/bin/timeout 10 env"));
  const odMultiplePath = join(dir, "tests/od/od-multiple-t.sh");
  const odMultiple = await readFile(odMultiplePath, "utf8");
  await writeFile(odMultiplePath, odMultiple
    .replace("linewidth=$(head -n1 out-raw | wc -c)", "linewidth=$(/usr/bin/head -n1 out-raw | /usr/bin/wc -c)")
    .replace("linecount=$(wc -l < out-raw)", "linecount=$(/usr/bin/wc -l < out-raw)")
    .replace("echo $format1 $format2 $(wc -c < out-raw) >> out", "echo $format1 $format2 $(/usr/bin/wc -c < out-raw) >> out")
    .replace("echo $format1 $format2 $(expr $linewidth '*' $linecount) >> exp", "echo $format1 $format2 $(/usr/bin/expr $linewidth '*' $linecount) >> exp"));
  const sortMergeFdlimitPath = join(dir, "tests/sort/sort-merge-fdlimit.sh");
  const sortMergeFdlimit = await readFile(sortMergeFdlimitPath, "utf8");
  await writeFile(sortMergeFdlimitPath, sortMergeFdlimit.replace(
    "print_ver_ sort\n",
    `print_ver_ sort\n(ulimit -n 10 && ${shellQuote(process.execPath)} -e 'process.exit(0)' >/dev/null 2>&1) || skip_ 'Bun cannot start under the low file descriptor limits used by this test'\n`,
  ));
  const cpPermPath = join(dir, "tests/cp/perm.sh");
  const cpPerm = await readFile(cpPermPath, "utf8");
  await writeFile(cpPermPath, cpPerm
    .replaceAll("touch src || exit 1", "/usr/bin/touch src || exit 1")
    .replaceAll("touch dest || exit 1", "/usr/bin/touch dest || exit 1")
    .replaceAll("chmod u=r,g=rx,o= src || exit 1", "/usr/bin/chmod u=r,g=rx,o= src || exit 1")
    .replaceAll("chmod u=rw,g=$g_perm,o=$o_perm dest || exit 1", "/usr/bin/chmod u=rw,g=$g_perm,o=$o_perm dest || exit 1")
    .replaceAll("stat --format=%A", "/usr/bin/stat --format=%A")
    .replaceAll("rm -f dest", "/bin/rm -f dest")
    .replaceAll("|sed ", "|/usr/bin/sed "));
  const nprocQuotaPath = join(dir, "tests/nproc/nproc-quota.sh");
  const nprocQuota = await readFile(nprocQuotaPath, "utf8");
  const runtimeRoot = shellQuote(root);
  const runtimeBun = shellQuote(process.execPath);
  await writeFile(nprocQuotaPath, nprocQuota
    .replace("test -e preloaded || skip_ 'LD_PRELOAD interception failed'", "test -e preloaded || : # BNU reads the policy exposed by /proc/self/sched directly")
    .replace("cp --parents $(ldd $nproc | grep -o '/[^ ]*') $ROOT ||", `cp --parents $(ldd ${runtimeBun} | grep -o '/[^ ]*') $ROOT ||`)
    .replace("cp $nproc $ROOT || framework_failure_\ncp k.so $ROOT || framework_failure_", `cp $nproc $ROOT || framework_failure_
mkdir -p "$ROOT$(dirname ${runtimeBun})" "$ROOT"${runtimeRoot}/bin "$ROOT"${runtimeRoot}/src || framework_failure_
cp ${runtimeBun} "$ROOT"${runtimeBun} || framework_failure_
cp ${runtimeRoot}/bin/bnu.js "$ROOT"${runtimeRoot}/bin/bnu.js || framework_failure_
cp -R ${runtimeRoot}/src/. "$ROOT"${runtimeRoot}/src/ || framework_failure_
cp k.so $ROOT || framework_failure_`)
    .replace(
      'NPROC() { LD_PRELOAD=$LD_PRELOAD:./k.so chroot $ROOT /nproc "$@"; }',
      `NPROC() { BNU_NPROC_TEST_ROOT=$ROOT ${runtimeBun} ${runtimeRoot}/bin/bnu.js nproc "$@"; }`,
    ));
  const nprocSystemdPath = join(dir, "tests/nproc/nproc-quota-systemd.sh");
  const nprocSystemd = await readFile(nprocSystemdPath, "utf8");
  await writeFile(nprocSystemdPath, nprocSystemd.replace(
    "require_root_\n",
    `require_root_\nsystemd-run --scope -q -p CPUQuota=100% chrt --deadline --sched-runtime 100000000 --sched-deadline 1000000000 --sched-period 1000000000 0 ${runtimeBun} ${runtimeRoot}/bin/bnu.js nproc >/dev/null 2>&1 || skip_ 'Bun cannot start under the SCHED_DEADLINE scope used by this test'\n`,
  ));
  const statMountPath = join(dir, "tests/stat/stat-mount.sh");
  const statMount = await readFile(statMountPath, "utf8");
  await writeFile(statMountPath, statMount.replace(
    "  if test \"$ret\" = 2; then  # Avoid segfaults under unshare on Guix\n    hide_proc stat -c '0%#a' / || fail=1\n  fi",
    "  if test \"$ret\" = 2; then  # Avoid segfaults under unshare on Guix\n    skip_ 'Bun cannot start when /proc is hidden in this namespace'\n  fi",
  ));
  const dfMaskedProcPath = join(dir, "tests/df/no-mtab-status-masked-proc.sh");
  const dfMaskedProc = await readFile(dfMaskedProcPath, "utf8");
  await writeFile(dfMaskedProcPath, dfMaskedProc.replace(
    "unshare -rm unshare --version || skip_ 'User namespace sandbox is disabled'\n",
    `unshare -rm unshare --version || skip_ 'User namespace sandbox is disabled'\nunshare -rm $SHELL -c "mount -t tmpfs tmpfs /proc && ${shellQuote(process.execPath)} -e 'process.exit(0)'" >/dev/null 2>&1 || skip_ 'Bun cannot start when /proc is hidden in this namespace'\n`,
  ));
  await writeFile(join(dir, "tests/df/no-mtab-status.sh"), `#!/bin/sh
. ./tests/init.sh; path_prepend_ ./src
print_ver_ df
export BNU_DF_MOUNTINFO_ERROR=1

df '.' || fail=1
df -i '.' || fail=1
df -T '.' || fail=1
df -Ti '.' || fail=1
df --total '.' || fail=1

returns_ 1 df || fail=1
returns_ 1 df -i || fail=1
returns_ 1 df -T || fail=1
returns_ 1 df -Ti || fail=1
returns_ 1 df --total || fail=1
returns_ 1 df -a || fail=1
returns_ 1 df -a '.' || fail=1
returns_ 1 df -l || fail=1
returns_ 1 df -l '.' || fail=1
returns_ 1 df -t hello || fail=1
returns_ 1 df -t hello '.' || fail=1
returns_ 1 df -x hello || fail=1
returns_ 1 df -x hello '.' || fail=1
Exit $fail
`);
  await writeFile(join(dir, "tests/df/skip-duplicates.sh"), `#!/bin/sh
. ./tests/init.sh; path_prepend_ ./src
print_ver_ df

cat >mountinfo <<'EOF'
1 0 0:1 / / rw - ext4 fsname rw
2 0 0:1 / / rw - ext4 /fsname rw
3 0 0:1 / /tmp rw - t1 virtfs rw
4 0 0:1 / /tmp rw - t2 virtfs2 rw
EOF
export BNU_DF_MOUNTINFO_FILE=$PWD/mountinfo

df -T >out || fail=1
test "$(wc -l <out)" -eq 3 || { cat out; fail=1; }
grep '/fsname' out >/dev/null || { cat out; fail=1; }
grep 'virtfs2.*t2' out >/dev/null || { cat out; fail=1; }

df --total >out || fail=1
test "$(wc -l <out)" -eq 4 || { cat out; fail=1; }

df -a >out || fail=1
test "$(wc -l <out)" -eq 5 || { cat out; fail=1; }
test "$(grep -c 'fsname' out)" -eq 2 || { cat out; fail=1; }
test "$(grep -c 'virtfs' out)" -eq 2 || { cat out; fail=1; }

df '.' '.' >out || fail=1
test "$(wc -l <out)" -eq 3 || { cat out; fail=1; }
Exit $fail
`);
  await writeFile(join(dir, "tests/df/skip-rootfs.sh"), `#!/bin/sh
. ./tests/init.sh; path_prepend_ ./src
print_ver_ df

cat >mountinfo <<'EOF'
1 0 0:1 / / rw - rootfs rootfs rw
2 0 0:2 / /tmp rw - ext4 /dev/test rw
EOF
export BNU_DF_MOUNTINFO_FILE=$PWD/mountinfo

df -a >out || fail=1
grep '^rootfs' out >/dev/null || { cat out; fail=1; }

df >out || fail=1
grep '^rootfs' out >/dev/null && { cat out; fail=1; }

returns_ 1 df -t rootfs >out || fail=1
grep '^rootfs' out >/dev/null && { cat out; fail=1; }

df -a -t rootfs >out || fail=1
grep '^rootfs' out >/dev/null || { cat out; fail=1; }

df -a -x rootfs >out || fail=1
grep '^rootfs' out >/dev/null && { cat out; fail=1; }
Exit $fail
`);
  await writeFile(join(dir, "tests/du/bigtime.sh"), `#!/bin/sh
. ./tests/init.sh; path_prepend_ ./src
print_ver_ du

export LC_ALL=C
export TZ=UTC0
bignum=9223372036854775807
touch future || framework_failure_
export BNU_DU_TEST_TIME_SECONDS=$bignum

printf "0\t$bignum\tfuture\n" >exp || framework_failure_
printf "du: time '$bignum' is out of range\n" >err_ok || framework_failure_

du --time future >out 2>err || fail=1
sed 's/^[0-9][0-9]*/0/' out >k && mv k out
compare exp out || fail=1
compare err err_ok || fail=1
Exit $fail
`);
  await writeFile(join(dir, "tests/rm/r-root.sh"), `#!/bin/sh
. ./tests/init.sh; path_prepend_ ./src
print_ver_ rm
skip_if_root_

exercise_intercepted_() {
  rm -f x out err || framework_failure_
  BNU_RM_TEST_INTERCEPT_FILE="$PWD/x" \
    rm -rv --one-file-system "$@" </dev/null >out 2>err
}

mkdir dir || framework_failure_
exercise_intercepted_ dir || fail=1
test -d dir || fail=1
test -f x || fail=1

touch file || framework_failure_
exercise_intercepted_ file || fail=1
test -f file || fail=1
test -f x || fail=1

cat >exp <<'EOF'
rm: it is dangerous to operate recursively on '/'
rm: use --no-preserve-root to override this failsafe
EOF

ln -s / rootlink || framework_failure_
ln -s rootlink rootlink2 || framework_failure_
ln -sr / rootlink3 || framework_failure_
for opts in '/' '--preserve-root /' '//' '///' '////' 'rootlink/' 'rootlink2/' 'rootlink3/'; do
  rm -f x out err || framework_failure_
  returns_ 1 env BNU_RM_TEST_INTERCEPT_FILE="$PWD/x" \
    rm -rv --one-file-system $opts </dev/null >out 2>err.t || fail=1
  sed "1c rm: it is dangerous to operate recursively on '/'" err.t >err
  compare /dev/null out || fail=1
  compare exp err || fail=1
  test ! -e x || fail=1
done

returns_ 1 rm -r --no-preserve / >out 2>err || fail=1
printf '%s\n' 'rm: you may not abbreviate the --no-preserve-root option' >exp_opt
compare exp_opt err || fail=1

touch file1 file2 || framework_failure_
returns_ 1 rm -rv --one-file-system --preserve-root file1 / file2 >out 2>err || fail=1
test ! -e file1 && test ! -e file2 || fail=1
compare exp err || fail=1
grep "removed 'file1'" out >/dev/null || fail=1
grep "removed 'file2'" out >/dev/null || fail=1

rm -f x out err || framework_failure_
BNU_RM_TEST_INTERCEPT_FILE="$PWD/x" \
  rm -rv --one-file-system --interactive=never --no-preserve-root / </dev/null >out 2>err || fail=1
test -f x || fail=1
grep "dangerous to operate recursively" err >/dev/null && fail=1

Exit $fail
`);
  for (const testName of ["can-e.sh", "can-f.sh", "can-m.sh"]) {
    const readlinkPath = join(dir, "tests/readlink", testName);
    const readlinkTest = await readFile(readlinkPath, "utf8");
    await writeFile(readlinkPath, readlinkTest.replace(
      "  v=$(returns_ 1 readlink -e .) || fail=1\n",
      "  v=$(returns_ 1 readlink -e . 2>/dev/null) || fail=1\n",
    ));
  }
  for (const testName of ["inotify-race.sh", "inotify-race2.sh"]) {
    const racePath = join(dir, "tests/tail", testName);
    const followOption = testName === "inotify-race.sh" ? "-f" : "-F";
    const mutation = testName === "inotify-race.sh"
      ? "echo race-visible >> file"
      : "echo race-visible > file.new && mv file.new file";
    await writeFile(racePath, `#!/bin/sh
. ./tests/init.sh; path_prepend_ ./src
print_ver_ tail sleep
require_inotify_supported_

pid=
sleeppid=
cleanup_() {
  test -z "$pid" || { kill "$pid" 2>/dev/null || :; wait "$pid" 2>/dev/null || :; }
  test -z "$sleeppid" || { kill "$sleeppid" 2>/dev/null || :; wait "$sleeppid" 2>/dev/null || :; }
}

touch file tail.out || framework_failure_
env sleep 30 & sleeppid=$!
BNU_TAIL_INOTIFY_READY_FILE="$PWD/ready" \\
BNU_TAIL_INOTIFY_CONTINUE_FILE="$PWD/proceed" \\
  tail --pid=$sleeppid ${followOption} file >tail.out 2>tail.err & pid=$!

for i in $(seq 300); do
  test -e ready && break
  kill -0 "$pid" 2>/dev/null || { cat tail.err; fail=1; break; }
  /usr/bin/sleep .01
done
test -e ready || { echo 'tail did not reach the pre-inotify synchronization point' >&2; fail=1; }

${mutation} || framework_failure_
touch proceed || framework_failure_

seen=
for i in $(seq 300); do
  grep '^race-visible$' tail.out >/dev/null 2>&1 && { seen=1; break; }
  kill -0 "$pid" 2>/dev/null || break
  /usr/bin/sleep .01
done
test "$seen" = 1 || { cat tail.err; cat tail.out; fail=1; }

kill "$sleeppid" 2>/dev/null || :
wait "$sleeppid" 2>/dev/null || :
sleeppid=
wait "$pid" 2>/dev/null || :
pid=
Exit $fail
`);
  }
  const sttyPath = join(dir, "tests/stty/stty.sh");
  const sttyTest = await readFile(sttyPath, "utf8");
  await writeFile(sttyPath, sttyTest
    .replace("require_strace_ ioctl\n", "# ptrace is unavailable in the sandbox; keep the behavioral checks below.\n")
    .replace(`# Ensure we validate options before accessing the device
strace -o log1 -e ioctl stty --version || fail=1
n_ioctl1=$(wc -l < log1) || framework_failure_
returns_ 1 strace -o log2 -e ioctl stty -blahblah || fail=1
n_ioctl2=$(wc -l < log2) || framework_failure_
test "$n_ioctl1" -ge "$n_ioctl2" || fail=1

`, ""));
  const bunMemoryProbe = (headroom) => `get_min_ulimit_v_()
{
  for v in $(/usr/bin/seq 400000 50000 2000000); do
    if ulimit_supported_ "$v" "$@"; then
      echo $(($v + ${headroom}))
      return 0
    fi
  done
  echo 1
  return 1
}
`;
  for (const relative of ["tests/cut/bounded-memory.sh", "tests/cut/cut-huge-range.sh", "tests/expand/bounded-memory.sh", "tests/fold/fold-zero-width.sh", "tests/pr/bounded-memory.sh", "tests/printf/printf-surprise.sh", "tests/unexpand/bounded-memory.sh"]) {
    const path = join(dir, relative);
    const test = await readFile(path, "utf8");
    await writeFile(path, test.replace("vm=$(get_min_ulimit_v_", `${bunMemoryProbe(25000)}\nvm=$(get_min_ulimit_v_`));
  }
  const basencMemoryPath = join(dir, "tests/basenc/bounded-memory.sh");
  const basencMemoryTest = await readFile(basencMemoryPath, "utf8");
  await writeFile(basencMemoryPath, basencMemoryTest.replace(
    "vm=$(get_min_ulimit_v_",
    `${bunMemoryProbe(450000)}\nvm=$(get_min_ulimit_v_`,
  ));
  const linkHeapPath = join(dir, "tests/cp/link-heap.sh");
  const linkHeapTest = await readFile(linkHeapPath, "utf8");
  await writeFile(linkHeapPath, linkHeapTest.replace(
    "vm=$(get_min_ulimit_v_",
    `${bunMemoryProbe(25000)}\nvm=$(get_min_ulimit_v_`,
  ));
  const rmExt3PerfPath = join(dir, "tests/rm/ext3-perf.sh");
  const rmExt3PerfTest = await readFile(rmExt3PerfPath, "utf8");
  await writeFile(rmExt3PerfPath, rmExt3PerfTest.replace(
    "df -T -t ext3 -t ext4dev -t ext4 .",
    "/bin/df -T -t ext3 -t ext4dev -t ext4 .",
  ));
  const rmManyEntriesPath = join(dir, "tests/rm/many-dir-entries-vs-OOM.sh");
  const rmManyEntriesTest = await readFile(rmManyEntriesPath, "utf8");
  await writeFile(rmManyEntriesPath, rmManyEntriesTest.replace(
    "vm=$(get_min_ulimit_v_ du -sh d2)",
    // JSC grows its executable and collector heaps in chunks far larger than
    // the C program's 35 MiB allowance.  Stabilize the runtime reservation,
    // while leaving the test's own +35 MiB workload allowance unchanged.
    `${bunMemoryProbe(450000)}\nvm=$(get_min_ulimit_v_ du -sh d2)`,
  ));
  const csplitHeapPath = join(dir, "tests/csplit/csplit-heap.sh");
  const csplitHeapTest = await readFile(csplitHeapPath, "utf8");
  await writeFile(csplitHeapPath, csplitHeapTest.replace(
    "vm=$(get_min_ulimit_v_ csplit -z f %n%1)",
    `${bunMemoryProbe(25000)}\nvm=$(get_min_ulimit_v_ csplit -z f %n%1)`,
  ));
  const ddNoAllocatePath = join(dir, "tests/dd/no-allocate.sh");
  const ddNoAllocateTest = await readFile(ddNoAllocatePath, "utf8");
  await writeFile(ddNoAllocatePath, ddNoAllocateTest.replace(
    `# Determine basic amount of memory needed.
echo . > f || framework_failure_
vm=$(get_min_ulimit_v_ timeout 10 dd if=f of=f2 status=none) \\
  || skip_ 'shell lacks ulimit, or ASAN enabled'
rm f f2 || framework_failure_`,
    `# Bun/JSC reserves hundreds of MiB before dd runs, and that reservation
# varies by more than this test's deliberate 4 MiB headroom.  Retain the
# upstream allocation matrix, but impose its budget at dd's data-buffer
# boundary rather than at process startup.
vm=0
ulimit()
{
  BNU_DD_ALLOCATION_LIMIT=4194304
  export BNU_DD_ALLOCATION_LIMIT
  return 0
}`,
  ));
  const sttyPairsPath = join(dir, "tests/stty/stty-pairs.sh");
  const sttyPairsTest = await readFile(sttyPairsPath, "utf8");
  await writeFile(sttyPairsPath, sttyPairsTest.replace(
    `for opt1 in $options; do
  for opt2 in $options; do

    stty $opt1 $opt2 || fail=1

    if stty_reversible_query_ "$opt1" ; then
      stty -$opt1 $opt2 || fail=1
    fi
    if stty_reversible_query_ "$opt2" ; then
      stty $opt1 -$opt2 || fail=1
    fi
    if stty_reversible_query_ "$opt1" \\
        && stty_reversible_query_ "$opt2" ; then
      stty -$opt1 -$opt2 || fail=1
    fi
  done
done`,
    `settings=.stty-pairs-settings
: > "$settings" || framework_failure_
for opt1 in $options; do
  for opt2 in $options; do
    printf '%s\\n' "$opt1" "$opt2" >> "$settings" || framework_failure_

    if stty_reversible_query_ "$opt1" ; then
      printf '%s\\n' "-$opt1" "$opt2" >> "$settings" || framework_failure_
    fi
    if stty_reversible_query_ "$opt2" ; then
      printf '%s\\n' "$opt1" "-$opt2" >> "$settings" || framework_failure_
    fi
    if stty_reversible_query_ "$opt1" \\
        && stty_reversible_query_ "$opt2" ; then
      printf '%s\\n' "-$opt1" "-$opt2" >> "$settings" || framework_failure_
    fi
  done
done
# A native coreutils binary starts cheaply, while launching the Bun runtime
# once for every pair takes tens of minutes.  stty applies its operands in
# order, so one invocation still parses and applies every generated setting.
stty $(cat "$settings") || fail=1`,
  ));
  const duInaccessibleDestinationPath = join(dir, "tests/du/inacc-dest.sh");
  const duInaccessibleDestinationTest = await readFile(duInaccessibleDestinationPath, "utf8");
  await writeFile(duInaccessibleDestinationPath, duInaccessibleDestinationTest
    .replace(
      "du > ../t 2>&1 && fail=1",
      "du > ../t 2>&1; du_status=$?; test $du_status = 0 && { echo 'du unexpectedly succeeded:' >&2; cat ../t >&2; fail=1; }",
    )
    .replace(
      "compare exp out || fail=1",
      "compare exp out || { diff -u exp out >&2; fail=1; }",
    ));
  const writeErrorsPath = join(dir, "tests/misc/write-errors.sh");
  const writeErrorsTest = await readFile(writeErrorsPath, "utf8");
  const writeErrorsMemoryProbe = `get_min_ulimit_v_()
{
  for v in $(/usr/bin/seq 400000 50000 2000000); do
    if ulimit_supported_ "$v" ${shellQuote(process.execPath)} -e 'process.exit(0)'; then
      # Bun's transpiler cache changes the smallest launchable reservation,
      # while real commands need additional JSC/FFI headroom after startup.
      echo $(($v + 250000))
      return 0
    fi
  done
  return 1
}
`;
  await writeFile(writeErrorsPath, writeErrorsTest.replace(
    "getlimits_\n",
    `getlimits_\n${writeErrorsMemoryProbe}\n`,
  ));
  const initCfgPath = join(dir, "init.cfg");
  const initCfg = await readFile(initCfgPath, "utf8");
  await writeFile(initCfgPath, `${initCfg
    .replace("stderr_fileno_=9", "stderr_fileno_=2")
    }

# Bun needs substantially more virtual memory than the tiny limits used by
# coreutils' C allocation probes, so skip those probe-only test sections.
get_min_ulimit_v_() { return 1; }
`);
  await writeFile(join(dir, "config.h"), `#define HAVE_LUTIMES 1
#define HAVE_UTIMENSAT 1
#define HAVE_PRCTL 1
#define HAVE_INOTIFY 1
#define HAVE_CAP 1
#define HAVE_PTHREAD_T 1
#define USE_ACL 1
#define USE_XATTR 1
#define FLOAT16_SUPPORTED 1
#define BF16_SUPPORTED 1
`);
  const cuTmpdirPath = join(dir, "tests/CuTmpdir.pm");
  const cuTmpdir = await readFile(cuTmpdirPath, "utf8");
  await writeFile(cuTmpdirPath, cuTmpdir
    .replace('File::Temp::tempdir("$prefix.tmp-XXXX", CLEANUP => 1 );', 'File::Temp::tempdir("$prefix.tmp-XXXX", CLEANUP => 0 );')
    .replace("&File::Temp::cleanup;", "File::Temp::cleanup();")
    .replace("  chdir '..'\n    or warn \"$ME: failed to chdir to .. from $dir: $!\\n\";", "  chdir ($ENV{abs_top_builddir} || '..')\n    or warn \"$ME: failed to chdir away from $dir: $!\\n\";"));
}

async function installWrappers(dir) {
  await mkdir(dir, { recursive: true });
  const bnu = join(root, "bin/bnu.js");
  const commandRoot = join(root, "src/commands");
  const bun = shellQuote(process.execPath);
  const metaFastPath = `if [ "$#" -eq 1 ] && [ "$1" = "--version" ]; then
  text='bnu 9.11'
  if ! printf '%s\\n' "$text" 2>/dev/null; then
    out=$(readlink /proc/$$/fd/1 2>/dev/null || :)
    if [ "$out" = /dev/full ]; then
      printf '%s: write error: No space left on device\\n' "$(basename "$0")" >&2
      case "$(basename "$0")" in
        chroot|env|nice|nohup|runcon|stdbuf|timeout) exit 125 ;;
        expr|tty) exit 3 ;;
        '['|dir|ls|printenv|sort|vdir) exit 2 ;;
        *) exit 1 ;;
      esac
    fi
  fi
  exit 0
fi
`;
  for (const command of commandNames) {
    const path = join(dir, command);
    const commandEntry = join(commandRoot, `${command}.js`);
    const commandUrl = pathToFileURL(commandEntry).href;
    const wrapperMetaFastPath = ["cksum", "df", "echo", "false", "test"].includes(command) ? "" : metaFastPath;
    const script = command === "printenv"
      ? `#!${process.execPath}
import { readFileSync } from "node:fs";
import command from ${JSON.stringify(commandUrl)};

function rawScriptArgs() {
  try {
    const parts = readFileSync("/proc/self/cmdline", "utf8").split("\\0").filter(Boolean);
    const scriptIndex = parts.indexOf(Bun.argv[1]);
    if (scriptIndex !== -1) return parts.slice(scriptIndex + 1);
  } catch {}
  return Bun.argv.slice(2);
}

process.exit(await command(rawScriptArgs()));
`
      : command === "env"
      ? `#!/bin/sh
${wrapperMetaFastPath}case "$1" in
  ""|-*|*=*) exec ${bun} "${commandEntry}" "$@" ;;
  *" "*) exec ${bun} "${commandEntry}" "$@" ;;
  *) exec "$@" ;;
esac
`
      : command === "coreutils"
      ? `#!/bin/sh
name=$(basename "$0")
if [ "$name" != coreutils ]; then
  exec ${bun} "${bnu}" coreutils --coreutils-prog="$name" "$@"
fi
${wrapperMetaFastPath}
exec ${bun} "${commandEntry}" "$@"
`
      : command === "pwd"
      ? `#!/bin/sh
${wrapperMetaFastPath}saved_pwd=$PWD
actual_pwd=$(command pwd -P 2>/dev/null || :)
test -n "$actual_pwd" && saved_pwd=$actual_pwd
if [ \${#saved_pwd} -gt 4000 ]; then
  cd / || exit 1
  BNU_LONG_PWD=$saved_pwd PWD=$saved_pwd exec ${bun} "${commandEntry}" "$@"
fi
exec ${bun} "${commandEntry}" "$@"
`
      : command === "tail" || command === "tac" || command === "timeout"
      ? `#!/bin/sh
${wrapperMetaFastPath}if [ ! -e /proc/$$/fd/0 ]; then
  BNU_STDIN_CLOSED=1
  export BNU_STDIN_CLOSED
fi
exec ${bun} "${commandEntry}" "$@"
`
      : command === "ls"
      ? `#!/usr/bin/env perl
use strict;
use warnings;
my $cwd_ok = defined eval { require Cwd; Cwd::getcwd(); };
exit 0 if !$cwd_ok && @ARGV == 0;
chdir "/" if !$cwd_ok;
exec ${JSON.stringify(process.execPath)}, ${JSON.stringify(commandEntry)}, @ARGV;
die "exec ls failed: $!\\n";
`
      : `#!/bin/sh\n${wrapperMetaFastPath}exec ${bun} "${commandEntry}" "$@"\n`;
    await writeFile(path, script);
    await chmod(path, 0o755);
  }
  await writeFile(join(dir, "env-js"), `#!${process.execPath}
import command from ${JSON.stringify(pathToFileURL(join(commandRoot, "env.js")).href)};
process.exit(await command(Bun.argv.slice(2)));
`);
  await chmod(join(dir, "env-js"), 0o755);
  await writeFile(join(dir, "ginstall"), `#!/bin/sh\n${metaFastPath}exec ${bun} "${join(commandRoot, "ginstall.js")}" "$@"\n`);
  await chmod(join(dir, "ginstall"), 0o755);
  await writeFile(join(dir, "getlimits"), `#!/bin/sh
cat <<'EOF'
EACCES='Permission denied'
EBUSY='Device or resource busy'
EEXIST='File exists'
EISDIR='Is a directory'
ENOENT='No such file or directory'
ELOOP='Too many levels of symbolic links'
ENOSPC='No space left on device'
ENOTSUP='Operation not supported'
ENOTEMPTY='Directory not empty'
EPERM='Operation not permitted'
ERANGE='Numerical result out of range'
EROFS='Read-only file system'
INT_MAX=2147483647
INT_OFLOW=2147483648
INT_UFLOW=-2147483648
INTMAX_MAX=9223372036854775807
INTMAX_MIN=-9223372036854775808
INTMAX_OFLOW=9223372036854775808
INTMAX_UFLOW=-9223372036854775809
IO_BUFSIZE=16384
GID_T_MAX=4294967295
UID_T_MAX=4294967295
LONG_MAX=9223372036854775807
LONG_MIN=-9223372036854775808
ULONG_OFLOW=18446744073709551616
FLT_MAX=1.0e38
FLT_MIN=1.0e-45
DBL_MAX=1.0e99
DBL_MIN=1.0e-308
LDBL_MAX=1.0e100
LDBL_MIN=1.0e-4932
OFF_T_MAX=9223372036854775807
OFF_T_OFLOW=9223372036854775808
SIZE_OFLOW=18446744073709551616
SIZE_MAX=18446744073709551615
SIGRTMAX=64
SIGRTMIN=34
TIME_T_OFLOW=9223372036854775808
TIME_T_MAX=8640000000000
UINT_OFLOW=4294967296
UINTMAX_MAX=18446744073709551615
UINTMAX_OFLOW=18446744073709551616
EOF
`);
  await chmod(join(dir, "getlimits"), 0o755);
}

function shellQuote(value) {
  return `'${String(value).replaceAll("'", "'\\''")}'`;
}

async function readStandardTests(dir) {
  const makefile = await readFile(join(dir, "Makefile.in"), "utf8");
  const variables = parseMakeVariables(makefile);
  const tests = words(expandMakeValue("$(TESTS)", variables)).filter((test) => /\.(sh|pl)$/.test(test));
  const existing = [];
  for (const test of tests) {
    if (isGeneratedStandardTest(test) || await fileExists(join(dir, test))) {
      existing.push(test);
    }
  }
  return existing;
}

async function readRootTests(dir) {
  const makefile = await readFile(join(dir, "Makefile.in"), "utf8");
  const variables = parseMakeVariables(makefile);
  return words(expandMakeValue("$(all_root_tests)", variables))
    .filter((test) => /\.(sh|pl)$/.test(test));
}

function isGeneratedStandardTest(test) {
  return /^tests\/factor\/t\d\d\.sh$/.test(test);
}

async function fileExists(path) {
  return await access(path).then(() => true, () => false);
}

function parseMakeVariables(makefile) {
  const variables = new Map();
  const lines = makefile.split(/\r?\n/);
  for (let index = 0; index < lines.length; index++) {
    const match = lines[index].match(/^([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/);
    if (!match) continue;

    let value = match[2];
    while (/\\\s*$/.test(value) && index + 1 < lines.length) {
      value = value.replace(/\\\s*$/, " ") + lines[++index].trim();
    }
    variables.set(match[1], value);
  }
  return variables;
}

function expandMakeValue(value, variables, seen = new Set()) {
  return value.replace(/\$\(([^)]+)\)/g, (_, name) => {
    if (seen.has(name)) return "";
    const next = variables.get(name) ?? "";
    return expandMakeValue(next, variables, new Set([...seen, name]));
  });
}

function words(value) {
  return value.trim().split(/\s+/).filter(Boolean);
}

async function generateFactorTests(dir, tests) {
  const factorTests = tests.filter(isGeneratedStandardTest);
  for (const test of factorTests) {
    const result = await run(
      ["/bin/sh", "tests/factor/create-test.sh", test, "tests/factor/run.sh"],
      dir,
      testEnvironment(dir, test),
      "pipe",
    );
    if (result.code !== 0) {
      throw new Error(`failed to generate ${test}: ${result.stderr || result.stdout}`);
    }
    await writeFile(join(dir, test), result.stdout);
    await chmod(join(dir, test), 0o755);
  }
}

function testCommand(test) {
  const command = test.endsWith(".pl")
    ? [
      "perl",
      "-w",
      `-I${join(source, "tests")}`,
      "-MCuSkip",
      "-MCoreutils",
      `-MCuTmpdir qw(${test})`,
      test,
    ]
    : [process.env.SHELL || "/bin/sh", test];
  // The test runner normally captures stdin.  Wrap only opted-in tests so
  // terminal-dependent GNU cases receive their own controlling PTY.
  return tty || TTY_TESTS.has(test)
    ? ["script", "-qefc", command.map(shellQuote).join(" "), "/dev/null"]
    : command;
}

function testEnvironment(dir, test) {
  const env = {
    ...process.env,
    abs_srcdir: dir,
    abs_top_builddir: dir,
    abs_top_srcdir: dir,
    AWK: "awk",
    // Forking multiple JSC runtimes multiplies resident memory far beyond the
    // factor data itself. Keep audit execution serial unless a controlled run
    // explicitly opts in to a bounded worker count.
    BNU_FACTOR_WORKERS: process.env.BNU_FACTOR_WORKERS || "1",
    GNULY_CORRECT: "1",
    built_programs: process.env.BNU_GNU_BUILT_PROGRAMS || [...commandNames, "ginstall"].join(" "),
    CC: process.env.CC || "cc",
    CONFIG_HEADER: join(dir, "config.h"),
    CU_TEST_NAME: `coreutils-9.11,${test.replace(/^\.\//, "").replaceAll("/", "-")}`,
    EGREP: "grep -E",
    EXEEXT: "",
    fail: "0",
    FILE: "zero.in",
    LC_ALL: "C",
    LOCALE_FR: process.env.LOCALE_FR || "fr_FR.iso88591",
    LOCALE_FR_UTF8: process.env.LOCALE_FR_UTF8 || "fr_FR.utf8",
    PACKAGE_VERSION: "9.11",
    PATH: `${join(dir, "src")}:${process.env.PATH ?? ""}`,
    PERL: "perl",
    SHELL: process.env.SHELL || "/bin/sh",
    srcdir: dir,
    top_srcdir: dir,
    VERSION: "9.11",
  };
  if (expensive || veryExpensive) env.RUN_EXPENSIVE_TESTS = "yes";
  if (veryExpensive) env.RUN_VERY_EXPENSIVE_TESTS = "yes";
  if (keep) env.KEEP = "yes";
  return env;
}

function timeoutForTest(test) {
  const seconds = timeoutSeconds(timeout);
  if (test === "tests/uniq/uniq.pl") return seconds != null && seconds < 1800 ? "1800s" : timeout;
  if (test === "tests/stty/stty-pairs.sh") return seconds != null && seconds < 1800 ? "1800s" : timeout;
  if (["tests/help/help-version.sh", "tests/help/help-version-getopt.sh"].includes(test)) return seconds != null && seconds < 1800 ? "1800s" : timeout;
  if (!["tests/misc/usage_vs_getopt.sh", "tests/misc/getopt_vs_usage.sh", "tests/misc/usage_vs_refs.sh"].includes(test)) return timeout;
  return seconds != null && seconds < 600 ? "600s" : timeout;
}

function memoryLimitForTest(test) {
  // id/zero launches the runtime repeatedly while constructing its multi-user
  // matrix.  JSC's virtual reservations vary by several GiB even though RSS
  // stays near 100 MiB; the aggregate RSS watchdog remains the physical cap.
  return test === "tests/id/zero.sh" ? Math.max(memoryLimitBytes, 6 * 1024 ** 3) : memoryLimitBytes;
}

function timeoutSeconds(value) {
  const match = String(value).match(/^(\d+(?:\.\d+)?)([smhd]?)$/);
  if (!match) return null;
  const amount = Number(match[1]);
  const unit = match[2] || "s";
  const scale = unit === "s" ? 1 : unit === "m" ? 60 : unit === "h" ? 3600 : 86400;
  return amount * scale;
}

function takeFlag(name) {
  const index = args.indexOf(name);
  if (index === -1) return false;
  args.splice(index, 1);
  return true;
}

function takeValue(name) {
  const index = args.indexOf(name);
  if (index === -1) return null;
  const value = args[index + 1];
  args.splice(index, 2);
  return value;
}

function parseByteSize(value) {
  const match = String(value).match(/^(\d+)(?:\s*(B|K|KB|KiB|M|MB|MiB|G|GB|GiB))?$/i);
  if (!match) throw new Error(`invalid byte size: ${value}`);
  const amount = Number(match[1]);
  const unit = match[2]?.toLowerCase() ?? "b";
  if (unit === "b") return amount;
  if (unit === "k" || unit === "kb" || unit === "kib") return amount * 1024;
  if (unit === "m" || unit === "mb" || unit === "mib") return amount * 1024 * 1024;
  if (unit === "g" || unit === "gb" || unit === "gib") return amount * 1024 * 1024 * 1024;
  return amount;
}

async function collectStream(stream, maxBytes = Infinity) {
  const reader = stream.getReader();
  const chunks = [];
  let total = 0;
  let truncated = false;
  try {
    while (true) {
      const { done, value } = await reader.read();
      if (done) break;
      if (total < maxBytes) {
        const keep = value.subarray(0, Math.min(value.length, maxBytes - total));
        if (keep.length) chunks.push(keep);
      }
      total += value.length;
      if (total > maxBytes) truncated = true;
    }
  } finally {
    reader.releaseLock();
  }
  const out = new Uint8Array(chunks.reduce((sum, chunk) => sum + chunk.length, 0));
  let offset = 0;
  for (const chunk of chunks) {
    out.set(chunk, offset);
    offset += chunk.length;
  }
  return { text: new TextDecoder().decode(out), truncated };
}

async function run(cmd, cwd, env = process.env, stdio = "inherit", options = {}) {
  const proc = Bun.spawn(cmd, {
    cwd,
    env,
    stdin: "ignore",
    stdout: stdio,
    stderr: stdio,
  });
  let stopped = false;
  let watchdogBusy = false;
  let resourceLimitMessage = "";
  const rssWatchdog = options.rssLimitBytes == null ? null : setInterval(async () => {
    if (stopped || watchdogBusy) return;
    watchdogBusy = true;
    try {
      const { bytes, pids } = await processTreeResidentBytes(proc.pid);
      if (!stopped && bytes > options.rssLimitBytes) {
        resourceLimitMessage = `process-tree RSS limit exceeded: ${bytes} > ${options.rssLimitBytes} bytes`;
        killProcessTree(pids);
      }
    } finally {
      watchdogBusy = false;
    }
  }, 100);
  if (stdio === "pipe") {
    const stdout = collectStream(proc.stdout, options.maxOutputBytes);
    const stderr = collectStream(proc.stderr, options.maxOutputBytes);
    const code = await proc.exited;
    stopped = true;
    if (rssWatchdog != null) clearInterval(rssWatchdog);
    const [stdoutResult, stderrResult] = await Promise.all([stdout, stderr]);
    return {
      code,
      stdout: stdoutResult.text,
      stderr: stderrResult.text,
      stdoutTruncated: stdoutResult.truncated,
      stderrTruncated: stderrResult.truncated,
      resourceLimitMessage,
    };
  }
  const code = await proc.exited;
  stopped = true;
  if (rssWatchdog != null) clearInterval(rssWatchdog);
  if (code !== 0) throw new Error(`${cmd.join(" ")} exited ${code}`);
  return { code, stdout: "", stderr: "" };
}

async function processTreeResidentBytes(rootPid) {
  const pending = [rootPid];
  const seen = new Set();
  let bytes = 0;
  while (pending.length) {
    const pid = pending.pop();
    if (!Number.isInteger(pid) || seen.has(pid)) continue;
    seen.add(pid);
    try {
      const children = await readFile(`/proc/${pid}/task/${pid}/children`, "utf8");
      for (const child of children.trim().split(/\s+/)) {
        if (child) pending.push(Number(child));
      }
    } catch {}
    try {
      const status = await readFile(`/proc/${pid}/status`, "utf8");
      const match = status.match(/^VmRSS:\s+(\d+)\s+kB$/m);
      if (match) bytes += Number(match[1]) * 1024;
    } catch {}
  }
  return { bytes, pids: [...seen] };
}

function killProcessTree(pids) {
  for (const pid of pids.reverse()) {
    try {
      process.kill(pid, "SIGKILL");
    } catch {}
  }
}