targeted-mutation 0.14.0

Targeted mutation testing for Rust: find the wrong implementations your tests let through
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
//! Isolated working copy synchronization, fixture confinement, module graph, and mark files.

use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use anyhow::{Context, Result, bail};

use crate::git_index;

use super::*;

pub(super) fn isolate_copy_command(command: &mut Command, work: &Path) {
    git_index::remove_git_overrides(command);
    command.env("GIT_CEILING_DIRECTORIES", work);
}

/// Name of the log of written files, beside the working copy.
pub(super) const WRITTEN_LOG: &str = ".tmt-written";

/// The working copy and what the runs did to it (formal/tla/WorkCopy.tla).
///
/// Most of the copy is plain copies of the user's files. The files a run instruments
/// are rewritten, and the log names them. The next run leaves those alone when it brings
/// the copy up to date, renders them again from the user's files, and writes only what
/// came out different: a file that keeps its modification time is not rebuilt by cargo.
/// What the log names and the run did not write again goes back to a plain copy.
pub(super) struct WorkCopy {
    pub(super) root: PathBuf,
    /// Directory inside `root` that corresponds to the user's `workspace`.
    /// Equal to `root` unless external parent-relative paths are staged above the workspace copy.
    pub(super) copy_root: PathBuf,
    pub(super) log: PathBuf,
    /// Paths of the copy that the current run wants to exist.
    pub(super) kept: RefCell<HashSet<PathBuf>>,
    /// Named by the log when the run started.
    pub(super) earlier: HashSet<PathBuf>,
    /// Those of `earlier` that the user's tree still has.
    pub(super) visited: RefCell<HashSet<PathBuf>>,
    /// Written by this run.
    pub(super) written: RefCell<HashSet<PathBuf>>,
    /// What the log names now.
    pub(super) logged: RefCell<HashSet<PathBuf>>,
}

impl WorkCopy {
    #[cfg(test)]
    pub(super) fn open(work: &Path, root: &Path) -> WorkCopy {
        Self::open_with_copy_root(work, root, root)
    }

    pub(super) fn open_with_copy_root(work: &Path, root: &Path, copy_root: &Path) -> WorkCopy {
        let log = work.join(WRITTEN_LOG);
        // The log is appended to and replaced: never through a link to somewhere else.
        for file in [log.clone(), log.with_extension("new")] {
            if fs::symlink_metadata(&file).is_ok_and(|found| found.is_symlink()) {
                let _ = fs::remove_file(&file);
            }
        }
        let earlier: HashSet<PathBuf> = fs::read_to_string(&log)
            .unwrap_or_default()
            .lines()
            .map(Path::new)
            // Nothing but a path below the copy: the entries are deleted and overwritten.
            .filter(|relative| {
                let mut parts = relative.components();
                parts.clone().next().is_some()
                    && parts.all(|part| matches!(part, std::path::Component::Normal(_)))
            })
            .map(|relative| root.join(relative))
            .collect();
        WorkCopy {
            root: root.to_path_buf(),
            copy_root: copy_root.to_path_buf(),
            log,
            kept: RefCell::default(),
            logged: RefCell::new(earlier.clone()),
            earlier,
            visited: RefCell::default(),
            written: RefCell::default(),
        }
    }

    pub(super) fn is_kept(&self, path: &Path) -> bool {
        self.kept.borrow().contains(path) || self.earlier.contains(path)
    }

    /// The text of the user's file that `path` of the copy stands for. `None` for what
    /// only an earlier run left there.
    pub(super) fn original(&self, path: &Path, workspace: &Path) -> Result<Option<String>> {
        let source = if !self.earlier.contains(path) {
            path.to_path_buf() // a plain copy
        } else if !self.visited.borrow().contains(path) {
            return Ok(None);
        } else {
            workspace.join(path.strip_prefix(&self.copy_root).unwrap_or(path))
        };
        fs::read_to_string(&source)
            .map(Some)
            .with_context(|| format!("reading {}", source.display()))
    }

    /// Writes a file of the working copy. Refuses to write anywhere else, and never
    /// through a symbolic link. The log names the file before it changes.
    pub(super) fn write(&self, path: &Path, content: &str) -> Result<()> {
        if fs::symlink_metadata(path).is_ok_and(|m| m.file_type().is_symlink()) {
            fs::remove_file(path)?;
        }
        let parent = path.parent().and_then(|p| p.canonicalize().ok());
        let inside = match (parent, self.root.canonicalize()) {
            (Some(parent), Ok(root)) => parent.starts_with(root),
            _ => false,
        };
        if !inside {
            bail!(
                "refusing to write outside the working copy: {}",
                path.display()
            );
        }
        self.record(path)?;
        if fs::read_to_string(path).is_ok_and(|existing| existing == content) {
            return Ok(()); // keep the modification time: nothing to rebuild
        }
        fs::write(path, content).with_context(|| format!("writing {}", path.display()))
    }

