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.
This commit is contained in:
Mex authored and GitHub committed 2026-11-15 15:39:38 +00:00
1 parent ca463bab52
commit 2a11a280f6
2 files changed
+28

No files matched your search

+3
View File
@@ -97,6 +97,9 @@ fn row(rows: &mut Vec<Row>, name: impl Into<String>, ms: f64, budget: Option<f64
/// Measure `doc`. `runs` = repetitions per row (median).
fn measure(doc: &Document, runs: usize) -> Result<Vec<Row>, 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();
+25
View File
@@ -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()));