Skip to content

Commit

Permalink
🐞 Fix filter function regarding multiple dots
Browse files Browse the repository at this point in the history
  • Loading branch information
tgotwig committed Nov 29, 2023
1 parent 3c57f83 commit 0e03e39
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
106 changes: 105 additions & 1 deletion src/helpers/vec_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf>, file_format: &str) -> Vec<PathBuf> {
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 {
Expand All @@ -13,3 +16,104 @@ pub fn filter_files(all_files: Vec<PathBuf>, file_format: &str) -> Vec<PathBuf>
}
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);
}
}

0 comments on commit 0e03e39

Please sign in to comment.