diff options
| -rw-r--r-- | README.md | 7 | ||||
| -rw-r--r-- | tests/architecture.rs | 91 |
2 files changed, 96 insertions, 2 deletions
@@ -119,7 +119,7 @@ Two tools keep this honest, and both are meant to be run, not read once: ``` cargo test --test architecture # ranks, no cycles, no globs, interface budgets -python3 tools/arch-census.py # the numbers, and --budget to check the ratchet +python3 tools/arch-census.py # the numbers; --list-dead names them, --budget checks the ratchet ``` ## Testing without root, network, or a real chroot @@ -256,4 +256,7 @@ These are not suggestions: items it hands out, and how many of them nothing outside the module ever names. Budgets may only shrink — a number that grows fails the suite, and a number that quietly got smaller has to be written back at the lower value. - `tools/arch-census.py` prints the same numbers for a human to read. + `tools/arch-census.py` prints the same numbers for a human to read, and + `--list-dead` names every item behind them. A `pub` item that stays `pub` + without an outside reader is allowed only by name, in the same budget file, + with the reason beside it. diff --git a/tests/architecture.rs b/tests/architecture.rs index 696bac1..b8de0ce 100644 --- a/tests/architecture.rs +++ b/tests/architecture.rs @@ -972,3 +972,94 @@ fn every_fixture_is_referenced() { either drive one or delete the directory.", ); } + +/// The two places the budget states how many unargued dead items a module may +/// have must say the same thing. +/// +/// `modules.<m>.deadonly` is the ratchet `interfaces_stay_within_budget` reads; +/// `dead_code.deadonly.<m>` is the one `tools/arch-census.py --budget` reads. +/// They are written by the same invocation and disagree only if someone edited +/// one by hand, which is exactly the moment a ratchet stops meaning anything. +/// Both are zero, and zero is the point: an item that is dead, exported and +/// unargued is not a budget question, it is a failure. +#[test] +fn the_dead_code_budget_matches_the_code() { + let path = root().join("tests/architecture-budget.json"); + let value: serde_json::Value = + serde_json::from_str(&std::fs::read_to_string(&path).expect("read budget")).expect("budget is json"); + let modules = value.get("modules").and_then(|m| m.as_object()).expect("modules"); + let dead_code = value + .get("dead_code") + .and_then(|d| d.get("deadonly")) + .and_then(|d| d.as_object()) + .expect("dead_code.deadonly"); + let census = Census::take(); + + let mut mismatch = Vec::new(); + for (module, row) in modules { + let interface = row.get("deadonly").and_then(|v| v.as_u64()).unwrap_or(u64::MAX); + let dead = dead_code.get(module).and_then(|v| v.as_u64()); + let actual = census + .modules + .get(module) + .map(|m| m.dead_only as u64) + .unwrap_or(u64::MAX); + if dead != Some(interface) || interface != actual { + mismatch.push(format!( + "{module}: dead_code.deadonly {:?}, modules.deadonly {interface}, actual {actual}", + dead + )); + } + } + for module in dead_code.keys() { + if !modules.contains_key(module) { + mismatch.push(format!("{module}: in dead_code.deadonly, absent from modules")); + } + } + mismatch.sort(); + assert!( + mismatch.is_empty(), + "the budget contradicts itself about how many unargued dead items a module has:\n{}", + mismatch.join("\n") + ); +} + +/// Two tools write one file, so neither may erase what the other owns. +/// +/// `tools/arch-census.py` writes the module numbers and the accepted dead-code +/// counts; `tools/test-surface.py` writes the test-surface counts; a human owns +/// the allowlist. A rewrite that drops a section does not fail anything until +/// the ratchet quietly stops checking that axis, so this runs both writers +/// against a copy and fails if either section disappears. +#[test] +fn the_budget_writers_preserve_each_other() { + let source = std::fs::read_to_string(root().join("tests/architecture-budget.json")).expect("read budget"); + let before: serde_json::Value = serde_json::from_str(&source).expect("budget is json"); + let tmp = std::env::temp_dir().join(format!("aur-budget-preserve-{}.json", std::process::id())); + std::fs::write(&tmp, &source).expect("write temp budget"); + + for tool in ["tools/arch-census.py", "tools/test-surface.py"] { + let out = std::process::Command::new("python3") + .arg(tool) + .arg("--write-budget") + .arg(&tmp) + .current_dir(root()) + .output() + .unwrap_or_else(|e| panic!("run {tool}: {e}")); + assert!(out.status.success(), "{tool} failed: {}", String::from_utf8_lossy(&out.stderr)); + let after: serde_json::Value = + serde_json::from_str(&std::fs::read_to_string(&tmp).expect("read temp budget")) + .expect("written budget is json"); + for section in ["test_surface", "dead_code"] { + assert!( + after.get(section).is_some(), + "{tool} erased the `{section}` section it does not own" + ); + } + assert_eq!( + after["dead_code"]["allowlist"], before["dead_code"]["allowlist"], + "{tool} rewrote the hand-written allowlist" + ); + } + std::fs::remove_file(&tmp).ok(); +} |
