supercov-engine 4.11.1

Rust instrumentation, evidence, attribution, and query engine for Supercov
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
//! Which first-party files a JavaScript or TypeScript test file loads, read
//! from its imports, and which test runner a configuration file belongs to.
//!
//! A test's record says which source it ran, never which helpers it loaded:
//! helpers sit beside the tests, outside what is measured. Without this a
//! helper of the end-to-end suite changing made every unit run out of date,
//! and a helper a test did call changing left `tests affected` saying no test
//! was affected.
//!
//! What cannot be read is said: an import whose path is computed, or one that
//! resolves to nothing Supercov can name, leaves the answer open, and an open
//! answer is "it may load anything".
use std::{
    cell::RefCell,
    collections::{BTreeMap, BTreeSet, VecDeque},
    fs,
    path::{Component, Path, PathBuf},
};

use serde_json::Value;

/// The files a set of entry files loads, themselves included, or why that
/// cannot be known.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Loads {
    pub files: BTreeSet<String>,
    /// The first import that could not be followed, where one could not.
    pub open: Option<String>,
}

#[derive(Debug, Clone)]
enum Specifier {
    Literal(String),
    /// `import(name)`, `require(path.join(...))`: anything at all.
    Computed,
}

const EXTENSIONS: [&str; 9] = [
    ".ts", ".tsx", ".mts", ".cts", ".js", ".jsx", ".mjs", ".cjs", ".json",
];

/// A file Supercov can read imports from.
pub fn reads_imports(file: &str) -> bool {
    file.ends_with(".py")
        || file.ends_with(".rb")
        || EXTENSIONS[..8]
            .iter()
            .any(|extension| file.ends_with(extension))
}

/// Imports of the files of one checkout, read once each.
pub struct Imports {
    root: PathBuf,
    packages: BTreeSet<String>,
    /// tsconfig `paths`: a pattern with at most one `*`, and where it maps.
    aliases: Vec<(String, Vec<PathBuf>)>,
    /// package.json `imports`, the `#name` specifiers.
    subpaths: BTreeMap<String, Vec<String>>,
    parsed: RefCell<BTreeMap<String, Option<Vec<Specifier>>>>,
    python: RefCell<BTreeMap<String, Option<Vec<PythonImport>>>>,
    ruby: RefCell<BTreeMap<String, Option<Vec<RubyRequire>>>>,
}

impl Imports {
    pub fn new(root: &Path) -> Self {
        let manifest = read_json(&root.join("package.json"));
        let mut packages = BTreeSet::new();
        let mut subpaths = BTreeMap::new();
        if let Some(manifest) = &manifest {
            for field in [
                "dependencies",
                "devDependencies",
                "peerDependencies",
                "optionalDependencies",
            ] {
                if let Some(names) = manifest.get(field).and_then(Value::as_object) {
                    packages.extend(names.keys().cloned());
                }
            }
            if let Some(name) = manifest.get("name").and_then(Value::as_str) {
                packages.insert(name.to_owned());
            }
            if let Some(imports) = manifest.get("imports").and_then(Value::as_object) {
                for (name, target) in imports {
                    let mut targets = Vec::new();
                    collect_strings(target, &mut targets);
                    subpaths.insert(name.clone(), targets);
                }
            }
        }
        Self {
            root: root.to_owned(),
            packages,
            aliases: aliases(root),
            subpaths,
            parsed: RefCell::new(BTreeMap::new()),
            python: RefCell::new(BTreeMap::new()),
            ruby: RefCell::new(BTreeMap::new()),
        }
    }

