diff --git a/src/fs.rs b/src/fs.rs index 3193036..d7cee44 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -292,6 +292,39 @@ mod tests { // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; + #[test] + fn test_zip_type_api() { + let zip = open_zip_via_read(&PathBuf::from( + "data/@babel-plugin-syntax-dynamic-import-npm-7.8.3-fb9ff5634a-8.zip", + )) + .unwrap(); + + assert_eq!(zip.file_type("node_modules").unwrap(), FileType::Directory); + assert_eq!(zip.file_type("node_modules/").unwrap(), FileType::Directory); + } + + #[test] + #[should_panic(expected = "Kind(NotFound)")] + fn test_zip_type_api_not_exist_dir_with_slash() { + let zip = open_zip_via_read(&PathBuf::from( + "data/@babel-plugin-syntax-dynamic-import-npm-7.8.3-fb9ff5634a-8.zip", + )) + .unwrap(); + + zip.file_type("not_exists/").unwrap(); + } + + #[test] + #[should_panic(expected = "Kind(NotFound)")] + fn test_zip_type_api_not_exist_dir_without_slash() { + let zip = open_zip_via_read(&PathBuf::from( + "data/@babel-plugin-syntax-dynamic-import-npm-7.8.3-fb9ff5634a-8.zip", + )) + .unwrap(); + + zip.file_type("not_exists").unwrap(); + } + #[test] fn test_zip_list() { let zip = open_zip_via_read(&PathBuf::from("data/@babel-plugin-syntax-dynamic-import-npm-7.8.3-fb9ff5634a-8.zip")) diff --git a/src/zip.rs b/src/zip.rs index 1564c2d..64b4962 100644 --- a/src/zip.rs +++ b/src/zip.rs @@ -55,7 +55,7 @@ where T : AsRef<[u8]> { } pub fn file_type(&self, p: &str) -> Result { - if self.dirs.contains(p) { + if self.is_dir(p) { Ok(FileType::Directory) } else if self.files.contains_key(p) { Ok(FileType::File) @@ -64,6 +64,14 @@ where T : AsRef<[u8]> { } } + fn is_dir(&self, p: &str) -> bool { + if p.ends_with('/') { + self.dirs.contains(p) + } else { + self.dirs.contains(&format!("{}/", p)) + } + } + pub fn read(&self, p: &str) -> Result, std::io::Error> { let entry = self.files.get(p) .ok_or(std::io::Error::from(std::io::ErrorKind::NotFound))?;