tests: Addressed additional CR comments and added tests for tail-logs

This commit is contained in:
2026-06-25 14:21:57 -06:00
parent cbca6bd916
commit 6df68b8a66
2 changed files with 59 additions and 5 deletions
+4 -4
View File
@@ -104,7 +104,7 @@ pub async fn tail_logs(no_color: bool) -> Result<()> {
.seek(SeekFrom::End(0))
.with_context(|| "Unable to tail log file")?;
tokio::spawn(async move {
tokio::task::spawn_blocking(move || {
let mut line_buf = String::new();
loop {
line_buf.clear();
@@ -114,7 +114,7 @@ pub async fn tail_logs(no_color: bool) -> Result<()> {
continue;
}
tokio::time::sleep(Duration::from_millis(100)).await;
std::thread::sleep(Duration::from_millis(100));
}
Ok(_) => {
let line = line_buf.trim_end();
@@ -126,7 +126,7 @@ pub async fn tail_logs(no_color: bool) -> Result<()> {
}
}
Err(_) => {
tokio::time::sleep(Duration::from_millis(100)).await;
std::thread::sleep(Duration::from_millis(100));
}
}
}
@@ -134,7 +134,7 @@ pub async fn tail_logs(no_color: bool) -> Result<()> {
.await?
}
fn was_log_rotated(file_path: &PathBuf, reader: &mut BufReader<File>) -> bool {
pub(crate) fn was_log_rotated(file_path: &PathBuf, reader: &mut BufReader<File>) -> bool {
let current_pos = reader.stream_position().unwrap_or(0);
let file_len = fs::metadata(file_path).map(|m| m.len()).unwrap_or(0);
+55 -1
View File
@@ -1,8 +1,11 @@
#[cfg(test)]
mod tests {
use std::fs::{self, File};
use std::io::{BufRead, BufReader, Seek, SeekFrom, Write};
use pretty_assertions::assert_eq;
use crate::utils::{convert_f64_to_gb, convert_runtime, convert_to_gb};
use crate::utils::{convert_f64_to_gb, convert_runtime, convert_to_gb, was_log_rotated};
#[test]
fn test_convert_to_gb() {
@@ -23,4 +26,55 @@ mod tests {
assert_eq!(hours, 2);
assert_eq!(minutes, 34);
}
#[test]
fn test_was_log_rotated_returns_false_when_file_has_not_rotated() {
let path = std::env::temp_dir().join("managarr_test_no_rotation.log");
fs::write(&path, "line one\nline two\n").unwrap();
let file = File::open(&path).unwrap();
let mut reader = BufReader::new(file);
reader.seek(SeekFrom::End(0)).unwrap();
assert!(!was_log_rotated(&path, &mut reader));
fs::remove_file(&path).unwrap();
}
#[test]
fn test_was_log_rotated_returns_true_and_reopens_reader_after_rotation() {
let path = std::env::temp_dir().join("managarr_test_rotation.log");
fs::write(&path, "original content that is long enough\n").unwrap();
let file = File::open(&path).unwrap();
let mut reader = BufReader::new(file);
reader.seek(SeekFrom::End(0)).unwrap();
fs::write(&path, "new\n").unwrap();
assert!(was_log_rotated(&path, &mut reader));
let mut line = String::new();
reader.read_line(&mut line).unwrap();
assert_eq!(line, "new\n");
fs::remove_file(&path).unwrap();
}
#[test]
fn test_was_log_rotated_returns_false_when_file_grows() {
let path = std::env::temp_dir().join("managarr_test_growing.log");
fs::write(&path, "initial\n").unwrap();
let file = File::open(&path).unwrap();
let mut reader = BufReader::new(file);
reader.seek(SeekFrom::End(0)).unwrap();
let mut appender = fs::OpenOptions::new().append(true).open(&path).unwrap();
appender.write_all(b"more data\n").unwrap();
assert!(!was_log_rotated(&path, &mut reader));
fs::remove_file(&path).unwrap();
}
}