fix: Don't trigger an extension match on hidden files (#299)

Addresses #52, closes #280.
This commit is contained in:
Gabriel de Perthuis 2019-09-07 17:05:25 +02:00 committed by Matan Kushner
parent 61abe6dd7a
commit 69ebab46a4

View File

@ -177,20 +177,20 @@ pub fn path_has_name<'a>(dir_entry: &PathBuf, names: &'a [&'a str]) -> bool {
} }
} }
/// checks if pathbuf matches the extension provided /// checks if pathbuf doesn't start with a dot and matches any provided extension
pub fn has_extension<'a>(dir_entry: &PathBuf, extensions: &'a [&'a str]) -> bool { pub fn has_extension<'a>(dir_entry: &PathBuf, extensions: &'a [&'a str]) -> bool {
let found_ext = extensions.iter().find(|ext| { if let Some(file_name) = dir_entry.file_name() {
dir_entry if file_name.to_string_lossy().starts_with('.') {
.extension() return false;
.and_then(OsStr::to_str) }
.unwrap_or_default() return extensions.iter().any(|ext| {
== **ext dir_entry
}); .extension()
.and_then(OsStr::to_str)
match found_ext { .map_or(false, |e| e == *ext)
Some(extension) => !extension.is_empty(), });
None => false,
} }
false
} }
fn get_current_branch(repository: &Repository) -> Option<String> { fn get_current_branch(repository: &Repository) -> Option<String> {
@ -228,6 +228,9 @@ mod tests {
buf.set_file_name("some-file.rs"); buf.set_file_name("some-file.rs");
assert_eq!(has_extension(&buf, &extensions), false); assert_eq!(has_extension(&buf, &extensions), false);
buf.set_file_name(".some-file.js");
assert_eq!(has_extension(&buf, &extensions), false);
buf.set_file_name("some-file.js"); buf.set_file_name("some-file.js");
assert_eq!(has_extension(&buf, &extensions), true) assert_eq!(has_extension(&buf, &extensions), true)
} }