1 use crate::file_handler::{File, FileHandler};
3 use std::path::PathBuf;
5 pub fn find_files(directory_path: &PathBuf) -> Vec<File> {
6 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);
23 result.push(File { path, file_type });
31 use std::collections::HashSet;
32 use std::fs::create_dir_all;
33 use std::path::PathBuf;
37 use crate::file_handler::File;
38 use crate::file_handler::FileType;
39 use test_utilities::*;
41 fn get_paths(root_directory: &PathBuf, files: &[File]) -> HashSet<String> {
46 .strip_prefix(root_directory)
55 fn finds_all_files() {
56 let test_dir = setup_test_dir();
57 create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory");
58 create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory");
59 create_test_file(&test_dir.join("test1.gmi"), "");
60 create_test_file(&test_dir.join("_layout.html"), "");
61 create_test_file(&test_dir.join("nested/nested.gmi"), "");
62 create_test_file(&test_dir.join("assets/style.css"), "");
63 create_test_file(&test_dir.join("image.png"), "");
65 let files = find_files(&test_dir);
66 let paths = get_paths(&test_dir, &files);
68 assert!(paths.contains("test1.gmi"));
69 assert!(paths.contains("_layout.html"));
70 assert!(paths.contains("nested/nested.gmi"));
71 assert!(paths.contains("assets/style.css"));
72 assert!(paths.contains("image.png"));
76 fn identifies_correct_file_types() {
77 let test_dir = setup_test_dir();
78 create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory");
79 create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory");
80 create_test_file(&test_dir.join("_layout.html"), "");
81 create_test_file(&test_dir.join("nested/nested.gmi"), "");
82 create_test_file(&test_dir.join("assets/style.css"), "");
83 create_test_file(&test_dir.join("image.png"), "");
84 let files = find_files(&test_dir);
87 let extension = file.path.extension().and_then(|e| e.to_str());
89 Some("gmi") => assert_eq!(file.file_type, FileType::Gemini),
91 if file.path.ends_with("_layout.html") {
92 assert_eq!(file.file_type, FileType::Layout)
94 assert_eq!(file.file_type, FileType::File)
97 _ => assert_eq!(file.file_type, FileType::File),
103 fn ignores_git_directory() {
104 let test_dir = setup_test_dir();
105 create_dir_all(test_dir.join("nested")).expect("Could not create nested test directory");
106 create_dir_all(test_dir.join("assets")).expect("Could not create assets test directory");
107 create_dir_all(test_dir.join(".git")).expect("Could not create git test directory");
108 create_test_file(&test_dir.join("_layout.html"), "");
109 create_test_file(&test_dir.join("nested/nested.gmi"), "");
110 create_test_file(&test_dir.join("assets/style.css"), "");
111 create_test_file(&test_dir.join("image.png"), "");
112 create_test_file(&test_dir.join(".git/config"), "");
113 let files = find_files(&test_dir);
115 let paths = get_paths(&test_dir, &files);
117 assert!(!paths.iter().any(|p| p.starts_with(".git/")));
118 assert!(!paths.contains(".gitignore"));
122 fn returns_empty_for_empty_directory() {
123 let test_dir = setup_test_dir();
124 let files = find_files(&test_dir);
125 assert!(files.is_empty());