Skip to content

feat: ✨ allow file type to be called with unslashed dir path #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
10 changes: 9 additions & 1 deletion src/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ where T : AsRef<[u8]> {
}

pub fn file_type(&self, p: &str) -> Result<FileType, std::io::Error> {
if self.dirs.contains(p) {
if self.is_dir(p) {
Ok(FileType::Directory)
} else if self.files.contains_key(p) {
Ok(FileType::File)
Expand All @@ -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<Vec<u8>, std::io::Error> {
let entry = self.files.get(p)
.ok_or(std::io::Error::from(std::io::ErrorKind::NotFound))?;
Expand Down