From 2a11a280f640db2441fa5ce74d939eccf426686f Mon Sep 17 00:00:00 2001 From: Mex <44814932+MMexicann@users.noreply.github.com> Date: Sun, 15 Nov 2026 16:39:38 +0100 Subject: [PATCH] cli: report an empty benchmark layout instead of panicking (#2839) * cli: report an empty benchmark layout instead of panicking Return a clear error before benchmark work starts when the document contains no spreads. * test: report a benchmark error for documents with no spreads Create a structurally valid round-tripped document with no spreads and require bench to exit 1 with the clear error. --- apps/designcraft-cli/src/perf.rs | 3 +++ apps/designcraft-cli/tests/it/cli.rs | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/apps/designcraft-cli/src/perf.rs b/apps/designcraft-cli/src/perf.rs index 3a3afe931..d67fffa4b 100644 --- a/apps/designcraft-cli/src/perf.rs +++ b/apps/designcraft-cli/src/perf.rs @@ -97,6 +97,9 @@ fn row(rows: &mut Vec, name: impl Into, ms: f64, budget: Option Result, String> { + if doc.spreads.is_empty() { + return Err("cannot benchmark a document with no spreads".into()); + } let mut rows = vec![]; let cores = std::thread::available_parallelism().map_or(1, std::num::NonZeroUsize::get); let chars: usize = doc.stories.values().map(|s| s.text.len()).sum(); diff --git a/apps/designcraft-cli/tests/it/cli.rs b/apps/designcraft-cli/tests/it/cli.rs index 7fe1d9cea..f12732578 100644 --- a/apps/designcraft-cli/tests/it/cli.rs +++ b/apps/designcraft-cli/tests/it/cli.rs @@ -7,6 +7,31 @@ fn cli() -> Command { Command::new(env!("CARGO_BIN_EXE_designcraft-cli")) } +#[test] +fn bench_without_spreads_reports_an_error_instead_of_panicking() { + struct Fixture(std::path::PathBuf); + impl Drop for Fixture { + fn drop(&mut self) { + let _ = std::fs::remove_file(&self.0); + } + } + let path = std::env::temp_dir().join(format!("dc-cli-empty-bench-{}.designcraft", std::process::id())); + assert!(std::fs::symlink_metadata(&path).is_err_and(|error| error.kind() == std::io::ErrorKind::NotFound)); + let fixture = Fixture(path); + let mut document = designcraft_doc::Document::new(&Default::default()); + document.spreads.clear(); + document.check().unwrap(); + let bytes = designcraft_format::save(&document).unwrap(); + assert!(designcraft_format::load(&bytes).unwrap().spreads.is_empty()); + std::fs::write(&fixture.0, bytes).unwrap(); + + let out = cli().args(["bench", fixture.0.to_str().unwrap(), "--runs", "1"]).output().unwrap(); + + let err = String::from_utf8_lossy(&out.stderr); + assert_eq!(out.status.code(), Some(1), "{err}"); + assert!(err.contains("cannot benchmark a document with no spreads"), "{err}"); +} + #[test] fn script_from_stdin_with_references_saves_and_exports() { let dir = std::env::temp_dir().join(format!("dc-cli-{}", std::process::id()));