Skip to content

Commit

Permalink
fix(resources): enhance error messages for globs (#10879)
Browse files Browse the repository at this point in the history
* fix(resources): enhance error messages for globs

ref: #10293 (comment)

* fmt
  • Loading branch information
amrbashir authored Sep 3, 2024
1 parent 11b3ab1 commit 976cad9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion crates/tauri-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ pub enum Error {
Glob(#[from] glob::GlobError),
/// Glob pattern did not find any results.
#[cfg(feature = "resources")]
#[error("path matching {0} not found.")]
#[error("glob pattern {0} path not found or didn't match any files.")]
GlobPathNotFound(String),
/// Error walking directory.
#[cfg(feature = "resources")]
Expand Down
78 changes: 73 additions & 5 deletions crates/tauri-utils/src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ impl<'a> ResourcePathsIter<'a> {
};
match self.next_glob_iter() {
Some(r) => return Some(r),
None => self.glob_iter = None,
None => {
self.glob_iter = None;
return Some(Err(crate::Error::GlobPathNotFound(pattern.clone())));
}
}
}

Expand Down Expand Up @@ -360,6 +363,8 @@ mod tests {
Path::new("src/index.html"),
Path::new("src/style.css"),
Path::new("src/script.js"),
Path::new("src/dir/another-dir/file1.txt"),
Path::new("src/dir/another-dir2/file2.txt"),
];

for path in paths {
Expand All @@ -369,7 +374,7 @@ mod tests {
}

#[test]
#[serial_test::serial]
#[serial_test::serial(resources)]
fn resource_paths_iter_slice_allow_walk() {
setup_test_dirs();

Expand Down Expand Up @@ -418,7 +423,7 @@ mod tests {
}

#[test]
#[serial_test::serial]
#[serial_test::serial(resources)]
fn resource_paths_iter_slice_no_walk() {
setup_test_dirs();

Expand Down Expand Up @@ -457,7 +462,7 @@ mod tests {
}

#[test]
#[serial_test::serial]
#[serial_test::serial(resources)]
fn resource_paths_iter_map_allow_walk() {
setup_test_dirs();

Expand Down Expand Up @@ -513,7 +518,7 @@ mod tests {
}

#[test]
#[serial_test::serial]
#[serial_test::serial(resources)]
fn resource_paths_iter_map_no_walk() {
setup_test_dirs();

Expand Down Expand Up @@ -550,4 +555,67 @@ mod tests {
}
}
}

#[test]
#[serial_test::serial(resources)]
fn resource_paths_errors() {
setup_test_dirs();

let dir = std::env::current_dir().unwrap().join("src-tauri");
let _ = std::env::set_current_dir(dir);

let resources = ResourcePaths::from_map(
&std::collections::HashMap::from_iter([
("../non-existent-file".into(), "file".into()),
("../non-existent-dir".into(), "dir".into()),
// exists but not allowed to walk
("../src".into(), "dir2".into()),
// doesn't exist but it is a glob and will return an error
("../non-existent-glob-dir/*".into(), "glob".into()),
// exists but only contains directories and will not produce any values
("../src/dir/*".into(), "dir3".into()),
]),
false,
)
.iter()
.collect::<Vec<_>>();

dbg!(&resources);

assert_eq!(resources.len(), 4);

assert!(resources.iter().all(|r| r.is_err()));

// hashmap order is not guaranteed so we check the error variant exists and how many
assert!(resources
.iter()
.any(|r| matches!(r, Err(crate::Error::ResourcePathNotFound(_)))));
assert_eq!(
resources
.iter()
.filter(|r| matches!(r, Err(crate::Error::ResourcePathNotFound(_))))
.count(),
2
);
assert!(resources
.iter()
.any(|r| matches!(r, Err(crate::Error::NotAllowedToWalkDir(_)))));
assert_eq!(
resources
.iter()
.filter(|r| matches!(r, Err(crate::Error::NotAllowedToWalkDir(_))))
.count(),
1
);
assert!(resources
.iter()
.any(|r| matches!(r, Err(crate::Error::GlobPathNotFound(_)))));
assert_eq!(
resources
.iter()
.filter(|r| matches!(r, Err(crate::Error::GlobPathNotFound(_))))
.count(),
1
);
}
}

0 comments on commit 976cad9

Please sign in to comment.