    /// Everything `entries` load, followed through first-party files.
    pub fn loads<'e>(&self, entries: impl IntoIterator<Item = &'e str>) -> Loads {
        let mut loads = Loads::default();
        let mut queue = entries
            .into_iter()
            .map(str::to_owned)
            .collect::<VecDeque<_>>();
        while let Some(file) = queue.pop_front() {
            if !loads.files.insert(file.clone()) || !reads_imports(&file) {
                continue;
            }
            if file.ends_with(".py") {
                self.python_loads(&file, &mut loads, &mut queue);
                continue;
            }
            if file.ends_with(".rb") {
                self.ruby_loads(&file, &mut loads, &mut queue);
                continue;
            }
            let Some(specifiers) = self.specifiers(&file) else {
                loads
                    .open
                    .get_or_insert_with(|| format!("{file} could not be read"));
                continue;
            };
            for specifier in specifiers {
                match specifier {
                    Specifier::Computed => {
                        loads
                            .open
                            .get_or_insert_with(|| format!("{file} imports a computed path"));
                    }
                    Specifier::Literal(specifier) => match self.resolve(&file, &specifier) {
                        Resolved::File(next) => queue.push_back(next),
                        Resolved::Outside => {}
                        Resolved::Unknown => {
                            loads.open.get_or_insert_with(|| {
                                format!("{file} imports '{specifier}', which Supercov cannot find")
                            });
                        }
                    },
                }
            }
        }
        loads
    }

    /// Files a runner's configuration names by path, such as a setup file or
    /// a global setup, where one exists: they load before every test. A
    /// pattern names test files, which each run's record already holds.
    pub fn named_by(&self, configuration: &str) -> Vec<String> {
        let Ok(source) = fs::read_to_string(self.root.join(configuration)) else {
            return Vec::new();
        };
        let directory = Path::new(configuration).parent().unwrap_or(Path::new(""));
        let mut named = Vec::new();
        for text in strings(configuration, &source) {
            if !(text.starts_with('.') || text.contains('/')) || text.contains(['*', '{', '?']) {
                continue;
            }
            for candidate in [directory.join(&text), PathBuf::from(&text)] {
                if let Resolved::File(file) = self.file(&candidate)
                    && !named.contains(&file)
                {
                    named.push(file);
                    break;
                }
            }
        }
        named
    }

    /// What a Python file loads: the modules it imports, the packages they
    /// sit in (whose `__init__.py` runs first), pytest's `conftest.py` in its
    /// directory and every directory above, and the plugins they name. A
    /// module found nowhere in the project is the standard library's or an
    /// installed package's; a computed import, or a change to `sys.path`,
    /// leaves the answer open.
    fn python_loads(&self, file: &str, loads: &mut Loads, queue: &mut VecDeque<String>) {
        for directory in Path::new(file).ancestors().skip(1) {
            if let Some(conftest) = normal(&directory.join("conftest.py"))
                && self.root.join(&conftest).is_file()
            {
                queue.push_back(conftest);
            }
        }
        let known = self.python.borrow().get(file).cloned();
        let imports = known.unwrap_or_else(|| {
            let parsed = fs::read_to_string(self.root.join(file))
                .ok()
                .and_then(|source| python_imports(&source));
            self.python
                .borrow_mut()
                .insert(file.to_owned(), parsed.clone());
            parsed
        });
        let Some(imports) = imports else {
            loads
                .open
                .get_or_insert_with(|| format!("{file} could not be read"));
            return;
        };
        for import in imports {
            match import {
                PythonImport::Computed(why) => {
                    loads.open.get_or_insert_with(|| format!("{file} {why}"));
                }
                PythonImport::Module {
                    module,
                    level,
                    names,
                } => {
                    queue.extend(self.python_resolve(file, &module, level, &names));
                }
            }
        }
    }

    /// What a Ruby file loads: what it requires by a literal path, relative
    /// to it or on the load path a runner sets up (`lib`, `spec`, `test`),
    /// and, for a spec, what `.rspec` requires before every one. A feature
    /// found nowhere in the project is a gem's or the standard library's; a
    /// computed path, a glob of support files, or a change to `$LOAD_PATH`
    /// leaves the answer open.
    fn ruby_loads(&self, file: &str, loads: &mut Loads, queue: &mut VecDeque<String>) {
        let exists = |path: &Path| normal(path).filter(|path| self.root.join(path).is_file());
        let feature = |name: &str| {
            let name = name.strip_suffix(".rb").unwrap_or(name);
            ["lib", "spec", "test", ""]
                .iter()
                .find_map(|base| exists(&Path::new(base).join(format!("{name}.rb"))))
        };
        if file.ends_with("_spec.rb")
            && let Ok(options) = fs::read_to_string(self.root.join(".rspec"))
        {
            let words = options.split_whitespace().collect::<Vec<_>>();
            for pair in words.windows(2) {
                if matches!(pair[0], "--require" | "-r")
                    && let Some(required) = feature(pair[1])
                {
                    queue.push_back(required);
                }
            }
            for word in &words {
                if let Some(name) = word.strip_prefix("--require=")
                    && let Some(required) = feature(name)
                {
                    queue.push_back(required);
                }
            }
        }
        let known = self.ruby.borrow().get(file).cloned();
        let requires = known.unwrap_or_else(|| {
            let parsed = fs::read_to_string(self.root.join(file))
                .ok()
                .and_then(|source| ruby_requires(&source));
            self.ruby
                .borrow_mut()
                .insert(file.to_owned(), parsed.clone());
            parsed
        });
        let Some(requires) = requires else {
            loads
                .open
                .get_or_insert_with(|| format!("{file} could not be read"));
            return;
        };
        let directory = Path::new(file).parent().unwrap_or(Path::new(""));
        for required in requires {
            match required {
                RubyRequire::Relative(path) => {
                    let path = path.strip_suffix(".rb").unwrap_or(&path).to_owned();
                    match exists(&directory.join(format!("{path}.rb"))) {
                        Some(next) => queue.push_back(next),
                        None => {
                            loads.open.get_or_insert_with(|| {
                                format!("{file} requires '{path}', which Supercov cannot find")
                            });
                        }
                    }
                }
                RubyRequire::Feature(name) => {
                    if let Some(next) = feature(&name) {
                        queue.push_back(next);
                    }
                }
                RubyRequire::Computed(why) => {
                    loads.open.get_or_insert_with(|| format!("{file} {why}"));
                }
            }
        }
    }

    /// The project files an import runs, or none for a module outside it.
    fn python_resolve(
        &self,
        file: &str,
        module: &str,
        level: u32,
        names: &[String],
    ) -> Vec<String> {
        let exists = |path: &Path| normal(path).filter(|path| self.root.join(path).is_file());
        let parts = module
            .split('.')
            .filter(|part| !part.is_empty())
            .collect::<Vec<_>>();
        let directory = Path::new(file).parent().unwrap_or(Path::new(""));
        let bases = if level > 0 {
            let mut base = directory.to_owned();
            for _ in 1..level {
                base = base.parent().unwrap_or(Path::new("")).to_owned();
            }
            vec![base]
        } else {
            // Where a test's imports are found: beside it and above (pytest
            // puts a test's root directory on the path), and a src layout.
            let mut bases = directory
                .ancestors()
                .map(Path::to_owned)
                .collect::<Vec<_>>();
            bases.push(PathBuf::from("src"));
            bases
        };
        for base in bases {
            let mut found = Vec::new();
            let mut package = base.clone();
            let mut resolved = parts.is_empty();
            for (index, part) in parts.iter().enumerate() {
                package = package.join(part);
                if let Some(init) = exists(&package.join("__init__.py")) {
                    found.push(init);
                    resolved = index + 1 == parts.len();
                } else if index + 1 == parts.len()
                    && let Some(module) = exists(&package.with_extension("py"))
                {
                    found.push(module);
                    resolved = true;
                } else {
                    break;
                }
            }
            if !resolved {
                continue;
            }
            // `from package import name` may name a submodule.
            for name in names {
                let submodule = package.join(name);
                if let Some(file) = exists(&submodule.with_extension("py"))
                    .or_else(|| exists(&submodule.join("__init__.py")))
                {
                    found.push(file);
                }
            }
            return found;
        }
        Vec::new()
    }

    fn specifiers(&self, file: &str) -> Option<Vec<Specifier>> {
        if let Some(known) = self.parsed.borrow().get(file) {
            return known.clone();
        }
        let parsed = fs::read_to_string(self.root.join(file))
            .ok()
            .and_then(|source| parse(file, &source));
        self.parsed
            .borrow_mut()
            .insert(file.to_owned(), parsed.clone());
        parsed
    }

    fn resolve(&self, from: &str, specifier: &str) -> Resolved {
        let specifier = specifier.split(['?', '#']).next().unwrap_or(specifier);
        if specifier.starts_with("./") || specifier.starts_with("../") || specifier == "." {
            let base = Path::new(from).parent().unwrap_or(Path::new(""));
            return self.file(&base.join(specifier));
        }
        if specifier.starts_with('/') {
            return Resolved::Unknown;
        }
        if let Some(name) = specifier.strip_prefix("node:") {
            let _ = name;
            return Resolved::Outside;
        }
        if specifier.starts_with('#') {
            return self.subpath(specifier);
        }
        for (pattern, targets) in &self.aliases {
            let rest = match pattern.split_once('*') {
                Some((prefix, suffix)) => specifier
                    .strip_prefix(prefix)
                    .and_then(|rest| rest.strip_suffix(suffix)),
                None => (specifier == pattern).then_some(""),
            };
            let Some(rest) = rest else { continue };
            for target in targets {
                let target = target.to_string_lossy().replacen('*', rest, 1);
                if let Resolved::File(file) = self.file(Path::new(&target)) {
                    return Resolved::File(file);
                }
            }
        }
        let package = package_name(specifier);
        // Installed where Node looks: beside the project or above it.
        if BUILTINS.contains(&package)
            || self.packages.contains(package)
            || self
                .root
                .ancestors()
                .any(|directory| directory.join("node_modules").join(package).exists())
        {
            return Resolved::Outside;
        }
        Resolved::Unknown
    }

    fn subpath(&self, specifier: &str) -> Resolved {
        let targets = self.subpaths.get(specifier).cloned().or_else(|| {
            self.subpaths.iter().find_map(|(pattern, targets)| {
                let (prefix, suffix) = pattern.split_once('*')?;
                let rest = specifier.strip_prefix(prefix)?.strip_suffix(suffix)?;
                Some(
                    targets
                        .iter()
                        .map(|target| target.replacen('*', rest, 1))
                        .collect(),
                )
            })
        });
        let Some(targets) = targets else {
            return Resolved::Unknown;
        };
        for target in targets {
            if !target.starts_with("./") {
                return Resolved::Outside;
            }
            if let Resolved::File(file) = self.file(Path::new(&target)) {
                return Resolved::File(file);
            }
        }
        Resolved::Unknown
    }

    /// A first-party file at `path` (relative to the root), the way Node,
    /// TypeScript and bundlers find it: as named, with an extension, as a
    /// directory's index, and a `.js` that names its TypeScript source.
    fn file(&self, path: &Path) -> Resolved {
        let Some(path) = normal(path) else {
            return Resolved::Unknown;
        };
        if path.split('/').any(|part| part == "node_modules") {
            return Resolved::Outside;
        }
        let exists = |candidate: &str| self.root.join(candidate).is_file();
        let mut candidates = vec![path.clone()];
        for (javascript, typescript) in [
            (".js", [".ts", ".tsx"]),
            (".jsx", [".tsx", ".tsx"]),
            (".mjs", [".mts", ".mts"]),
            (".cjs", [".cts", ".cts"]),
        ] {
            if let Some(stem) = path.strip_suffix(javascript) {
                candidates.extend(typescript.iter().map(|ts| format!("{stem}{ts}")));
            }
        }
        candidates.extend(
            EXTENSIONS
                .iter()
                .map(|extension| format!("{path}{extension}")),
        );
        candidates.extend(
            EXTENSIONS
                .iter()
                .map(|extension| format!("{path}/index{extension}")),
        );
        match candidates.into_iter().find(|candidate| exists(candidate)) {
            Some(found) => Resolved::File(found),
            None => Resolved::Unknown,
        }
    }
}

