Cotton code
57 lines · 2.6 KB · RustRaw
//! Package records live in real tables. The legacy exchange table remains//! until its non-additive retirement; only the move and the preserved shape//! and receipt rules may name it, so ordinary engines cannot write into it.use std::path::{Path, PathBuf};/// Every file allowed to name the retired store, with why.const ALLOWED: &[(&str, &str)] = &[    ("cotton-store/src/schema/move_intents.rs", "the declared move explicitly keeps the legacy exchange shape until retirement"),    ("cotton-store/src/schema/ddl.rs", "the legacy table is preserved until its non-additive retirement"),    ("cotton-store/src/schema/table_specs.rs", "the legacy exchange shape is still served"),    ("cotton-store/src/receipts.rs", "legacy receipts retain per-key merge semantics inside the floor"),    ("cotton-core/src/primitives.rs", "describes the legacy table until retirement"),    ("cotton-store/src/migrate.rs", "RETIRED_TABLES: its receipts are silence"),];fn sources(dir: &Path, out: &mut Vec<PathBuf>) {    for entry in std::fs::read_dir(dir).expect("reads") {        let path = entry.expect("an entry").path();        let name = path.file_name().and_then(|n| n.to_str()).unwrap_or_default();        if path.is_dir() {            if !matches!(name, "target" | "tests") {                sources(&path, out);            }        } else if name.ends_with(".rs") && !name.ends_with("_tests.rs") {            out.push(path);        }    }}/// RULE: Only migrations, the legacy exchange shape and its receipt rules may name cotton_records, because an ordinary engine left on that store would bypass the real package tables.#[test]fn no_code_reads_or_writes_the_retired_store() {    let manifest = cotton_store::manifest_dir();    let crates = manifest.parent().expect("the crates directory");    let mut files = Vec::new();    sources(crates, &mut files);    let mut strays = Vec::new();    for file in files {        let shown = file.strip_prefix(crates).unwrap().to_string_lossy().to_string();        if ALLOWED.iter().any(|(path, _)| *path == shown) {            continue;        }        let text = std::fs::read_to_string(&file).unwrap_or_default();        let mut in_tests = false;        for (n, line) in text.lines().enumerate() {            if line.trim_start().starts_with("#[cfg(test)]") {                in_tests = true;            }            let code = line.split("//").next().unwrap_or_default();            if !in_tests && code.contains("cotton_records") {                strays.push(format!("{shown}:{}", n + 1));            }        }    }    assert!(strays.is_empty(), "the retired store is named outside the move: {strays:#?}");}