mod common;
use common::*;
use std::fs;
#[test]
fn test_logging_json_format() -> Result<(), Box<dyn std::error::Error>> {
let dir = test_dir();
let test_data = b"json logging test data";
let input = create_test_file(dir.path(), "test.txt", test_data);
let log_file = dir.path().join("test.log");
let _assert = crush_cmd()
.arg("--log-format")
.arg("json")
.arg("--log-file")
.arg(&log_file)
.arg("-v")
.arg("compress")
.arg(&input)
.assert()
.success();
assert!(log_file.exists(), "Log file should be created");
let log_contents = fs::read_to_string(&log_file)?;
assert!(log_contents.contains("{"), "Should contain JSON objects");
assert!(
log_contents.contains("\"timestamp\"") || log_contents.contains("\"time\""),
"Should have timestamp field"
);
assert!(
log_contents.contains("\"level\"") || log_contents.contains("\"severity\""),
"Should have level field"
);
assert!(
log_contents.contains("\"message\"") || log_contents.contains("\"msg\""),
"Should have message field"
);
Ok(())
}
#[test]
fn test_logging_error_context() -> Result<(), Box<dyn std::error::Error>> {
let dir = test_dir();
let log_file = dir.path().join("error.log");
let assert = crush_cmd()
.arg("--log-file")
.arg(&log_file)
.arg("-v")
.arg("compress")
.arg("nonexistent_file_that_does_not_exist.txt")
.assert()
.failure();
let stderr = String::from_utf8(assert.get_output().stderr.clone())?;
assert!(
stderr.contains("not found") || stderr.contains("does not exist"),
"Should show error message"
);
if log_file.exists() {
let log_contents = fs::read_to_string(&log_file)?;
assert!(
log_contents.contains("nonexistent") || log_contents.contains("Starting"),
"Log should contain context about the operation"
);
}
Ok(())
}
#[test]
fn test_logging_file_creation() -> Result<(), Box<dyn std::error::Error>> {
let dir = test_dir();
let test_data = b"log file test data";
let input = create_test_file(dir.path(), "test.txt", test_data);
let log_file = dir.path().join("compression.log");
assert!(!log_file.exists(), "Log file should not exist before test");
crush_cmd()
.arg("--log-file")
.arg(&log_file)
.arg("-v")
.arg("compress")
.arg(&input)
.assert()
.success();
assert!(log_file.exists(), "Log file should be created");
let log_contents = fs::read_to_string(&log_file)?;
assert!(!log_contents.is_empty(), "Log file should not be empty");
assert!(
log_contents.contains("compress")
|| log_contents.contains("Starting")
|| log_contents.contains("Compressed"),
"Log should contain compression operation info"
);
Ok(())
}