use crate::infrastructure::file_repository; use std::path::Path; pub fn list_markdown_files(dir: &str) -> Result, String> { let path = Path::new(dir); if !path.is_dir() { return Err(format!("{dir} n'est pas un dossier")); } let mut files = Vec::new(); file_repository::collect_md_files(path, &mut files).map_err(|e| e.to_string())?; files.sort(); Ok(files) } #[cfg(test)] mod tests { use super::*; use std::fs; fn tmpdir(name: &str) -> std::path::PathBuf { std::env::temp_dir().join(name) } #[test] fn list_markdown_files_sorted() { let dir = tmpdir("pena_fs_sorted"); fs::create_dir_all(&dir).unwrap(); fs::write(dir.join("z.md"), "").unwrap(); fs::write(dir.join("a.md"), "").unwrap(); fs::write(dir.join("m.md"), "").unwrap(); let result = list_markdown_files(dir.to_str().unwrap()).unwrap(); fs::remove_dir_all(&dir).unwrap(); assert_eq!(result.len(), 3); assert!(result[0] < result[1] && result[1] < result[2]); assert!(result[0].ends_with("a.md")); assert!(result[2].ends_with("z.md")); } #[test] fn list_markdown_files_ignores_non_md() { let dir = tmpdir("pena_fs_non_md"); fs::create_dir_all(&dir).unwrap(); fs::write(dir.join("doc.md"), "").unwrap(); fs::write(dir.join("notes.txt"), "").unwrap(); let result = list_markdown_files(dir.to_str().unwrap()).unwrap(); fs::remove_dir_all(&dir).unwrap(); assert_eq!(result.len(), 1); assert!(result[0].ends_with("doc.md")); } #[test] fn list_markdown_files_not_a_dir() { let result = list_markdown_files("/nonexistent/pena_fs_dir"); assert!(result.is_err()); } #[test] fn list_markdown_files_empty_dir() { let dir = tmpdir("pena_fs_empty"); fs::create_dir_all(&dir).unwrap(); let result = list_markdown_files(dir.to_str().unwrap()).unwrap(); fs::remove_dir_all(&dir).unwrap(); assert_eq!(result.len(), 0); } }