xtask/
delivery_isolation.rs1use std::path::{Path, PathBuf};
13
14use crate::greenfield_status::{in_test_range, test_mod_ranges};
15use crate::module_lines::is_whole_test_file;
16
17pub fn marker() -> String {
20 ["EventsFanout delivery", " failed"].concat()
21}
22
23pub fn occurrences(root: &Path, needle: &str) -> Vec<String> {
27 let mut found = Vec::new();
28 let Ok(entries) = std::fs::read_dir(root) else {
29 return found;
30 };
31 let mut crates: Vec<PathBuf> = entries
32 .filter_map(Result::ok)
33 .map(|e| e.path())
34 .filter(|p| p.join("src").is_dir() && p.file_name().is_some_and(|n| n != "xtask"))
35 .collect();
36 crates.sort();
37 for krate in crates {
38 scan(&krate.join("src"), root, needle, &mut found);
39 }
40 found
41}
42
43fn scan(dir: &Path, root: &Path, needle: &str, found: &mut Vec<String>) {
44 let Ok(entries) = std::fs::read_dir(dir) else {
45 return;
46 };
47 let mut paths: Vec<PathBuf> = entries.filter_map(Result::ok).map(|e| e.path()).collect();
48 paths.sort();
49 for path in paths {
50 if path.is_dir() {
51 scan(&path, root, needle, found);
52 } else if path.extension().is_some_and(|e| e == "rs")
53 && let Ok(source) = std::fs::read_to_string(&path)
54 {
55 let rel = path
58 .strip_prefix(root)
59 .unwrap_or(&path)
60 .components()
61 .map(|c| c.as_os_str().to_string_lossy())
62 .collect::<Vec<_>>()
63 .join("/");
64 if rel
68 .split_once("/src/")
69 .is_some_and(|(_, in_src)| is_whole_test_file(in_src))
70 {
71 continue;
72 }
73 found.extend(
74 occurrences_in(&source, needle)
75 .into_iter()
76 .map(|line| format!("{rel}:{line}")),
77 );
78 }
79 }
80}
81
82pub fn occurrences_in(source: &str, needle: &str) -> Vec<usize> {
84 let lines: Vec<&str> = source.lines().collect();
85 let tests = test_mod_ranges(&lines);
86 lines
87 .iter()
88 .enumerate()
89 .filter(|(i, l)| l.contains(needle) && !in_test_range(*i, &tests))
90 .map(|(i, _)| i + 1)
91 .collect()
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 #[test]
99 fn a_test_module_copy_is_not_an_occurrence_but_a_production_one_is() {
100 let needle = marker();
101 let source = format!(
102 "fn a() {{ log(\"{needle}\"); }}\n#[cfg(test)]\nmod tests {{\n fn t() {{ log(\"{needle}\"); }}\n}}\nfn b() {{ log(\"{needle}\"); }}\n"
103 );
104 assert_eq!(occurrences_in(&source, &needle), [1, 6]);
105 }
106}