21d8e53034
`--changed` / `--changed-files` now narrow each harness's recorded Rust sources to those its probes can reach (tools/parity_reach.py): an item-level reachability walk from the probe modules a harness ran (or the binary it built) over free items, inherent methods, trait impls and macros, matched by name so it only over-approximates. Parent modules count through their `mod` declarations, compared with the merge base. A harness whose probe can't be placed keeps every source; `--by-crate` turns the narrowing off. On the last 20 PRs merged on 2026-10-25: 2255 -> 1061 harnesses picked, 2384 -> 1592 harness-minutes. Replaying the week's 61 failed CI parity runs, every failed harness the old selection picked is still picked; LLVM coverage of six harnesses' probes executes only kept files. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
112 lines
5.3 KiB
Python
112 lines
5.3 KiB
Python
"""tools/parity_reach.py: which workspace sources a harness's probe can reach."""
|
|
from pathlib import Path
|
|
import tempfile
|
|
import textwrap
|
|
import unittest
|
|
|
|
from tools import parity, parity_reach
|
|
|
|
PROBE = [["example", "world_probe", "release", ["-p", "a"], True]]
|
|
|
|
|
|
def workspace(root, files):
|
|
for path, text in files.items():
|
|
(root / path).parent.mkdir(parents=True, exist_ok=True)
|
|
(root / path).write_text(textwrap.dedent(text), encoding="utf-8")
|
|
|
|
|
|
class ReachTests(unittest.TestCase):
|
|
def setUp(self):
|
|
self.directory = tempfile.TemporaryDirectory()
|
|
self.root = Path(self.directory.name)
|
|
workspace(self.root, {
|
|
"crates/a/Cargo.toml": '[package]\nname = "a"\n\n[[example]]\nname = "world_probe"\npath = "examples/probe/main.rs"\n',
|
|
"crates/a/src/lib.rs": """
|
|
pub mod used;
|
|
pub mod unused;
|
|
mod methods;
|
|
mod display;
|
|
mod table;
|
|
#[cfg(test)]
|
|
mod tests;
|
|
""",
|
|
"crates/a/src/used.rs": """
|
|
pub struct Thing;
|
|
pub fn helper() -> u32 { 1 }
|
|
""",
|
|
# Same names as used.rs, but nothing names this module.
|
|
"crates/a/src/unused.rs": "pub struct Thing;\npub fn helper() -> u32 { 2 }\n",
|
|
"crates/a/src/methods.rs": """
|
|
use crate::used::Thing;
|
|
impl Thing {
|
|
pub fn method(&self) -> u32 { 3 }
|
|
pub fn never_called(&self) -> u32 { 4 }
|
|
}
|
|
""",
|
|
"crates/a/src/display.rs": """
|
|
impl std::fmt::Display for crate::used::Thing {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { write!(f, "thing") }
|
|
}
|
|
""",
|
|
"crates/a/src/table.rs": """
|
|
macro_rules! table { () => { pub struct Table; }; }
|
|
table!();
|
|
""",
|
|
"crates/a/src/tests.rs": "use crate::used::Thing;\nimpl Thing { pub fn method_in_tests(&self) {} }\n",
|
|
"crates/a/examples/probe/main.rs": "mod dispatch;\nmod p;\n",
|
|
"crates/a/examples/probe/dispatch.rs": "pub fn args() {}\n",
|
|
"crates/a/examples/probe/p.rs": """
|
|
use a::used::{helper, Thing};
|
|
pub fn main() { println!("{} {} {}", helper(), Thing.method(), Thing); }
|
|
""",
|
|
})
|
|
self.dependencies = sorted(str(path.relative_to(self.root)) for path in self.root.rglob("*") if path.is_file())
|
|
self.dependencies.remove("crates/a/examples/probe/main.rs") # parity.only_probes_run leaves it out
|
|
|
|
def tearDown(self):
|
|
self.directory.cleanup()
|
|
|
|
def narrowed(self, changed=(), artifacts=PROBE):
|
|
graph = parity_reach.SourceGraph(self.root, parity.rust_code)
|
|
return parity_reach.narrow(self.dependencies, artifacts, graph, set(changed))
|
|
|
|
def test_keeps_what_the_probe_reaches(self):
|
|
kept = {path for path, (whole, _) in self.narrowed().items() if whole}
|
|
self.assertIn("crates/a/src/used.rs", kept)
|
|
self.assertIn("crates/a/src/methods.rs", kept) # Thing.method()
|
|
self.assertIn("crates/a/src/display.rs", kept) # std's Display for a reached type
|
|
self.assertIn("crates/a/Cargo.toml", kept)
|
|
self.assertNotIn("crates/a/src/unused.rs", kept) # its names match, but nothing names its module
|
|
self.assertNotIn("crates/a/src/table.rs", kept)
|
|
self.assertNotIn("crates/a/src/tests.rs", kept) # compiled into tests only
|
|
|
|
def test_module_parents_count_through_their_declarations(self):
|
|
self.assertEqual(self.narrowed()["crates/a/src/lib.rs"], (False, {"used", "methods", "display"}))
|
|
text = (self.root / "crates/a/src/lib.rs").read_text()
|
|
self.assertEqual(parity_reach.declarations(text, {"used"}, False), "pub mod used;")
|
|
self.assertEqual(parity_reach.declarations(text, {"tests"}, False), "#[cfg(test)]\nmod tests;")
|
|
# Adding or dropping a module beside the reached ones changes neither view.
|
|
other = text.replace("mod table;", "mod table2;\nmod more;")
|
|
self.assertEqual(parity_reach.declarations(other, {"used"}, False), "pub mod used;")
|
|
self.assertEqual(parity_reach.declarations(other, {"used"}, True), parity_reach.declarations(text, {"used"}, True))
|
|
self.assertNotEqual(parity_reach.declarations(text + "pub fn f() {}\n", {"used"}, True),
|
|
parity_reach.declarations(text, {"used"}, True))
|
|
|
|
def test_a_new_file_counts_once_it_is_reached(self):
|
|
(self.root / "crates/a/src/lib.rs").write_text("pub mod used;\nmod extra;\n")
|
|
workspace(self.root, {"crates/a/src/extra.rs": "impl crate::used::Thing { pub fn method(&self) -> u32 { 5 } }\n"})
|
|
self.assertIn("crates/a/src/extra.rs", self.narrowed(changed={"crates/a/src/extra.rs"}))
|
|
|
|
def test_a_deleted_dependency_counts(self):
|
|
(self.root / "crates/a/src/unused.rs").unlink()
|
|
self.assertIn("crates/a/src/unused.rs", self.narrowed())
|
|
|
|
def test_unknown_probes_keep_everything(self):
|
|
self.assertEqual(set(self.narrowed(artifacts=[])), set(self.dependencies))
|
|
self.assertEqual(set(self.narrowed(artifacts=[["example", "missing", "release", ["-p", "a"], True]])),
|
|
set(self.dependencies))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|