    pub(super) fn record(&self, path: &Path) -> Result<()> {
        self.written.borrow_mut().insert(path.to_path_buf());
        if self.logged.borrow().contains(path) {
            return Ok(());
        }
        let line = path
            .strip_prefix(&self.root)
            .ok()
            .and_then(Path::to_str)
            .filter(|text| !text.chars().any(char::is_control));
        let Some(line) = line else {
            bail!(
                "cannot instrument {}: its name cannot be written on a line of the log",
                path.display()
            );
        };
        use std::io::Write as _;
        let mut log = fs::OpenOptions::new()
            .create(true)
            .append(true)
            .open(&self.log)
            .with_context(|| format!("opening {}", self.log.display()))?;
        writeln!(log, "{line}").with_context(|| format!("writing {}", self.log.display()))?;
        self.logged.borrow_mut().insert(path.to_path_buf());
        Ok(())
    }

    /// Before the build: what earlier runs wrote and this one did not is a plain copy
    /// again (or gone, like its source), and only then leaves the log.
    pub(super) fn settle(&self, workspace: &Path) -> Result<()> {
        let written = self.written.borrow();
        let kept = self.kept.borrow();
        for path in self.earlier.difference(&written) {
            // If a previous run had a different staging layout and this run intentionally
            // placed an external file at `path` outside `copy_root`, keep the new file.
            if !path.starts_with(&self.copy_root) && kept.contains(path) {
                continue;
            }
            if fs::symlink_metadata(path).is_ok() {
                fs::remove_file(path).with_context(|| format!("removing {}", path.display()))?;
            }
            if self.visited.borrow().contains(path)
                && let Ok(relative) = path.strip_prefix(&self.copy_root)
            {
                let source = workspace.join(relative);
                copy_as_new(&source, path)?;
            }
        }
        let mut lines: Vec<&str> = written
            .iter()
            .filter_map(|path| path.strip_prefix(&self.root).ok()?.to_str())
            .collect();
        lines.sort_unstable();
        let fresh = self.log.with_extension("new");
        fs::write(
            &fresh,
            lines
                .iter()
                .map(|line| format!("{line}\n"))
                .collect::<String>(),
        )?;
        fs::rename(&fresh, &self.log)?;
        *self.logged.borrow_mut() = written.clone();
        Ok(())
    }
}

/// Makes `dst` a copy of the file `src`, leaving it alone if it already is one. A file
/// that keeps its modification time is not rebuilt by cargo, so a second run only
/// recompiles what changed.
pub(super) fn sync_file(src: &Path, dst: &Path, state: &WorkCopy) -> Result<()> {
    state.kept.borrow_mut().insert(dst.to_path_buf());
    if dst.starts_with(&state.copy_root) && state.earlier.contains(dst) {
        state.visited.borrow_mut().insert(dst.to_path_buf());
        if fs::symlink_metadata(dst).is_ok_and(|m| m.is_file()) {
            // Written by an earlier run: this one decides what becomes of it.
            return Ok(());
        }
    }
    // Times say nothing certain: a file put back from an archive is older than the copy
    // of what it replaces. A copy is what holds the same bytes.
    // A link is not a copy, whatever it points to.
    let up_to_date = fs::symlink_metadata(dst).is_ok_and(|to| to.is_file()) && same_bytes(src, dst);
    if up_to_date {
        return Ok(());
    }
    if let Ok(existing) = fs::symlink_metadata(dst) {
        if existing.is_dir() {
            fs::remove_dir_all(dst)?;
        } else {
            fs::remove_file(dst)?;
        }
    }
    copy_as_new(src, dst)
}

/// Whether two files hold the same bytes. One that cannot be read is not the same.
pub(super) fn same_bytes(a: &Path, b: &Path) -> bool {
    let (Ok(a), Ok(b)) = (fs::File::open(a), fs::File::open(b)) else {
        return false;
    };
    if let (Ok(la), Ok(lb)) = (a.metadata(), b.metadata())
        && la.is_file()
        && lb.is_file()
        && la.len() != lb.len()
    {
        return false;
    }
    compare_streams(a, b).unwrap_or(false)
}

/// Makes `dir` a directory of the working copy. Whatever else is in the way below the
/// copy's root goes first, a link above all: a link is followed, and what is written
/// "into the copy" would land where it points.
pub(super) fn real_dir(dir: &Path, state: &WorkCopy) -> Result<()> {
    if let Ok(below) = dir.strip_prefix(&state.root) {
        let mut path = state.root.clone();
        for part in below.components() {
            path.push(part);
            match fs::symlink_metadata(&path) {
                Ok(found) if !found.is_dir() => fs::remove_file(&path)
                    .with_context(|| format!("removing {}", path.display()))?,
                _ => {}
            }
        }
    }
    fs::create_dir_all(dir).with_context(|| format!("creating {}", dir.display()))
}

/// Copies a file and gives the copy the current time. Some systems copy the modification
/// time along, and a file that changed in the working copy but looks older than the last
/// build would not be rebuilt.
pub(super) fn copy_as_new(src: &Path, dst: &Path) -> Result<()> {
    fs::copy(src, dst).with_context(|| format!("copying {}", src.display()))?;
    // Opened for reading: the copy of a read-only file is read-only too.
    fs::File::open(dst)
        .and_then(|file| file.set_modified(std::time::SystemTime::now()))
        .with_context(|| format!("touching {}", dst.display()))
}

/// Removes from the working copy whatever the current run did not put there.
pub(super) fn prune(dir: &Path, state: &WorkCopy) -> Result<bool> {
    let mut empty = true;
    for entry in fs::read_dir(dir)? {
        let path = entry?.path();
        let is_dir = fs::symlink_metadata(&path)?.is_dir();
        if is_dir {
            if prune(&path, state)? && !state.is_kept(&path) {
                fs::remove_dir(&path)?;
            } else {
                empty = false;
            }
        } else if state.is_kept(&path) {
            empty = false;
        } else {
            fs::remove_file(&path)?;
        }
    }
    Ok(empty)
}

/// Copies one file, directory or symbolic link into the working copy.
///
/// The copy never contains a symbolic link: instrumented sources are written into it, and
/// a link would send those writes back to the user's files. A link to a file becomes a
/// plain file with the same content. A link to a directory is followed only when the
/// directory lies inside `workspace` and does not contain the link itself.
pub(super) fn copy_entry(
    src: &Path,
    dst: &Path,
    workspace: &Path,
    skip: &dyn Fn(&Path) -> bool,
    state: &WorkCopy,
) -> Result<()> {
    let kind = fs::symlink_metadata(src)?.file_type();
    if kind.is_symlink() {
        let Ok(target) = src.canonicalize() else {
            return Ok(()); // dangling
        };
        // Only check skipped ancestors strictly below `workspace` so a workspace situated
        // under a directory named e.g. `node_modules` does not drop its own symlinks.
        let skipped = if target.starts_with(workspace) {
            target
                .ancestors()
                .take_while(|ancestor| *ancestor != workspace && ancestor.starts_with(workspace))
                .any(&skip)
        } else {
            skip(&target)
        };
        if skipped {
            return Ok(());
        }
        if target.is_dir() {
            let contains_link = src
                .parent()
                .is_some_and(|dir| dir.canonicalize().is_ok_and(|dir| dir.starts_with(&target)));
            if !target.starts_with(workspace) || contains_link {
                return Ok(());
            }
            return copy_tree(&target, dst, workspace, skip, state);
        }
        if target.is_file() {
            sync_file(&target, dst, state)?;
        }
    } else if kind.is_dir() {
        // Build output and other caches (cargo marks its target directories this way).
        if !src.join("CACHEDIR.TAG").exists() {
            copy_tree(src, dst, workspace, skip, state)?;
        }
    } else if kind.is_file() {
        sync_file(src, dst, state)?;
    }
    Ok(())
}

pub(super) fn copy_tree(
    from: &Path,
    to: &Path,
    workspace: &Path,
    skip: &dyn Fn(&Path) -> bool,
    state: &WorkCopy,
) -> Result<()> {
    real_dir(to, state)?;
    for entry in fs::read_dir(from).with_context(|| format!("reading {}", from.display()))? {
        let src = entry?.path();
        if skip(&src) {
            continue;
        }
        let Some(name) = src.file_name() else {
            continue;
        };
        copy_entry(&src, &to.join(name), workspace, skip, state)?;
    }
    Ok(())
}

/// An external file or directory staged relative to the isolated workspace copy,
/// for monorepo packages whose `build.rs` or `include_str!` references parent paths.
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub struct StageExternal {
    /// Source path (workspace-relative or absolute).
    pub from: PathBuf,
    /// Relative destination from the isolated workspace copy (must ascend via `..`).
    pub rel: PathBuf,
}

impl std::str::FromStr for StageExternal {
    type Err = anyhow::Error;