enum Resolved {
    File(String),
    /// A package or a built-in: not the project's own code.
    Outside,
    Unknown,
}

const BUILTINS: [&str; 40] = [
    "assert",
    "async_hooks",
    "buffer",
    "child_process",
    "cluster",
    "console",
    "constants",
    "crypto",
    "dgram",
    "diagnostics_channel",
    "dns",
    "domain",
    "events",
    "fs",
    "http",
    "http2",
    "https",
    "inspector",
    "module",
    "net",
    "os",
    "path",
    "perf_hooks",
    "process",
    "punycode",
    "querystring",
    "readline",
    "repl",
    "stream",
    "string_decoder",
    "timers",
    "tls",
    "tty",
    "url",
    "util",
    "v8",
    "vm",
    "worker_threads",
    "zlib",
    "test",
];

fn package_name(specifier: &str) -> &str {
    let mut parts = specifier.splitn(3, '/');
    let first = parts.next().unwrap_or(specifier);
    if first.starts_with('@')
        && let Some(second) = parts.next()
    {
        return &specifier[..first.len() + 1 + second.len()];
    }
    first
}

/// `a/./b/../c` as `a/c`, refusing a path that climbs out of the root.
fn normal(path: &Path) -> Option<String> {
    let mut parts = Vec::new();
    for component in path.components() {
        match component {
            Component::Normal(part) => parts.push(part.to_str()?.to_owned()),
            Component::CurDir => {}
            Component::ParentDir => {
                parts.pop()?;
            }
            _ => return None,
        }
    }
    Some(parts.join("/"))
}

