cosca 0.5.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
use super::*;
use std::ffi::OsString;
use std::path::Path;

/// The host's own platform.
///
/// The rule this encodes is DIRECTIONAL, not symmetric. Simulating POSIX on Windows is never safe
/// for a filesystem test: `C:\\Users\\...` is split on its own drive colon, so every candidate is
/// shredded and the test either fails or — worse — passes vacuously. Any filesystem test that
/// could run POSIX-simulated must therefore use this flag.
///
/// Simulating WINDOWS on a POSIX host is the carve-out, and several filesystem tests below take
/// it deliberately (`go_win`/`go_win_path`, plus the system-directory block). It works because a
/// POSIX tempdir path contains no `;` and no drive prefix, so `;`-splitting is a no-op and
/// `is_absolute()` agrees with the host — the Windows rules then apply to paths they cannot
/// mangle. That is a property of the paths these tests construct, not a general licence: a
/// `windows: true` filesystem test that used a path containing `;` would be back in the shredding
/// case. The carve-out exists because the alternative is worse — the located candidate order, the
/// `PATH` quoting rule and the system-directory precedence would otherwise be gated only on the
/// Windows runner.
const HOST_WINDOWS: bool = cfg!(windows);

/// A `PATH` value in the HOST's syntax, for filesystem tests.
fn path_var(dirs: &[&Path]) -> OsString {
    std::env::join_paths(dirs).unwrap()
}

fn touch(dir: &Path, name: &str) -> std::path::PathBuf {
    let p = dir.join(name);
    std::fs::write(&p, b"x").unwrap();
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o755)).unwrap();
    }
    p
}

/// A filename resolution is guaranteed to look for on the HOST platform, for a logical
/// extensionless name `base` searched via `go()` (which uses `HOST_WINDOWS`, per this file's own
/// doc above) — `.exe` appended on Windows, `base` unchanged on POSIX. Planting this keeps a
/// `go()`-driven filesystem test working on whichever host actually runs it, including a real
/// Windows CI runner, rather than re-deriving the rule at each call site.
///
/// `.exe` is the candidate a BARE name resolves through, and it is also the second candidate a
/// LOCATED name falls back to, so planting it is correct for either shape (see the "filename
/// candidate rule" tests below). Tests that care about the located axis specifically — that the
/// exact name is tried, and tried FIRST — force `windows: true` via `go_win`/`go_win_path` and
/// plant real filenames, so they gate in ordinary CI rather than only on the Windows runner.
fn exe_name(base: &str) -> String {
    if HOST_WINDOWS {
        format!("{base}.exe")
    } else {
        base.to_string()
    }
}

fn go(program: &str, cwd: &Path, path: Option<&OsStr>) -> Result<std::path::PathBuf, Error> {
    resolve(ResolveInput {
        program: Path::new(program),
        cwd: Some(cwd),
        system_dirs: &no_system_dirs,
        path_var: path,
        windows: HOST_WINDOWS,
        loadable_only: false,
        normalise: &as_written,
    })
}

/// Join directories into a `PATH` string using the GIVEN platform's separator, independent of the
/// host — unlike [`path_var`], which uses [`std::env::join_paths`] and therefore only agrees with
/// a `windows` flag that matches the real host (see this file's `HOST_WINDOWS` doc). The
/// system-directory tests below deliberately force `windows: true`/`false` regardless of host —
/// that IS the point (system directories are a Windows-only policy, exercised from any host per
/// this module's own design) — so they need a `PATH` string built for the SIMULATED platform, not
/// the host's.
fn path_var_for(dirs: &[&Path], windows: bool) -> OsString {
    let sep = if windows { ";" } else { ":" };
    OsString::from(
        dirs.iter()
            .map(|d| d.display().to_string())
            .collect::<Vec<_>>()
            .join(sep),
    )
}

// ── pure logic: safe to simulate either platform, since nothing touches the filesystem ──

#[skuld::test]
fn path_var_is_split_on_the_simulated_platforms_separator() {
    let win = split_path_var(Some(OsStr::new(r"C:\a;C:\b")), true);
    assert_eq!(win.len(), 2, "{win:?}");
    let nix = split_path_var(Some(OsStr::new("/a:/b")), false);
    assert_eq!(nix.len(), 2, "{nix:?}");
    // The cross case is exactly what broke the filesystem tests on Windows.
    let shredded = split_path_var(Some(OsStr::new(r"C:\a")), false);
    assert_eq!(
        shredded.len(),
        2,
        "a Windows path split POSIX-style is shredded: {shredded:?}"
    );
}

// ── quoted PATH elements ─────────────────────────────────────────────────────────────

