Merge pull request #10258 from dotCooCoo/fix/ci-select-tests

CI: run only the test shards a pull request's changes reach
This commit is contained in:
.CooCoo
2026-12-02 11:43:34 -08:00
committed by GitHub
+38 -24
View File
@@ -426,6 +426,9 @@ pub fn describe(runs: &[Run]) -> String {
/// The shards of this workspace, after the checks in [`assign`] and [`feature_differences`].
pub fn check() -> Result<Vec<Shard>, String> {
// The shared gate runs only the shards that test a crate a pull request reaches, by these
// lines, so a mismatch fails every shard instead of one test that selection may skip.
workflow_members(include_str!("../../.github/workflows/ci.yml"))?;
let members = members(&crate::metadata()?)?;
let shards = assign(GROUPS, &members)?;
let workspace = tree_features(&tree(&["--workspace".to_owned()])?, &members);
@@ -451,6 +454,39 @@ pub fn check() -> Result<Vec<Shard>, String> {
Ok(shards)
}
/// The lines of the `shard-members: |` block in `workflow`, trimmed.
fn shard_members(workflow: &str) -> Vec<&str> {
let mut lines = workflow.lines().skip_while(|l| l.trim() != "shard-members: |");
let Some(key) = lines.next() else { return Vec::new() };
let indent = |l: &str| l.len() - l.trim_start().len();
lines.take_while(|l| !l.trim().is_empty() && indent(l) > indent(key)).map(str::trim).collect()
}
/// Checks that `workflow`'s `shard-members` block names each shard's crates as [`GROUPS`] does.
fn workflow_members(workflow: &str) -> Result<(), String> {
let expected: Vec<String> = GROUPS
.iter()
.map(|g| match g {
Group::Part(split, _) => split.crates.join(" "),
// The selection names each crate on one line only, so a shard that runs a crate's
// integration tests leaves it off its line and relies on a crate there that
// depends on it (checked in the tests).
Group::Targets(lists) => {
lists.iter().filter(|(t, _)| *t != Targets::Integration).flat_map(|(_, c)| c.iter().copied()).collect::<Vec<_>>().join(" ")
}
Group::Rest => "(rest)".to_owned(),
Group::Checks => "(always)".to_owned(),
})
.collect();
let found = shard_members(workflow);
if found != expected {
return Err(format!(
"shard-members in .github/workflows/ci.yml does not match xtask/src/shards.rs › GROUPS:\n found: {found:?}\n expected: {expected:?}"
));
}
Ok(())
}
/// `cargo xtask shards`: prints each shard's crates after [`check`], and returns the shards.
pub fn run() -> Result<Vec<Shard>, String> {
let shards = check()?;
@@ -834,32 +870,10 @@ geom v0.1.0 (/w/geom)|
);
}
/// The lines of the `shard-members: |` block in `workflow`, trimmed.
fn shard_members(workflow: &str) -> Vec<&str> {
let mut lines = workflow.lines().skip_while(|l| l.trim() != "shard-members: |");
let Some(key) = lines.next() else { return Vec::new() };
let indent = |l: &str| l.len() - l.trim_start().len();
lines.take_while(|l| !l.trim().is_empty() && indent(l) > indent(key)).map(str::trim).collect()
}
#[test]
fn ci_names_the_crates_of_every_shard_for_test_selection() {
// The shared gate runs only the shards that test a crate a pull request reaches, by these lines.
let expected: Vec<String> = GROUPS
.iter()
.map(|g| match g {
Group::Part(split, _) => split.crates.join(" "),
// The selection names each crate on one line only, so a shard that runs a crate's
// integration tests leaves it off its line and relies on a crate there that
// depends on it (checked below).
Group::Targets(lists) => {
lists.iter().filter(|(t, _)| *t != Targets::Integration).flat_map(|(_, c)| c.iter().copied()).collect::<Vec<_>>().join(" ")
}
Group::Rest => "(rest)".to_owned(),
Group::Checks => "(always)".to_owned(),
})
.collect();
assert_eq!(shard_members(include_str!("../../.github/workflows/ci.yml")), expected, "shard-members in .github/workflows/ci.yml");
assert_eq!(workflow_members(include_str!("../../.github/workflows/ci.yml")), Ok(()));
assert!(workflow_members(" shard-members: |\n a b\n (rest)\n next: 1\n").is_err());
let meta = crate::metadata().unwrap();
let deps: BTreeMap<&str, Vec<&str>> = meta["packages"]
.as_array()