    fn from_str(text: &str) -> Result<Self> {
        if text.bytes().any(|b| b < 0x20 || b == 0x7f) {
            bail!("--stage-external must not contain control characters");
        }
        let (from, rel) = match text.split_once('=') {
            Some((from, rel)) => (from, rel),
            None => (text, text),
        };
        if from.is_empty() {
            bail!("--stage-external source path must not be empty");
        }
        let rel = validate_external_rel(Path::new(rel))?;
        Ok(Self {
            from: PathBuf::from(from),
            rel,
        })
    }
}

/// Validates and normalizes a parent-relative staging target (e.g. `../../proto`).
/// Returns `(normalized_rel, parent_depth)`.
pub(super) fn parse_external_rel(path: &Path) -> Result<(PathBuf, usize)> {
    use std::path::Component;
    let mut parents = 0usize;
    let mut tail: Vec<std::ffi::OsString> = Vec::new();
    for part in path.components() {
        match part {
            Component::CurDir => {}
            Component::ParentDir => {
                if tail.pop().is_none() {
                    parents += 1;
                }
            }
            Component::Normal(name) => tail.push(name.to_os_string()),
            Component::RootDir | Component::Prefix(_) => {
                bail!(
                    "--stage-external destination must be a relative path: {}",
                    path.display()
                );
            }
        }
    }
    if parents == 0 {
        bail!(
            "--stage-external destination must escape the workspace via `..` (use --copy-fixture for paths inside the workspace): {}",
            path.display()
        );
    }
    if tail.is_empty() {
        bail!(
            "--stage-external destination must name a file or directory under a parent, not the parent itself: {}",
            path.display()
        );
    }
    let mut normalized = PathBuf::new();
    for _ in 0..parents {
        normalized.push("..");
    }
    for name in tail {
        normalized.push(name);
    }
    Ok((normalized, parents))
}

fn validate_external_rel(path: &Path) -> Result<PathBuf> {
    parse_external_rel(path).map(|(normalized, _)| normalized)
}

/// Computes the subdirectory inside `<work>/src` where the workspace copy should live
/// so that every `--stage-external` parent-relative target stays confined under `<work>/src`.
pub(super) fn staged_copy_subdir(workspace: &Path, staged: &[StageExternal]) -> Result<PathBuf> {
    if staged.is_empty() {
        return Ok(PathBuf::new());
    }
    let mut max_depth = 0usize;
    for entry in staged {
        let (_, depth) = parse_external_rel(&entry.rel)?;
        max_depth = max_depth.max(depth);
    }
    let repo_prefix = super::repository_prefix(workspace);
    let mut parts: Vec<String> = Path::new(&repo_prefix)
        .components()
        .filter_map(|part| match part {
            std::path::Component::Normal(name) => Some(name.to_string_lossy().into_owned()),
            _ => None,
        })
        .collect();
    let mut pad = 0usize;
    while parts.len() < max_depth {
        parts.insert(0, format!("_parent_{pad}"));
        pad += 1;
    }
    Ok(parts.into_iter().collect())
}

/// Case-folded component comparison to prevent collisions on case-insensitive filesystems.
pub(super) fn paths_overlap_case_insensitive(a: &Path, b: &Path) -> bool {
    let fold = |p: &Path| -> Vec<String> {
        p.components()
            .filter_map(|c| match c {
                std::path::Component::Normal(name) => Some(name.to_string_lossy().to_lowercase()),
                _ => None,
            })
            .collect()
    };
    let a = fold(a);
    let b = fold(b);
    let min_len = a.len().min(b.len());
    min_len > 0 && a[..min_len] == b[..min_len]
}