fn read_json(path: &Path) -> Option<Value> {
    let text = fs::read_to_string(path).ok()?;
    serde_json::from_str(&crate::source_discovery::strip_trailing_commas(
        &crate::source_discovery::strip_jsonc_comments(&text),
    ))
    .ok()
}

fn collect_strings(value: &Value, into: &mut Vec<String>) {
    match value {
        Value::String(text) => into.push(text.clone()),
        Value::Object(map) => map.values().for_each(|value| collect_strings(value, into)),
        Value::Array(items) => items.iter().for_each(|value| collect_strings(value, into)),
        _ => {}
    }
}

/// The root tsconfig's `paths`, through its `extends` chain, each target
/// relative to the root.
fn aliases(root: &Path) -> Vec<(String, Vec<PathBuf>)> {
    let mut found = Vec::new();
    let mut config = root.join("tsconfig.json");
    let mut seen = BTreeSet::new();
    while seen.insert(config.clone()) && seen.len() < 16 {
        let Some(json) = read_json(&config) else {
            break;
        };
        let directory = config.parent().unwrap_or(root).to_owned();
        let options = json.get("compilerOptions");
        let base = options
            .and_then(|options| options.get("baseUrl"))
            .and_then(Value::as_str)
            .map_or(directory.clone(), |base| directory.join(base));
        if found.is_empty()
            && let Some(paths) = options
                .and_then(|options| options.get("paths"))
                .and_then(Value::as_object)
        {
            for (pattern, targets) in paths {
                let targets = targets
                    .as_array()
                    .into_iter()
                    .flatten()
                    .filter_map(Value::as_str)
                    .filter_map(|target| {
                        base.join(target)
                            .strip_prefix(root)
                            .ok()
                            .map(Path::to_owned)
                    })
                    .collect();
                found.push((pattern.clone(), targets));
            }
        }
        match json.get("extends").and_then(Value::as_str) {
            Some(parent) if parent.starts_with('.') => {
                config = directory.join(parent);
                if config.extension().is_none() {
                    config.set_extension("json");
                }
            }
            _ => break,
        }
    }
    found
}

