From 2694187ff09b5926b4cd1e1a036cdec155d4cd6a Mon Sep 17 00:00:00 2001 From: nbabcock Date: Sun, 24 Nov 2024 17:40:49 -0600 Subject: [PATCH] test: cover empty events from processing newlines --- src/read_until_any.rs | 14 +++++++++++--- src/test.rs | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/read_until_any.rs b/src/read_until_any.rs index d9fbddb..d186883 100644 --- a/src/read_until_any.rs +++ b/src/read_until_any.rs @@ -36,8 +36,6 @@ pub fn read_until_any( .position(|&b| delims.iter().any(|&d| d == b)) .map(|i| i + start_delims); - // println!("start_delims: {start_delims}, first_delim_index: {first_delim_index:?}"); - match first_delim_index { Some(i) => { buf.extend_from_slice(&available[..=i]); @@ -51,7 +49,17 @@ pub fn read_until_any( }; r.consume(used); read += used; - if done || used == 0 { + + if done { + return Ok(read); + } + + // Catch final trailing delimiters + if used == 0 && buf.iter().all(|&b| delims.iter().any(|&d| d == b)) { + return Ok(0); + } + + if used == 0 { return Ok(read); } } diff --git a/src/test.rs b/src/test.rs index 2e95ecf..fa5d201 100644 --- a/src/test.rs +++ b/src/test.rs @@ -676,3 +676,22 @@ fn test_stdout_interleaved_frames_fallback() -> anyhow::Result<()> { Ok(()) } + +/// Make sure consectuive new lines in logs don't result in empty events. +#[test] +fn test_no_empty_events() -> anyhow::Result<()> { + let empty_events = FfmpegCommand::new() + .testsrc() + .rawvideo() + .spawn()? + .iter()? + .filter(|event| match event { + FfmpegEvent::Log(_, msg) if msg.is_empty() => true, + _ => false, + }) + .count(); + + assert!(empty_events == 0); + + Ok(()) +}