diff --git a/src/main.rs b/src/main.rs index 12ab1d60..3f6f0fab 100644 --- a/src/main.rs +++ b/src/main.rs @@ -565,6 +565,10 @@ fn write_flamegraph( output: Option, options: &mut inferno::flamegraph::Options<'_>, ) -> Result { + // Zero-sized entries are omitted from folded stacks, and inferno writes an error SVG for empty input. + if stacks.is_empty() { + bail!("No disk usage to render as a flame graph"); + } let stacks = String::from_utf8(stacks).expect("folded stacks are valid UTF-8"); if let Some(path) = output { let file = fs::File::create(&path) @@ -1161,6 +1165,24 @@ mod tests { ); } + #[test] + fn empty_flamegraphs_are_rejected_with_and_without_explicit_output() { + let dir = tempfile::tempdir().expect("temporary directory"); + for output in [None, Some(dir.path().join("usage.svg"))] { + let error = write_flamegraph( + Vec::new(), + output, + &mut inferno::flamegraph::Options::default(), + ) + .expect_err("empty stacks cannot be rendered"); + assert_eq!( + error.to_string(), + "No disk usage to render as a flame graph" + ); + } + assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 0); + } + #[test] fn writes_explicit_and_temporary_flamegraphs() { let dir = tempfile::tempdir().expect("temporary directory");