fix: Don't check extensions of directories (#64)

* now checks for type of path when doing scan
* added unit test to cover failure case
This commit is contained in:
Tim Mulqueen 2019-05-22 12:04:51 -04:00 committed by Matan Kushner
parent b2edadce05
commit 4d034351e8

View File

@ -105,9 +105,11 @@ impl<'a> ScanDir<'a> {
/// if any of this criteria match or exist and returning a boolean
pub fn scan(&mut self) -> bool {
self.dir_files.iter().any(|path| {
path_has_name(&path, &self.folders)
|| path_has_name(&path, &self.files)
|| has_extension(&path, &self.extensions)
if path.is_dir() {
return path_has_name(&path, &self.folders);
} else {
return path_has_name(&path, &self.files) || has_extension(&path, &self.extensions);
}
})
}
}
@ -144,6 +146,13 @@ pub fn has_extension<'a>(dir_entry: &PathBuf, extensions: &'a [&'a str]) -> bool
}
}
fn get_current_branch(repository: &Repository) -> Option<String> {
let head = repository.head().ok()?;
let shorthand = head.shorthand();
shorthand.map(|branch| branch.to_string())
}
#[cfg(test)]
mod tests {
use super::*;
@ -177,7 +186,7 @@ mod tests {
}
#[test]
fn test_criteria_scan() {
fn test_criteria_scan_fails() {
let mut failing_criteria = ScanDir {
dir_files: &vec![PathBuf::new()],
files: &["package.json"],
@ -188,6 +197,19 @@ mod tests {
// fails if buffer does not match any criteria
assert_eq!(failing_criteria.scan(), false);
let mut failing_dir_criteria = ScanDir {
dir_files: &vec![PathBuf::from("/package.js/dog.go")],
files: &["package.json"],
extensions: &["js"],
folders: &["node_modules"],
};
// fails when passed a pathbuf dir matches extension path
assert_eq!(failing_dir_criteria.scan(), false);
}
#[test]
fn test_criteria_scan_passes() {
let mut passing_criteria = ScanDir {
dir_files: &vec![PathBuf::from("package.json")],
files: &["package.json"],
@ -198,10 +220,3 @@ mod tests {
assert_eq!(passing_criteria.scan(), true);
}
}
fn get_current_branch(repository: &Repository) -> Option<String> {
let head = repository.head().ok()?;
let shorthand = head.shorthand();
shorthand.map(|branch| branch.to_string())
}