From f77d3415e15eecd259a99047f37b65ecc938e3de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kh=E1=BA=A3i?= Date: Fri, 3 Nov 2023 07:37:22 +0700 Subject: [PATCH] refactor: functional style for `testing_utils::fs` (#173) --- crates/testing-utils/src/fs.rs | 76 ++++++++++++---------------------- 1 file changed, 27 insertions(+), 49 deletions(-) diff --git a/crates/testing-utils/src/fs.rs b/crates/testing-utils/src/fs.rs index d76a82d9..62a98c50 100644 --- a/crates/testing-utils/src/fs.rs +++ b/crates/testing-utils/src/fs.rs @@ -1,7 +1,8 @@ -use std::{io, path::Path}; +use std::{fs, io, path::Path}; +use walkdir::WalkDir; pub fn get_filenames_in_folder(path: &Path) -> Vec { - let mut files = std::fs::read_dir(path) + let mut files = fs::read_dir(path) .unwrap() .map(|e| e.unwrap().file_name().to_string_lossy().to_string()) .collect::>(); @@ -10,57 +11,34 @@ pub fn get_filenames_in_folder(path: &Path) -> Vec { files } -pub fn get_all_folders(root: &Path) -> Vec { - let mut files = Vec::new(); - for entry in walkdir::WalkDir::new(root) { - let entry = entry.unwrap(); - let entry_path = entry.path(); - if entry.file_type().is_dir() || entry.file_type().is_symlink() { - // We need this mutation to ensure that both Unix and Windows paths resolves the same. - // TODO: Find a better way to do this? - let simple_path = entry_path - .strip_prefix(root) - .unwrap() - .components() - .map(|c| c.as_os_str().to_str().expect("invalid UTF-8")) - .collect::>() - .join("/"); +fn normalized_suffix(path: &Path, prefix: &Path) -> String { + path.strip_prefix(prefix) + .expect("strip prefix from path") + .to_str() + .expect("convert suffix to UTF-8") + .replace('\\', "/") +} - if !simple_path.is_empty() { - files.push(simple_path); - } - } - } - files.sort(); - files +pub fn get_all_folders(root: &Path) -> Vec { + WalkDir::new(root) + .sort_by_file_name() + .into_iter() + .map(|entry| entry.expect("access entry")) + .filter(|entry| entry.file_type().is_dir() || entry.file_type().is_symlink()) + .map(|entry| normalized_suffix(entry.path(), root)) + .filter(|suffix| !suffix.is_empty()) + .collect() } pub fn get_all_files(root: &Path) -> Vec { - let mut files = Vec::new(); - for entry in walkdir::WalkDir::new(root) { - let entry = entry.unwrap(); - let entry_path = entry.path(); - - if entry.file_type().is_dir() { - continue; - } - - // We need this mutation to ensure that both Unix and Windows paths resolves the same. - // TODO: Find a better way to do this? - let simple_path = entry_path - .strip_prefix(root) - .unwrap() - .components() - .map(|c| c.as_os_str().to_str().expect("invalid UTF-8")) - .collect::>() - .join("/"); - - if !simple_path.is_empty() { - files.push(simple_path); - } - } - files.sort(); - files + WalkDir::new(root) + .sort_by_file_name() + .into_iter() + .map(|entry| entry.expect("access entry")) + .filter(|entry| !entry.file_type().is_dir()) + .map(|entry| normalized_suffix(entry.path(), root)) + .filter(|suffix| !suffix.is_empty()) + .collect() } // Helper function to check if a path is a symlink or junction