diff --git a/src/cmds/system/log_cmd.rs b/src/cmds/system/log_cmd.rs index cd2ca717e0..6270ff563c 100644 --- a/src/cmds/system/log_cmd.rs +++ b/src/cmds/system/log_cmd.rs @@ -1,6 +1,6 @@ //! Deduplicates repeated log lines and shows counts instead. -use crate::core::guard::never_worse; +use crate::core::runner::emit_guarded; use crate::core::tracking; use crate::core::truncate::{CAP_WARNINGS, reduced}; use crate::core::utils::truncate; @@ -8,7 +8,7 @@ use anyhow::Result; use regex::Regex; use std::collections::HashMap; use std::fs; -use std::io::{self, BufRead}; +use std::io; use std::path::Path; use std::sync::LazyLock; @@ -32,10 +32,8 @@ pub fn run_file(file: &Path, verbose: u8) -> Result<()> { } let content = fs::read_to_string(file)?; - let result = analyze_logs(&content); - let shown = never_worse(&content, &result); - println!("{}", shown); - timer.track(&super::read::cat_label(file), "rtk log", &content, shown); + let shown = emit_guarded(&analyze_logs(&content), None, &content); + timer.track(&super::read::cat_label(file), "rtk log", &content, &shown); Ok(()) } @@ -43,18 +41,9 @@ pub fn run_file(file: &Path, verbose: u8) -> Result<()> { pub fn run_stdin(_verbose: u8) -> Result<()> { let timer = tracking::TimedExecution::start(); - let mut content = String::new(); - let stdin = io::stdin(); - for line in stdin.lock().lines() { - content.push_str(&line?); - content.push('\n'); - } - - let result = analyze_logs(&content); - let shown = never_worse(&content, &result); - println!("{}", shown); - - timer.track("log (stdin)", "rtk log (stdin)", &content, shown); + let content = io::read_to_string(io::stdin().lock())?; + let shown = emit_guarded(&analyze_logs(&content), None, &content); + timer.track("log (stdin)", "rtk log (stdin)", &content, &shown); Ok(()) } diff --git a/tests/guard_integration_test.rs b/tests/guard_integration_test.rs index 004d83dda7..0752385bb7 100644 --- a/tests/guard_integration_test.rs +++ b/tests/guard_integration_test.rs @@ -21,6 +21,11 @@ fn rtk_stdin(args: &[&str], input: &str) -> String { .write_all(input.as_bytes()) .expect("write stdin"); let out = child.wait_with_output().expect("wait rtk"); + assert!( + out.status.success(), + "rtk {args:?} exited with {}", + out.status + ); String::from_utf8_lossy(&out.stdout).into_owned() } @@ -78,6 +83,32 @@ fn rtk_in_dir(dir: &std::path::Path, args: &[&str]) -> (String, Option) { (stdout, code) } +#[test] +fn log_file_and_stdin_emit_the_same_line_endings() { + let dir = tempfile::tempdir().expect("tempdir"); + let repeated = "INFO ready\n".repeat(80); + let summary = concat!( + "Log Summary\n", + " [error] 0 errors (0 unique)\n", + " [warn] 0 warnings (0 unique)\n", + " [info] 80 info messages\n", + ); + for (input, expected) in [ + ("one\r\ntwo\r\n", "one\r\ntwo\r\n"), + ("", ""), + ("ok\n", "ok\n"), + ("ok", "ok\n"), + ("ok\n\n", "ok\n\n"), + (repeated.as_str(), summary), + ] { + std::fs::write(dir.path().join("sample.log"), input).expect("write log"); + let (file_output, code) = rtk_in_dir(dir.path(), &["log", "sample.log"]); + assert_eq!(code, Some(0)); + assert_eq!(rtk_stdin(&["log"], input), expected, "stdin: {input:?}"); + assert_eq!(file_output, expected, "file: {input:?}"); + } +} + fn rg_available() -> bool { Command::new("rg") .arg("--version")