use assert_cmd::Command;
use predicates::prelude::*;
use std::io::Write;
use tempfile::NamedTempFile;
#[test]
fn test_cli_json_output() {
let mut temp_file = NamedTempFile::new().unwrap();
writeln!(
temp_file,
"2025-03-21 14:00:00,123 [ERROR] NullPointerException"
)
.unwrap();
writeln!(temp_file, "2025-03-21 14:01:00,456 [WARN] Some warning").unwrap();
let file_path = temp_file.path().to_str().unwrap();
let mut cmd = Command::cargo_bin("timber").unwrap();
let assert = cmd.arg("--json").arg("--stats").arg(file_path).assert();
assert
.success()
.stdout(predicates::str::contains("{"))
.stdout(predicates::str::contains("}"))
.stdout(predicates::str::contains("\"matched_lines\""))
.stdout(predicates::str::contains("\"count\""))
.stdout(predicates::str::contains("\"stats\""));
}
#[test]
fn test_cli_top_errors_option() {
let mut temp_file = NamedTempFile::new().unwrap();
writeln!(
temp_file,
"2025-03-21 14:00:00,123 [ERROR] NullPointerException"
)
.unwrap();
writeln!(
temp_file,
"2025-03-21 14:01:00,456 [ERROR] Connection timeout"
)
.unwrap();
writeln!(
temp_file,
"2025-03-21 14:02:00,789 [ERROR] OutOfMemoryError"
)
.unwrap();
writeln!(
temp_file,
"2025-03-21 14:03:00,123 [ERROR] FileNotFoundException"
)
.unwrap();
let file_path = temp_file.path().to_str().unwrap();
let mut cmd = Command::cargo_bin("timber").unwrap();
let assert = cmd
.arg("--stats")
.arg("--top-errors")
.arg("2")
.arg(file_path)
.assert();
assert
.success()
.stdout(predicates::str::contains("Top error types:"))
.stdout(predicates::str::contains("1."))
.stdout(predicates::str::contains("2."))
.stdout(predicates::str::contains("3.").not()); }
#[test]
fn test_cli_show_unique_option() {
let mut temp_file = NamedTempFile::new().unwrap();
writeln!(
temp_file,
"2025-03-21 14:00:00,123 [ERROR] NullPointerException"
)
.unwrap();
writeln!(temp_file, "2025-03-21 14:01:00,456 [WARN] Some warning").unwrap();
let file_path = temp_file.path().to_str().unwrap();
let mut cmd = Command::cargo_bin("timber").unwrap();
let assert = cmd
.arg("--stats")
.arg("--show-unique")
.arg(file_path)
.assert();
assert
.success()
.stdout(predicates::str::contains("Unique messages:"))
.stdout(predicates::str::contains("NullPointerException")) .stdout(predicates::str::contains("Some warning")); }
#[test]
fn test_cli_json_with_new_options() {
let mut temp_file = NamedTempFile::new().unwrap();
writeln!(
temp_file,
"2025-03-21 14:00:00,123 [ERROR] NullPointerException"
)
.unwrap();
writeln!(temp_file, "2025-03-21 14:01:00,456 [WARN] Some warning").unwrap();
writeln!(
temp_file,
"2025-03-21 14:02:00,789 [ERROR] Connection timeout"
)
.unwrap();
let file_path = temp_file.path().to_str().unwrap();
let mut cmd = Command::cargo_bin("timber").unwrap();
let assert = cmd
.arg("--json")
.arg("--stats")
.arg("--show-unique")
.arg("--top-errors")
.arg("1")
.arg(file_path)
.assert();
assert
.success()
.stdout(predicates::str::contains("\"unique_messages\""))
.stdout(predicates::str::contains("\"error_types\""));
}