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);
35 use std::collections::HashSet;
36 use std::path::PathBuf;
37 use std::fs::create_dir_all;
41 use crate::file_handler::FileType;
42 use crate::file_handler::File;
43 use test_utilities::*;
45 fn get_paths(root_directory: &PathBuf, files: &Vec<File>) -> HashSet<String> {
50 .strip_prefix(root_directory)
59 fn finds_all_files() {
60 let test_dir = setup_test_dir();
61 create_dir_all(&test_dir.join("nested"))
62 .expect("Could not create nested test directory");
63 create_dir_all(&test_dir.join("assets"))
64 .expect("Could not create assets test directory");
65 create_test_file(&test_dir.join("test1.gmi"), "");
66 create_test_file(&test_dir.join("_layout.html"), "");
67 create_test_file(&test_dir.join("nested/nested.gmi"), "");
68 create_test_file(&test_dir.join("assets/style.css"), "");
69 create_test_file(&test_dir.join("image.png"), "");
71 let files = find_files(&test_dir);
72 let paths = get_paths(&test_dir, &files);
74 assert!(paths.contains("test1.gmi"));
75 assert!(paths.contains("_layout.html"));
76 assert!(paths.contains("nested/nested.gmi"));
77 assert!(paths.contains("assets/style.css"));
78 assert!(paths.contains("image.png"));
82 fn identifies_correct_file_types() {
83 let test_dir = setup_test_dir();
84 create_dir_all(&test_dir.join("nested"))
85 .expect("Could not create nested test directory");
86 create_dir_all(&test_dir.join("assets"))
87 .expect("Could not create assets test directory");
88 create_test_file(&test_dir.join("_layout.html"), "");
89 create_test_file(&test_dir.join("nested/nested.gmi"), "");
90 create_test_file(&test_dir.join("assets/style.css"), "");
91 create_test_file(&test_dir.join("image.png"), "");
92 let files = find_files(&test_dir);
95 let extension = file.path.extension().and_then(|e| e.to_str());
97 Some("gmi") => assert_eq!(file.file_type, FileType::Gemini),
99 if file.path.ends_with("_layout.html") {
100 assert_eq!(file.file_type, FileType::Layout)
102 assert_eq!(file.file_type, FileType::File)
105 _ => assert_eq!(file.file_type, FileType::File),
111 fn ignores_git_directory() {
112 let test_dir = setup_test_dir();
113 create_dir_all(&test_dir.join("nested"))
114 .expect("Could not create nested test directory");
115 create_dir_all(&test_dir.join("assets"))
116 .expect("Could not create assets test directory");
117 create_dir_all(&test_dir.join(".git"))
118 .expect("Could not create git test directory");
119 create_test_file(&test_dir.join("_layout.html"), "");
120 create_test_file(&test_dir.join("nested/nested.gmi"), "");
121 create_test_file(&test_dir.join("assets/style.css"), "");
122 create_test_file(&test_dir.join("image.png"), "");
123 create_test_file(&test_dir.join(".git/config"), "");
124 let files = find_files(&test_dir);
126 let paths = get_paths(&test_dir, &files);
128 assert!(!paths.iter().any(|p| p.starts_with(".git/")));
129 assert!(!paths.contains(".gitignore"));
133 fn returns_empty_for_empty_directory() {
134 let test_dir = setup_test_dir();
135 let files = find_files(&test_dir);
136 assert!(files.is_empty());