use assert_cmd::cargo::cargo_bin_cmd;
use predicates::prelude::*;
use tempfile::TempDir;
#[test]
fn test_check_large_file_with_issues() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("large.txt");
let mut content = String::new();
for i in 0..500_000 {
if i % 1000 == 0 {
content.push_str(&format!("Line {i} with trailing spaces \n"));
} else {
content.push_str(&format!("Line {i}\n"));
}
}
content.pop();
std::fs::write(&file_path, content).unwrap();
let mut cmd = cargo_bin_cmd!("lineguard");
cmd.current_dir(&temp_dir);
cmd.arg("large.txt");
cmd.assert()
.code(1) .stdout(predicate::str::contains("Trailing spaces"))
.stdout(predicate::str::contains("Missing newline"));
}
#[test]
fn test_fix_large_file() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("large.txt");
let mut content = String::new();
for i in 0..250_000 {
content.push_str(&format!("Line {i} with trailing spaces \n"));
}
std::fs::write(&file_path, content).unwrap();
let mut cmd = cargo_bin_cmd!("lineguard");
cmd.current_dir(&temp_dir);
cmd.arg("large.txt");
cmd.arg("--fix");
cmd.assert().success();
let fixed_content = std::fs::read_to_string(&file_path).unwrap();
assert!(!fixed_content.contains(" \n"));
assert!(fixed_content.ends_with('\n'));
}
#[test]
fn test_memory_efficient_processing() {
let temp_dir = TempDir::new().unwrap();
let file_path = temp_dir.path().join("huge.txt");
let line = "This is a normal line without issues\n";
let mut content = String::with_capacity(50 * 1024 * 1024);
for _ in 0..(50 * 1024 * 1024 / line.len()) {
content.push_str(line);
}
std::fs::write(&file_path, content).unwrap();
let mut cmd = cargo_bin_cmd!("lineguard");
cmd.current_dir(&temp_dir);
cmd.arg("huge.txt");
cmd.assert().success();
}