mirror of
https://github.com/XAMPPRocky/tokei
synced 2024-09-13 21:11:29 +00:00
Only add regular files to path list (#75)
* only add regular files to path list This fixes multiple issues. Before this commit, a directory with a known file extension in its name would be added to the list of "files" to open and read from. Another issue occurred with broken symbolic links being added to the files list. * add test to ensure directories not treated as file * replace `unwrap()` with `rs_error!`
This commit is contained in:
parent
8f028b7aac
commit
24fec6788f
|
@ -48,7 +48,9 @@ pub fn get_all_files<'a>(paths: Cow<'a, [&'a str]>,
|
|||
get_language!(languages, &path)
|
||||
};
|
||||
|
||||
language.files.push(path.to_owned());
|
||||
if rs_error!(path.metadata()).is_file() {
|
||||
language.files.push(path.to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -72,7 +74,9 @@ pub fn get_all_files<'a>(paths: Cow<'a, [&'a str]>,
|
|||
get_language!(languages, entry.path())
|
||||
};
|
||||
|
||||
language.files.push(entry.path().to_owned());
|
||||
if rs_error!(entry.metadata()).is_file() {
|
||||
language.files.push(entry.path().to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -98,3 +102,27 @@ pub fn get_extension<P: AsRef<Path>>(path: P) -> Option<String> {
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
extern crate tempdir;
|
||||
use super::*;
|
||||
use std::fs::create_dir;
|
||||
use language::languages::Languages;
|
||||
use language::LanguageType;
|
||||
use self::tempdir::TempDir;
|
||||
|
||||
|
||||
#[test]
|
||||
fn walker_directory_as_file() {
|
||||
let tmp_dir = TempDir::new("test").expect("Couldn't create temp dir");
|
||||
let path_name = tmp_dir.path().join("directory.rs");
|
||||
create_dir(&path_name).expect("Couldn't create directory.rs within temp");
|
||||
|
||||
let mut l = Languages::new();
|
||||
get_all_files(vec![tmp_dir.into_path().to_str().unwrap()].into(), vec![].into(), &mut l);
|
||||
|
||||
assert_eq!(0, l.get(&LanguageType::Rust).unwrap().files.len());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue