use std::path::Path;
use std::process::Command;
fn main() {
std::process::exit(run());
}
fn stdout_of(bin: &Path, f: &Path) -> Option<String> {
Command::new(bin)
.arg(f)
.env("PYTHONHASHSEED", "0")
.env("PYTHONIOENCODING", "utf-8")
.env("TZ", "UTC")
.env("LANG", "en_US.UTF-8")
.env("LC_ALL", "en_US.UTF-8")
.output()
.ok()
.map(|o| String::from_utf8_lossy(&o.stdout).into_owned())
}
fn run() -> i32 {
let dir = Path::new("examples");
if !dir.exists() {
eprintln!("parity: no examples/ directory — measured nothing");
return 2;
}
let mut files: Vec<_> = match std::fs::read_dir(dir) {
Ok(rd) => rd
.filter_map(|e| e.ok().map(|e| e.path()))
.filter(|p| p.extension().map(|e| e == "py").unwrap_or(false))
.collect(),
Err(e) => {
eprintln!("parity: cannot read examples/: {e}");
return 2;
}
};
files.sort();
if files.is_empty() {
eprintln!("parity: examples/ matched zero *.py scripts — measured nothing");
return 2;
}
let oracle = match pythonrs::oracle::resolve() {
Ok(p) => p,
Err(e) => {
eprintln!("parity: {e} — measured nothing");
return 2;
}
};
println!("parity: oracle {}", pythonrs::oracle::identify(&oracle));
let ours_bin = std::env::current_exe()
.ok()
.and_then(|p| p.parent().map(|d| d.join("python")))
.unwrap_or_else(|| Path::new("python").to_path_buf());
let mut pass = 0;
let mut fail = 0;
let mut skipped = 0;
for f in &files {
let ours = stdout_of(&ours_bin, f);
let theirs = stdout_of(&oracle, f);
match (ours, theirs) {
(Some(a), Some(b)) if a == b => {
pass += 1;
println!("ok {}", f.display());
}
(Some(a), Some(b)) => {
fail += 1;
println!("DIFF {}\n ours: {a:?}\n python: {b:?}", f.display());
}
(None, _) => {
fail += 1;
println!("ERR {} (pythonrs failed to run)", f.display());
}
(_, None) => {
skipped += 1;
println!("skip {} (oracle failed to run)", f.display());
}
}
}
println!("\nparity: {pass} passed, {fail} failed, {skipped} skipped");
if skipped > 0 {
eprintln!(
"parity: FAIL — {skipped}/{} script(s) had no reference to compare against; \
install a CPython or this run proves nothing",
files.len()
);
return 2;
}
if fail > 0 {
eprintln!(
"parity: FAIL — {fail} of {} script(s) diverged",
files.len()
);
return 1;
}
println!(
"parity: PASS — compared {pass} script(s) against {}",
pythonrs::oracle::identify(&oracle)
);
0
}