diff --git a/crates/tauri-utils/src/lib.rs b/crates/tauri-utils/src/lib.rs index 215851183424..87c3ec8720d6 100644 --- a/crates/tauri-utils/src/lib.rs +++ b/crates/tauri-utils/src/lib.rs @@ -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")] diff --git a/crates/tauri-utils/src/resources.rs b/crates/tauri-utils/src/resources.rs index fc0235ca4488..ee2ef1b94b9c 100644 --- a/crates/tauri-utils/src/resources.rs +++ b/crates/tauri-utils/src/resources.rs @@ -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()))); + } } } @@ -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 { @@ -369,7 +374,7 @@ mod tests { } #[test] - #[serial_test::serial] + #[serial_test::serial(resources)] fn resource_paths_iter_slice_allow_walk() { setup_test_dirs(); @@ -418,7 +423,7 @@ mod tests { } #[test] - #[serial_test::serial] + #[serial_test::serial(resources)] fn resource_paths_iter_slice_no_walk() { setup_test_dirs(); @@ -457,7 +462,7 @@ mod tests { } #[test] - #[serial_test::serial] + #[serial_test::serial(resources)] fn resource_paths_iter_map_allow_walk() { setup_test_dirs(); @@ -513,7 +518,7 @@ mod tests { } #[test] - #[serial_test::serial] + #[serial_test::serial(resources)] fn resource_paths_iter_map_no_walk() { setup_test_dirs(); @@ -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::>(); + + 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 + ); + } }