/// What a file imports: static imports and re-exports, `import()`, `require`
/// and the module mocks of Jest and Vitest, which name a module by its path.
/// Imports of types only are erased before anything runs.
fn parse(file: &str, source: &str) -> Option<Vec<Specifier>> {
    use oxc_ast::ast::*;
    use oxc_ast_visit::{Visit, walk};

    let source_type = crate::js_instrumenter::project_source_type(Path::new(file)).ok()?;
    let allocator = oxc_allocator::Allocator::default();
    let parsed = oxc_parser::Parser::new(&allocator, source, source_type).parse();
    if parsed.panicked {
        return None;
    }
    struct Collector {
        found: Vec<Specifier>,
    }
    fn literal(expression: &Expression<'_>) -> Specifier {
        match expression {
            Expression::StringLiteral(text) => Specifier::Literal(text.value.to_string()),
            Expression::TemplateLiteral(template) if template.expressions.is_empty() => template
                .quasis
                .first()
                .and_then(|quasi| quasi.value.cooked.as_ref())
                .map_or(Specifier::Computed, |text| {
                    Specifier::Literal(text.to_string())
                }),
            _ => Specifier::Computed,
        }
    }
    impl<'a> Visit<'a> for Collector {
        fn visit_import_declaration(&mut self, it: &ImportDeclaration<'a>) {
            if !it.import_kind.is_type() {
                self.found
                    .push(Specifier::Literal(it.source.value.to_string()));
            }
        }
        fn visit_export_all_declaration(&mut self, it: &ExportAllDeclaration<'a>) {
            if !it.export_kind.is_type() {
                self.found
                    .push(Specifier::Literal(it.source.value.to_string()));
            }
        }
        fn visit_export_named_declaration(&mut self, it: &ExportNamedDeclaration<'a>) {
            if let Some(source) = &it.source
                && !it.export_kind.is_type()
            {
                self.found
                    .push(Specifier::Literal(source.value.to_string()));
            }
            walk::walk_export_named_declaration(self, it);
        }
        fn visit_ts_import_equals_declaration(&mut self, it: &TSImportEqualsDeclaration<'a>) {
            if let TSModuleReference::ExternalModuleReference(reference) = &it.module_reference
                && !it.import_kind.is_type()
            {
                self.found
                    .push(Specifier::Literal(reference.expression.value.to_string()));
            }
        }
        fn visit_import_expression(&mut self, it: &ImportExpression<'a>) {
            self.found.push(literal(&it.source));
            walk::walk_import_expression(self, it);
        }
        fn visit_call_expression(&mut self, it: &CallExpression<'a>) {
            let loads = match &it.callee {
                Expression::Identifier(name) => name.name == "require",
                Expression::StaticMemberExpression(member) => {
                    let object = match &member.object {
                        Expression::Identifier(name) => name.name.as_str(),
                        _ => "",
                    };
                    let property = member.property.name.as_str();
                    matches!(
                        (object, property),
                        ("require", "resolve")
                            | ("jest", "mock" | "requireActual" | "doMock")
                            | ("vi", "mock" | "importActual" | "doMock")
                    )
                }
                _ => false,
            };
            if loads && let Some(first) = it.arguments.first() {
                self.found.push(match first.as_expression() {
                    Some(expression) => literal(expression),
                    None => Specifier::Computed,
                });
            }
            walk::walk_call_expression(self, it);
        }
    }
    let mut collector = Collector { found: Vec::new() };
    collector.visit_program(&parsed.program);
    Some(collector.found)
}

