Skip to content

Commit

Permalink
refactor: improve code code coverage (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen authored Feb 1, 2024
1 parent 2bd72d1 commit 79510b4
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 26 deletions.
7 changes: 0 additions & 7 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 18 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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);
}
}
9 changes: 9 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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://"));
}
2 changes: 1 addition & 1 deletion src/specifier.rs
Original file line number Diff line number Diff line change
@@ -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>,
Expand Down
16 changes: 1 addition & 15 deletions src/tests/imports_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
16 changes: 15 additions & 1 deletion src/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::env;

use crate::Resolver;
use crate::{ResolveOptions, Resolver};

#[test]
fn simple() {
Expand Down Expand Up @@ -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());
}
2 changes: 1 addition & 1 deletion tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 79510b4

Please sign in to comment.