mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-21 20:28:51 +00:00
Merge branch 'main' into test/menu-font-fixture-names
This commit is contained in:
@@ -30,6 +30,15 @@ jobs:
|
||||
# shutdown signal" (exit 143). Panics still name file and line; local builds keep Cargo.toml's
|
||||
# line tables.
|
||||
dev-strip: true
|
||||
# The workspace crates also build without line tables. A panic still names its file and line.
|
||||
dev-debug: "0"
|
||||
# Five legs instead of one: shards 1 to 4 each build and run the tests of their own crates,
|
||||
# and shard 5 runs the other `cargo xtask ci` steps (xtask/src/shards.rs). `gate (<os>)`
|
||||
# passes only when all five did. On pushes to main, a `cache warm` leg builds what every
|
||||
# shard builds and saves the dependency cache.
|
||||
test-shards: 5
|
||||
shard-command: cargo xtask ci shard
|
||||
warm-command: cargo xtask ci warm
|
||||
packages: >-
|
||||
pkg-config libxkbcommon-dev libwayland-dev libx11-dev libxcb1-dev libxcursor-dev
|
||||
libxrandr-dev libxi-dev libfontconfig1-dev libfreetype-dev mesa-vulkan-drivers
|
||||
|
||||
Generated
+2
@@ -6683,6 +6683,7 @@ dependencies = [
|
||||
"craft-image-codecs",
|
||||
"crc32fast",
|
||||
"proptest",
|
||||
"vectorcraft-affinity",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -7044,6 +7045,7 @@ dependencies = [
|
||||
"vectorcraft-doc",
|
||||
"vectorcraft-effects",
|
||||
"vectorcraft-geom 0.24.1",
|
||||
"vectorcraft-pdf",
|
||||
"vectorcraft-testkit",
|
||||
"vectorcraft-text",
|
||||
]
|
||||
|
||||
@@ -21,3 +21,6 @@ proptest = { workspace = true }
|
||||
# The tests build containers and decode previews.
|
||||
crc32fast = { workspace = true }
|
||||
craft-image-codecs = { workspace = true }
|
||||
# tests/it's `import_limits`, `outline_limits` and `text` modules need `synth`, also when this crate
|
||||
# is tested without vectorcraft-engine (`cargo xtask ci shard`).
|
||||
vectorcraft-affinity = { path = ".", features = ["synth"] }
|
||||
|
||||
@@ -56,6 +56,9 @@ serde_json = { workspace = true }
|
||||
krilla = { workspace = true, features = ["simple-text"] }
|
||||
# How many times the symbol list is read to find symbols by name.
|
||||
vectorcraft-doc = { workspace = true, features = ["symbol-read-count"] }
|
||||
# The counters vectorcraft-engine's tests turn on, also when this crate is tested without
|
||||
# vectorcraft-engine (`cargo xtask ci shard`).
|
||||
vectorcraft-pdf = { path = ".", features = ["read-count"] }
|
||||
|
||||
# An independent raster oracle for gradient placement in PDF forms. These are test-only macOS
|
||||
# bindings; the exporter stays portable Rust and does not depend on an OS PDF renderer.
|
||||
|
||||
+158
-30
@@ -13,6 +13,7 @@ mod layers;
|
||||
mod pdf_deps;
|
||||
mod pngsuite;
|
||||
mod sha256;
|
||||
mod shards;
|
||||
mod stats;
|
||||
mod test262;
|
||||
mod ui_commands;
|
||||
@@ -37,6 +38,11 @@ commands:
|
||||
wasm cargo check --target wasm32-unknown-unknown for the wasm-safe crates and the web app
|
||||
test the workspace tests (cargo nextest when installed, else cargo test), then the doctests
|
||||
ci fmt --check, clippy -D warnings, test, doctests, no-bundled-fonts, pdf-deps, assets, brands, cursors (warnings only), ui-commands --check, layers, wasm (stops at first failure)
|
||||
ci shard shard $GATE_SHARD of $GATE_SHARDS of ci (xtask/src/shards.rs): a test shard runs
|
||||
the tests and doctests of its crates, the last shard the other steps
|
||||
ci warm build what every shard builds, without running it (fills the gate's cache)
|
||||
shards list the crates each shard tests; fails when a member is in no shard or in two,
|
||||
or builds with other features in its shard than in the whole workspace
|
||||
corpus [--download] [--affinity]
|
||||
show where test corpora live; --download fetches PngSuite into corpus/pngsuite,
|
||||
--affinity the pinned public Affinity documents into corpus/affinity
|
||||
@@ -66,7 +72,10 @@ fn main() -> ExitCode {
|
||||
Some("pdf-deps") => pdf_deps::run(&root()),
|
||||
Some("wasm") => cmd_wasm(),
|
||||
Some("test") => cmd_test().and_then(|()| cmd_doctests()),
|
||||
Some("ci") if rest.first() == Some(&"shard") => cmd_ci_shard(),
|
||||
Some("ci") if rest.first() == Some(&"warm") => cmd_ci_warm(),
|
||||
Some("ci") => cmd_ci(),
|
||||
Some("shards") => shards::run().map(drop),
|
||||
Some("corpus") if rest.contains(&"--affinity") => affinity_corpus::fetch(),
|
||||
Some("corpus") => cmd_corpus(rest.contains(&"--download")),
|
||||
Some("bundle") => bundle::run(&root()),
|
||||
@@ -246,30 +255,162 @@ fn has_nextest() -> bool {
|
||||
/// The workspace's unit and integration tests, without examples (clippy still checks those).
|
||||
/// nextest runs each test in its own process, in parallel across all test binaries.
|
||||
fn cmd_test() -> Result<(), String> {
|
||||
test_packages(&["--workspace".to_owned()], "--workspace")
|
||||
}
|
||||
|
||||
/// [`cmd_test`] for the packages `select` picks (`--workspace` or `-p` ids), shown as `shown`.
|
||||
fn test_packages(select: &[String], shown: &str) -> Result<(), String> {
|
||||
const TARGETS: [&str; 3] = ["--lib", "--bins", "--tests"];
|
||||
let mut c = cargo();
|
||||
if has_nextest() {
|
||||
c.args(["nextest", "run", "--workspace"]).args(TARGETS);
|
||||
run(c, "cargo nextest run --workspace --lib --bins --tests")
|
||||
c.args(["nextest", "run"]).args(select).args(TARGETS);
|
||||
run(c, &format!("cargo nextest run {shown} --lib --bins --tests"))
|
||||
} else {
|
||||
eprintln!("note: cargo-nextest is not installed; running cargo test (cargo install cargo-nextest for faster runs)");
|
||||
c.args(["test", "--workspace"]).args(TARGETS);
|
||||
run(c, "cargo test --workspace --lib --bins --tests")
|
||||
c.arg("test").args(select).args(TARGETS);
|
||||
run(c, &format!("cargo test {shown} --lib --bins --tests"))
|
||||
}
|
||||
}
|
||||
|
||||
/// The doctests, which nextest doesn't run.
|
||||
fn cmd_doctests() -> Result<(), String> {
|
||||
let mut c = cargo();
|
||||
c.args(["test", "--workspace", "--doc"]);
|
||||
run(c, "cargo test --workspace --doc")
|
||||
doctest_packages(&["--workspace".to_owned()], "--workspace")
|
||||
}
|
||||
|
||||
/// [`cmd_doctests`] for the packages `select` picks, shown as `shown`.
|
||||
fn doctest_packages(select: &[String], shown: &str) -> Result<(), String> {
|
||||
let mut c = cargo();
|
||||
c.arg("test").args(select).arg("--doc");
|
||||
run(c, &format!("cargo test {shown} --doc"))
|
||||
}
|
||||
|
||||
/// One `cargo xtask ci` step: its name and what it runs.
|
||||
type Step = (&'static str, Box<dyn Fn() -> Result<(), String>>);
|
||||
|
||||
/// The `ci` steps that build and run tests of the whole workspace; `ci shard` runs them per shard.
|
||||
const TEST_STEPS: [&str; 3] = ["test", "doctests", "i18n-report"];
|
||||
|
||||
/// `cargo xtask ci`: each step in turn, stopping at the first that fails; the summary names the
|
||||
/// steps that passed and, after a failure, the one that failed and those skipped.
|
||||
fn cmd_ci() -> Result<(), String> {
|
||||
type Step = (&'static str, Box<dyn Fn() -> Result<(), String>>);
|
||||
let steps: Vec<Step> = vec![
|
||||
run_steps(&ci_steps())
|
||||
}
|
||||
|
||||
/// `cargo xtask ci shard`: shard `GATE_SHARD` of `GATE_SHARDS` (1-based, as the shared gate
|
||||
/// workflow sets them). A test shard runs the shard checks, then the tests and doctests of its
|
||||
/// crates, and the i18n report where it tests vectorcraft-ui-egui. The checks shard runs every
|
||||
/// other `ci` step and the shard checks.
|
||||
fn cmd_ci_shard() -> Result<(), String> {
|
||||
let number = |key: &str| -> Result<usize, String> {
|
||||
let value = std::env::var(key).map_err(|_| format!("ci shard: {key} is not set (the gate workflow sets it)"))?;
|
||||
value.trim().parse().map_err(|_| format!("ci shard: {key} is `{value}`, not a number"))
|
||||
};
|
||||
let (shard, count) = (number("GATE_SHARD")?, number("GATE_SHARDS")?);
|
||||
if count != shards::GROUPS.len() {
|
||||
return Err(format!(
|
||||
"ci shard: GATE_SHARDS is {count}, and xtask/src/shards.rs has {} shards (test-shards in .github/workflows/ci.yml)",
|
||||
shards::GROUPS.len()
|
||||
));
|
||||
}
|
||||
if !(1..=count).contains(&shard) {
|
||||
return Err(format!("ci shard: GATE_SHARD is {shard}, not 1 to {count}"));
|
||||
}
|
||||
let checked = shards::run()?;
|
||||
let mut steps: Vec<Step> = Vec::new();
|
||||
match &checked[shard - 1] {
|
||||
shards::Shard::Tests(crates) => {
|
||||
let names = shards::names(crates);
|
||||
eprintln!("ci shard {shard}/{count}: the tests of {names}");
|
||||
let select = shards::select(crates);
|
||||
let shown = crates.iter().map(|m| format!("-p {}", m.name)).collect::<Vec<_>>().join(" ");
|
||||
let (s, w) = (select.clone(), shown.clone());
|
||||
steps.push(("test", Box::new(move || test_packages(&s, &w))));
|
||||
let (s, w) = (select.clone(), shown.clone());
|
||||
steps.push(("doctests", Box::new(move || doctest_packages(&s, &w))));
|
||||
if crates.iter().any(|m| m.name == "vectorcraft-ui-egui") {
|
||||
steps.push(("i18n-report", Box::new(move || i18n_report_packages(&select, &shown))));
|
||||
}
|
||||
}
|
||||
shards::Shard::Checks => {
|
||||
eprintln!("ci shard {shard}/{count}: the ci steps other than the tests");
|
||||
steps.extend(ci_steps().into_iter().filter(|(name, _)| !TEST_STEPS.contains(name)));
|
||||
}
|
||||
}
|
||||
run_steps(&steps)
|
||||
}
|
||||
|
||||
/// `cargo xtask ci warm`: for each test shard, builds its crates' library and binary tests without
|
||||
/// running them, then builds what the checks shard builds, so one dependency-cache entry holds the
|
||||
/// dependencies of every shard with the features that shard builds them with (cargo resolves
|
||||
/// features over the packages each command selects). The integration tests are left out: they
|
||||
/// build no dependency that the library tests do not, since both take the crate's dev-dependencies.
|
||||
fn cmd_ci_warm() -> Result<(), String> {
|
||||
let checked = shards::run()?;
|
||||
let mut steps: Vec<Step> = Vec::new();
|
||||
for shard in checked {
|
||||
if let shards::Shard::Tests(crates) = shard {
|
||||
steps.push((
|
||||
"build tests",
|
||||
Box::new(move || {
|
||||
let mut c = cargo();
|
||||
c.arg("test").args(shards::select(&crates)).args(["--lib", "--bins", "--no-run"]);
|
||||
run(c, &format!("cargo test --lib --bins --no-run ({})", shards::names(&crates)))
|
||||
}),
|
||||
));
|
||||
}
|
||||
}
|
||||
steps.push((
|
||||
"clippy",
|
||||
Box::new(|| {
|
||||
let mut c = cargo();
|
||||
c.args(["clippy", "--workspace", "--all-targets", "--keep-going"]);
|
||||
run(c, "cargo clippy --workspace --all-targets --keep-going")
|
||||
}),
|
||||
));
|
||||
steps.push((
|
||||
"no-bundled-fonts",
|
||||
Box::new(|| {
|
||||
let mut c = cargo();
|
||||
c.args(["test", "-p", "vectorcraft-text", "--no-default-features", "--test", "without_bundled_fonts", "--no-run"]);
|
||||
run(c, "cargo test -p vectorcraft-text --no-default-features --test without_bundled_fonts --no-run")?;
|
||||
let mut c = cargo();
|
||||
c.args(["clippy", "-p", "vectorcraft-ui-egui", "--no-default-features", "--all-targets"]);
|
||||
run(c, "cargo clippy -p vectorcraft-ui-egui --no-default-features --all-targets")?;
|
||||
let mut c = cargo();
|
||||
c.args(["test", "-p", "vectorcraft-ui-egui", "--no-default-features", "--test", "install_fonts", "--no-run"]);
|
||||
run(c, "cargo test -p vectorcraft-ui-egui --no-default-features --test install_fonts --no-run")
|
||||
}),
|
||||
));
|
||||
steps.push(("wasm", Box::new(cmd_wasm)));
|
||||
run_steps(&steps)
|
||||
}
|
||||
|
||||
/// Runs `steps` in turn, stopping at the first that fails; the summary names the steps that passed
|
||||
/// and, after a failure, the one that failed and those skipped.
|
||||
fn run_steps(steps: &[Step]) -> Result<(), String> {
|
||||
let mut done = Vec::new();
|
||||
for (name, f) in steps {
|
||||
eprintln!("\n=== ci: {name} ===");
|
||||
if let Err(e) = f() {
|
||||
println!("\nCI summary:");
|
||||
for d in &done {
|
||||
println!(" ok {d}");
|
||||
}
|
||||
println!(" FAIL {name}: {e}");
|
||||
for (n, _) in steps.iter().skip(done.len() + 1) {
|
||||
println!(" skip {n}");
|
||||
}
|
||||
return Err(format!("ci failed at `{name}`"));
|
||||
}
|
||||
done.push(*name);
|
||||
}
|
||||
println!("\nCI summary: all {} steps passed ({})", done.len(), done.join(", "));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Every `cargo xtask ci` step, in order.
|
||||
fn ci_steps() -> Vec<Step> {
|
||||
vec![
|
||||
(
|
||||
"fmt",
|
||||
Box::new(|| {
|
||||
@@ -316,25 +457,7 @@ fn cmd_ci() -> Result<(), String> {
|
||||
("ui-commands", Box::new(|| ui_commands::run(&root(), true))),
|
||||
("layers", Box::new(cmd_layers)),
|
||||
("wasm", Box::new(cmd_wasm)),
|
||||
];
|
||||
let mut done = Vec::new();
|
||||
for (name, f) in &steps {
|
||||
eprintln!("\n=== ci: {name} ===");
|
||||
if let Err(e) = f() {
|
||||
println!("\nCI summary:");
|
||||
for d in &done {
|
||||
println!(" ok {d}");
|
||||
}
|
||||
println!(" FAIL {name}: {e}");
|
||||
for (n, _) in steps.iter().skip(done.len() + 1) {
|
||||
println!(" skip {n}");
|
||||
}
|
||||
return Err(format!("ci failed at `{name}`"));
|
||||
}
|
||||
done.push(*name);
|
||||
}
|
||||
println!("\nCI summary: all {} steps passed ({})", done.len(), done.join(", "));
|
||||
Ok(())
|
||||
]
|
||||
}
|
||||
|
||||
fn cmd_corpus(download: bool) -> Result<(), String> {
|
||||
@@ -379,7 +502,12 @@ fn cmd_i18n_report() -> Result<(), String> {
|
||||
/// differently and recompiled for about 3.5 minutes. Every other test binary runs with no test
|
||||
/// matching the filter.
|
||||
fn cmd_ci_i18n_report() -> Result<(), String> {
|
||||
i18n_report_packages(&["--workspace".to_owned()], "--workspace")
|
||||
}
|
||||
|
||||
/// [`cmd_ci_i18n_report`] for the packages `select` picks (a `ci shard`'s), shown as `shown`.
|
||||
fn i18n_report_packages(select: &[String], shown: &str) -> Result<(), String> {
|
||||
let mut c = cargo();
|
||||
c.args(["test", "--workspace", "--lib", "--bins", "--tests", "--", "i18n::tests::untranslated_report", "--exact", "--nocapture"]);
|
||||
run(c, "cargo test --workspace --lib --bins --tests -- i18n::tests::untranslated_report --exact --nocapture")
|
||||
c.arg("test").args(select).args(["--lib", "--bins", "--tests", "--", "i18n::tests::untranslated_report", "--exact", "--nocapture"]);
|
||||
run(c, &format!("cargo test {shown} --lib --bins --tests -- i18n::tests::untranslated_report --exact --nocapture"))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,294 @@
|
||||
//! The gate's shards (`cargo xtask ci shard`, with `GATE_SHARD` of `GATE_SHARDS` set by the shared
|
||||
//! gate workflow): which workspace crates each shard tests, and the checks that keep the split
|
||||
//! complete.
|
||||
//!
|
||||
//! Each test shard builds and runs the tests of its own crates and what they depend on. The last
|
||||
//! shard runs the `cargo xtask ci` steps that are not tests. [`GROUPS`] names the crates of the
|
||||
//! first shards; every other workspace member goes to the [`Group::Rest`] shard, so a new crate is
|
||||
//! tested without a change here. [`check`] fails when a named crate is not a workspace member or
|
||||
//! is named twice, when a group is empty, and when a crate's features in its shard differ from
|
||||
//! its features in `cargo test --workspace`. Cargo resolves features over the packages a command
|
||||
//! selects, so a feature that only another crate's dev-dependencies turn on is off in a shard
|
||||
//! without that crate, and the tests behind it do not build.
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet};
|
||||
|
||||
use serde_json::Value;
|
||||
|
||||
/// What one shard runs.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Group {
|
||||
/// The tests of these workspace crates (package names).
|
||||
Crates(&'static [&'static str]),
|
||||
/// The tests of every workspace member no other group names.
|
||||
Rest,
|
||||
/// The `cargo xtask ci` steps that are not tests.
|
||||
Checks,
|
||||
}
|
||||
|
||||
/// The shards in order; `test-shards` in `.github/workflows/ci.yml` is its length.
|
||||
pub const GROUPS: &[Group] = &[
|
||||
Group::Crates(&["vectorcraft-ui-egui"]),
|
||||
Group::Crates(&["vectorcraft-engine"]),
|
||||
Group::Crates(&["vectorcraft", "vectorcraft-web", "vectorcraft-cli", "vectorcraft-mcp", "xtask"]),
|
||||
Group::Rest,
|
||||
Group::Checks,
|
||||
];
|
||||
|
||||
/// A workspace member, as `cargo metadata` gives it.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Member {
|
||||
pub name: String,
|
||||
/// The package id, which `-p` takes where the name would be ambiguous: craft-libs builds
|
||||
/// against this repo's geom and pathops by git, so the graph holds a second copy of each.
|
||||
pub id: String,
|
||||
pub version: String,
|
||||
}
|
||||
|
||||
/// What one shard runs, with its crates resolved.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Shard {
|
||||
Tests(Vec<Member>),
|
||||
Checks,
|
||||
}
|
||||
|
||||
/// The workspace members of `cargo metadata --no-deps` output, in its order.
|
||||
pub fn members(meta: &Value) -> Result<Vec<Member>, String> {
|
||||
let ids: BTreeSet<&str> = meta["workspace_members"].as_array().into_iter().flatten().filter_map(Value::as_str).collect();
|
||||
let members: Vec<Member> = meta["packages"]
|
||||
.as_array()
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.filter(|p| p["id"].as_str().is_some_and(|id| ids.contains(id)))
|
||||
.map(|p| {
|
||||
let field = |key: &str| p[key].as_str().map(str::to_owned).ok_or_else(|| format!("cargo metadata: a workspace package has no `{key}`"));
|
||||
Ok(Member { name: field("name")?, id: field("id")?, version: field("version")? })
|
||||
})
|
||||
.collect::<Result<_, String>>()?;
|
||||
if members.is_empty() {
|
||||
return Err("cargo metadata lists no workspace members".into());
|
||||
}
|
||||
Ok(members)
|
||||
}
|
||||
|
||||
/// Each group of `groups` with its crates: a named crate must be a workspace member and named
|
||||
/// once, there must be exactly one [`Group::Rest`], and no test shard may be empty.
|
||||
pub fn assign(groups: &[Group], members: &[Member]) -> Result<Vec<Shard>, String> {
|
||||
const WHERE: &str = "xtask/src/shards.rs › GROUPS";
|
||||
let mut named: BTreeMap<&str, usize> = BTreeMap::new();
|
||||
for (i, g) in groups.iter().enumerate() {
|
||||
if let Group::Crates(names) = g {
|
||||
if names.is_empty() {
|
||||
return Err(format!("shard {} names no crates ({WHERE})", i + 1));
|
||||
}
|
||||
for name in *names {
|
||||
if !members.iter().any(|m| m.name == *name) {
|
||||
return Err(format!("shard {} names `{name}`, which is not a workspace member ({WHERE})", i + 1));
|
||||
}
|
||||
if let Some(first) = named.insert(name, i) {
|
||||
return Err(format!("`{name}` is in shard {} and shard {} ({WHERE})", first + 1, i + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let rests = groups.iter().filter(|g| **g == Group::Rest).count();
|
||||
if rests != 1 {
|
||||
return Err(format!("{rests} shards take the remaining crates, not 1 ({WHERE})"));
|
||||
}
|
||||
let pick = |f: &dyn Fn(&Member) -> bool| members.iter().filter(|m| f(m)).cloned().collect::<Vec<_>>();
|
||||
groups
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, g)| match g {
|
||||
Group::Crates(names) => Ok(Shard::Tests(pick(&|m| names.contains(&m.name.as_str())))),
|
||||
Group::Rest => {
|
||||
let rest = pick(&|m| !named.contains_key(m.name.as_str()));
|
||||
if rest.is_empty() {
|
||||
return Err(format!("shard {} takes the remaining crates, and there are none ({WHERE})", i + 1));
|
||||
}
|
||||
Ok(Shard::Tests(rest))
|
||||
}
|
||||
Group::Checks => Ok(Shard::Checks),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The features `cargo tree -e normal,dev,build --prefix none -f '{p}|{f}'` shows for each of
|
||||
/// `members`, by name; a crate the tree holds twice (once for the host, say) has two entries.
|
||||
/// Other copies of a member's name (craft-libs' git copies of geom and pathops) are not members.
|
||||
pub fn tree_features(tree: &str, members: &[Member]) -> BTreeMap<String, BTreeSet<String>> {
|
||||
let mut found: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
|
||||
for line in tree.lines() {
|
||||
let line = line.trim_end();
|
||||
let line = line.strip_suffix(" (*)").unwrap_or(line);
|
||||
let Some((package, features)) = line.split_once('|') else { continue };
|
||||
let Some(m) =
|
||||
members.iter().find(|m| package.strip_prefix(&format!("{} v{} (", m.name, m.version)).is_some_and(|source| !source.contains("://")))
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
let mut features: Vec<&str> = features.split(',').map(str::trim).filter(|f| !f.is_empty()).collect();
|
||||
features.sort_unstable();
|
||||
found.entry(m.name.clone()).or_default().insert(features.join(","));
|
||||
}
|
||||
found
|
||||
}
|
||||
|
||||
/// One line for each crate of `shard` whose features in `alone` (the shard's tree) differ from
|
||||
/// `workspace` (the whole workspace's tree).
|
||||
pub fn feature_differences(
|
||||
shard: usize,
|
||||
crates: &[Member],
|
||||
workspace: &BTreeMap<String, BTreeSet<String>>,
|
||||
alone: &BTreeMap<String, BTreeSet<String>>,
|
||||
) -> Vec<String> {
|
||||
let show = |set: Option<&BTreeSet<String>>| match set {
|
||||
None => "absent".to_owned(),
|
||||
Some(set) => set.iter().map(|f| format!("[{f}]")).collect::<Vec<_>>().join(" "),
|
||||
};
|
||||
crates
|
||||
.iter()
|
||||
.filter(|m| workspace.get(&m.name) != alone.get(&m.name))
|
||||
.map(|m| {
|
||||
format!("{}: features {} in shard {shard}, {} with the whole workspace", m.name, show(alone.get(&m.name)), show(workspace.get(&m.name)))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// `cargo tree` with the features of each package, for `select` (`--workspace` or `-p` ids).
|
||||
fn tree(select: &[String]) -> Result<String, String> {
|
||||
let mut c = crate::cargo();
|
||||
// Without `--color never`, CARGO_TERM_COLOR=always (as the gate sets it) colors the ` (*)` marks.
|
||||
c.args(["tree", "--color", "never", "-e", "normal,dev,build", "--prefix", "none", "-f", "{p}|{f}"]).args(select);
|
||||
let out = c.output().map_err(|e| format!("cargo tree: failed to spawn: {e}"))?;
|
||||
if !out.status.success() {
|
||||
return Err(format!("cargo tree {}: exited with {}:\n{}", select.join(" "), out.status, String::from_utf8_lossy(&out.stderr)));
|
||||
}
|
||||
Ok(String::from_utf8_lossy(&out.stdout).into_owned())
|
||||
}
|
||||
|
||||
/// `-p <id>` for each crate.
|
||||
pub fn select(crates: &[Member]) -> Vec<String> {
|
||||
crates.iter().flat_map(|m| ["-p".to_owned(), m.id.clone()]).collect()
|
||||
}
|
||||
|
||||
/// The package names of `crates`, for messages.
|
||||
pub fn names(crates: &[Member]) -> String {
|
||||
crates.iter().map(|m| m.name.as_str()).collect::<Vec<_>>().join(", ")
|
||||
}
|
||||
|
||||
/// The shards of this workspace, after the checks in [`assign`] and [`feature_differences`].
|
||||
pub fn check() -> Result<Vec<Shard>, String> {
|
||||
let members = members(&crate::metadata()?)?;
|
||||
let shards = assign(GROUPS, &members)?;
|
||||
let workspace = tree_features(&tree(&["--workspace".to_owned()])?, &members);
|
||||
let mut differences = Vec::new();
|
||||
for (i, shard) in shards.iter().enumerate() {
|
||||
if let Shard::Tests(crates) = shard {
|
||||
let alone = tree_features(&tree(&select(crates))?, &members);
|
||||
differences.extend(feature_differences(i + 1, crates, &workspace, &alone));
|
||||
}
|
||||
}
|
||||
if !differences.is_empty() {
|
||||
return Err(format!(
|
||||
"{} crate(s) build with other features in their shard than in `cargo test --workspace`, so some of their tests \
|
||||
would build differently or not at all (turn the feature on in the crate's own dev-dependencies, or move the crate \
|
||||
to the shard of the crate that turns it on):\n {}",
|
||||
differences.len(),
|
||||
differences.join("\n ")
|
||||
));
|
||||
}
|
||||
Ok(shards)
|
||||
}
|
||||
|
||||
/// `cargo xtask shards`: prints each shard's crates after [`check`], and returns the shards.
|
||||
pub fn run() -> Result<Vec<Shard>, String> {
|
||||
let shards = check()?;
|
||||
for (i, shard) in shards.iter().enumerate() {
|
||||
match shard {
|
||||
Shard::Tests(crates) => println!("shard {}/{}: tests of {} crate(s): {}", i + 1, shards.len(), crates.len(), names(crates)),
|
||||
Shard::Checks => println!("shard {}/{}: the other ci steps", i + 1, shards.len()),
|
||||
}
|
||||
}
|
||||
println!("OK: every workspace member is tested on one shard, with the features `cargo test --workspace` gives it.");
|
||||
Ok(shards)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn m(name: &str) -> Member {
|
||||
Member { name: name.into(), id: format!("path+file:///w/{name}#0.1.0"), version: "0.1.0".into() }
|
||||
}
|
||||
|
||||
fn crates(shard: &Shard) -> Vec<&str> {
|
||||
match shard {
|
||||
Shard::Tests(c) => c.iter().map(|m| m.name.as_str()).collect(),
|
||||
Shard::Checks => Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_rest_shard_takes_every_crate_no_group_names() {
|
||||
let ws = [m("a"), m("b"), m("c"), m("d")];
|
||||
let shards = assign(&[Group::Crates(&["c"]), Group::Rest, Group::Checks], &ws).unwrap();
|
||||
assert_eq!(shards.iter().map(crates).collect::<Vec<_>>(), [vec!["c"], vec!["a", "b", "d"], vec![]]);
|
||||
assert_eq!(shards[2], Shard::Checks);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_crate_named_twice_or_unknown_fails() {
|
||||
let ws = [m("a"), m("b")];
|
||||
let twice = assign(&[Group::Crates(&["a"]), Group::Crates(&["a"]), Group::Rest], &ws).unwrap_err();
|
||||
assert!(twice.contains("`a` is in shard 1 and shard 2"), "{twice}");
|
||||
let unknown = assign(&[Group::Crates(&["z"]), Group::Rest], &ws).unwrap_err();
|
||||
assert!(unknown.contains("`z`, which is not a workspace member"), "{unknown}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_groups_and_a_missing_or_second_rest_fail() {
|
||||
let ws = [m("a"), m("b")];
|
||||
assert!(assign(&[Group::Crates(&[]), Group::Rest], &ws).unwrap_err().contains("names no crates"));
|
||||
assert!(assign(&[Group::Crates(&["a", "b"]), Group::Rest], &ws).unwrap_err().contains("there are none"));
|
||||
assert!(assign(&[Group::Crates(&["a"])], &ws).unwrap_err().contains("0 shards take the remaining crates"));
|
||||
assert!(assign(&[Group::Rest, Group::Rest], &ws).unwrap_err().contains("2 shards take the remaining crates"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_workspace_member_is_tested_on_exactly_one_shard() {
|
||||
let ws = members(&crate::metadata().unwrap()).unwrap();
|
||||
let shards = assign(GROUPS, &ws).unwrap();
|
||||
let mut tested: Vec<&str> = shards.iter().flat_map(crates).collect();
|
||||
tested.sort_unstable();
|
||||
let mut all: Vec<&str> = ws.iter().map(|m| m.name.as_str()).collect();
|
||||
all.sort_unstable();
|
||||
assert_eq!(tested, all);
|
||||
assert_eq!(shards.iter().filter(|s| **s == Shard::Checks).count(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_feature_only_another_shard_turns_on_is_a_difference() {
|
||||
let ws = [m("lib"), m("geom")];
|
||||
let workspace = "\
|
||||
lib v0.1.0 (/w/lib)|default,synth
|
||||
geom v0.1.0 (/w/geom)|
|
||||
geom v0.18.0 (https://github.com/x/y?rev=1#1)|old
|
||||
lib v0.1.0 (/w/lib)|default,synth (*)
|
||||
";
|
||||
let alone = "\
|
||||
lib v0.1.0 (/w/lib)|default
|
||||
geom v0.1.0 (/w/geom)|
|
||||
";
|
||||
let workspace = tree_features(workspace, &ws);
|
||||
assert_eq!(workspace["lib"], BTreeSet::from(["default,synth".to_owned()]));
|
||||
assert_eq!(workspace["geom"], BTreeSet::from([String::new()]));
|
||||
let alone = tree_features(alone, &ws);
|
||||
assert_eq!(feature_differences(4, &ws, &workspace, &alone), ["lib: features [default] in shard 4, [default,synth] with the whole workspace"]);
|
||||
assert!(feature_differences(4, &ws, &workspace, &workspace).is_empty());
|
||||
assert_eq!(
|
||||
feature_differences(4, &[m("lib")], &workspace, &BTreeMap::new()),
|
||||
["lib: features absent in shard 4, [default,synth] with the whole workspace"]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user