#[derive(Debug, Clone)]
enum PythonImport {
    Module {
        module: String,
        /// Leading dots of a relative import.
        level: u32,
        names: Vec<String>,
    },
    /// Why what it loads cannot be read.
    Computed(&'static str),
}

/// Every import in a Python file, at any depth, with `importlib` calls that
/// name a module literally and pytest's `pytest_plugins`.
fn python_imports(source: &str) -> Option<Vec<PythonImport>> {
    use ruff_python_ast::{
        Expr, Stmt,
        visitor::{Visitor, walk_expr, walk_stmt},
    };
    let parsed = ruff_python_parser::parse_module(source).ok()?;
    struct Collector(Vec<PythonImport>);
    fn module(name: &str) -> PythonImport {
        PythonImport::Module {
            module: name.to_owned(),
            level: 0,
            names: Vec::new(),
        }
    }
    fn strings(expression: &Expr) -> Option<Vec<String>> {
        match expression {
            Expr::StringLiteral(text) => Some(vec![text.value.to_str().to_owned()]),
            Expr::List(list) => list
                .elts
                .iter()
                .map(|item| strings(item).map(|mut s| s.remove(0)))
                .collect(),
            Expr::Tuple(tuple) => tuple
                .elts
                .iter()
                .map(|item| strings(item).map(|mut s| s.remove(0)))
                .collect(),
            _ => None,
        }
    }
    fn is_sys_path(expression: &Expr) -> bool {
        matches!(expression, Expr::Attribute(attribute)
            if attribute.attr.as_str() == "path"
                && matches!(&*attribute.value, Expr::Name(name) if name.id.as_str() == "sys"))
    }
    impl<'a> Visitor<'a> for Collector {
        fn visit_stmt(&mut self, stmt: &'a Stmt) {
            match stmt {
                Stmt::Import(import) => {
                    for alias in &import.names {
                        self.0.push(module(alias.name.as_str()));
                    }
                }
                Stmt::ImportFrom(import) => self.0.push(PythonImport::Module {
                    module: import
                        .module
                        .as_ref()
                        .map(|m| m.to_string())
                        .unwrap_or_default(),
                    level: import.level,
                    names: import
                        .names
                        .iter()
                        .map(|alias| alias.name.to_string())
                        .collect(),
                }),
                Stmt::Assign(assign) => {
                    for target in &assign.targets {
                        if matches!(target, Expr::Name(name) if name.id.as_str() == "pytest_plugins")
                        {
                            match strings(&assign.value) {
                                Some(plugins) => self.0.extend(plugins.iter().map(|p| module(p))),
                                None => self.0.push(PythonImport::Computed(
                                    "names its pytest plugins by computation",
                                )),
                            }
                        }
                        if is_sys_path(target) {
                            self.0.push(PythonImport::Computed("changes sys.path"));
                        }
                    }
                }
                _ => {}
            }
            walk_stmt(self, stmt);
        }
        fn visit_expr(&mut self, expr: &'a Expr) {
            if let Expr::Call(call) = expr {
                let first = call.arguments.args.first();
                match &*call.func {
                    Expr::Attribute(attribute)
                        if is_sys_path(&attribute.value)
                            && matches!(
                                attribute.attr.as_str(),
                                "insert" | "append" | "extend"
                            ) =>
                    {
                        self.0.push(PythonImport::Computed("changes sys.path"));
                    }
                    Expr::Attribute(attribute) if attribute.attr.as_str() == "import_module" => {
                        self.0.push(match first.and_then(strings) {
                            Some(mut names) => module(&names.remove(0)),
                            None => PythonImport::Computed("imports a module named by computation"),
                        });
                    }
                    Expr::Name(name)
                        if matches!(name.id.as_str(), "__import__" | "import_module") =>
                    {
                        self.0.push(match first.and_then(strings) {
                            Some(mut names) => module(&names.remove(0)),
                            None => PythonImport::Computed("imports a module named by computation"),
                        });
                    }
                    _ => {}
                }
            }
            walk_expr(self, expr);
        }
    }
    let mut collector = Collector(Vec::new());
    collector.visit_body(&parsed.syntax().body);
    Some(collector.0)
}

/// The test runner a configuration file belongs to, by its name. A change to
/// one runner's configuration changes nothing another runner does.
#[derive(Debug, Clone)]
enum RubyRequire {
    /// `require_relative "x"`.
    Relative(String),
    /// `require "x"`, `load "x.rb"`, `autoload :X, "x"`: on the load path.
    Feature(String),
    Computed(&'static str),
}

/// What a Ruby file requires, or None when it does not parse.
fn ruby_requires(source: &str) -> Option<Vec<RubyRequire>> {
    use ruby_prism::{CallNode, GlobalVariableReadNode, Visit};
    let parsed = ruby_prism::parse(source.as_bytes());
    if parsed.errors().next().is_some() {
        return None;
    }
    struct Requires(Vec<RubyRequire>);
    impl<'pr> Visit<'pr> for Requires {
        fn visit_call_node(&mut self, node: &CallNode<'pr>) {
            let name = node.name();
            let name = name.as_slice();
            if node.receiver().is_none()
                && matches!(
                    name,
                    b"require" | b"require_relative" | b"load" | b"autoload"
                )
            {
                let arguments = node
                    .arguments()
                    .map(|arguments| arguments.arguments().iter().collect::<Vec<_>>())
                    .unwrap_or_default();
                // `autoload :Name, "path"` names its path second.
                let path = if name == b"autoload" {
                    arguments.get(1)
                } else {
                    arguments.first()
                };
                let literal = path
                    .and_then(|path| path.as_string_node())
                    .and_then(|path| String::from_utf8(path.unescaped().to_vec()).ok());
                self.0.push(match (literal, name) {
                    (Some(path), b"require_relative") => RubyRequire::Relative(path),
                    (Some(path), _) => RubyRequire::Feature(path),
                    (None, _) => RubyRequire::Computed("requires a computed path"),
                });
            }
            ruby_prism::visit_call_node(self, node);
        }
        fn visit_global_variable_read_node(&mut self, node: &GlobalVariableReadNode<'pr>) {
            if matches!(node.name().as_slice(), b"$LOAD_PATH" | b"$:") {
                self.0.push(RubyRequire::Computed("changes $LOAD_PATH"));
            }
        }
    }
    let mut requires = Requires(Vec::new());
    requires.visit(&parsed.node());
    Some(requires.0)
}

pub fn configuration_runner(file: &str) -> Option<&'static str> {
    let name = file.rsplit('/').next()?.to_ascii_lowercase();
    if !name.contains(".config.") && !name.contains(".workspace.") {
        return None;
    }
    let stem = name.split('.').next()?;
    match stem {
        "playwright" | "playwright-ct" => Some("playwright"),
        "vitest" => Some("vitest"),
        "jest" => Some("jest"),
        "ava" => Some("ava"),
        _ => None,
    }
}

fn strings(file: &str, source: &str) -> Vec<String> {
    use oxc_ast::ast::*;
    use oxc_ast_visit::Visit;
    let Ok(source_type) = crate::js_instrumenter::project_source_type(Path::new(file)) else {
        return Vec::new();
    };
    let allocator = oxc_allocator::Allocator::default();
    let parsed = oxc_parser::Parser::new(&allocator, source, source_type).parse();
    struct Collector(Vec<String>);
    impl<'a> Visit<'a> for Collector {
        fn visit_string_literal(&mut self, it: &StringLiteral<'a>) {
            self.0.push(it.value.to_string());
        }
    }
    let mut collector = Collector(Vec::new());
    collector.visit_program(&parsed.program);
    collector.0
}

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