/// Copies an external file or directory into the confined staging area under `<work>/src`.
/// Symlinks inside a staged directory are confined to the canonical root of the staged source.
pub(super) fn copy_external_entry(
    from: &Path,
    to: &Path,
    external_root: Option<&Path>,
    work: &Path,
    forbidden: &dyn Fn(&Path) -> bool,
    state: &WorkCopy,
    visiting: &mut BTreeSet<PathBuf>,
) -> Result<()> {
    let resolved = from
        .canonicalize()
        .with_context(|| format!("resolving external path {}", from.display()))?;
    let confined_root = external_root.unwrap_or(&resolved);
    if !resolved.starts_with(confined_root) {
        bail!(
            "--stage-external entry {} escapes its staged root {}",
            from.display(),
            confined_root.display()
        );
    }
    if resolved.starts_with(work)
        || resolved.components().any(|part| part.as_os_str() == ".git")
        || forbidden(&resolved)
    {
        bail!(
            "--stage-external source resolves to a forbidden path: {}",
            resolved.display()
        );
    }
    let kind = fs::metadata(&resolved)?.file_type();
    if let Some(parent) = to.parent() {
        real_dir(parent, state)?;
    }
    if kind.is_file() {
        return sync_file(&resolved, to, state);
    }
    if !kind.is_dir() || !visiting.insert(resolved.clone()) {
        bail!(
            "--stage-external source {} is a cycle or is not a regular file or directory",
            from.display()
        );
    }
    real_dir(to, state)?;
    state.kept.borrow_mut().insert(to.to_path_buf());
    for entry in
        fs::read_dir(&resolved).with_context(|| format!("reading {}", resolved.display()))?
    {
        let entry = entry?;
        let path = entry.path();
        if path
            .file_name()
            .is_some_and(|n| n == ".git" || n == "node_modules")
            || (path.is_dir() && path.join("CACHEDIR.TAG").exists())
            || forbidden(&path)
        {
            continue;
        }
        copy_external_entry(
            &path,
            &to.join(entry.file_name()),
            Some(confined_root),
            work,
            forbidden,
            state,
            visiting,
        )?;
    }
    visiting.remove(&resolved);
    Ok(())
}

pub(super) fn fixture_relative(path: &Path) -> Result<PathBuf> {
    use std::path::Component;
    let relative = path
        .components()
        .filter(|part| *part != Component::CurDir)
        .collect::<PathBuf>();
    if relative.as_os_str().is_empty()
        || relative
            .components()
            .any(|part| !matches!(part, Component::Normal(_)))
    {
        bail!(
            "fixture path must be inside the workspace: {}",
            path.display()
        );
    }
    Ok(relative)
}

pub(super) fn wrapper_relative(path: &Path) -> Result<PathBuf> {
    use std::path::Component;
    let raw = path.to_string_lossy();
    if raw.bytes().any(|b| b < 0x20 || b == 0x7f) {
        bail!(
            "--test-wrapper must not contain control characters: {}",
            path.display()
        );
    }
    let relative = path
        .components()
        .filter(|part| *part != Component::CurDir)
        .collect::<PathBuf>();
    if relative.as_os_str().is_empty()
        || relative
            .components()
            .any(|part| !matches!(part, Component::Normal(_)))
    {
        bail!(
            "--test-wrapper must be a path inside the workspace: {}",
            path.display()
        );
    }
    Ok(relative)
}

pub(super) fn fixture_exclusions(
    workspace: &Path,
    roots: &[PathBuf],
    excluded: &[PathBuf],
) -> Result<BTreeSet<PathBuf>> {
    excluded
        .iter()
        .map(|path| {
            let relative = fixture_relative(path)?;
            if !roots
                .iter()
                .any(|root| relative != *root && relative.starts_with(root))
            {
                bail!(
                    "--copy-fixture-exclude must name an entry under a --copy-fixture: {}",
                    path.display()
                );
            }
            let path = workspace.join(relative);
            fs::symlink_metadata(&path)
                .with_context(|| format!("excluding fixture {}", path.display()))?;
            confined_fixture_path(workspace, &path)?;
            Ok(path)
        })
        .collect()
}

pub(super) fn confined_fixture_path(workspace: &Path, path: &Path) -> Result<()> {
    use std::collections::VecDeque;
    use std::path::Component;

    let parts = |relative: &Path| {
        relative
            .components()
            .map(|part| part.as_os_str().to_owned())
            .collect::<Vec<_>>()
    };
    let mut pending = VecDeque::from(parts(path.strip_prefix(workspace)?));
    let mut resolved = workspace.to_path_buf();
    let mut hops = 0;
    while let Some(part) = pending.pop_front() {
        match Path::new(&part).components().next() {
            Some(Component::ParentDir) if resolved != workspace => {
                resolved.pop();
            }
            Some(Component::CurDir) => {}
            Some(Component::Normal(_)) => {
                resolved.push(part);
                match fs::symlink_metadata(&resolved) {
                    Ok(metadata) if metadata.is_symlink() => {
                        hops += 1;
                        if hops > 40 {
                            bail!("fixture {} contains a symbolic link cycle", path.display());
                        }
                        let target = fs::read_link(&resolved)?;
                        resolved.pop();
                        let target = if target.is_absolute() {
                            resolved = workspace.to_path_buf();
                            target.strip_prefix(workspace).with_context(|| {
                                format!("fixture {} points outside the workspace", path.display())
                            })?
                        } else {
                            &target
                        };
                        for component in parts(target).into_iter().rev() {
                            pending.push_front(component);
                        }
                    }
                    Ok(_) => {}
                    Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
                    Err(err) => {
                        return Err(err)
                            .with_context(|| format!("checking fixture {}", path.display()));
                    }
                }
            }
            _ => bail!("fixture {} points outside the workspace", path.display()),
        }
    }
    Ok(())
}

