Skip to content

Commit d6a00b9

Browse files
committed
feat: ✨ allow file type to be called with unslashed dir path
1 parent 80e67fc commit d6a00b9

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/fs.rs

+33
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,39 @@ mod tests {
292292
// Note this useful idiom: importing names from outer (for mod tests) scope.
293293
use super::*;
294294

295+
#[test]
296+
fn test_zip_type_api() {
297+
let zip = open_zip_via_read(&PathBuf::from(
298+
"data/@babel-plugin-syntax-dynamic-import-npm-7.8.3-fb9ff5634a-8.zip",
299+
))
300+
.unwrap();
301+
302+
assert_eq!(zip.file_type("node_modules").unwrap(), FileType::Directory);
303+
assert_eq!(zip.file_type("node_modules/").unwrap(), FileType::Directory);
304+
}
305+
306+
#[test]
307+
#[should_panic(expected = "Kind(NotFound)")]
308+
fn test_zip_type_api_not_exist_dir_with_slash() {
309+
let zip = open_zip_via_read(&PathBuf::from(
310+
"data/@babel-plugin-syntax-dynamic-import-npm-7.8.3-fb9ff5634a-8.zip",
311+
))
312+
.unwrap();
313+
314+
zip.file_type("not_exists/").unwrap();
315+
}
316+
317+
#[test]
318+
#[should_panic(expected = "Kind(NotFound)")]
319+
fn test_zip_type_api_not_exist_dir_without_slash() {
320+
let zip = open_zip_via_read(&PathBuf::from(
321+
"data/@babel-plugin-syntax-dynamic-import-npm-7.8.3-fb9ff5634a-8.zip",
322+
))
323+
.unwrap();
324+
325+
zip.file_type("not_exists").unwrap();
326+
}
327+
295328
#[test]
296329
fn test_zip_list() {
297330
let zip = open_zip_via_read(&PathBuf::from("data/@babel-plugin-syntax-dynamic-import-npm-7.8.3-fb9ff5634a-8.zip"))

src/zip.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ where T : AsRef<[u8]> {
5555
}
5656

5757
pub fn file_type(&self, p: &str) -> Result<FileType, std::io::Error> {
58-
if self.dirs.contains(p) {
58+
if self.is_dir(p) {
5959
Ok(FileType::Directory)
6060
} else if self.files.contains_key(p) {
6161
Ok(FileType::File)
@@ -64,6 +64,14 @@ where T : AsRef<[u8]> {
6464
}
6565
}
6666

67+
fn is_dir(&self, p: &str) -> bool {
68+
if p.ends_with('/') {
69+
self.dirs.contains(p)
70+
} else {
71+
self.dirs.contains(&format!("{}/", p))
72+
}
73+
}
74+
6775
pub fn read(&self, p: &str) -> Result<Vec<u8>, std::io::Error> {
6876
let entry = self.files.get(p)
6977
.ok_or(std::io::Error::from(std::io::ErrorKind::NotFound))?;

0 commit comments

Comments
 (0)