From 5d51190169c6f49db4271940cfb91cb1bf106123 Mon Sep 17 00:00:00 2001 From: Josh Megnauth Date: Thu, 1 Feb 2024 23:31:50 -0500 Subject: [PATCH] Support hidden files for tests --- src/app.rs | 43 +++++++++++++++++++++++++++++++++++++------ src/tab.rs | 7 ++++--- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/app.rs b/src/app.rs index 3075f30..9893f7e 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1080,18 +1080,19 @@ pub(crate) mod test_utils { // Default number of files, directories, and nested directories for test file system pub const NUM_FILES: usize = 2; + pub const NUM_HIDDEN: usize = 1; pub const NUM_DIRS: usize = 2; pub const NUM_NESTED: usize = 1; pub const NAME_LEN: usize = 5; /// Add `n` temporary files in `dir` /// - /// Each file is assigned a numeric name from [0, n). - pub fn file_flat_hier>(dir: D, n: usize) -> io::Result> { + /// Each file is assigned a numeric name from [0, n) with a prefix. + pub fn file_flat_hier>(dir: D, n: usize, prefix: &str) -> io::Result> { let dir = dir.as_ref(); (0..n) .map(|i| -> io::Result { - let name = i.to_string(); + let name = format!("{prefix}{i}"); let path = dir.join(&name); let mut file = File::create(path)?; @@ -1108,8 +1109,17 @@ pub(crate) mod test_utils { } /// Create a small, temporary file hierarchy. + /// + /// # Arguments + /// + /// * `files` - Number of files to create in temp directories + /// * `hidden` - Number of hidden files to create + /// * `dirs` - Number of directories to create + /// * `nested` - Number of nested directories to create in new dirs + /// * `name_len` - Length of randomized directory names pub fn simple_fs( files: usize, + hidden: usize, dirs: usize, nested: usize, name_len: usize, @@ -1118,6 +1128,7 @@ pub(crate) mod test_utils { // TempDir won't leak resources as long as the destructor runs let root = tempdir()?; debug!("Root temp directory: {}", root.as_ref().display()); + trace!("Creating {files} files and {hidden} hidden files in {dirs} temp dirs with {nested} nested temp dirs"); // All paths for directories and nested directories let paths = (0..dirs).flat_map(|_| { @@ -1132,7 +1143,11 @@ pub(crate) mod test_utils { // Create directories from `paths` and add a few files for path in paths { fs::create_dir_all(&path)?; - file_flat_hier(&path, files)?; + + // Normal files + file_flat_hier(&path, files, "")?; + // Hidden files + file_flat_hier(&path, hidden, ".")?; for entry in path.read_dir()? { let entry = entry?; @@ -1179,8 +1194,24 @@ pub(crate) mod test_utils { .expect("temp entries should have names") .to_str() .expect("temp entries should be valid UTF-8"); - let metadata = path.is_dir(); + let is_dir = path.is_dir(); - name == item.name && metadata == item.metadata.is_dir() && path == item.path + // NOTE: I don't want to change `tab::hidden_attribute` to `pub(crate)` for + // tests without asking + #[cfg(not(target_os = "windows"))] + let is_hidden = name.starts_with('.'); + + #[cfg(target_os = "windows")] + let is_hidden = { + use std::os::windows::fs::MetadataExt; + const FILE_ATTRIBUTE_HIDDEN: u32 = 2; + let metadata = path.metadata().expect("fetching file metadata"); + metadata.file_attributes() & FILE_ATTRIBUTE_HIDDEN == FILE_ATTRIBUTE_HIDDEN + }; + + name == item.name + && is_dir == item.metadata.is_dir() + && path == item.path + && is_hidden == item.hidden } } diff --git a/src/tab.rs b/src/tab.rs index eaf87ef..31fb312 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -1043,12 +1043,13 @@ mod tests { use super::scan_path; use crate::app::test_utils::{ - empty_fs, eq_path_item, simple_fs, sort_files, NAME_LEN, NUM_DIRS, NUM_FILES, NUM_NESTED, + empty_fs, eq_path_item, simple_fs, sort_files, NAME_LEN, NUM_DIRS, NUM_FILES, NUM_HIDDEN, + NUM_NESTED, }; #[test] fn scan_path_succeeds_on_valid_path() -> io::Result<()> { - let fs = simple_fs(NUM_FILES, NUM_DIRS, NUM_NESTED, NAME_LEN)?; + let fs = simple_fs(NUM_FILES, NUM_HIDDEN, NUM_DIRS, NUM_NESTED, NAME_LEN)?; let path = fs.path(); let mut entries: Vec<_> = path @@ -1074,7 +1075,7 @@ mod tests { #[test] fn scan_path_returns_empty_vec_for_invalid_path() -> io::Result<()> { - let fs = simple_fs(NUM_FILES, NUM_DIRS, NUM_NESTED, NAME_LEN)?; + let fs = simple_fs(NUM_FILES, NUM_NESTED, NUM_DIRS, NUM_NESTED, NAME_LEN)?; let path = fs.path(); // A nonexisting path within the temp dir