/// An explicitly selected fixture may include ignored directories. Resolve links to
/// their contents inside the workspace, without following them into a parent cycle.
pub(super) fn copy_fixture_entry(
    from: &Path,
    to: &Path,
    workspace: &Path,
    forbidden: &dyn Fn(&Path) -> bool,
    state: &WorkCopy,
    visiting: &mut BTreeSet<PathBuf>,
) -> Result<()> {
    let resolved = from
        .canonicalize()
        .with_context(|| format!("resolving fixture {}", from.display()))?;
    let allowed = |path: &Path| {
        path.starts_with(workspace)
            && path != workspace
            && !path
                .ancestors()
                .take_while(|part| *part != workspace)
                .any(forbidden)
    };
    if !allowed(from) || !allowed(&resolved) {
        bail!(
            "fixture {} resolves to an excluded path: {}",
            from.display(),
            resolved.display()
        );
    }
    let kind = fs::metadata(&resolved)?.file_type();
    real_dir(to.parent().expect("fixture has a parent"), state)?;
    if kind.is_file() {
        return sync_file(&resolved, to, state);
    }
    if !kind.is_dir() || !visiting.insert(resolved.clone()) {
        bail!(
            "fixture {} is a cycle or is not a regular file or directory",
            from.display()
        );
    }
    real_dir(to, state)?;
    state.kept.borrow_mut().insert(to.to_path_buf());
    for entry in fs::read_dir(&resolved)? {
        let entry = entry?;
        let path = entry.path();
        let destination = to.join(entry.file_name());
        let projected = workspace.join(
            destination
                .strip_prefix(&state.root)
                .expect("fixture under copy"),
        );
        if !forbidden(&path) && !forbidden(&projected) {
            copy_fixture_entry(&path, &destination, workspace, forbidden, state, visiting)?;
        }
    }
    visiting.remove(&resolved);
    Ok(())
}

/// The files under `dir` that git tracks or would track (not ignored), relative to `dir`.
/// `None` when `dir` is not in a git work tree, or is itself ignored: then git has nothing
/// to say about which files matter.
pub(super) fn git_visible_files(dir: &Path) -> Result<Option<Vec<PathBuf>>> {
    let git = |args: &[&str]| {
        Command::new("git")
            .arg("-C")
            .arg(dir)
            .args(args)
            .stderr(Stdio::null())
            .output()
    };
    // No git, not a work tree, or a directory git ignores: nothing to go by.
    let Ok(inside) = git(&["rev-parse", "--is-inside-work-tree"]) else {
        return Ok(None);
    };
    if !inside.status.success() {
        return Ok(None);
    }
    // Exit status 0 means "ignored".
    if git(&["check-ignore", "-q", "."])?.status.success() {
        return Ok(None);
    }
    // From here on a failure must not turn into "copy everything": that would copy the
    // very files the ignore rules keep out.
    let listing = git(&[
        "ls-files",
        "--cached",
        "--others",
        "--exclude-standard",
        "-z",
    ])?;
    if !listing.status.success() {
        bail!(
            "git could not list the files of {}; pass --all-files to copy everything",
            dir.display()
        );
    }
    Ok(Some(
        listing
            .stdout
            .split(|b| *b == 0)
            .filter(|name| !name.is_empty())
            .filter_map(|name| std::str::from_utf8(name).ok())
            .map(PathBuf::from)
            .collect(),
    ))
}

/// Copies the files of `from` that git does not ignore. A submodule is listed by its
/// parent as one directory and has ignore rules of its own, so it is copied the same way.
pub(super) fn copy_git_visible(
    from: &Path,
    to: &Path,
    files: Vec<PathBuf>,
    workspace: &Path,
    skip: &dyn Fn(&Path) -> bool,
    state: &WorkCopy,
) -> Result<usize> {
    let mut copied = 0;
    for relative in files {
        let (src, dst) = (from.join(&relative), to.join(&relative));
        if skip(&src)
            || src
                .ancestors()
                .any(|a| a != from && a.starts_with(from) && skip(a))
        {
            continue;
        }
        let Ok(kind) = fs::symlink_metadata(&src).map(|m| m.file_type()) else {
            continue; // deleted in the working tree
        };
        if let Some(parent) = dst.parent() {
            real_dir(parent, state)?;
        }
        if kind.is_dir() {
            match git_visible_files(&src)? {
                Some(inner) => {
                    copied += copy_git_visible(&src, &dst, inner, workspace, skip, state)?
                }
                None => copy_tree(&src, &dst, workspace, skip, state)?,
            }
        } else {
            copy_entry(&src, &dst, workspace, skip, state)?;
            copied += 1;
        }
    }
    Ok(copied)
}

