diff --git a/CHANGELOG.md b/CHANGELOG.md index 5db36a3..4c86029 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 🎉 [Unreleased] + +### Fixed + +- Fix filter function which filtered out files with multiple dots in the filename + ## 🎉 [0.3.1] - 2023-01-26 ### Fixed diff --git a/src/helpers/vec_helper.rs b/src/helpers/vec_helper.rs index 0e0105d..a8c7e57 100644 --- a/src/helpers/vec_helper.rs +++ b/src/helpers/vec_helper.rs @@ -2,8 +2,11 @@ use std::path::PathBuf; use regex::Regex; +/// Returns a vector of PathBufs that match the given file format. +/// Also filters out files that start with a dot. pub fn filter_files(all_files: Vec, file_format: &str) -> Vec { - let re = Regex::new(format!(r"[\\/][^.]*\.{}$", regex::escape(file_format)).as_str()).unwrap(); + let re = Regex::new(format!(r"[\\/][^.\\/][^\\/]*\.{}$", regex::escape(file_format)).as_str()) + .unwrap(); let mut filtered_files = Vec::new(); for possible_file_to_merge in all_files { @@ -13,3 +16,104 @@ pub fn filter_files(all_files: Vec, file_format: &str) -> Vec } filtered_files } + +#[cfg(test)] +mod tests { + use super::*; + use std::path::PathBuf; + + #[test] + fn test_filter_files() { + let all_files = vec![ + PathBuf::from("/path/to/file1.txt"), + PathBuf::from("/path/to/file2.txt"), + PathBuf::from("/path/to/file3.doc"), + ]; + let file_format = "txt"; + let filtered_files = filter_files(all_files, file_format); + + assert_eq!(filtered_files.len(), 2); + assert!(filtered_files.contains(&PathBuf::from("/path/to/file1.txt"))); + assert!(filtered_files.contains(&PathBuf::from("/path/to/file2.txt"))); + } + + #[test] + fn test_filter_files_with_dots_at_the_start() { + let all_files = vec![ + PathBuf::from("/path/to/file1.txt"), + PathBuf::from("/path/to/file2.txt"), + PathBuf::from("/path/to/file3.doc"), + PathBuf::from("/path/to/file4.txt.txt"), + PathBuf::from("/path/to/.file5.txt"), + PathBuf::from("/path/to/.file6.txt.txt"), + ]; + let file_format = "txt"; + let filtered_files = filter_files(all_files, file_format); + + assert_eq!(filtered_files.len(), 3); + assert!(filtered_files.contains(&PathBuf::from("/path/to/file1.txt"))); + assert!(filtered_files.contains(&PathBuf::from("/path/to/file2.txt"))); + assert!(filtered_files.contains(&PathBuf::from("/path/to/file4.txt.txt"))); + } + + #[test] + fn test_filter_files_with_multiple_dots_after_the_start() { + let all_files = vec![ + PathBuf::from("/path/to/file1.txt"), + PathBuf::from("/path/to/file2.txt"), + PathBuf::from("/path/to/file3.doc"), + PathBuf::from("/path/to/file4.txt.txt"), + ]; + let file_format = "txt"; + let filtered_files = filter_files(all_files, file_format); + + assert_eq!(filtered_files.len(), 3); + assert!(filtered_files.contains(&PathBuf::from("/path/to/file1.txt"))); + assert!(filtered_files.contains(&PathBuf::from("/path/to/file2.txt"))); + assert!(filtered_files.contains(&PathBuf::from("/path/to/file4.txt.txt"))); + } + + #[test] + fn test_filter_files_with_no_matches() { + let all_files = vec![ + PathBuf::from("/path/to/file1.txt"), + PathBuf::from("/path/to/file2.txt"), + PathBuf::from("/path/to/file3.doc"), + ]; + let file_format = "mp4"; + let filtered_files = filter_files(all_files, file_format); + + assert_eq!(filtered_files.len(), 0); + } + + #[test] + fn test_filter_files_with_no_files() { + let all_files = vec![]; + let file_format = "mp4"; + let filtered_files = filter_files(all_files, file_format); + + assert_eq!(filtered_files.len(), 0); + } + + #[test] + fn test_filter_files_with_no_file_format() { + let all_files = vec![ + PathBuf::from("/path/to/file1.txt"), + PathBuf::from("/path/to/file2.txt"), + PathBuf::from("/path/to/file3.doc"), + ]; + let file_format = ""; + let filtered_files = filter_files(all_files, file_format); + + assert_eq!(filtered_files.len(), 0); + } + + #[test] + fn test_filter_files_with_no_file_format_and_no_files() { + let all_files = vec![]; + let file_format = ""; + let filtered_files = filter_files(all_files, file_format); + + assert_eq!(filtered_files.len(), 0); + } +}