summaryrefslogtreecommitdiffstats
path: root/tests/architecture.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/architecture.rs')
-rw-r--r--tests/architecture.rs172
1 files changed, 170 insertions, 2 deletions
diff --git a/tests/architecture.rs b/tests/architecture.rs
index 3eafcdc..b32ea54 100644
--- a/tests/architecture.rs
+++ b/tests/architecture.rs
@@ -224,6 +224,7 @@ struct Module {
test_only: usize,
dead_only: usize,
pub_crate: usize,
+ over_wide: usize,
globs: usize,
lines: usize,
}
@@ -301,6 +302,40 @@ impl Census {
}
}
+ // A `pub(crate)` item is over-wide when no file except the one that
+ // declares it writes its name: it offers the crate a thing nothing
+ // outside its own file reaches, and plain private is what it should be.
+ // `tests/` is not consulted — a separate crate cannot name a `pub(crate)`
+ // item, so a mention there says nothing. As with `dead`, a name declared
+ // in several files is flagged only when no file outside all of them says
+ // it, and the compiler, not this count, gives the verdict.
+ let mut pc_declared: BTreeMap<String, BTreeSet<PathBuf>> = BTreeMap::new();
+ for (path, text) in &texts {
+ for line in text.lines() {
+ if let Some(name) = pub_crate_decl(line) {
+ pc_declared.entry(name).or_default().insert(path.clone());
+ }
+ }
+ }
+ let over_allow = allowed_overwide_items();
+ for (name, files) in &pc_declared {
+ let read_outside = texts
+ .iter()
+ .any(|(path, text)| !files.contains(path) && mentions(text, name));
+ if read_outside {
+ continue;
+ }
+ for path in files {
+ let module = module_of(path);
+ // Argued, so not counted: the reason it stays `pub(crate)` is in
+ // the budget file next to its name, exactly as on the `pub` side.
+ if over_allow.contains(&format!("{module}.{name}")) {
+ continue;
+ }
+ modules.get_mut(&module).unwrap().over_wide += 1;
+ }
+ }
+
let mut edges: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
for (path, text) in &texts {
let source = module_of(path);
@@ -374,6 +409,27 @@ fn allowed_pub_items() -> BTreeSet<String> {
.unwrap_or_default()
}
+/// The `over_visibility.allowlist` keys — `module.item` for every `pub(crate)`
+/// item that stays `pub(crate)` by decision, with the compiler fact beside it.
+fn allowed_overwide_items() -> BTreeSet<String> {
+ let path = root().join("tests/architecture-budget.json");
+ let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {}", path.display(), e));
+ let value: serde_json::Value = serde_json::from_str(&text).expect("budget file is json");
+ value
+ .get("over_visibility")
+ .and_then(|d| d.get("allowlist"))
+ .and_then(|a| a.as_object())
+ .map(|a| {
+ a.keys()
+ .map(|k| {
+ assert!(!k.trim().is_empty(), "an over-wide permission key cannot be blank");
+ k.clone()
+ })
+ .collect()
+ })
+ .unwrap_or_default()
+}
+
/// A top-level section of the budget file, as `key -> allowed`.
fn budget_section(section: &str) -> BTreeMap<String, usize> {
let path = root().join("tests/architecture-budget.json");
@@ -513,11 +569,12 @@ fn glob_reexports_stay_on_the_allowlist() {
}
/// The four comparisons the ratchet makes, as `(key, actual)` pairs.
-fn budgeted(row: &Module) -> [(&'static str, usize); 4] {
+fn budgeted(row: &Module) -> [(&'static str, usize); 5] {
[
("pub", row.pub_items),
("dead", row.dead),
("deadonly", row.dead_only),
+ ("overwide", row.over_wide),
("glob", row.globs),
]
}
@@ -616,6 +673,7 @@ fn the_census_tool_and_this_test_agree() {
("test", mine.test_only),
("deadonly", mine.dead_only),
("priv", mine.pub_crate),
+ ("overwide", mine.over_wide),
("glob", mine.globs),
] {
let got = row.get(key).and_then(|v| v.as_u64()).unwrap_or(u64::MAX) as usize;
@@ -873,6 +931,69 @@ fn the_dead_code_allowlist_names_real_items() {
);
}
+/// The `over_visibility.allowlist` is a set of decisions too, and it is checked
+/// the same two ways: an entry must name something that is still `pub(crate)` in
+/// the module it names, and it must carry a reason worth reading.
+///
+/// The first direction matters more here than on the `pub` side. A `pub(crate)`
+/// item that has since gone private still matches a `module.item` key written
+/// against the module's declared names, so the check has to look at the
+/// `pub(crate)` declarations specifically — otherwise the permission quietly
+/// re-widens the item it was left behind by.
+#[test]
+fn the_over_visibility_allowlist_names_real_items() {
+ let path = root().join("tests/architecture-budget.json");
+ let text = std::fs::read_to_string(&path).unwrap_or_else(|e| panic!("read {}: {}", path.display(), e));
+ let value: serde_json::Value = serde_json::from_str(&text).expect("budget file is json");
+ let allow = value
+ .get("over_visibility")
+ .and_then(|d| d.get("allowlist"))
+ .and_then(|a| a.as_object())
+ .expect("budget file has an over_visibility.allowlist object");
+
+ let mut pub_crate_declared: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
+ for path in src_files() {
+ let module = module_of(&path);
+ for line in std::fs::read_to_string(&path).expect("read source").lines() {
+ if let Some(name) = pub_crate_decl(line) {
+ pub_crate_declared.entry(module.clone()).or_default().insert(name);
+ }
+ }
+ }
+
+ let mut stale = Vec::new();
+ let mut blank = Vec::new();
+ for (key, reason) in allow {
+ let Some((module, name)) = key.split_once('.') else {
+ stale.push(format!("{key}: a key is `module.item`"));
+ continue;
+ };
+ if reason.as_str().map(|r| r.trim().len() < 20).unwrap_or(true) {
+ blank.push(key.clone());
+ }
+ if !pub_crate_declared
+ .get(module)
+ .is_some_and(|names| names.contains(name))
+ {
+ stale.push(format!("{key} at {reason}"));
+ }
+ }
+ stale.sort();
+ blank.sort();
+ assert!(
+ stale.is_empty(),
+ "these over-wide permissions name something that is no longer a `pub(crate)`\n\
+ item in that module — it went private, or it was never one:\n{}",
+ stale.join("\n")
+ );
+ assert!(
+ blank.is_empty(),
+ "these over-wide permissions have no reason (or one too short to be one):\n{}\n\
+ say what the compiler refuses to let go private, not that it felt safer.",
+ blank.join("\n")
+ );
+}
+
/// `tests/common/mod.rs` is compiled into every test binary, so the suite keeps
/// a crate-level `allow(dead_code)` there — a helper used by one binary is dead
/// code in the other twenty, and the lint would fire everywhere and mean
@@ -1037,6 +1158,53 @@ fn the_dead_code_budget_matches_the_code() {
);
}
+#[test]
+fn the_over_visibility_budget_matches_the_code() {
+ // The same self-consistency the dead-code budget is held to: the accepted
+ // count, the number in the `modules` section, and the number the census
+ // actually measures have to be one number, or the ratchet is checking a
+ // figure nothing reads.
+ 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 over = value
+ .get("over_visibility")
+ .and_then(|d| d.get("overwide"))
+ .and_then(|d| d.as_object())
+ .expect("over_visibility.overwide");
+ let census = Census::take();
+
+ let mut mismatch = Vec::new();
+ for (module, row) in modules {
+ let stated = row.get("overwide").and_then(|v| v.as_u64()).unwrap_or(u64::MAX);
+ let accepted = over.get(module).and_then(|v| v.as_u64());
+ let actual = census
+ .modules
+ .get(module)
+ .map(|m| m.over_wide as u64)
+ .unwrap_or(u64::MAX);
+ if accepted != Some(stated) || stated != actual {
+ mismatch.push(format!(
+ "{module}: over_visibility.overwide {:?}, modules.overwide {stated}, actual {actual}",
+ accepted
+ ));
+ }
+ }
+ for module in over.keys() {
+ if !modules.contains_key(module) {
+ mismatch.push(format!("{module}: in over_visibility.overwide, absent from modules"));
+ }
+ }
+ mismatch.sort();
+ assert!(
+ mismatch.is_empty(),
+ "the budget contradicts itself about how many over-wide `pub(crate)` items a\n\
+ 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
@@ -1063,7 +1231,7 @@ fn the_budget_writers_preserve_each_other() {
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"] {
+ for section in ["test_surface", "dead_code", "over_visibility"] {
assert!(
after.get(section).is_some(),
"{tool} erased the `{section}` section it does not own"