diff --git a/crates/horizon-world/examples/probe/dispatch.rs b/crates/horizon-world/examples/probe/dispatch.rs new file mode 100644 index 000000000..0b396edab --- /dev/null +++ b/crates/horizon-world/examples/probe/dispatch.rs @@ -0,0 +1,40 @@ +//! How the probe binary runs a probe. main.rs lists the probes and nothing else (tools/parity.py leaves it out of +//! the harnesses' dependencies); code every probe relies on goes here. +use std::process::ExitCode; + +/// `probes! { a, b, }` declares the binary's `main`, which runs the probe module named by its first argument. +macro_rules! probes { + ($($name:ident,)*) => { + fn run(name: &str) -> Option { + use std::process::Termination; + Some(match name { + $(stringify!($name) => $name::main().report(),)* + _ => return None, + }) + } + + fn main() -> std::process::ExitCode { + dispatch::main(&[$(stringify!($name)),*], run) + } + }; +} + +/// The probe's command line as its own example saw it: the program, then the arguments after the probe's name. +pub fn args() -> std::vec::IntoIter { + let mut args: Vec = std::env::args().collect(); + if args.len() > 1 { + args.remove(1); + } + args.into_iter() +} + +pub fn main(names: &[&str], run: fn(&str) -> Option) -> ExitCode { + let name = std::env::args().nth(1).unwrap_or_default(); + run(&name).unwrap_or_else(|| { + eprintln!( + "usage: probe [arguments...]\nprobes: {}", + names.join(" ") + ); + ExitCode::from(2) + }) +} diff --git a/crates/horizon-world/examples/probe/main.rs b/crates/horizon-world/examples/probe/main.rs index d9a0289b8..f3b9b6ef5 100644 --- a/crates/horizon-world/examples/probe/main.rs +++ b/crates/horizon-world/examples/probe/main.rs @@ -1,7 +1,13 @@ //! Every horizon-world parity probe in one binary: `probe [arguments]` runs `.rs` as its own //! example did (tools/test_*_parity.py). Linking ~170 examples, each instantiating the crate's generics again, //! was most of the parity job's probe build; one binary builds them once. -use std::process::{ExitCode, Termination}; +//! +//! This file only lists the probes, so that adding one reruns no harness: tools/parity.py leaves it out of +//! their dependencies. How a probe runs is in dispatch.rs. +#[macro_use] +mod dispatch; + +use dispatch::args; /// Code shared by several probes. mod support { @@ -180,20 +186,6 @@ mod world_fixture; mod world_ray_fixture; mod worldgen_random_fixture; -/// `run(name)` runs the probe module `name`; NAMES lists them. -macro_rules! probes { - ($($name:ident,)*) => { - const NAMES: &[&str] = &[$(stringify!($name)),*]; - - fn run(name: &str) -> Option { - Some(match name { - $(stringify!($name) => $name::main().report(),)* - _ => return None, - }) - } - }; -} - probes! { active_effect_tick_fixture, active_effects_fixture, @@ -364,23 +356,3 @@ probes! { world_ray_fixture, worldgen_random_fixture, } - -/// The probe's command line as its own example saw it: the program, then the arguments after the probe's name. -fn args() -> std::vec::IntoIter { - let mut args: Vec = std::env::args().collect(); - if args.len() > 1 { - args.remove(1); - } - args.into_iter() -} - -fn main() -> ExitCode { - let name = std::env::args().nth(1).unwrap_or_default(); - run(&name).unwrap_or_else(|| { - eprintln!( - "usage: probe [arguments...]\nprobes: {}", - NAMES.join(" ") - ); - ExitCode::from(2) - }) -} diff --git a/tests/test_parity_runner.py b/tests/test_parity_runner.py index 1ecd790f0..633163bf5 100644 --- a/tests/test_parity_runner.py +++ b/tests/test_parity_runner.py @@ -57,12 +57,14 @@ class ParityRunnerTests(unittest.TestCase): (probe / "support").mkdir(parents=True) (probe / "a_fixture.rs").write_text('mod integration { include!("d_fixture.rs"); }') (probe / "d_fixture.rs").write_text("use crate::support::shared as s;") - for name in ("b_fixture.rs", "c_fixture.rs", "main.rs", "support/shared.rs", "support/other.rs"): + (probe / "b_fixture.rs").write_text("use crate::e_fixture;") + for name in ("c_fixture.rs", "dispatch.rs", "e_fixture.rs", "main.rs", "support/shared.rs", "support/other.rs"): (probe / name).write_text("") paths = [f"crates/w/examples/probe/{name}" for name in ("a_fixture.rs", "b_fixture.rs", "c_fixture.rs", - "d_fixture.rs", "main.rs", "support/other.rs", "support/shared.rs")] + "d_fixture.rs", "dispatch.rs", "e_fixture.rs", "main.rs", + "support/other.rs", "support/shared.rs")] self.assertEqual(parity.only_probes_run(["crates/w/src/lib.rs", *paths], {"a_fixture", "b_fixture"}), - ["crates/w/src/lib.rs", *paths[:2], *paths[3:5], paths[6]]) + ["crates/w/src/lib.rs", *paths[:2], *paths[3:6], paths[8]]) self.assertEqual(parity.only_probes_run(paths, set()), paths) def test_java_sources_include_classes_pulled_from_the_sourcepath(self): diff --git a/tools/parity.py b/tools/parity.py index 8d12963e1..68ff16e73 100644 --- a/tools/parity.py +++ b/tools/parity.py @@ -482,16 +482,17 @@ def probes_run(commands): def only_probes_run(paths, names): - """A probe binary's dep-info lists every probe in it. Keep those the harness ran, the probes they include! - and the shared support modules they name (support::), so that editing one probe reruns only its own - harnesses.""" + """A probe binary's dep-info lists every probe in it. Keep those the harness ran, the probes they include! or + name (crate::) and the shared support modules they name (support::), so that editing one probe + reruns only its own harnesses. Files in a probe's own subdirectory and dispatch.rs, which runs them, are always + kept; main.rs, which only lists them, is left out, so adding a probe reruns nothing.""" if not names: return paths - names, used = {*names, "main"}, "" + names, used = {*names, "dispatch"}, "" while True: ran = [path for path in paths if (match := PROBE_SOURCE.search(path)) and not match.group(1) and match.group(2) in names] used = "".join((ROOT / path).read_text(encoding="utf-8", errors="replace") for path in ran) - included = set(re.findall(r'include!\("(\w+)\.rs"\)', used)) - names + included = {*re.findall(r'include!\("(\w+)\.rs"\)', used), *re.findall(r"\bcrate::(\w+)", used)} - names if not included: break names |= included