diff --git a/lychee-bin/src/commands/check.rs b/lychee-bin/src/commands/check.rs index 1defc0a..76186f4 100644 --- a/lychee-bin/src/commands/check.rs +++ b/lychee-bin/src/commands/check.rs @@ -70,7 +70,7 @@ where let verbose = cfg.verbose; async move { while let Some(response) = recv_resp.recv().await { - show_progress(&pb, &response, verbose)?; + show_progress(&mut io::stdout(), &pb, &response, verbose)?; stats.add(response); } Ok((pb, stats)) @@ -143,6 +143,7 @@ async fn handle(client: &Client, cache: Arc, request: Request) -> Respons } fn show_progress( + output: &mut dyn Write, progress_bar: &Option, response: &Response, verbose: bool, @@ -155,10 +156,36 @@ fn show_progress( pb.println(out); } } else { - if (response.status().is_success() || response.status().is_excluded()) && !verbose { - return Ok(()); + #[allow(clippy::collapsible_if)] + if !verbose { + if response.status().is_success() || response.status().is_excluded() { + return Ok(()); + } } - writeln!(io::stdout(), "{out}")?; + writeln!(output, "{out}")?; } Ok(()) } + +#[cfg(test)] +mod tests { + use lychee_lib::{CacheStatus, InputSource, ResponseBody, Uri}; + + use super::*; + + #[test] + fn test_skip_cached_responses_in_progress_output() { + let mut buf = Vec::new(); + let response = Response( + InputSource::Stdin, + ResponseBody { + uri: Uri::try_from("http://127.0.0.1").unwrap(), + status: Status::Cached(CacheStatus::Ok(200)), + }, + ); + show_progress(&mut buf, &None, &response, false).unwrap(); + + println!("{:?}", String::from_utf8_lossy(&buf)); + assert!(buf.is_empty()); + } +} diff --git a/lychee-lib/src/types/status.rs b/lychee-lib/src/types/status.rs index dc9feb3..c09219d 100644 --- a/lychee-lib/src/types/status.rs +++ b/lychee-lib/src/types/status.rs @@ -88,21 +88,27 @@ impl Status { #[must_use] /// Returns `true` if the check was successful pub const fn is_success(&self) -> bool { - matches!(self, Status::Ok(_)) + matches!(self, Status::Ok(_) | Status::Cached(CacheStatus::Ok(_))) } #[inline] #[must_use] /// Returns `true` if the check was not successful pub const fn is_failure(&self) -> bool { - matches!(self, Status::Error(_)) + matches!( + self, + Status::Error(_) | Status::Cached(CacheStatus::Error(_)) + ) } #[inline] #[must_use] /// Returns `true` if the check was excluded pub const fn is_excluded(&self) -> bool { - matches!(self, Status::Excluded) + matches!( + self, + Status::Excluded | Status::Cached(CacheStatus::Excluded) + ) } #[inline] @@ -116,7 +122,10 @@ impl Status { #[must_use] /// Returns `true` if a URI is unsupported pub const fn is_unsupported(&self) -> bool { - matches!(self, Status::Unsupported(_)) + matches!( + self, + Status::Unsupported(_) | Status::Cached(CacheStatus::Unsupported) + ) } #[must_use]