From 79510b4bde3159177e72e2a2cfa327269c4551a3 Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 1 Feb 2024 19:06:20 +0800 Subject: [PATCH] refactor: improve code code coverage (#67) --- src/cache.rs | 7 ------- src/error.rs | 19 ++++++++++++++++++- src/path.rs | 9 +++++++++ src/specifier.rs | 2 +- src/tests/imports_field.rs | 16 +--------------- src/tests/simple.rs | 16 +++++++++++++++- tests/integration_test.rs | 2 +- 7 files changed, 45 insertions(+), 26 deletions(-) diff --git a/src/cache.rs b/src/cache.rs index f4554c19..3a022a46 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 Cache { #[derive(Clone)] pub struct CachedPath(Arc); -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(&self, state: &mut H) { self.0.hash.hash(state); diff --git a/src/error.rs b/src/error.rs index bef13342..85068582 100644 --- a/src/error.rs +++ b/src/error.rs @@ -124,7 +124,7 @@ impl PartialEq for IOError { } } -impl From for std::io::Error { +impl From 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 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 7ac0b8e3..67bca5cd 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 d7bc3c9e..29668d98 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>, diff --git a/src/tests/imports_field.rs b/src/tests/imports_field.rs index 34663633..ab431d8e 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 1cfcfa73..47d1646f 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::::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 ab0f74f6..301920f3 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]