use std::{ collections::HashMap, fs::FileType, path::{Path, PathBuf}, }; use…
- User
use std::{ collections::HashMap, fs::FileType, path::{Path, PathBuf}, };
use walkdir::WalkDir;
use crate::{config::Config, manifest::Manifest};
#[derive(PartialEq, Eq)] #[cfg_attr(test, derive(Debug))] enum FileSystemTree { Node(PathBuf), Branch(HashMap<String, FileSystemTree>), }
impl FileSystemTree { fn new() -> Self { Self::Branch(HashMap::new()) }
fn insert(&mut self, target: &Path, source: PathBuf) { let mut current = self; for subpath in target.components().skip(1) { let name = subpath.as_os_str().to_string_lossy().to_string(); match current { Self::Node(_) => { *current = Self::Branch({ let mut map = HashMap::new(); map.insert(name, Self::new()); map }); } Self::Branch(m) => { current = m.entry(name).or_insert(FileSystemTree::new()); } } } *current = Self::Node(source); }}
pub fn preview( config: Config, source_dir: PathBuf, target_dir: PathBuf, ) -> anyhow::Result { let mut tree = FileSystemTree::new();
for portal in &config.portals { let source = source_dir.clone().join(&portal.from); let target = target_dir.clone().join(&portal.to); if !source.exists() { anyhow::bail!("Source path {} does not exist", source.display()); } if source.is_file() { tree.insert(&target, source); } else if source.is_dir() { for entry in WalkDir::new(&source).into_iter().flatten() { let a = entry.path().strip_prefix(&source).unwrap(); tree.insert(&target.join(a), entry.path().to_path_buf()); } } } Ok(tree)}
#[cfg(test)] mod test { use std::fs::{File, create_dir_all};
use crate::config::Portal; use super::*; use tempfile::{TempDir, tempdir}; use test_case::test_case; fn generate_branch(ls: Vec<(&'static str, FileSystemTree)>) -> FileSystemTree { FileSystemTree::Branch({ let mut map = HashMap::new(); for (k, v) in ls { map.insert(k.to_string(), v); } map }) } #[test_case(|temp|{ File::create(temp.path().join("file1")).unwrap(); }, vec![ Portal { from: "file1".to_string(), to: "file1".to_string(), file_type: None, }, ], |temp|{ generate_branch(vec![( "file1", FileSystemTree::Node(temp.path().join("file1")), )]) }; "")] fn test_preview<T: FnOnce(&TempDir), F: FnOnce(&TempDir) -> FileSystemTree>( create: T, portals: Vec<Portal>, tree: F, ) { let temp = tempdir().unwrap(); create(&temp); assert_eq!( tree(&temp), preview( Config { portals, ..Default::default() }, temp.path().to_path_buf(), PathBuf::from("/"), ) .unwrap() ); }}
write me more test case
- Assistant
Certainly! Here are some additional test cases to cover different scenarios for the
previewfunction:-
Test with a directory:
- Create a directory structure and ensure the function correctly maps directories.
-
Test with nested directories:
- Create nested directories and ensure the function correctly maps the entire structure.
-
Test with multiple files and directories:
- Create a mix of files and directories and ensure the function correctly maps all of them.
-
Test with non-existent source:
- Ensure the function correctly handles the case where the source path does not exist.
-
Test with empty directories:
- Ensure the function correctly handles empty directories.
Here are the implementations for these test cases:
#[test_case(|temp|{ create_dir_all(temp.path().join("dir1")).unwrap(); }, vec![ Portal { from: "dir1".to_string(), to: "dir1".to_string(), file_type: None, }, ], |temp|{ generate_branch(vec![( "dir1", FileSystemTree::Node(temp.path().join("dir1")), )]) }; "directory")] fn test_preview_directory<T: FnOnce(&TempDir), F: FnOnce(&TempDir) -> FileSystemTree>( create: T, portals: Vec<Portal>, tree: F, ) { let temp = tempdir().unwrap(); create(&temp); assert_eq!( tree(&temp), preview( Config { portals, ..Default::default() }, temp.path().to_path_buf(), PathBuf::from("/"), ) .unwrap() ); } #[test_case(|temp|{ create_dir_all(temp.path().join("dir1/dir2")).unwrap(); File::create(temp.path().join("dir1/dir2/file2")).unwrap(); }, vec![ Portal { from: "dir1/dir2".to_string(), to: "dir1/dir2".to_string(), file_type: None, }, ], |temp|{ generate_branch(vec![( "dir1", generate_branch(vec![( "dir2", generate_branch(vec![( "file2", FileSystemTree::Node(temp.path().join("dir1/dir2/file2")), )]), ))],), )]) }; "nested_directories")] fn test_preview_nested_directories<T: FnOnce(&TempDir), F: FnOnce(&TempDir) -> FileSystemTree>( create: T, portals: Vec<Portal>, tree: F, ) { let temp = tempdir().unwrap(); create(&temp); assert_eq!( tree(&temp), preview( Config { portals, ..Default::default() }, temp.path().to_path_buf(), PathBuf::from("/"), ) .unwrap() ); } #[test_case(|temp|{ File::create(temp.path().join("file1")).unwrap(); create_dir_all(temp.path().join("dir1")).unwrap(); File::create(temp.path().join("dir1/file2")).unwrap(); }, vec![ Portal { from: "file1".to_string(), to: "file1".to_string(), file_type: None, }, Portal { from: "dir1".to_string(), to: "dir1".to_string(), file_type: None, }, ], |temp|{ generate_branch(vec![ ( "file1", FileSystemTree::Node(temp.path().join("file1")), ), ( "dir1", generate_branch(vec![( "file2", FileSystemTree::Node(temp.path().join("dir1/file2")), )]), ), ]) }; "multiple_files_and_directories")] fn test_preview_multiple_files_and_directories<T: FnOnce(&TempDir), F: FnOnce(&TempDir) -> FileSystemTree>( create: T, portals: Vec<Portal>, tree: F, ) { let temp = tempdir().unwrap(); create(&temp); assert_eq!( tree(&temp), preview( Config { portals, ..Default::default() }, temp.path().to_path_buf(), PathBuf::from("/"), ) .unwrap() ); } #[test_case(|temp|{}, vec![ Portal { from: "nonexistent".to_string(), to: "nonexistent".to_string(), file_type: None, }, ], |temp|{ FileSystemTree::new() }; "non_existent_source")] fn test_preview_non_existent_source<T: FnOnce(&TempDir), F: FnOnce(&TempDir) -> FileSystemTree>( create: T, portals: Vec<Portal>, tree: F, ) { let temp = tempdir().unwrap(); create(&temp); assert!(preview( Config { portals, ..Default::default() }, temp.path().to_path_buf(), PathBuf::from("/"), ) .is_err()); } #[test_case(|temp|{ create_dir_all(temp.path().join("empty_dir")).unwrap(); }, vec![ Portal { from: "empty_dir".to_string(), to: "empty_dir".to_string(), file_type: None, }, ], |temp|{ generate_branch(vec![( "empty_dir", FileSystemTree::Node(temp.path().join("empty_dir")), )]) }; "empty_directories")] fn test_preview_empty_directories<T: FnOnce(&TempDir), F: FnOnce(&TempDir) -> FileSystemTree>( create: T, portals: Vec<Portal>, tree: F, ) { let temp = tempdir().unwrap(); create(&temp); assert_eq!( tree(&temp), preview( Config { portals, ..Default::default() }, temp.path().to_path_buf(), PathBuf::from("/"), ) .unwrap() ); }These test cases cover a variety of scenarios to ensure the
previewfunction works correctly under different conditions. -
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.