use std::path::{Path, PathBuf};
fn rust_sources(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else {
return;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
rust_sources(&path, out);
} else if path.extension().is_some_and(|e| e == "rs") {
out.push(path);
}
}
}
fn call_text(text: &str, at: usize) -> &str {
let rest = &text[at..];
let end = [".output()", ".spawn()", ".status()", "])"]
.iter()
.filter_map(|m| rest.find(m))
.min()
.unwrap_or(rest.len().min(400));
&rest[..end]
}
#[test]
fn every_powershell_spawn_skips_the_profile() {
let mut paths = Vec::new();
rust_sources(Path::new("src"), &mut paths);
let mut missing = Vec::new();
for path in paths {
let Ok(text) = std::fs::read_to_string(&path) else {
continue;
};
for needle in [
"Command::new(\"powershell\")",
"Command::new(\"powershell.exe\")",
"(\"powershell\",",
"(\n \"powershell\",",
] {
let mut from = 0;
while let Some(pos) = text[from..].find(needle) {
let at = from + pos;
from = at + needle.len();
let line_start = text[..at].rfind('\n').map_or(0, |n| n + 1);
if text[line_start..].trim_start().starts_with("//") {
continue;
}
if !call_text(&text, at).contains("-NoProfile") {
let line = text[..at].matches('\n').count() + 1;
missing.push(format!("{}:{line}", path.display()));
}
}
}
}
assert!(
missing.is_empty(),
"PowerShell started without -NoProfile, which runs the user's profile first: {missing:?}"
);
}