1 use crate::file_handler::{File, FileHandler};
3 use std::path::PathBuf;
5 pub fn find_files(directory_path: &PathBuf) -> Vec<File> {
6 return find_files_recursively(directory_path, directory_path);
9 fn find_files_recursively(root_path: &PathBuf, directory_path: &PathBuf) -> Vec<File> {
10 let mut result: Vec<File> = vec![];
11 let file_handler = FileHandler::default();
12 let entries = read_dir(&directory_path).unwrap();
13 for entry in entries {
14 let path = entry.unwrap().path();
15 let relative_path = path.strip_prefix(&root_path).unwrap();
16 if relative_path.starts_with(".git") || relative_path.starts_with(".gitignore") {
20 result.append(&mut find_files_recursively(&root_path, &path))
22 let file_type = file_handler.identify(&path);
36 use crate::file_handler::FileType;
37 use std::collections::HashSet;
38 use std::fs::create_dir_all;
40 fn fixtures_dir() -> PathBuf {
41 PathBuf::from("tests/fixtures")
44 fn get_paths(files: &Vec<File>) -> HashSet<String> {
47 .map(|f| f.path.strip_prefix(&fixtures_dir()).unwrap().to_string_lossy().to_string())
52 fn finds_all_files() {
53 let files = find_files(&fixtures_dir());
54 let paths = get_paths(&files);
56 assert!(paths.contains("test1.gmi"));
57 assert!(paths.contains("_layout.html"));
58 assert!(paths.contains("nested/nested.gmi"));
59 assert!(paths.contains("assets/style.css"));
60 assert!(paths.contains("image.png"));
64 fn identifies_correct_file_types() {
65 let files = find_files(&fixtures_dir());
68 let extension = file.path.extension().and_then(|e| e.to_str());
70 Some("gmi") => assert_eq!(file.file_type, FileType::Gemini),
72 if file.path.ends_with("_layout.html") {
73 assert_eq!(file.file_type, FileType::Layout)
75 assert_eq!(file.file_type, FileType::File)
78 _ => assert_eq!(file.file_type, FileType::File),
84 fn ignores_git_directory() {
85 let files = find_files(&fixtures_dir());
86 let paths = get_paths(&files);
88 // These files should exist in the fixtures but not be included
89 assert!(!paths.iter().any(|p| p.starts_with(".git/")));
90 assert!(!paths.contains(".gitignore"));
94 fn handles_nested_directories() {
95 let files = find_files(&fixtures_dir());
96 let paths = get_paths(&files);
98 // Check that files in nested directories are found
99 assert!(paths.contains("nested/nested.gmi"));
100 assert!(paths.contains("assets/style.css"));
102 // Verify directory structure is preserved in paths
103 let nested_files: Vec<_> = files.iter()
104 .filter(|f| f.path.starts_with(fixtures_dir().join("nested")))
106 assert!(!nested_files.is_empty());
110 fn returns_empty_for_empty_directory() {
111 let empty_dir = fixtures_dir().join("empty");
112 let _ = create_dir_all(&empty_dir);
113 let files = find_files(&empty_dir);
114 assert!(files.is_empty());