diff options
author | Tim Mulqueen <Multimo@users.noreply.github.com> | 2019-05-22 12:04:51 -0400 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-05-22 12:04:51 -0400 |
commit | 4d034351e8cbedee06ec7516bb382b4bdb878a6a (patch) | |
tree | 4d16752b53a1b092e9d5d4c69967ca39d65c2c90 | |
parent | b2edadce050accc09b71f78353788495e583acd1 (diff) |
fix: Don't check extensions of directories (#64)
* now checks for type of path when doing scan
* added unit test to cover failure case
-rw-r--r-- | src/context.rs | 37 |
1 files changed, 26 insertions, 11 deletions
diff --git a/src/context.rs b/src/context.rs index fbf85268c..b86bd0708 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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()) -} |