Repository: oxc-project/oxc-resolver
Pull request #67 (2024-02-01 11:00:57)

refactor: improve code code coverage

codspeed-hq[bot]: ## [CodSpeed Performance Report](https://codspeed.io/oxc-project/oxc-resolver/branches/improve-code-coverage)

### Merging #67 will **not alter performance**

<sub>:warning: No base runs were found</sub>

<sub>Falling back to comparing <code>improve-code-coverage</code> (2b152ba) with <code>main</code> (3a1191e)</sub>



### Summary

`✅ 2` untouched benchmarks






Patch:
diff --git a/src/cache.rs b/src/cache.rs
index f4554c1..3a022a4 100644
--- a/src/cache.rs
+++ b/src/cache.rs
@@ -2,7 +2,6 @@ use once_cell::sync::OnceCell as OnceLock;
 use std::{
     borrow::{Borrow, Cow},
     convert::AsRef,
-    fmt,
     hash::{BuildHasherDefault, Hash, Hasher},
     io,
     ops::Deref,
@@ -90,12 +89,6 @@ impl<Fs: FileSystem + Default> Cache<Fs> {
 #[derive(Clone)]
 pub struct CachedPath(Arc<CachedPathImpl>);
 
-impl fmt::Debug for CachedPath {
-    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
-        self.0.path.fmt(f)
-    }
-}
-
 impl Hash for CachedPath {
     fn hash<H: Hasher>(&self, state: &mut H) {
         self.0.hash.hash(state);
diff --git a/src/error.rs b/src/error.rs
index bef1334..8506858 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -124,7 +124,7 @@ impl PartialEq for IOError {
     }
 }
 
-impl From<IOError> for std::io::Error {
+impl From<IOError> for io::Error {
     fn from(error: IOError) -> Self {
         let io_error = error.0.as_ref();
         Self::new(io_error.kind(), io_error.to_string())
@@ -136,3 +136,20 @@ impl From<io::Error> for ResolveError {
         Self::IOError(IOError(Arc::new(err)))
     }
 }
+
+#[test]
+fn test_into_io_error() {
+    use std::io::{self, ErrorKind};
+    let error_string = "IOError occurred";
+    let string_error = io::Error::new(ErrorKind::Interrupted, error_string.to_string());
+    let string_error2 = io::Error::new(ErrorKind::Interrupted, error_string.to_string());
+    let resolve_io_error: ResolveError = ResolveError::from(string_error2);
+
+    assert_eq!(resolve_io_error, ResolveError::from(string_error));
+    if let ResolveError::IOError(io_error) = resolve_io_error {
+        // fix for https://github.com/web-infra-dev/rspack/issues/4564
+        let std_io_error: io::Error = io_error.into();
+        assert_eq!(std_io_error.kind(), ErrorKind::Interrupted);
+        assert_eq!(std_io_error.to_string(), error_string);
+    }
+}
diff --git a/src/path.rs b/src/path.rs
index 7ac0b8e..67bca5c 100644
--- a/src/path.rs
+++ b/src/path.rs
@@ -108,4 +108,13 @@ fn is_invalid_exports_target() {
     for case in test_cases {
         assert!(Path::new(case).is_invalid_exports_target(), "{case}");
     }
+
+    assert!(!Path::new("C:").is_invalid_exports_target());
+    assert!(!Path::new("/").is_invalid_exports_target());
+}
+
+#[test]
+fn normalize() {
+    assert_eq!(Path::new("/foo/.././foo/").normalize(), Path::new("/foo"));
+    assert_eq!(Path::new("C://").normalize(), Path::new("C://"));
 }
diff --git a/src/specifier.rs b/src/specifier.rs
index d7bc3c9..29668d9 100644
--- a/src/specifier.rs
+++ b/src/specifier.rs
@@ -1,7 +1,7 @@
 use crate::error::SpecifierError;
 use std::borrow::Cow;
 
-#[derive(Debug, Clone, Eq, PartialEq)]
+#[derive(Debug, Eq, PartialEq)]
 pub struct Specifier<'a> {
     path: Cow<'a, str>,
     pub query: Option<&'a str>,


Test patch:
diff --git a/src/tests/imports_field.rs b/src/tests/imports_field.rs
index 3466363..ab431d8 100644
--- a/src/tests/imports_field.rs
+++ b/src/tests/imports_field.rs
@@ -5,7 +5,7 @@
 use serde_json::json;
 
 use crate::{Ctx, MatchObject, PathUtil, ResolveError, ResolveOptions, Resolver};
-use std::{io::ErrorKind, path::Path};
+use std::path::Path;
 
 #[test]
 fn test() {
@@ -1292,17 +1292,3 @@ fn test_cases() {
         }
     }
 }
-
-#[test]
-fn test_into_io_error() {
-    let error_string = "IOError occurred";
-    let string_error = std::io::Error::new(ErrorKind::Interrupted, error_string.to_string());
-    let resolve_io_error: ResolveError = string_error.into();
-
-    if let ResolveError::IOError(io_error) = resolve_io_error {
-        // fix for https://github.com/web-infra-dev/rspack/issues/4564
-        let std_io_error: std::io::Error = io_error.into();
-        assert_eq!(std_io_error.kind(), ErrorKind::Interrupted);
-        assert_eq!(std_io_error.to_string(), error_string);
-    }
-}
diff --git a/src/tests/simple.rs b/src/tests/simple.rs
index 1cfcfa7..47d1646 100644
--- a/src/tests/simple.rs
+++ b/src/tests/simple.rs
@@ -2,7 +2,7 @@
 
 use std::env;
 
-use crate::Resolver;
+use crate::{ResolveOptions, Resolver};
 
 #[test]
 fn simple() {
@@ -45,3 +45,17 @@ fn dashed_name() {
         assert_eq!(resolved_path, Ok(expected), "{path:?} {request}");
     }
 }
+
+#[test]
+#[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows.
+fn no_package() {
+    use super::memory_fs::MemoryFS;
+    use crate::ResolverGeneric;
+    use std::path::Path;
+    let f = Path::new("/");
+    let file_system = MemoryFS::new(&[]);
+    let resolver =
+        ResolverGeneric::<MemoryFS>::new_with_file_system(file_system, ResolveOptions::default());
+    let resolved_path = resolver.resolve(f, "package");
+    assert!(resolved_path.is_err());
+}
diff --git a/tests/integration_test.rs b/tests/integration_test.rs
index ab0f74f..301920f 100644
--- a/tests/integration_test.rs
+++ b/tests/integration_test.rs
@@ -35,7 +35,7 @@ fn eq() {
 #[test]
 fn package_json() {
     let resolution = resolve("./tests/package.json");
-    assert!(resolution.package_json().is_some());
+    assert!(resolution.package_json().is_some_and(|json| json.raw_json().is_object()));
 }
 
 #[test]