    fn project(files: &[(&str, &str)]) -> PathBuf {
        static UNIQUE: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
        let root = std::env::temp_dir().join(format!(
            "supercov-test-loads-{}-{}-{}",
            std::process::id(),
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos(),
            UNIQUE.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
        ));
        fs::create_dir(&root).unwrap();
        for (file, text) in files {
            let path = root.join(file);
            fs::create_dir_all(path.parent().unwrap()).unwrap();
            fs::write(path, text).unwrap();
        }
        root
    }

    #[test]
    fn a_test_loads_its_helpers_and_not_another_suites() {
        let root = project(&[
            (
                "package.json",
                r#"{"devDependencies":{"@playwright/test":"1"}}"#,
            ),
            (
                "tsconfig.json",
                r#"{"compilerOptions":{"baseUrl":".","paths":{"@/*":["src/*"]}}}"#,
            ),
            (
                "tests/e2e/helpers.ts",
                "import { x } from '@/lib/x';\nexport const get = () => x;\n",
            ),
            (
                "tests/e2e/app.spec.ts",
                "import { test } from '@playwright/test';\nimport { get } from './helpers';\nimport type { T } from './types';\n",
            ),
            ("tests/e2e/types.ts", "export type T = 1;\n"),
            (
                "tests/unit/a.test.ts",
                "import { y } from '../support/y.js';\n",
            ),
            ("tests/support/y.ts", "export const y = 1;\n"),
            ("src/lib/x.ts", "export const x = 1;\n"),
        ]);
        let imports = Imports::new(&root);
        let e2e = imports.loads(["tests/e2e/app.spec.ts"]);
        assert_eq!(e2e.open, None);
        assert!(e2e.files.contains("tests/e2e/helpers.ts"));
        assert!(e2e.files.contains("src/lib/x.ts"), "{e2e:?}");
        assert!(
            !e2e.files.contains("tests/e2e/types.ts"),
            "a type import loads nothing"
        );
        assert!(!e2e.files.contains("tests/support/y.ts"));
        let unit = imports.loads(["tests/unit/a.test.ts"]);
        assert!(unit.files.contains("tests/support/y.ts"), "{unit:?}");
        assert!(!unit.files.contains("tests/e2e/helpers.ts"));
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn what_cannot_be_followed_leaves_the_answer_open() {
        let root = project(&[
            ("package.json", "{}"),
            ("a.test.js", "const name = 'b';\nrequire(`./${name}`);\n"),
            ("c.test.js", "import 'unknown-alias/thing';\n"),
            (
                "d.test.js",
                "import fs from 'node:fs';\nimport path from 'path';\nimport './e.js';\n",
            ),
            ("e.js", "export {};\n"),
        ]);
        let imports = Imports::new(&root);
        assert!(imports.loads(["a.test.js"]).open.is_some());
        assert!(imports.loads(["c.test.js"]).open.is_some());
        let closed = imports.loads(["d.test.js"]);
        assert_eq!(closed.open, None);
        assert!(closed.files.contains("e.js"));
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn a_python_test_loads_its_conftest_and_what_it_imports() {
        let root = project(&[
            (
                "tests/conftest.py",
                "import pytest\nfrom tests.support import factory\n",
            ),
            ("tests/__init__.py", ""),
            ("tests/support/__init__.py", ""),
            ("tests/support/factory.py", "import json\n"),
            (
                "tests/unit/test_a.py",
                "from .helpers import make\nfrom shop import cart\n",
            ),
            ("tests/unit/__init__.py", ""),
            ("tests/unit/helpers.py", "def make():\n    return 1\n"),
            (
                "tests/e2e/test_b.py",
                "import requests\nfrom tests.e2e import pages\n",
            ),
            ("tests/e2e/__init__.py", ""),
            ("tests/e2e/pages.py", ""),
            ("src/shop/__init__.py", ""),
            ("src/shop/cart.py", ""),
            (
                "tests/odd/test_c.py",
                "import sys\nsys.path.insert(0, 'x')\n",
            ),
        ]);
        let imports = Imports::new(&root);
        let unit = imports.loads(["tests/unit/test_a.py"]);
        assert_eq!(unit.open, None, "{unit:?}");
        for file in [
            "tests/conftest.py",
            "tests/support/factory.py",
            "tests/unit/helpers.py",
            "src/shop/cart.py",
        ] {
            assert!(unit.files.contains(file), "{file}: {unit:?}");
        }
        assert!(!unit.files.contains("tests/e2e/pages.py"));
        let e2e = imports.loads(["tests/e2e/test_b.py"]);
        assert!(
            e2e.files.contains("tests/e2e/pages.py")
                && !e2e.files.contains("tests/unit/helpers.py"),
            "{e2e:?}"
        );
        assert!(imports.loads(["tests/odd/test_c.py"]).open.is_some());
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn a_ruby_spec_loads_what_it_requires_and_what_rspec_requires() {
        let root = project(&[
            (".rspec", "--require spec_helper\n--format documentation\n"),
            (
                "spec/spec_helper.rb",
                "require \"json\"\nrequire_relative \"support/factories\"\n",
            ),
            ("spec/support/factories.rb", "module Factories; end\n"),
            ("spec/support/unused.rb", "module Unused; end\n"),
            (
                "spec/orders_spec.rb",
                "require \"orders\"\nRSpec.describe(Orders) { }\n",
            ),
            (
                "lib/orders.rb",
                "autoload :Refunds, \"orders/refunds\"\nmodule Orders; end\n",
            ),
            ("lib/orders/refunds.rb", "module Refunds; end\n"),
            (
                "spec/glob_spec.rb",
                "Dir[File.join(__dir__, \"support/*.rb\")].each { |f| require f }\n",
            ),
            ("spec/path_spec.rb", "$LOAD_PATH.unshift(\"vendor\")\n"),
        ]);
        let imports = Imports::new(&root);
        let loads = imports.loads(["spec/orders_spec.rb"]);
        assert_eq!(loads.open, None);
        assert_eq!(
            loads.files.into_iter().collect::<Vec<_>>(),
            [
                "lib/orders.rb",
                "lib/orders/refunds.rb",
                "spec/orders_spec.rb",
                "spec/spec_helper.rb",
                "spec/support/factories.rb",
            ]
        );
        assert!(imports.loads(["spec/glob_spec.rb"]).open.is_some());
        assert!(imports.loads(["spec/path_spec.rb"]).open.is_some());
        fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn a_configuration_belongs_to_its_runner() {
        assert_eq!(
            configuration_runner("tests/e2e/playwright.config.ts"),
            Some("playwright")
        );
        assert_eq!(configuration_runner("vitest.config.mts"), Some("vitest"));
        assert_eq!(configuration_runner("jest.config.js"), Some("jest"));
        assert_eq!(
            configuration_runner("vite.config.ts"),
            None,
            "a build shared with the app"
        );
        assert_eq!(configuration_runner("tsconfig.json"), None);
        assert_eq!(configuration_runner("next.config.ts"), None);
    }

    #[test]
    fn a_configuration_names_its_setup_files() {
        let root = project(&[
            (
                "vitest.config.ts",
                "export default { test: { setupFiles: ['tests/unit/setup.ts'], include: ['tests/**/*.test.ts'] } };\n",
            ),
            ("tests/unit/setup.ts", "export {};\n"),
            (
                "e2e/playwright.config.ts",
                "export default { globalSetup: './global-setup', testMatch: '*.spec.ts' };\n",
            ),
            ("e2e/global-setup.ts", "export default () => {};\n"),
        ]);
        let imports = Imports::new(&root);
        assert_eq!(
            imports.named_by("vitest.config.ts"),
            ["tests/unit/setup.ts"]
        );
        assert_eq!(
            imports.named_by("e2e/playwright.config.ts"),
            ["e2e/global-setup.ts"]
        );
        fs::remove_dir_all(root).unwrap();
    }
}