Explorar o código

fix(resources): enhance error messages for globs (#10879)

* fix(resources): enhance error messages for globs

ref: https://github.com/tauri-apps/tauri/pull/10293#issuecomment-2326435392

* fmt
Amr Bashir hai 11 meses
pai
achega
976cad97c1
Modificáronse 2 ficheiros con 74 adicións e 6 borrados
  1. 1 1
      crates/tauri-utils/src/lib.rs
  2. 73 5
      crates/tauri-utils/src/resources.rs

+ 1 - 1
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")]

+ 73 - 5
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::<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
+    );
+  }
 }