#[derive(Default)]
pub(super) struct ModuleOwners {
    pub(super) production: BTreeSet<PathBuf>,
    pub(super) consumers: BTreeSet<PathBuf>,
}

pub(super) fn source_graph(
    roots: impl IntoIterator<Item = (PathBuf, bool)>,
    copy: &Path,
    mut original: impl FnMut(&Path) -> Result<Option<String>>,
) -> Result<BTreeMap<PathBuf, ModuleOwners>> {
    use std::collections::{VecDeque, btree_map::Entry};
    let mut files: BTreeMap<PathBuf, ModuleOwners> = BTreeMap::new();
    let mut edges = BTreeMap::new();
    for (root, production) in roots {
        let directory = root.parent().expect("Cargo target directory").to_path_buf();
        let mut pending = VecDeque::from([(root.clone(), directory, production)]);
        let mut seen = HashSet::new();
        while let Some((path, directory, active)) = pending.pop_front() {
            if !seen.insert((path.clone(), directory.clone(), active)) {
                continue;
            }
            // A file can resolve relative modules differently as a target root or a module.
            // Keep eligibility from the file itself so this can also be reused by test roots.
            let children = match edges.entry((path.clone(), directory.clone())) {
                Entry::Occupied(entry) => entry.into_mut(),
                Entry::Vacant(entry) => {
                    let children = original(&path)?.map(|source| {
                        crate::modules::external(&source, &path, &directory)
                            .into_iter()
                            .filter_map(|module| {
                                let file = module
                                    .paths
                                    .iter()
                                    .map(|path| normalize(path))
                                    .find(|path| path.starts_with(copy) && path.is_file())?;
                                Some((file, normalize(&module.directory), module.production))
                            })
                            .collect::<Vec<_>>()
                    });
                    entry.insert(children)
                }
            };
            let Some(children) = children else {
                continue;
            };
            let record = files.entry(path).or_default();
            record.consumers.insert(root.clone());
            if active {
                record.production.insert(root.clone());
            }
            pending.extend(children.iter().map(|(child, directory, production)| {
                (child.clone(), directory.clone(), active && *production)
            }));
        }
    }
    files.retain(|_, owners| !owners.production.is_empty());
    Ok(files)
}

/// Resolves `.` and `..` without touching the file system.
pub(super) fn normalize(path: &Path) -> PathBuf {
    let mut out = PathBuf::new();
    for component in path.components() {
        match component {
            std::path::Component::CurDir => {}
            std::path::Component::ParentDir => {
                out.pop();
            }
            other => out.push(other),
        }
    }
    out
}

pub(super) fn relative_source(path: &Path, package: &Path) -> PathBuf {
    for (depth, ancestor) in package.ancestors().enumerate() {
        if let Ok(local) = path.strip_prefix(ancestor) {
            let mut relative: PathBuf = std::iter::repeat_n("..", depth).collect();
            relative.push(local);
            return relative;
        }
    }
    path.to_path_buf()
}