#[skuld::test]
fn windows_path_var_quoting_protects_an_embedded_separator() {
    // A Windows PATH element may be wrapped in `"` so a directory containing a literal `;`
    // survives as ONE element rather than being torn in half by a naive byte-level `;` split.
    let got = split_path_var(Some(OsStr::new(r#""C:\a;b";C:\c"#)), true);
    assert_eq!(got, vec![PathBuf::from(r"C:\a;b"), PathBuf::from(r"C:\c")], "{got:?}");
}

#[skuld::test]
fn windows_path_var_quoting_strips_the_wrapping_quotes() {
    // A quoted-but-unstripped element (`"C:\bin"`, quote characters retained) fails the
    // `joined.is_absolute()` check inside `resolve()`'s search loop — a leading `"` is not a
    // recognised drive prefix — and is therefore SILENTLY DROPPED rather than erroring. So
    // `split_path_var_windows`, which parses quotes itself rather than deferring to
    // `std::env::split_paths`, must strip them.
    //
    // `is_absolute()` is host-specific (see this file's HOST_WINDOWS note), so that half of the
    // claim is proven separately, end to end, by
    // `a_quoted_path_entry_with_an_embedded_semicolon_is_not_silently_dropped` on a real Windows
    // host; this test pins only the quote-stripping itself, which is pure byte logic.
    let got = split_path_var(Some(OsStr::new(r#""C:\bin""#)), true);
    assert_eq!(got, vec![PathBuf::from(r"C:\bin")], "{got:?}");
}

#[skuld::test]
fn posix_path_var_quotes_are_not_special() {
    // On POSIX, `"` is an ordinary filename character and `;` is not a PATH separator: quoting
    // must NOT be applied there. Only `:` splits, and any quote characters in an element are
    // preserved literally (they are part of the path, not delimiters).
    let got = split_path_var(Some(OsStr::new(r#""/a;b":/c"#)), false);
    assert_eq!(got, vec![PathBuf::from(r#""/a;b""#), PathBuf::from("/c")], "{got:?}");
}

// ── the filename candidate rule ──────────────────────────────────────────────────────
//
// `.exe` belongs to names that get SEARCHED, not to files that get LOADED, so the rule differs by
// shape.
//
// SEARCHED (bare): one candidate, `tool.exe`. Measured on real Windows CI (amd64 and arm64):
// `CreateProcessW` with a NULL `lpApplicationName`, `cmd.exe`, `pwsh` 7, and Windows PowerShell
// 5.1 all refuse to run an extensionless PE by bare name, so an extensionless fallback candidate
// never ran on any of those four surfaces — omitting it is a narrowing. `PATHEXT` has no entry
// meaning "try the bare name", so there is nothing to be compatible with either. Those same three
// shells (unlike `CreateProcessW`) DO resolve a bare DOTTED name via PATHEXT (`foo.bar` runs
// `foo.bar.exe`), which the old has-a-dot heuristic could never produce — `python3.11` could never
// resolve even though every shell finds `python3.11.exe`. So the rule also widens for a dotted
// bare name.
//
// LOADED (located): the exact name FIRST, always. The PE format makes no extension normative and
// `CreateProcessW` documents "no default extension is assumed" for the `lpApplicationName` this
// resolver feeds, so a file the caller named by path must stay nameable. A second `.exe` candidate
// follows ONLY when the name has no extension at all, keeping `executable("bin/my-program")`
// portable while leaving a dotted name (`thing.bin`) with exactly one candidate — which is what
// `main` did, and what keeps this axis a no-op rather than a widening.
//
// See `filename_candidates`'s doc in `src/resolve.rs` for the full rationale, including why
// exactly `.exe`/`.com` (not scripts).
//
// A BARE name asserts there is exactly ONE candidate: a second, never-matching candidate would
// pass every assertion here while quietly leaving the pre-fix ordering hazard (an ambient
// extensionless file able to win in some future directory ordering) in place. A LOCATED name may have
// two, but only when it carries no extension and names a file — see `takes_the_exe_fallback`.

fn candidate(n: &str, w: bool) -> Vec<String> {
    filename_candidates(OsStr::new(n), w, classify(OsStr::new(n), w))
        .iter()
        .map(|c| c.to_string_lossy().into_owned())
        .collect()
}

#[skuld::test]
fn bare_extensionless_name_gets_only_the_exe_candidate() {
    // Catches the extensionless fallback candidate coming back: under the old two-candidate rule
    // this would have been `vec!["tool.exe", "tool"]`, which this exact-equality `assert_eq!`
    // against the single-element `vec!["tool.exe"]` rejects just as surely as a candidate list of
    // `vec!["tool"]` alone would be.
    let got = candidate("tool", true);
    assert_eq!(got, vec!["tool.exe"], "{got:?}");
}

#[skuld::test]
fn dotted_name_without_a_loadable_extension_still_gets_exe_appended() {
    // Catches the OLD has-a-dot heuristic returning: under that rule `python3.11` (already
    // containing a dot) would have been left unchanged and never resolved, even though
    // `python3.11.exe` is exactly what every shell finds for it.
    let got = candidate("python3.11", true);
    assert_eq!(got, vec!["python3.11.exe"], "{got:?}");
}

#[skuld::test]
fn a_name_already_ending_in_exe_is_not_doubled() {
    let got = candidate("tool.exe", true);
    assert_eq!(got, vec!["tool.exe"], "{got:?}");
}

#[skuld::test]
fn the_exe_extension_check_is_case_insensitive() {
    // Catches a `== ".exe"` (exact-case) comparison: `TOOL.EXE`/`Tool.Exe` must not become
    // `TOOL.EXE.exe`/`Tool.Exe.exe`.
    assert_eq!(candidate("TOOL.EXE", true), vec!["TOOL.EXE"]);
    assert_eq!(candidate("Tool.Exe", true), vec!["Tool.Exe"]);
}

#[skuld::test]
fn a_com_extension_is_in_the_allowlist() {
    // `more.com`/`chcp.com`/`tree.com` are ordinary PEs shipped in System32 with a cosmetic `.com`
    // extension; `.com` missing from the allowlist would break resolving them.
    let got = candidate("more.com", true);
    assert_eq!(got, vec!["more.com"], "{got:?}");
}

#[skuld::test]
fn a_bat_extension_is_not_yet_in_the_allowlist() {
    // Batch resolution is a separate, not-yet-implemented feature (planned as its own follow-up),
    // not a statement that scripts are unsafe — see `filename_candidates`'s doc. Until it lands,
    // `.bat` is treated like any other non-`.exe`/`.com` dotted name: `.exe` is appended, which
    // will simply miss.
    let got = candidate("tool.bat", true);
    assert_eq!(got, vec!["tool.bat.exe"], "{got:?}");
}

#[skuld::test]
fn a_located_name_tries_the_exact_name_before_the_exe_one() {
    // The `.exe` convention belongs to names that get SEARCHED, not to files that get LOADED:
    // the PE/COFF format makes no extension normative, and `CreateProcessW` documents
    // "no default extension is assumed" for the `lpApplicationName` this resolver feeds. So a
    // pathed name keeps naming the file the caller wrote — exact FIRST — while still appending
    // `.exe` as a fallback so `executable("bin/my-program")` stays portable across platforms.
    //
    // Catches both regressions: dropping the exact candidate (which made an extensionless or
    // `.bin` PE unnameable, the `main` -> this-branch regression) and dropping the `.exe` one
    // (which would break the cross-platform ergonomic the rule exists for).
    let got = candidate("bin/my-program", true);
    assert_eq!(got, vec!["bin/my-program", "bin/my-program.exe"], "{got:?}");
}

#[skuld::test]
fn a_located_dotted_name_gets_only_the_exact_candidate() {
    // `.bin` is not a LOADABLE extension, but it IS an extension — and `main` keyed its `.exe`
    // fallback on `Path::extension().is_none()`. Appending here would let
    // `executable(r"tools\thing.bin")` resolve to `thing.bin.exe` when `thing.bin` is absent,
    // which `main` refused: a widening on the located axis, and one a writer of that directory
    // could exploit. One candidate, the name as written.
    let got = candidate(r"tools\thing.bin", true);
    assert_eq!(got, vec![r"tools\thing.bin"], "{got:?}");
}

#[skuld::test]
fn a_located_name_with_only_a_leading_dot_still_gets_the_exe_fallback() {
    // `Path::extension()` treats a leading dot as part of the stem, not an extension separator,
    // so `.helper` has NO extension and keeps the portable `.exe` fallback. Pinned because
    // `has_any_extension` reimplements that rule byte-wise and could easily get it backwards.
    let got = candidate(r"bin\.helper", true);
    assert_eq!(got, vec![r"bin\.helper", r"bin\.helper.exe"], "{got:?}");
}

#[skuld::test]
fn a_located_name_already_ending_in_exe_or_com_gets_one_candidate() {
    // Nothing to append: appending would only ever produce `x.exe.exe`, which names a file the
    // caller did not write. One candidate, same as the bare case.
    for n in [r"bin\tool.exe", r"bin\TOOL.EXE", r"bin\more.com"] {
        let got = candidate(n, true);
        assert_eq!(got.len(), 1, "{n:?} -> {got:?}");
        assert_eq!(got[0], n, "{got:?}");
    }
}

#[skuld::test]
fn posix_never_appends_exe_for_any_of_the_above() {
    for n in [
        "tool",
        "python3.11",
        "tool.exe",
        "TOOL.EXE",
        "more.com",
        "tool.bat",
        "bin/my-program",
    ] {
        let got = candidate(n, false);
        assert_eq!(
            got,
            vec![n.to_string()],
            "POSIX must never append .exe: {n:?} -> {got:?}"
        );
    }
}

#[skuld::test]
fn every_searched_name_produces_exactly_one_candidate() {
    // BARE names only — the ones that visit `system_dirs`/`PATH`. A second candidate here would
    // reintroduce the ordering hazard: an ambient extensionless file winning in some directory
    // the search visits. A LOCATED name visits exactly one directory, the one the caller named,
    // so it carries no such hazard and may have a second candidate (tested above).
    for (n, w) in [
        ("tool", true),
        ("python3.11", true),
        ("tool.exe", true),
        ("TOOL.EXE", true),
        ("more.com", true),
        ("tool.bat", true),
        ("tool", false),
        ("python3.11", false),
    ] {
        let got = filename_candidates(OsStr::new(n), w, classify(OsStr::new(n), w));
        assert_eq!(got.len(), 1, "{n:?} (windows={w}) -> {got:?}");
    }
}

// ── shape classification ─────────────────────────────────────────────────────────────

#[skuld::test]
fn a_drive_relative_name_is_located_not_bare() {
    // `C:tool` has no separator, so a naive rule calls it a bare name and searches PATH — but
    // joining a PATH directory onto it collapses back to `C:tool`, which resolves through drive
    // C's current directory. It must never reach the PATH search.
    assert_eq!(classify(OsStr::new("C:tool"), true), Shape::Located);
    assert_eq!(classify(OsStr::new("tool"), true), Shape::BareName);
    // A drive prefix is a WINDOWS notion: off Windows `C:tool` is an ordinary one-component
    // filename, and calling it located would stop it being searched for on `PATH` at all.
    assert_eq!(classify(OsStr::new("C:tool"), false), Shape::BareName);
    // Any one UTF-16 unit makes a drive, as `RtlDetermineDosPathNameType_U` reads it: `1:tool`
    // is relative to drive `1`, so it is located on Windows too.
    assert_eq!(classify(OsStr::new("1:tool"), true), Shape::Located);
    assert_eq!(classify(OsStr::new(r"dir\tool"), true), Shape::Located);
    assert_eq!(classify(OsStr::new("dir/tool"), true), Shape::Located);
    // Off Windows a backslash is an ordinary character and there are no drive prefixes.
    assert_eq!(classify(OsStr::new(r"dir\tool"), false), Shape::BareName);
    assert_eq!(classify(OsStr::new("dir/tool"), false), Shape::Located);
}

// ── the current directory is not searched ────────────────────────────────────────────

#[skuld::test]
fn bare_name_is_not_resolved_from_the_current_directory() {
    let cwd = tempfile::tempdir().unwrap();
    // `&exe_name("tool")`, not literal `"tool"`: on a Windows host the candidate this resolution
    // actually looks for is `tool.exe` (see the "filename candidate rule" tests above).
    // Planting extensionless `tool` here made this vacuous on the one platform this test exists
    // for — re-adding `cwd` to `Shape::BareName`'s dir list would still find nothing named
    // `tool.exe` and this would keep passing for the wrong reason.
    touch(cwd.path(), &exe_name("tool"));
    // A miss, not a refusal: the name is fine, the cwd is simply not a place this searches.
    assert_not_found("tool", go("tool", cwd.path(), None));
}

#[skuld::test]
fn bare_name_resolves_from_path() {
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    let want = touch(bin.path(), &exe_name("tool"));
    let p = path_var(&[bin.path()]);
    assert_eq!(go("tool", cwd.path(), Some(&p)).unwrap(), want);
}

#[skuld::test]
fn path_wins_over_an_identically_named_file_in_cwd() {
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    touch(cwd.path(), &exe_name("tool"));
    let want = touch(bin.path(), &exe_name("tool"));
    let p = path_var(&[bin.path()]);
    assert_eq!(go("tool", cwd.path(), Some(&p)).unwrap(), want);
}

#[skuld::test]
fn a_name_with_a_separator_resolves_against_cwd() {
    let cwd = tempfile::tempdir().unwrap();
    // Located (contains a separator) tries the exact name first and `.exe` second, so planting
    // either one resolves; `exe_name` plants whichever the HOST would find first.
    let want = touch(cwd.path(), &exe_name("tool"));
    let got = go("./tool", cwd.path(), None).unwrap();
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

/// Drive `resolve` with the WINDOWS rules regardless of host, so rules that only ship on Windows
/// are still gated in ordinary CI rather than only on the Windows runner.
///
/// Nothing here touches a Windows API: `is_execable` reduces to `is_file()` when `windows` is
/// true, and the `PATH` splitting that the bare-name callers exercise is pure byte logic over
/// tempdir paths that contain no `;`. See `HOST_WINDOWS` for why simulating Windows on POSIX is
/// safe here while the reverse never is.
fn go_win_path(program: &str, cwd: &Path, path: Option<&OsStr>) -> Result<std::path::PathBuf, Error> {
    resolve(ResolveInput {
        program: Path::new(program),
        cwd: Some(cwd),
        system_dirs: &no_system_dirs,
        path_var: path,
        windows: true,
        loadable_only: false,
        normalise: &as_written,
    })
}

fn go_win(program: &str, cwd: &Path) -> Result<std::path::PathBuf, Error> {
    go_win_path(program, cwd, None)
}

#[skuld::test]
fn a_located_extensionless_image_resolves_on_windows() {
    // THE REGRESSION GATE. `main` returned an existing absolute/pathed file verbatim; extending
    // the bare-name rule to the located axis briefly made `executable(r"C:\tools\myapp")` a hard
    // `NotFound` for any image not named `.exe`/`.com`. Neither `CreateProcessW` mode does that —
    // the command-line mode
    // documents "if the file name contains a path, .exe is not appended", and `lpApplicationName`
    // (what this resolver actually feeds) documents "no default extension is assumed".
    let cwd = tempfile::tempdir().unwrap();
    let want = touch(cwd.path(), "myapp");
    let got = go_win("./myapp", cwd.path()).expect("an extensionless pathed image must resolve");
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

#[skuld::test]
fn a_located_image_with_an_unrelated_extension_resolves_on_windows() {
    // Same gate, for the `.bin`/`.dat`/staged-payload shape: the PE/COFF format makes no
    // extension normative, so cosca must not invent one for a file the caller named by path.
    let cwd = tempfile::tempdir().unwrap();
    let want = touch(cwd.path(), "thing.bin");
    let got = go_win("./thing.bin", cwd.path()).expect("a .bin image must resolve");
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

#[skuld::test]
fn a_located_name_prefers_the_exact_file_over_the_exe_one() {
    // Candidate ORDER, pinned: with both `myapp` and `myapp.exe` present, the exact name the
    // caller wrote wins. This is `main`'s ordering, kept deliberately — "you named this file"
    // is the whole point of the located axis. Flipping the two candidates fails here.
    let cwd = tempfile::tempdir().unwrap();
    let want = touch(cwd.path(), "myapp");
    touch(cwd.path(), "myapp.exe");
    let got = go_win("./myapp", cwd.path()).unwrap();
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

#[skuld::test]
fn a_located_name_still_falls_back_to_exe_when_only_that_exists() {
    // The cross-platform ergonomic the `.exe` rule exists for: `executable("bin/my-program")`
    // written once must find `my-program.exe` on Windows without the caller appending it. Losing
    // the second candidate fails here, so neither candidate can be dropped without a red test.
    let cwd = tempfile::tempdir().unwrap();
    let want = touch(cwd.path(), "my-program.exe");
    let got = go_win("./my-program", cwd.path()).unwrap();
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

#[skuld::test]
fn empty_path_elements_are_skipped() {
    let cwd = tempfile::tempdir().unwrap();
    touch(cwd.path(), &exe_name("tool"));
    let bin = tempfile::tempdir().unwrap();
    touch(bin.path(), &exe_name("tool"));
    // Positive control: the same lookup DOES succeed with a real element, so the assertion below
    // cannot pass merely because the PATH string was malformed for this host.
    assert!(go("tool", cwd.path(), Some(&path_var(&[bin.path()]))).is_ok());
    // An empty element means "the current directory" — resolving through it would reopen the
    // binary-planting hole `resolve()`'s doc on the current directory exists to close. This is
    // guarded by `resolve()`'s single `joined.is_absolute()` check: an empty `PATH` element joins
    // to a RELATIVE path (`Shape::BareName` never puts `cwd` itself in `dirs`), which `is_execable`
    // would then stat against the PROCESS's real OS cwd, not the `cwd` parameter `go()` was handed.
    // For that hazard to be live, the process's real cwd must actually BE `cwd.path()` (holding the
    // planted file) for the duration — otherwise deleting `joined.is_absolute() &&` still finds
    // nothing there and this passes for the wrong reason, on any host. That process must not be
    // THIS one (every other test in this binary shares its cwd), so the check runs in a re-exec'd
    // child spawned with `cwd.path()` as its OS-level cwd instead — see
    // `crate::test_child::run_fixture_with_cwd`.
    crate::test_child::run_fixture_with_cwd(
        crate::test_child::fixture_path!(fixture_empty_path_elements_are_skipped),
        cwd.path(),
        FIXTURE_EMPTY_PATH_ELEMENTS_MARKER,
    );
}

const FIXTURE_EMPTY_PATH_ELEMENTS_MARKER: &str = "COSCA_FIXTURE_EMPTY_PATH_ELEMENTS";

/// The child half of [`empty_path_elements_are_skipped`]: a no-op when picked up by an ordinary,
/// unfiltered suite run ([`FIXTURE_EMPTY_PATH_ELEMENTS_MARKER`] is unset there). Re-executed via
/// `run_fixture_with_cwd`, [`crate::test_child::expected_cwd`] both fetches the directory the
/// parent prepared AND asserts `std::env::current_dir()` here actually IS it.
#[skuld::test]
fn fixture_empty_path_elements_are_skipped() {
    let Some(cwd) = crate::test_child::expected_cwd(FIXTURE_EMPTY_PATH_ELEMENTS_MARKER) else {
        return; // picked up by an ordinary suite run — deliberately inert
    };
    assert!(
        cwd.join(exe_name("tool")).is_file(),
        "the parent must have planted `tool` in this directory: {cwd:?}"
    );
    let empty = if HOST_WINDOWS { ";;" } else { "::" };
    assert_not_found("tool", go("tool", &cwd, Some(OsStr::new(empty))));
}

#[skuld::test]
fn relative_path_elements_are_skipped() {
    let cwd = tempfile::tempdir().unwrap();
    touch(cwd.path(), &exe_name("tool"));
    let bin = tempfile::tempdir().unwrap();
    touch(bin.path(), &exe_name("tool"));
    assert!(
        go("tool", cwd.path(), Some(&path_var(&[bin.path()]))).is_ok(),
        "positive control"
    );
    // `.` resolves against the process cwd just as surely as an empty element does — and is
    // rejected by the same `joined.is_absolute()` check `empty_path_elements_are_skipped`
    // exercises above, not a distinct code path. Same reasoning as there: the process's real OS cwd
    // must actually be `cwd.path()` for this to be a live check, and that process must not be THIS
    // one — see `fixture_empty_path_elements_are_skipped`'s doc.
    crate::test_child::run_fixture_with_cwd(
        crate::test_child::fixture_path!(fixture_relative_path_elements_are_skipped),
        cwd.path(),
        FIXTURE_RELATIVE_PATH_ELEMENTS_MARKER,
    );
}

const FIXTURE_RELATIVE_PATH_ELEMENTS_MARKER: &str = "COSCA_FIXTURE_RELATIVE_PATH_ELEMENTS";

/// The child half of [`relative_path_elements_are_skipped`]; see
/// [`fixture_empty_path_elements_are_skipped`]'s doc for the shared shape.
#[skuld::test]
fn fixture_relative_path_elements_are_skipped() {
    let Some(cwd) = crate::test_child::expected_cwd(FIXTURE_RELATIVE_PATH_ELEMENTS_MARKER) else {
        return; // picked up by an ordinary suite run — deliberately inert
    };
    assert!(
        cwd.join(exe_name("tool")).is_file(),
        "the parent must have planted `tool` in this directory: {cwd:?}"
    );
    assert_not_found("tool", go("tool", &cwd, Some(OsStr::new("."))));
}

// ── the Windows .exe rule ────────────────────────────────────────────────────────────

#[skuld::test]
fn the_exe_file_wins_over_an_extensionless_namesake_beside_it() {
    // Named for what it can actually observe. Re-adding an extensionless SECOND candidate for a
    // bare name would NOT fail this test — `tool.exe` is tried first and wins either way — so it
    // cannot gate the one-candidate property despite the directory being set up for it. That
    // property is gated by `bare_extensionless_name_gets_only_the_exe_candidate`, which compares
    // the candidate list directly; this one pins the end-to-end outcome on a real filesystem.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    touch(bin.path(), "tool"); // must be ignored entirely, not merely lose a preference
    let want = touch(bin.path(), "tool.exe");
    let p = path_var_for(&[bin.path()], true);
    assert_eq!(go_win_path("tool", cwd.path(), Some(&p)).unwrap(), want);
}

#[skuld::test]
fn an_extensionless_file_no_longer_resolves_even_with_no_exe_on_path() {
    // There is no extensionless fallback candidate, so a bare `tool` with only an extensionless
    // `tool` file on PATH — no `tool.exe` anywhere — must fail to resolve, matching
    // `CreateProcessW`/`cmd.exe`/`pwsh`/`powershell.exe`, all of which refuse to run it (measured
    // on real Windows CI). Catches a two-candidate rule being reintroduced.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    touch(bin.path(), "tool");
    let p = path_var_for(&[bin.path()], true);
    assert_not_found("tool", go_win_path("tool", cwd.path(), Some(&p)));

    // Positive control: the SAME directory and PATH resolve once `tool.exe` is present, so the
    // miss above is the candidate rule and not a broken `path_var_for` silently dropping the
    // entry — which would leave this test green for the wrong reason.
    let want = touch(bin.path(), "tool.exe");
    assert_eq!(go_win_path("tool", cwd.path(), Some(&p)).unwrap(), want);
}

#[skuld::test]
fn a_quoted_path_entry_with_an_embedded_semicolon_is_not_silently_dropped() {
    // N1, end to end: before quote handling, an unquoted split shredded `sub;dir` at the `;`,
    // and even a quoted-but-unstripped entry failed `is_absolute()` and vanished with no error —
    // a PATH entry that disappears rather than erroring. A real executable, reachable only
    // through a quoted entry naming a directory that itself contains a literal `;`, must resolve.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    let semi_dir = bin.path().join("sub;dir");
    std::fs::create_dir(&semi_dir).unwrap();
    // `tool.exe`, not extensionless `tool`: the bare-name rule (see the "filename
    // candidate rule" tests above) means a bare `tool` only ever looks for `tool.exe` on
    // Windows now, so this quoting test must plant the file it can actually find.
    let want = touch(&semi_dir, "tool.exe");
    let quoted = OsString::from(format!("\"{}\"", semi_dir.display()));
    assert_eq!(go_win_path("tool", cwd.path(), Some(&quoted)).unwrap(), want);
}

// ── the contract every backend depends on ────────────────────────────────────────────

#[skuld::test]
fn result_is_always_absolute() {
    let cwd = tempfile::tempdir().unwrap();
    touch(cwd.path(), &exe_name("tool"));
    let got = go("./tool", cwd.path(), None).unwrap();
    assert!(
        got.is_absolute(),
        "backends skip their own search only if this is absolute: {got:?}"
    );
}

// ── the execute bit, and the cwd ─────────────────────────────────────────────────────

#[cfg(unix)]
#[skuld::test]
fn a_readable_but_non_executable_match_is_skipped() {
    use std::os::unix::fs::PermissionsExt;
    let cwd = tempfile::tempdir().unwrap();
    let a = tempfile::tempdir().unwrap();
    let b = tempfile::tempdir().unwrap();
    let dud = touch(a.path(), "tool");
    std::fs::set_permissions(&dud, std::fs::Permissions::from_mode(0o644)).unwrap();
    let want = touch(b.path(), "tool");
    std::fs::set_permissions(&want, std::fs::Permissions::from_mode(0o755)).unwrap();
    let p = path_var(&[a.path(), b.path()]);
    // execvp skips the non-executable match and keeps searching; keying on existence alone
    // would return `a/tool` and hand it to exec as EACCES.
    assert_eq!(go("tool", cwd.path(), Some(&p)).unwrap(), want);
}

#[skuld::test]
fn a_relative_cwd_is_absolutised_so_it_cannot_be_applied_twice() {
    let tmp = tempfile::tempdir().unwrap();
    let sub = tmp.path().join("sub");
    std::fs::create_dir(&sub).unwrap();
    // Only `cfg(unix)` reads `want` (to set the execute bit) — the fixture below reconstructs the
    // same path independently, so a non-unix build never uses this binding at all.
    #[cfg_attr(
        not(unix),
        allow(unused_variables, reason = "only cfg(unix) reads `want` (to set the execute bit)")
    )]
    let want = touch(&sub, &exe_name("tool"));
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(&want, std::fs::Permissions::from_mode(0o755)).unwrap();
    }
    // `resolve()` absolutises a relative `cwd` via `std::env::current_dir()` (see resolve.rs's own
    // "applied twice" note) — see `empty_path_elements_are_skipped`'s doc for why this runs in a
    // re-exec'd child.
    crate::test_child::run_fixture_with_cwd(
        crate::test_child::fixture_path!(fixture_relative_cwd_is_absolutised),
        tmp.path(),
        FIXTURE_RELATIVE_CWD_MARKER,
    );
}

const FIXTURE_RELATIVE_CWD_MARKER: &str = "COSCA_FIXTURE_RELATIVE_CWD";

/// The child half of [`a_relative_cwd_is_absolutised_so_it_cannot_be_applied_twice`]; see
/// [`fixture_empty_path_elements_are_skipped`]'s doc for the shared shape. Reconstructs `want`
/// from its own (already-`tmp.path()`) cwd rather than receiving it from the parent, since the two
/// are guaranteed equal by construction.
#[skuld::test]
fn fixture_relative_cwd_is_absolutised() {
    let Some(cwd) = crate::test_child::expected_cwd(FIXTURE_RELATIVE_CWD_MARKER) else {
        return; // picked up by an ordinary suite run — deliberately inert
    };
    let want = cwd.join("sub").join(exe_name("tool"));
    assert!(want.is_file(), "the parent must have planted `tool` here: {want:?}");
    // On Windows the base is completed by the caller, as the raw backend does, before `resolve`.
    #[cfg(windows)]
    let got = {
        use crate::child::spawn::windows_raw::{env_snapshot::EnvSnapshot, resolve};
        let base = resolve::effective_cwd(
            Some(Path::new("sub")),
            &resolve::DriveDirs::new(&EnvSnapshot::read().unwrap()),
            || std::env::current_dir().map_err(Error::Io),
        )
        .unwrap();
        resolve::resolve_executable(Path::new("./tool"), Some(&base), None).unwrap()
    };
    #[cfg(not(windows))]
    let got = go("./tool", Path::new("sub"), None).unwrap();
    assert!(got.is_absolute(), "{got:?}");
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

/// The end-to-end half of [`a_drive_relative_name_is_refused_on_shape`], on a real Windows runner
/// with a plantable file where the process's own current directory is.
///
/// The refusal itself no longer depends on the host: `resolve` states it explicitly, before any
/// candidate is built, so the host-independent test pins it on every platform. What only a
/// Windows runner can show is that a drive-relative name does not resolve through the process's
/// REAL current directory on drive C — the directory the `cwd` parameter never names, since
/// `PathBuf::push` clears for any prefixed path.
#[cfg(windows)]
#[skuld::test]
fn a_drive_relative_name_fails_closed() {
    let cwd = tempfile::tempdir().unwrap();
    // Planted where the process's actual OS cwd is about to be, so any route that reached drive
    // C's current directory has something to find; a tempdir under `%TEMP%`, as here, lives on the
    // runner's system drive, which is `C:` on every GitHub-hosted Windows runner this crate targets.
    touch(cwd.path(), "tool.exe");
    // The refusal must hold even where the process's REAL cwd is `cwd.path()` — see
    // `empty_path_elements_are_skipped`'s doc for why this runs in a re-exec'd child.
    crate::test_child::run_fixture_with_cwd(
        crate::test_child::fixture_path!(fixture_drive_relative_name_fails_closed),
        cwd.path(),
        FIXTURE_DRIVE_RELATIVE_MARKER,
    );
}

#[cfg(windows)]
const FIXTURE_DRIVE_RELATIVE_MARKER: &str = "COSCA_FIXTURE_DRIVE_RELATIVE";

/// The child half of [`a_drive_relative_name_fails_closed`]; see
/// [`fixture_empty_path_elements_are_skipped`]'s doc for the shared shape.
#[cfg(windows)]
#[skuld::test]
fn fixture_drive_relative_name_fails_closed() {
    let Some(cwd) = crate::test_child::expected_cwd(FIXTURE_DRIVE_RELATIVE_MARKER) else {
        return; // picked up by an ordinary suite run — deliberately inert
    };
    assert!(
        cwd.join("tool.exe").is_file(),
        "the parent must have planted `tool.exe` in this directory: {cwd:?}"
    );
    assert_refused_on_shape("C:tool", go("C:tool", &cwd, None));
}

// ── Windows system directories precede PATH for a bare name ─────────────────────────────
//
// These four tests exercise `ResolveInput::system_dirs` directly: `System32`/the Windows
// directory precede `PATH` for a bare name, on Windows only. They force `windows` explicitly
// true/false — never `HOST_WINDOWS` — because the policy under test is Windows-only by definition
// and this module is deliberately built to be exercised from any host; forcing the flag (rather
// than relying on the host actually being Windows) is what makes these tests run in ordinary CI,
// not just the Windows runner. `system_dirs` here always calls back into a fabricated closure the
// test controls, so these tests do not depend on real system directories existing.

#[skuld::test]
fn bare_name_in_a_system_dir_and_on_path_resolves_from_the_system_dir() {
    // THE regression gate. If system-directory precedence over PATH is ever silently dropped
    // again (e.g. by a future edit that forgets to prepend `system_dirs` before `split_path_var`,
    // or that gates it on the wrong condition), this test starts finding the PATH decoy instead of
    // the system-dir file, and fails. That is the entire point of this test's existence: pin the
    // ordering, not just that resolution succeeds.
    let cwd = tempfile::tempdir().unwrap();
    let sysdir = tempfile::tempdir().unwrap();
    let pathdir = tempfile::tempdir().unwrap();
    // `tool.exe`, not extensionless `tool`: forced `windows: true` below means resolution only
    // ever looks for `tool.exe` (the bare-name rule — see the "filename candidate
    // filename rule" tests above), so an extensionless file here would never be found by either
    // side and `resolve(...).unwrap()` would panic on `NotFound` instead of exercising the
    // ordering this test exists to pin.
    let want = touch(sysdir.path(), "tool.exe");
    touch(pathdir.path(), "tool.exe"); // PATH decoy: same name, must lose to the system dir.
    let system_dirs = [sysdir.path().to_path_buf()];
    let system_dirs_fn = || -> Result<Vec<PathBuf>, Error> { Ok(system_dirs.to_vec()) };
    let path = path_var_for(&[pathdir.path()], true);
    let got = resolve(ResolveInput {
        program: Path::new("tool"),
        cwd: Some(cwd.path()),
        system_dirs: &system_dirs_fn,
        path_var: Some(&path),
        windows: true,
        loadable_only: false,
        normalise: &as_written,
    })
    .unwrap();
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

#[skuld::test]
fn bare_name_only_on_path_still_resolves_from_path() {
    // Adding a search step ahead of PATH must not turn into REPLACING PATH: a name that exists
    // only on PATH, with real (non-matching) system dirs present, must still resolve — otherwise
    // this "fix" would trade the widening it closes for a new, opposite regression: real commands
    // installed only via PATH (which is most of them) breaking outright.
    let cwd = tempfile::tempdir().unwrap();
    let sysdir = tempfile::tempdir().unwrap(); // present, but has nothing named "tool.exe"
    let pathdir = tempfile::tempdir().unwrap();
    let want = touch(pathdir.path(), "tool.exe"); // forced windows: true below -> only "tool.exe" is tried
    let system_dirs = [sysdir.path().to_path_buf()];
    let system_dirs_fn = || -> Result<Vec<PathBuf>, Error> { Ok(system_dirs.to_vec()) };
    let path = path_var_for(&[pathdir.path()], true);
    let got = resolve(ResolveInput {
        program: Path::new("tool"),
        cwd: Some(cwd.path()),
        system_dirs: &system_dirs_fn,
        path_var: Some(&path),
        windows: true,
        loadable_only: false,
        normalise: &as_written,
    })
    .unwrap();
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

#[skuld::test]
fn empty_system_dirs_reproduces_the_pre_fix_path_only_search() {
    // Every test in this file predating this fix calls `go()`, which passes
    // `system_dirs: &no_system_dirs` (see `go`'s definition above). That is only a safe default if
    // returning no directories is truly a no-op — otherwise those tests would have silently
    // stopped meaning what their own doc comments say the day this field was added, without a
    // single one of them failing to notice. This test pins the no-op directly: positive control
    // resolves via PATH exactly as `bare_name_resolves_from_path` above expects, and the negative
    // control (nothing anywhere) still fails closed exactly as
    // `bare_name_is_not_resolved_from_the_current_directory` expects.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    let want = touch(bin.path(), "tool.exe"); // forced windows: true below -> only "tool.exe" is tried
    let path = path_var_for(&[bin.path()], true);
    let got = resolve(ResolveInput {
        program: Path::new("tool"),
        cwd: Some(cwd.path()),
        system_dirs: &no_system_dirs,
        path_var: Some(&path),
        windows: true,
        loadable_only: false,
        normalise: &as_written,
    })
    .unwrap();
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());

    // Negative control: nothing on PATH and an empty system_dirs must still fail closed, not
    // silently succeed by, say, treating "no system directories" as "search the cwd instead".
    let miss = resolve(ResolveInput {
        program: Path::new("tool"),
        cwd: Some(cwd.path()),
        system_dirs: &no_system_dirs,
        path_var: None,
        windows: true,
        loadable_only: false,
        normalise: &as_written,
    });
    assert_not_found("tool", miss);
}

#[skuld::test]
fn posix_ignores_system_dirs_entirely() {
    // `system_dirs` exists to reproduce a WINDOWS-only search order (`CreateProcessW`'s
    // NULL-`lpApplicationName` rule) — POSIX's own `execvp`/`posix_spawn` PATH search has no
    // system-directory step at all, so consulting `system_dirs` off Windows would fabricate a
    // search step POSIX resolution never had, which is a widening with no upstream justification
    // (and directly contradicts this module's own POSIX-coverage doc). Gating on `input.windows`
    // itself — rather than trusting every POSIX caller to always return no directories — is what
    // makes that impossible even if a future POSIX caller's `system_dirs` closure would return a
    // non-empty answer. The system dir here genuinely contains a matching, executable file: if the
    // guard were ever weakened to call the closure regardless of `windows`, this test starts
    // panicking from inside the closure instead of passing, and that flip is exactly what it
    // exists to catch.
    let cwd = tempfile::tempdir().unwrap();
    let sysdir = tempfile::tempdir().unwrap();
    touch(sysdir.path(), "tool");
    let system_dirs = [sysdir.path().to_path_buf()];
    // Panics, rather than merely returning the (non-empty, matching) directory, if the `windows`
    // guard is ever weakened to call this off Windows: a call that returned the real answer would
    // pass silently and defeat the point of this test.
    let system_dirs_fn = || -> Result<Vec<PathBuf>, Error> {
        panic!("system_dirs must not be queried when windows is false: {system_dirs:?}")
    };
    let got = resolve(ResolveInput {
        program: Path::new("tool"),
        cwd: Some(cwd.path()),
        system_dirs: &system_dirs_fn,
        path_var: None,
        windows: false,
        loadable_only: false,
        normalise: &as_written,
    });
    assert_not_found("tool", got);
}

#[skuld::test]
fn a_located_name_still_resolves_when_the_system_dirs_query_fails() {
    // `system_dirs` is called only from the `BareName` arm (see `ResolveInput::system_dirs`'s own
    // doc), after every shape refusal above it has already passed — a `Located` name (one
    // containing a separator) never reaches that arm at all. So a query that CAN fail (the
    // production caller reaches the OS; see `windows_raw::resolve::windows_system_dirs`) must not
    // be able to break resolving one. Panics, rather than returning `Err`, if `system_dirs` is ever
    // called here: a call that happened to return `Err` and got silently absorbed would pass this
    // test and defeat the point of it.
    let cwd = tempfile::tempdir().unwrap();
    let want = touch(cwd.path(), "myapp");
    let system_dirs_fn =
        || -> Result<Vec<PathBuf>, Error> { panic!("system_dirs must not be queried for a Located name") };
    let got = resolve(ResolveInput {
        program: Path::new("./myapp"),
        cwd: Some(cwd.path()),
        system_dirs: &system_dirs_fn,
        path_var: None,
        windows: true,
        loadable_only: false,
        normalise: &as_written,
    })
    .unwrap();
    assert_eq!(got.canonicalize().unwrap(), want.canonicalize().unwrap());
}

#[skuld::test]
fn a_bare_name_fails_closed_when_the_system_dirs_query_fails() {
    // The mirror image of the test above: a TRUE bare name (no separator) does reach the
    // `BareName` arm, so a failing `system_dirs` query must fail the whole resolution — the
    // specific error the query returns, not a silent fall-through to `PATH` (which would be the
    // same accidental widening `ResolveInput::system_dirs`'s doc describes for a `system_dirs` that
    // returns no directories on purpose) and not a generic `NotFound` that discards which query
    // failed and why. A matching file sits on `PATH` so a pass here could only come from swallowing
    // the error and searching `PATH` anyway, not from an honest miss.
    let cwd = tempfile::tempdir().unwrap();
    let pathdir = tempfile::tempdir().unwrap();
    touch(pathdir.path(), "tool.exe");
    let path = path_var_for(&[pathdir.path()], true);
    let system_dirs_fn =
        || -> Result<Vec<PathBuf>, Error> { Err(Error::Io(std::io::Error::other("system_dirs query failed"))) };
    let got = resolve(ResolveInput {
        program: Path::new("tool"),
        cwd: Some(cwd.path()),
        system_dirs: &system_dirs_fn,
        path_var: Some(&path),
        windows: true,
        loadable_only: false,
        normalise: &as_written,
    });
    match got {
        Err(Error::Io(e)) => {
            assert_eq!(e.to_string(), "system_dirs query failed", "{e}");
        }
        other => panic!("expected the system_dirs error to propagate verbatim, got {other:?}"),
    }
}

// ── the located axis does not search, and a miss is NotFound ─────────────────────────

#[skuld::test]
fn a_located_name_never_falls_back_to_a_search() {
    // `resolve_executable_in`'s doc promises a name containing a separator "resolves against
    // base_cwd with NO SEARCH AT ALL". Nothing gated that: making `Shape::Located` also visit
    // `system_dirs`/`PATH` passed the whole suite. The consequence is concrete —
    // `executable("./helper")` with no `./helper` present would silently load a `helper.exe` from
    // `PATH`, a file the caller explicitly did not name.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    // A decoy the search WOULD find, under both located candidate spellings.
    touch(bin.path(), "helper");
    touch(bin.path(), "helper.exe");
    let pv = path_var_for(&[bin.path()], true);
    // A miss, not a refusal: `./helper` is a perfectly good name, it just is not there.
    assert_not_found("./helper", go_win_path("./helper", cwd.path(), Some(&pv)));
}

#[skuld::test]
fn a_miss_is_reported_as_not_found() {
    // `resolve_executable_in`'s doc promises `ErrorKind::NotFound` for a miss. Nothing asserted
    // the kind, so changing it was invisible. (A drive-relative name is a REFUSAL, not a miss —
    // see the module doc's kind rule and `a_drive_relative_name_is_refused_on_shape`.)
    let cwd = tempfile::tempdir().unwrap();
    let err = go_win_path("no-such-program-41d9", cwd.path(), None).unwrap_err();
    match err {
        Error::Io(e) => assert_eq!(e.kind(), std::io::ErrorKind::NotFound, "{e:?}"),
        other => panic!("a miss must be an Io(NotFound), got {other:?}"),
    }
}

#[skuld::test]
fn a_directory_named_like_the_program_is_not_returned() {
    // `is_execable` keys on `is_file()`, not `exists()`. It matters most on POSIX, where
    // `faccessat(X_OK)` SUCCEEDS on a directory — so an `exists()`-based check would hand a
    // directory to exec. Both existing gates for this live in the `#[cfg(windows)]` module, which
    // is the wrong platform for the hazard.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    std::fs::create_dir(bin.path().join(exe_name("tool"))).unwrap();
    let p = path_var(&[bin.path()]);
    // A directory is not a match, so this is an ordinary miss.
    assert_not_found("tool", go("tool", cwd.path(), Some(&p)));

    // Positive control: a real file in the same slot DOES resolve, so the assertion above is
    // gating `is_file()` rather than an unrelated lookup failure.
    let bin2 = tempfile::tempdir().unwrap();
    let want = touch(bin2.path(), &exe_name("tool"));
    let p2 = path_var(&[bin2.path()]);
    assert_eq!(go("tool", cwd.path(), Some(&p2)).unwrap(), want);
}

#[skuld::test]
fn a_separator_terminated_located_name_gets_no_exe_fallback() {
    // THE WIDENING GUARD. Appending to a name that ends in a separator produces a DOTFILE INSIDE
    // the named directory — `C:\tools\thing.bin\.exe` — which a writer of that directory can
    // plant, and which `main` never looked for (it appended via `Path::with_extension`, a no-op
    // when the path has no file name). Catches both halves: treating the empty final component as
    // "no extension", and appending a raw string instead of replacing an extension.
    for n in [r"tools\thing.bin\", r"bin\tool\", "bin/tool/", r"C:\"] {
        let got = candidate(n, true);
        assert_eq!(
            got,
            vec![n.to_string()],
            "{n:?} must yield exactly one candidate: {got:?}"
        );
    }
}

#[skuld::test]
fn an_empty_program_never_resolves() {
    // `classify("")` is a bare name, and the `.exe` rule turned it into the single candidate
    // `.exe` — so `executable("")` resolved to any file literally named `.exe` sitting in a PATH
    // or system directory, which is trivially plantable. An empty name names no file; it must
    // fail closed on every axis.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    touch(bin.path(), ".exe");
    let pv = path_var_for(&[bin.path()], true);
    assert_refused_on_shape("", go_win_path("", cwd.path(), Some(&pv)));
    assert_refused_on_shape("", go_win_path("", cwd.path(), None));
}

/// A refused name must be refused for its SHAPE, not merely missed by the search. Asserting
/// `is_err()` alone passes vacuously — the file does not exist either — so it would not have
/// caught the planted-sibling case at all. See `resolve`'s module doc for the kind rule.
fn assert_refused_on_shape(name: &str, got: Result<std::path::PathBuf, Error>) {
    match got {
        Err(Error::Io(e)) if e.kind() == std::io::ErrorKind::InvalidInput => {}
        other => panic!("{name:?} names no file and must be refused on shape, got {other:?}"),
    }
}

/// The other half of the kind rule: the name was ACCEPTED and searched, and nothing matched.
fn assert_not_found(name: &str, got: Result<std::path::PathBuf, Error>) {
    match got {
        Err(Error::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => {}
        other => panic!("{name:?} was searched and missed, so it must be NotFound, got {other:?}"),
    }
}

#[skuld::test]
fn a_drive_relative_name_is_refused_on_shape() {
    // `C:tool` names a file relative to drive C's OWN current directory — state cosca does not
    // track and never will, so no filesystem can make it resolve. Under the kind rule that is a
    // refusal, not a search miss. It reported `NotFound` only incidentally: `PathBuf::push` clears
    // for a prefixed path, so every candidate failed `resolve`'s `is_absolute()` filter and the
    // loop fell through to the generic trailing error, which would have changed kind silently if
    // that filter ever did.
    let cwd = tempfile::tempdir().unwrap();
    for n in ["C:tool", "D:sub/x", "C:tool.exe", r"Z:a\b", "1:tool", "\u{e9}:tool"] {
        assert_refused_on_shape(n, go_win_path(n, cwd.path(), None));
    }
}

#[skuld::test]
fn an_accepted_name_that_is_simply_absent_is_not_found() {
    // The negative control for every refusal in this block, and the other half of the kind rule:
    // each of these IS accepted, IS searched, and merely misses — a different disk (or `PATH`)
    // resolves any of them, so none may be reported as refused.
    let cwd = tempfile::tempdir().unwrap();
    for n in [
        "tool",
        // `std` recognises a verbatim drive whichever separator follows it, so this is
        // `\\?\C:` plus a path, not a bare prefix. Pins the parser against `std`'s own rule.
        r"\\?\C:/x",
        "./missing",
        r"C:\abs\missing.exe",
        // A dot-run INTERIOR component is an ordinary directory name: searched, not refused.
        r"C:\dir\...\tool.exe",
        r"sub\missing",
        "sub/missing",
    ] {
        assert_not_found(n, go_win_path(n, cwd.path(), None));
    }
    assert_not_found("tool", go(&exe_name("tool"), cwd.path(), None));
}

#[skuld::test]
fn a_program_with_no_stem_is_refused() {
    // A path whose final component is empty, `.` or `..` names a DIRECTORY, so it cannot name an
    // executable — and resolving it anyway is not merely futile, it invents names. `C:\t\.` grew
    // the candidate `C:\t\..exe`, a file in `C:\t` that the caller never wrote; a bare `.` grew
    // `..exe` and searched `PATH` for it. Refusing on the shape closes every such spelling at
    // once, where guarding one candidate rule at a time leaves the next one open.
    let cwd = tempfile::tempdir().unwrap();
    for n in [
        "",
        r"C:\t\thing.bin\",
        r"C:\t\dir\",
        "bin/",
        r"C:\t\.",
        r"C:\t\..",
        ".",
        "..",
        r"C:\",
        "C:",
        "C:.",
        "C:..",
    ] {
        assert_refused_on_shape(n, go_win_path(n, cwd.path(), None));
    }
}

#[skuld::test]
fn a_stemless_program_is_refused_on_posix_too() {
    // The rule is not a Windows one: `execvp("bin/")`, `execvp(".")` and `execvp("..")` fail for
    // the same reason. Only the separator set is platform-dependent, so `bin\` stays a legitimate
    // single-component filename here — a backslash is an ordinary character on POSIX.
    let cwd = tempfile::tempdir().unwrap();
    for n in ["", "bin/", ".", "..", "/"] {
        assert_refused_on_shape(n, go(n, cwd.path(), None));
    }
}

#[skuld::test]
fn a_dot_terminated_name_never_invents_a_planted_sibling() {
    // The live half of the bug, on the filesystem: with `..exe` planted beside it, `C:\t\.` used
    // to resolve to that file. The directory it names is real and the plant is real, so nothing
    // but the stem rule stops this one.
    let cwd = tempfile::tempdir().unwrap();
    let dir = cwd.path().join("t");
    std::fs::create_dir(&dir).unwrap();
    touch(&dir, "..exe");
    let named = format!("{}/.", dir.display());
    assert_refused_on_shape(&named, go_win_path(&named, cwd.path(), None));
}

#[skuld::test]
fn a_trailing_dot_or_space_is_an_ordinary_name_on_both_platforms() {
    // A trailing dot or space is an ordinary filename character on either platform, so each of
    // these names a file and gets searched for. Win32's trimming is not modelled: it describes a
    // path STRING entering an API, and Microsoft's own rule concedes such a file CAN be created.
    let cwd = tempfile::tempdir().unwrap();
    for n in [
        "...",
        "....",
        ". ",
        " .",
        ".. ",
        "   ",
        "tool.",
        "tool ",
        r"C:\t\...",
        r"C:\t\. ",
        r"C:\t\dir\.. ",
    ] {
        assert!(!names_no_file(OsStr::new(n), true), "{n:?} names a file on Windows");
        assert_not_found(n, go_win_path(n, cwd.path(), None));
    }
    for n in ["tool.", "tool ", "...", ". ", " .", "....", "a.", "bin/tool."] {
        assert!(!names_no_file(OsStr::new(n), false), "{n:?} names a file on POSIX");
        assert_eq!(candidate(n, false), vec![n.to_string()], "POSIX never rewrites a name");
    }
}

#[skuld::test]
fn a_dot_run_name_is_searched_for_like_any_other_bare_name() {
    // The live half, on the filesystem: `...` is a NAME, so the bare rule appends `.exe` to it and
    // the search finds `....exe` exactly as it finds `tool.exe` for `tool`. Not a plantable
    // invention — the candidate is the caller's own bytes plus the documented suffix, and a writer
    // who can drop `....exe` into a searched directory can drop `tool.exe` there just as easily.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    let planted = touch(bin.path(), "....exe");
    let pv = path_var_for(&[bin.path()], true);
    let got = go_win_path("...", cwd.path(), Some(&pv)).expect("`...` names a file and must resolve");
    assert_eq!(got, planted, "{got:?}");
}

#[skuld::test]
fn the_exe_rule_appends_to_the_name_as_written() {
    // `.exe` goes onto the name the caller wrote, unmodified: `tool.` -> `tool..exe` is the SAME
    // rule as `tool` -> `tool.exe`. Trimming the dot first would search every system and `PATH`
    // directory for `tool.exe`, a DIFFERENT file from the one named.
    assert_eq!(candidate("tool.", true), vec!["tool..exe"]);
    assert_eq!(candidate("tool ", true), vec!["tool .exe"]);
    assert_eq!(candidate("tool. ", true), vec!["tool. .exe"]);
    assert_eq!(candidate("...", true), vec!["....exe"]);
    // `tool.exe.` does not END in `.exe`, so the allowlist does not match and the rule applies.
    assert_eq!(candidate("tool.exe.", true), vec!["tool.exe..exe"]);
    // LOCATED: `tool ` carries no extension, so it keeps the portable fallback — `main`, keying on
    // `Path::extension()`, looked for `bin\tool .exe` too.
    assert_eq!(candidate(r"bin\tool ", true), vec![r"bin\tool ", r"bin\tool .exe"]);
    // `tool.` carries an empty one (`Path::extension()` is `Some("")`), so it gets no fallback —
    // the same rule that gives `tools\thing.bin` exactly one candidate.
    assert_eq!(candidate(r"bin\tool.", true), vec![r"bin\tool."]);
    // The trailing space keeps the fallback wherever the component sits — including under a
    // dot-run directory, which is an ordinary interior component and changes nothing.
    assert_eq!(
        candidate(r"C:\dir\...\tool ", true),
        vec![r"C:\dir\...\tool ", r"C:\dir\...\tool .exe"]
    );
}

#[skuld::test]
fn a_prefix_only_located_name_is_refused_like_a_drive_root() {
    // A share, a volume or a device namespace is a ROOT, not a file — the same shape as `C:\`,
    // which was already refused while these were not. On Windows `Path::file_name()` is `None`
    // for every one of them (they parse as a prefix plus a root, with no `Normal` component), so
    // `main`'s `with_extension("exe")` was a NO-OP — `set_extension` returns false when there is
    // no file stem — and `main` never looked for `\\server\share.exe`. Stripping only a DRIVE
    // prefix read `share` as a filename and grew exactly that candidate.
    let cwd = tempfile::tempdir().unwrap();
    for n in [
        r"\\server\share",
        r"\\server\share\",
        "//server/share",
        r"\\?\C:",
        r"\\?\C:\",
        r"\\?\UNC\server\share",
        r"\\?\GLOBALROOT",
        r"\\.\pipe",
        r"\\",
        r"\\?\",
    ] {
        assert_refused_on_shape(n, go_win_path(n, cwd.path(), None));
    }
}

#[skuld::test]
fn a_verbatim_prefix_is_split_on_the_same_separators_as_the_rest_of_the_string() {
    // ONE string must not parse under TWO separator sets. Reading a verbatim prefix with a
    // `\`-only rule while the final component is split on `/` and `\` alike let the prefix swallow
    // `srv/shr\a` whole: the final component came out empty, and `resolve` refused a path that
    // names the file `a`. `PureWindowsPath` — this module's reference for what a path NAMES —
    // reports `a` for every input below.
    for (n, want) in [
        (r"\\?\UNC\srv/shr\a", "a"),
        (r"\\?\UNC\srv\shr/a", "a"),
        (r"\\?\UNC\srv/shr/a", "a"),
        (r"\\?\ns/a", "a"),
        (r"\\?\GLOBALROOT/Device/X/tool.exe", "tool.exe"),
        // `UNC` marks a UNC path only before `\`, as NT reads it, so these are the namespace
        // `UNC` and the names after it, not a share root. std's `parse_prefix` rewrites `/` to `\`
        // in the first eight bytes and would read a share root.
        (r"\\?\UNC/srv\tool.exe", "tool.exe"),
        (r"\\?\UNC/server\share", "share"),
        (r"\\?\UNC/srv/shr", "shr"),
    ] {
        let got = final_component(OsStr::new(n), true);
        assert_eq!(got, want.as_bytes(), "{n:?} -> {:?}", String::from_utf8_lossy(got));
        assert!(!names_no_file(OsStr::new(n), true), "{n:?} names a file");
    }
    // The other half of the same rule, so widening the separator set cannot be "fixed" by dropping
    // the prefix parse: a share root is still a root however its halves are spelt, and
    // `PureWindowsPath` names nothing for any of these either. The marker is matched
    // case-insensitively, as NT matches it.
    for n in [
        r"\\?\UNC\srv/shr",
        r"\\?\UNC\srv\shr",
        r"\\?\unc\srv\shr",
        r"\\?\ns",
        r"\\?\GLOBALROOT",
    ] {
        assert!(names_no_file(OsStr::new(n), true), "{n:?} names no file");
    }
}

#[skuld::test]
fn a_prefix_only_located_name_never_grows_an_exe_candidate() {
    // `resolve` refuses these before `filename_candidates` runs; this pins the candidate rule
    // itself, which `takes_the_exe_fallback`'s own doc promises is defence in depth for any
    // future caller that does not go through `resolve`.
    for n in [r"\\server\share", r"\\?\C:", r"\\?\UNC\server\share", r"\\.\pipe"] {
        let got = candidate(n, true);
        assert_eq!(got, vec![n.to_string()], "{n:?} must not grow a candidate: {got:?}");
    }
}

#[skuld::test]
fn a_name_that_names_no_file_grows_no_candidate_on_either_axis() {
    // `resolve` refuses all of these before `filename_candidates` runs, but the candidate rule
    // must not depend on that. The two axes must answer with ONE predicate: a `.`-terminated
    // LOCATED name reads its final component as `.` exactly as a bare `.` does, and appending
    // there yields `C:\t\..exe` — the plant `resolve`'s refusal exists to prevent, and the very
    // file `a_dot_terminated_name_never_invents_a_planted_sibling` drops on disk.
    for n in [
        "",
        ".",
        "..",
        r"C:\t\.",
        r"C:\t\..",
        r"a\.",
        "./.",
        r"\\server\share\.",
        r"\\?\C:\.",
        r"\\.\pipe\.",
        r"C:\t\dir\",
    ] {
        assert_eq!(candidate(n, true), vec![n.to_string()], "{n:?}");
    }
}

#[skuld::test]
fn a_name_under_a_windows_prefix_is_still_a_filename() {
    // The negative control: only the PREFIX itself is not a filename. Everything below it is one,
    // with the ordinary candidate rule — including `\\.\pipe\x`, whose `x` IS a `Normal`
    // component on Windows, so `main` appended there too and this stays main-parity.
    for (n, want) in [
        (
            r"\\server\share\tool",
            vec![r"\\server\share\tool", r"\\server\share\tool.exe"],
        ),
        (r"\\?\C:\tools\thing.bin", vec![r"\\?\C:\tools\thing.bin"]),
        (r"\\?\UNC\server\share\tool.exe", vec![r"\\?\UNC\server\share\tool.exe"]),
        (r"\\.\pipe\x", vec![r"\\.\pipe\x", r"\\.\pipe\x.exe"]),
    ] {
        assert!(!names_no_file(OsStr::new(n), true), "{n:?} names a file");
        let got = candidate(n, true);
        assert_eq!(got, want, "{n:?} -> {got:?}");
    }
}

/// `#[cfg(unix)]`, not a `windows: false` simulation: this touches the filesystem, and
/// POSIX-simulating on a Windows host shreds every candidate on its drive colon (see
/// `HOST_WINDOWS`). On a POSIX host `go` already applies the POSIX rules.
#[cfg(unix)]
#[skuld::test]
fn a_posix_name_with_a_stem_still_resolves() {
    // The POSIX twin, and the negative control for the separator set — the one half of the stem
    // rule that is platform-dependent. A backslash is an ordinary filename character here, so
    // `bin\` and `a\.` are single-component names WITH a stem: a rule reading them through the
    // Windows separator set would see `bin/` and `a/.` and refuse both.
    let cwd = tempfile::tempdir().unwrap();
    let bin = tempfile::tempdir().unwrap();
    let pv = path_var_for(&[bin.path()], false);
    for n in [r"bin\", r"a\.", ".helper", "..helper"] {
        touch(bin.path(), n);
        let got = go(n, cwd.path(), Some(&pv));
        assert!(got.is_ok(), "{n:?} has a stem on POSIX and must resolve, got {got:?}");
    }
}

#[skuld::test]
fn a_name_with_a_stem_still_resolves() {
    // Negative control for the three tests above: the refusal must key on a MISSING stem, not on
    // a leading dot. `.helper` and `..helper` are ordinary filenames and must survive.
    let cwd = tempfile::tempdir().unwrap();
    let bin = cwd.path().join("bin");
    std::fs::create_dir(&bin).unwrap();
    for n in [".helper", "..helper", "tool.exe"] {
        touch(&bin, n);
        let got = go_win_path(&format!("{}/{}", bin.display(), n), cwd.path(), None);
        assert!(got.is_ok(), "{n:?} has a stem and must resolve, got {got:?}");
    }
}

// ===== the elevated image allowlist =====

/// The allowlist itself; see [`reject_unloadable_image`]'s doc for why only the name can be gated.
#[skuld::test]
fn an_image_without_a_loadable_extension_is_refused() {
    for n in [
        r"C:\dir\tool",
        r"C:\dir\tool.bat",
        r"C:\dir\tool.cmd",
        r"C:\dir\tool.exe.bat",
        r"C:\dir\tool.exe.",
        r"C:\dir\tool.bin",
    ] {
        assert!(
            reject_unloadable_image(Path::new(n), true).is_err(),
            "{n:?} is not a loadable image name and must be refused"
        );
    }
}

/// An allowlist refuses every other registered association by construction; a denylist of script
/// extensions would let these through.
#[skuld::test]
fn the_elevated_allowlist_also_refuses_the_wider_association_surface() {
    for n in [
        r"C:\dir\shortcut.lnk",
        r"C:\dir\script.vbs",
        r"C:\dir\script.ps1",
        r"C:\dir\script.js",
        r"C:\dir\page.hta",
        r"C:\dir\installer.msi",
    ] {
        assert!(
            reject_unloadable_image(Path::new(n), true).is_err(),
            "{n:?} is a shell association, not a loadable image, and must be refused"
        );
    }
}

/// Negative control, case-insensitive. `.com` because `more.com`/`chcp.com`/`tree.com` ship in
/// System32 as ordinary PEs.
#[skuld::test]
fn a_loadable_image_name_is_accepted() {
    for n in [
        r"C:\dir\tool.exe",
        r"C:\dir\TOOL.EXE",
        r"C:\dir\Tool.Exe",
        r"C:\dir\more.com",
        r"C:\dir\MORE.COM",
        r"C:\dir\tool.bat.exe",
    ] {
        assert!(
            reject_unloadable_image(Path::new(n), true).is_ok(),
            "{n:?} is a loadable image name and must be accepted"
        );
    }
}

/// Kills dropping the `names_no_file` guard: without it the whole-string suffix test accepts a
/// share root whose share happens to be named `*.exe`.
#[skuld::test]
fn a_prefix_ending_in_exe_is_not_an_image_name() {
    for n in [r"\\server\share.exe", r"\\?\UNC\server\share.com"] {
        assert!(
            reject_unloadable_image(Path::new(n), true).is_err(),
            "{n:?} names no file and must be refused"
        );
    }
}