use crate::config::IGNORE_FILE_PATH;
use std::fs;
use std::path::Path;
pub fn read_ignore_file() -> std::io::Result<Vec<String>> {
let mutex = IGNORE_FILE_PATH.get_or_init(|| std::sync::Mutex::new(std::path::PathBuf::new()));
let guard = mutex
.lock()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
let content = fs::read_to_string(&*guard)?;
let lines: Vec<String> = content.lines().map(|l| l.to_string()).collect();
Ok(lines)
}
pub fn read_file(file_path: &Path) -> Vec<String> {
fs::read_to_string(file_path)
.unwrap_or_default()
.split_whitespace()
.map(|w| w.to_string())
.collect()
}
pub fn write_file(file_path: &Path, content: &str) -> std::io::Result<()> {
fs::write(file_path, content)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::tempdir;
#[test]
fn test_read_file_returns_words() {
let dir = tempdir().unwrap();
let file = dir.path().join("test.txt");
fs::write(&file, "hello world foo bar").unwrap();
let words = read_file(&file);
assert_eq!(words, vec!["hello", "world", "foo", "bar"]);
}
#[test]
fn test_read_file_missing_returns_empty() {
let result = read_file(std::path::Path::new("/nonexistent/file.txt"));
assert!(result.is_empty());
}
#[test]
fn test_write_file_creates_file() {
let dir = tempdir().unwrap();
let file = dir.path().join("output.txt");
write_file(&file, "hello").unwrap();
assert_eq!(fs::read_to_string(&file).unwrap(), "hello");
}
#[test]
fn test_write_file_overwrites() {
let dir = tempdir().unwrap();
let file = dir.path().join("output.txt");
write_file(&file, "first").unwrap();
write_file(&file, "second").unwrap();
assert_eq!(fs::read_to_string(&file).unwrap(), "second");
}
}