/// Rewrites `path = "../outside"` dependencies of a manifest to absolute paths when they
/// point outside `workspace`. Returns `(rewritten_manifest, external_dep_paths)`.
pub(super) fn absolutize_path_dependencies_with_externals(
    manifest: &str,
    manifest_dir: &Path,
    workspace: &Path,
    copy: &Path,
) -> Result<(Option<String>, Vec<PathBuf>)> {
    use toml_edit::{DocumentMut, Item, TableLike, value};

    fn fix(
        deps: &mut dyn TableLike,
        dirs: (&Path, &Path, &Path),
        changed: &mut bool,
        externals: &mut Vec<PathBuf>,
    ) {
        let (manifest_dir, workspace, copy) = dirs;
        for (_, dep) in deps.iter_mut() {
            let Some(dep) = dep.as_table_like_mut() else {
                continue;
            };
            let Some(path) = dep.get("path").and_then(Item::as_str) else {
                continue;
            };
            let absolute = Path::new(path).is_absolute();
            let target = normalize(&manifest_dir.join(path));
            let rewritten = match target.strip_prefix(workspace) {
                // Outside the workspace: not copied, so point at the original.
                Err(_) => {
                    externals.push(target.clone());
                    target
                }
                // Inside, but spelled as an absolute path: that is the original too, and
                // the build has to use the copy.
                Ok(inside) if absolute => copy.join(inside),
                Ok(_) => continue,
            };
            dep.insert("path", value(rewritten.to_string_lossy().into_owned()));
            *changed = true;
        }
    }
    const SECTIONS: [&str; 3] = ["dependencies", "dev-dependencies", "build-dependencies"];
    fn fix_sections(
        table: &mut dyn TableLike,
        dirs: (&Path, &Path, &Path),
        changed: &mut bool,
        externals: &mut Vec<PathBuf>,
    ) {
        for section in SECTIONS {
            if let Some(deps) = table.get_mut(section).and_then(Item::as_table_like_mut) {
                fix(deps, dirs, changed, externals);
            }
        }
    }

    let mut doc: DocumentMut = manifest.parse().context("parsing Cargo.toml")?;
    let mut changed = false;
    let mut externals = Vec::new();
    let dirs = (manifest_dir, workspace, copy);
    fix_sections(doc.as_table_mut(), dirs, &mut changed, &mut externals);
    if let Some(ws) = doc.get_mut("workspace").and_then(Item::as_table_like_mut) {
        fix_sections(ws, dirs, &mut changed, &mut externals);
    }
    for per_key in ["target", "patch"] {
        let Some(outer) = doc.get_mut(per_key).and_then(Item::as_table_like_mut) else {
            continue;
        };
        for (_, inner) in outer.iter_mut() {
            let Some(inner) = inner.as_table_like_mut() else {
                continue;
            };
            if per_key == "target" {
                fix_sections(inner, dirs, &mut changed, &mut externals);
            } else {
                fix(inner, dirs, &mut changed, &mut externals);
            }
        }
    }
    Ok((changed.then(|| doc.to_string()), externals))
}

#[cfg(test)]
pub(super) fn absolutize_path_dependencies(
    manifest: &str,
    manifest_dir: &Path,
    workspace: &Path,
    copy: &Path,
) -> Result<Option<String>> {
    Ok(absolutize_path_dependencies_with_externals(manifest, manifest_dir, workspace, copy)?.0)
}

/// The directory of the marks in a working directory: made if it is not there, used if
/// it is one. Anything else of that name (a file, a link) is somebody's, in a directory
/// that was given to work in before it was this tool's: it is left alone and said.
pub(super) fn marks_dir(work: &Path) -> Result<PathBuf> {
    work_directory(work, "marks")
}

pub(super) fn work_directory(work: &Path, name: &str) -> Result<PathBuf> {
    let path = work.join(name);
    match fs::symlink_metadata(&path) {
        Ok(found) if found.is_dir() => {}
        Ok(_) => bail!(
            "{} is in the way: the working directory needs a directory of that name, and \
             this is not one. Move it, or choose another --work-dir",
            path.display()
        ),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            fs::create_dir(&path).with_context(|| format!("creating {}", path.display()))?
        }
        Err(err) => return Err(err).with_context(|| format!("inspecting {}", path.display())),
    }
    Ok(path)
}

/// Makes the file by which a run says that it gave up: there for the run, and gone when
/// the run could not go on as its mutant. The runtime only has to take it away, which
/// it can do on a full disk too. The exit status would say the same, but only that of
/// the tests themselves: a program a test started gives its status to the test, which
/// fails for it.
///
/// The file is new: one of the same name that is there already is left as it is, and
/// another name is tried.
pub(super) fn reserve_file(
    dir: &Path,
    label: &str,
    runs: &std::sync::atomic::AtomicUsize,
) -> Result<PathBuf> {
    // The tests run in a directory of their own, and so does what they start: a place
    // given from here (`TMPDIR=.`) has to be the same place there.
    let dir = std::path::absolute(dir).unwrap_or(dir.to_path_buf());
    loop {
        let mark = dir.join(format!(
            "tmt-{label}-{}-{}",
            std::process::id(),
            runs.fetch_add(1, std::sync::atomic::Ordering::SeqCst)
        ));
        match fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .open(&mark)
        {
            Ok(_) => return Ok(mark),
            Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {}
            // If it cannot be made, nothing could be told from the run.
            Err(err) => {
                return Err(err).with_context(|| format!("creating {}", mark.display()));
            }
        }
    }
}

/// Takes the mark away after the run and says whether the run had given up (the mark
/// was gone). Anything else that keeps it from being taken away leaves the question
/// open, and is an error rather than an answer.
pub(super) fn release_mark(mark: &Path) -> Result<bool> {
    match fs::remove_file(mark) {
        Ok(()) => Ok(false),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(true),
        Err(err) => Err(err).with_context(|| {
            format!(
                "removing {}: cannot tell whether the tests ran the mutant to the end",
                mark.display()
            )
        }),
    }
}