#![allow(dead_code)]
use std::path::{Path, PathBuf};
use std::process::Command;
use knobe::Value;
pub fn protocol_dir() -> PathBuf {
let dir = std::env::var_os("KNOBE_PROTOCOL_DIR")
.map(PathBuf::from)
.unwrap_or_else(|| Path::new(env!("CARGO_MANIFEST_DIR")).join("../knobe-protocol"));
assert!(
dir.join("lens.py").is_file(),
"KNOBE protocol checkout not found at {} (set KNOBE_PROTOCOL_DIR)",
dir.display()
);
dir
}
pub fn corpus() -> Vec<PathBuf> {
let root = protocol_dir();
let mut out = Vec::new();
for sub in ["test-vectors", "examples", "site"] {
walk(&root.join(sub), &mut out);
}
out.sort();
assert!(!out.is_empty(), "no .knobe.md files under {}", root.display());
out
}
fn walk(dir: &Path, out: &mut Vec<PathBuf>) {
let Ok(entries) = std::fs::read_dir(dir) else { return };
for e in entries.flatten() {
let p = e.path();
if p.is_dir() {
walk(&p, out);
} else if p.to_string_lossy().ends_with(".knobe.md") {
out.push(p);
}
}
}
#[derive(Debug, PartialEq)]
pub struct Gate {
pub state: Value,
pub conformance: Value,
pub body_verified: Value,
pub computed: Value,
pub stored: Value,
pub block_count: Value,
pub multiple_blocks: Value,
pub exit_code: i32,
}
pub fn gate_from_json(stdout: &str, exit_code: i32) -> Gate {
let v = knobe::json::parse(stdout).unwrap_or_else(|e| panic!("bad JSON ({e}): {stdout}"));
let get = |k: &str| v.get(k).cloned().unwrap_or(Value::Null);
Gate {
state: get("state"),
conformance: get("conformance"),
body_verified: get("body_verified"),
computed: get("computed"),
stored: get("stored"),
block_count: get("block_count"),
multiple_blocks: get("multiple_blocks"),
exit_code,
}
}
pub fn lens(file: &Path) -> Option<Gate> {
let out = Command::new("python3")
.arg(protocol_dir().join("lens.py"))
.arg("--json")
.arg(file)
.output()
.expect("python3 must be installed to run parity tests");
let stdout = String::from_utf8_lossy(&out.stdout);
if stdout.trim().is_empty() {
return None;
}
Some(gate_from_json(&stdout, out.status.code().unwrap_or(-1)))
}
pub fn rust(file: &Path) -> Gate {
let r = knobe::verify_path(file);
gate_from_json(&r.to_json(), r.exit_code())
}
fn stdout_and_code(cmd: &mut Command) -> (Vec<u8>, i32) {
let out = cmd.output().unwrap_or_else(|e| panic!("cannot run {cmd:?}: {e}"));
(out.stdout, out.status.code().unwrap_or(-1))
}
pub fn lens_cli(args: &[&str], file: &Path) -> (Vec<u8>, i32) {
stdout_and_code(Command::new("python3").arg(protocol_dir().join("lens.py")).args(args).arg(file))
}
pub fn knobe_cli(args: &[&str], file: &Path) -> (Vec<u8>, i32) {
stdout_and_code(Command::new(env!("CARGO_BIN_EXE_knobe")).arg("verify").args(args).arg(file))
}
pub fn lens_has_display_patch() -> bool {
static PATCHED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*PATCHED.get_or_init(|| {
let check = "import sys; sys.path.insert(0, sys.argv[1]); import lens; \
print(lens.safe('\\u202e\\ud800') == '\\\\u202e\\\\ud800')";
let out = Command::new("python3")
.args(["-B", "-c", check])
.arg(protocol_dir())
.output()
.expect("python3 must be installed to run parity tests");
assert!(out.status.success(), "cannot load lens.py: {}", String::from_utf8_lossy(&out.stderr));
String::from_utf8_lossy(&out.stdout).trim() == "True"
})
}
pub fn python_version() -> &'static str {
static VERSION: std::sync::OnceLock<String> = std::sync::OnceLock::new();
VERSION.get_or_init(|| {
let out = Command::new("python3")
.args(["-c", "import sys; print('%d.%d' % sys.version_info[:2])"])
.output()
.expect("python3 must be installed to run parity tests");
String::from_utf8_lossy(&out.stdout).trim().to_string()
})
}
const CPYTHON_313_ONLY: &[&str] = &[
"deep-nesting-9997",
"deep-nesting-9997-scalar-leaf",
"deep-nesting-9998",
"deep-nesting-9998-then-dup-key",
"top-level-list-9998",
"top-level-list-9999",
"deep-object-9995",
"deep-object-9996",
"deep-object-dup-key-9994",
"deep-object-dup-key-9995",
"deep-object-dup-key-9996",
"deep-float-9995",
"deep-float-9996",
"deep-nan-9995",
"deep-nan-9996",
"deep-int-9998",
];
pub fn skip_on_this_python(name: &str) -> bool {
let skip = CPYTHON_313_ONLY.contains(&name) && python_version() != "3.13";
if skip {
eprintln!("{name}: skipped, its limit is CPython 3.13's and python3 is {}", python_version());
}
skip
}