diff --git a/crates/oxc_resolver/README.md b/crates/oxc_resolver/README.md index d161e5a53..fdf960dc0 100644 --- a/crates/oxc_resolver/README.md +++ b/crates/oxc_resolver/README.md @@ -37,6 +37,8 @@ ## Test Tests ported from [enhanced-resolve](https://github.com/webpack/enhanced-resolve). +Test cases are located in `./src/tests`, fixtures are located in `./tests` + Crossed out test files are irrelevant. - [x] ~CachedInputFileSystem.test.js~ diff --git a/crates/oxc_resolver/src/lib.rs b/crates/oxc_resolver/src/lib.rs index f6bd74040..1e537fc5a 100644 --- a/crates/oxc_resolver/src/lib.rs +++ b/crates/oxc_resolver/src/lib.rs @@ -17,6 +17,9 @@ mod path; mod request; mod resolution; +#[cfg(test)] +mod tests; + use std::{ borrow::Cow, cmp::Ordering, @@ -27,15 +30,15 @@ use std::{ use crate::{ cache::{Cache, CacheValue}, file_system::FileSystemOs, + package_json::{ExportsField, MatchObject}, package_json::{ExportsKey, PackageJson}, + path::PathUtil, request::{Request, RequestPath}, }; pub use crate::{ error::{JSONError, ResolveError}, file_system::{FileMetadata, FileSystem}, options::{Alias, AliasValue, ResolveOptions}, - package_json::{ExportsField, MatchObject}, - path::PathUtil, resolution::Resolution, }; @@ -535,9 +538,7 @@ impl ResolverGeneric { } /// PACKAGE_EXPORTS_RESOLVE(packageURL, subpath, exports, conditions) - /// - /// # Errors - pub fn package_exports_resolve( + fn package_exports_resolve( &self, package_url: &Path, subpath: &str, @@ -550,8 +551,7 @@ impl ResolverGeneric { let mut without_dot = false; for key in map.keys() { has_dot = has_dot || matches!(key, ExportsKey::Main | ExportsKey::Pattern(_)); - without_dot = without_dot - || matches!(key, ExportsKey::Hash(_) | ExportsKey::CustomCondition(_)); + without_dot = without_dot || matches!(key, ExportsKey::CustomCondition(_)); if has_dot && without_dot { return Err(ResolveError::InvalidPackageConfig( package_url.join("package.json"), @@ -661,9 +661,7 @@ impl ResolverGeneric { } /// PACKAGE_IMPORTS_EXPORTS_RESOLVE(matchKey, matchObj, packageURL, isImports, conditions) - /// - /// # Errors - pub fn package_imports_exports_resolve( + fn package_imports_exports_resolve( &self, match_key: &str, match_obj: &MatchObject, diff --git a/crates/oxc_resolver/src/package_json.rs b/crates/oxc_resolver/src/package_json.rs index 7344915ab..464ad2d83 100644 --- a/crates/oxc_resolver/src/package_json.rs +++ b/crates/oxc_resolver/src/package_json.rs @@ -75,7 +75,6 @@ impl ExportsField { pub enum ExportsKey { Main, Pattern(String), - Hash(String), CustomCondition(String), } diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/alias.rs b/crates/oxc_resolver/src/tests/alias.rs similarity index 97% rename from crates/oxc_resolver/tests/enhanced_resolve/test/alias.rs rename to crates/oxc_resolver/src/tests/alias.rs index 3c083a538..181dd0580 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/alias.rs +++ b/crates/oxc_resolver/src/tests/alias.rs @@ -2,11 +2,9 @@ use std::path::{Path, PathBuf}; -use oxc_resolver::{ - AliasValue, Resolution, ResolveError, ResolveOptions, Resolver, ResolverGeneric, -}; +use crate::{AliasValue, Resolution, ResolveError, ResolveOptions, Resolver, ResolverGeneric}; -use crate::MemoryFS; +use super::memory_fs::MemoryFS; #[test] #[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows. diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/browser_field.rs b/crates/oxc_resolver/src/tests/browser_field.rs similarity index 97% rename from crates/oxc_resolver/tests/enhanced_resolve/test/browser_field.rs rename to crates/oxc_resolver/src/tests/browser_field.rs index cfbe06be7..092312205 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/browser_field.rs +++ b/crates/oxc_resolver/src/tests/browser_field.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; -use oxc_resolver::{Resolution, ResolveError, ResolveOptions, Resolver}; +use crate::{Resolution, ResolveError, ResolveOptions, Resolver}; fn fixture() -> PathBuf { super::fixture().join("browser-module") diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/exports_field.rs b/crates/oxc_resolver/src/tests/exports_field.rs similarity index 99% rename from crates/oxc_resolver/tests/enhanced_resolve/test/exports_field.rs rename to crates/oxc_resolver/src/tests/exports_field.rs index 3cea0769d..2accbd7a9 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/exports_field.rs +++ b/crates/oxc_resolver/src/tests/exports_field.rs @@ -2,7 +2,7 @@ //! //! The huge exports field test cases are at the bottom of this file. -use oxc_resolver::{ExportsField, PathUtil, Resolution, ResolveError, ResolveOptions, Resolver}; +use crate::{ExportsField, PathUtil, Resolution, ResolveError, ResolveOptions, Resolver}; use serde_json::json; use std::path::Path; diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/extension_alias.rs b/crates/oxc_resolver/src/tests/extension_alias.rs similarity index 97% rename from crates/oxc_resolver/tests/enhanced_resolve/test/extension_alias.rs rename to crates/oxc_resolver/src/tests/extension_alias.rs index 11ccb011b..7b0968fd4 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/extension_alias.rs +++ b/crates/oxc_resolver/src/tests/extension_alias.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; -use oxc_resolver::{Resolution, ResolveError, ResolveOptions, Resolver}; +use crate::{Resolution, ResolveError, ResolveOptions, Resolver}; fn fixture() -> PathBuf { super::fixture().join("extension-alias") diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/extensions.rs b/crates/oxc_resolver/src/tests/extensions.rs similarity index 97% rename from crates/oxc_resolver/tests/enhanced_resolve/test/extensions.rs rename to crates/oxc_resolver/src/tests/extensions.rs index 77d6c2615..8fa9166f5 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/extensions.rs +++ b/crates/oxc_resolver/src/tests/extensions.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; -use oxc_resolver::{Resolution, ResolveError, ResolveOptions, Resolver}; +use crate::{Resolution, ResolveError, ResolveOptions, Resolver}; fn fixture() -> PathBuf { super::fixture().join("extensions") diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/fallback.rs b/crates/oxc_resolver/src/tests/fallback.rs similarity index 97% rename from crates/oxc_resolver/tests/enhanced_resolve/test/fallback.rs rename to crates/oxc_resolver/src/tests/fallback.rs index 3f2c9cbae..18c250793 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/fallback.rs +++ b/crates/oxc_resolver/src/tests/fallback.rs @@ -2,9 +2,9 @@ use std::path::{Path, PathBuf}; -use oxc_resolver::{AliasValue, Resolution, ResolveError, ResolveOptions, ResolverGeneric}; +use crate::{AliasValue, Resolution, ResolveError, ResolveOptions, ResolverGeneric}; -use crate::MemoryFS; +use super::memory_fs::MemoryFS; #[test] #[cfg(not(target_os = "windows"))] // MemoryFS's path separator is always `/` so the test will not pass in windows. diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/imports_field.rs b/crates/oxc_resolver/src/tests/imports_field.rs similarity index 99% rename from crates/oxc_resolver/tests/enhanced_resolve/test/imports_field.rs rename to crates/oxc_resolver/src/tests/imports_field.rs index 7ea4658b7..d57fac1fa 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/imports_field.rs +++ b/crates/oxc_resolver/src/tests/imports_field.rs @@ -4,7 +4,7 @@ use serde_json::json; -use oxc_resolver::{MatchObject, PathUtil, Resolution, ResolveError, ResolveOptions, Resolver}; +use crate::{MatchObject, PathUtil, Resolution, ResolveError, ResolveOptions, Resolver}; use std::path::Path; #[test] diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/incorrect_description_file.rs b/crates/oxc_resolver/src/tests/incorrect_description_file.rs similarity index 66% rename from crates/oxc_resolver/tests/enhanced_resolve/test/incorrect_description_file.rs rename to crates/oxc_resolver/src/tests/incorrect_description_file.rs index 62ecd65bd..1be994db2 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/incorrect_description_file.rs +++ b/crates/oxc_resolver/src/tests/incorrect_description_file.rs @@ -1,8 +1,8 @@ //! -use std::path::PathBuf; +use std::{env, path::PathBuf}; -use oxc_resolver::{JSONError, ResolveError, Resolver}; +use crate::{JSONError, Resolution, ResolveError, ResolveOptions, Resolver}; fn fixture() -> PathBuf { super::fixture().join("incorrect-package") @@ -45,3 +45,21 @@ fn incorrect_description_file_3() { let resolution = Resolver::default().resolve(f.join("pack2"), "."); assert!(resolution.is_err()); } + +// `enhanced_resolve` does not have this test case +#[test] +fn no_description_file() { + let f = env::current_dir().unwrap().join("tests/enhanced_resolve"); + + // has description file + let resolver = Resolver::default(); + assert_eq!( + resolver.resolve(&f, ".").map(Resolution::into_path_buf), + Ok(f.join("lib/index.js")) + ); + + // without description file + let resolver = + Resolver::new(ResolveOptions { description_files: vec![], ..ResolveOptions::default() }); + assert_eq!(resolver.resolve(&f, "."), Err(ResolveError::NotFound(f.into_boxed_path()))); +} diff --git a/crates/oxc_resolver/tests/memory_fs.rs b/crates/oxc_resolver/src/tests/memory_fs.rs similarity index 97% rename from crates/oxc_resolver/tests/memory_fs.rs rename to crates/oxc_resolver/src/tests/memory_fs.rs index eeb924fb5..8d2790f76 100644 --- a/crates/oxc_resolver/tests/memory_fs.rs +++ b/crates/oxc_resolver/src/tests/memory_fs.rs @@ -3,7 +3,7 @@ use std::{ path::{Path, PathBuf}, }; -use oxc_resolver::{FileMetadata, FileSystem}; +use crate::{FileMetadata, FileSystem}; pub struct MemoryFS { fs: vfs::MemoryFS, diff --git a/crates/oxc_resolver/src/tests/mod.rs b/crates/oxc_resolver/src/tests/mod.rs new file mode 100644 index 000000000..e8ec0a53f --- /dev/null +++ b/crates/oxc_resolver/src/tests/mod.rs @@ -0,0 +1,37 @@ +mod alias; +mod browser_field; +mod exports_field; +mod extension_alias; +mod extensions; +mod fallback; +mod imports_field; +mod incorrect_description_file; +mod memory_fs; +mod resolve; +mod roots; +mod scoped_packages; +mod simple; +mod symlink; + +use crate::Resolver; +use std::{env, path::PathBuf, sync::Arc, thread}; + +pub fn fixture() -> PathBuf { + env::current_dir().unwrap().join("tests/enhanced_resolve/test/fixtures") +} + +#[test] +fn threaded_environment() { + let cwd = env::current_dir().unwrap(); + let resolver = Arc::new(Resolver::default()); + for _ in 0..2 { + _ = thread::spawn({ + let cwd = cwd.clone(); + let resolver = Arc::clone(&resolver); + move || { + _ = resolver.resolve(cwd, "."); + } + }) + .join(); + } +} diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/resolve.rs b/crates/oxc_resolver/src/tests/resolve.rs similarity index 98% rename from crates/oxc_resolver/tests/enhanced_resolve/test/resolve.rs rename to crates/oxc_resolver/src/tests/resolve.rs index 74549e9e0..3a0493fcf 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/resolve.rs +++ b/crates/oxc_resolver/src/tests/resolve.rs @@ -1,6 +1,6 @@ //! -use oxc_resolver::{Resolution, ResolveOptions, Resolver}; +use crate::{Resolution, ResolveOptions, Resolver}; #[test] fn resolve() { diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/roots.rs b/crates/oxc_resolver/src/tests/roots.rs similarity index 96% rename from crates/oxc_resolver/tests/enhanced_resolve/test/roots.rs rename to crates/oxc_resolver/src/tests/roots.rs index 8983fc0a8..5c97d0c81 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/roots.rs +++ b/crates/oxc_resolver/src/tests/roots.rs @@ -2,7 +2,7 @@ use std::env; -use oxc_resolver::{AliasValue, Resolution, ResolveError, ResolveOptions, Resolver}; +use crate::{AliasValue, Resolution, ResolveError, ResolveOptions, Resolver}; #[test] fn roots() { diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/scoped_packages.rs b/crates/oxc_resolver/src/tests/scoped_packages.rs similarity index 94% rename from crates/oxc_resolver/tests/enhanced_resolve/test/scoped_packages.rs rename to crates/oxc_resolver/src/tests/scoped_packages.rs index 62ef6434a..a18d516e6 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/scoped_packages.rs +++ b/crates/oxc_resolver/src/tests/scoped_packages.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; -use oxc_resolver::{Resolution, ResolveOptions, Resolver}; +use crate::{Resolution, ResolveOptions, Resolver}; fn fixture() -> PathBuf { super::fixture().join("scoped") diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/simple.rs b/crates/oxc_resolver/src/tests/simple.rs similarity index 96% rename from crates/oxc_resolver/tests/enhanced_resolve/test/simple.rs rename to crates/oxc_resolver/src/tests/simple.rs index 105ef737d..4151de079 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/simple.rs +++ b/crates/oxc_resolver/src/tests/simple.rs @@ -2,7 +2,7 @@ use std::env; -use oxc_resolver::Resolver; +use crate::Resolver; #[test] fn simple() { diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/symlink.rs b/crates/oxc_resolver/src/tests/symlink.rs similarity index 98% rename from crates/oxc_resolver/tests/enhanced_resolve/test/symlink.rs rename to crates/oxc_resolver/src/tests/symlink.rs index 75f531fe1..145f0501b 100644 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/symlink.rs +++ b/crates/oxc_resolver/src/tests/symlink.rs @@ -1,6 +1,6 @@ use std::{env, fs, io, path::Path}; -use oxc_resolver::{Resolution, ResolveOptions, Resolver}; +use crate::{Resolution, ResolveOptions, Resolver}; #[derive(Debug, Clone, Copy)] enum FileType { diff --git a/crates/oxc_resolver/tests/description_file.rs b/crates/oxc_resolver/tests/description_file.rs deleted file mode 100644 index 0b425edb0..000000000 --- a/crates/oxc_resolver/tests/description_file.rs +++ /dev/null @@ -1,23 +0,0 @@ -//! `enhanced_resolve` does not have any tests for description files, -//! this is added for completeness. - -use std::env; - -use oxc_resolver::{Resolution, ResolveError, ResolveOptions, Resolver}; - -#[test] -fn no_description_file() { - let f = env::current_dir().unwrap().join("tests/enhanced_resolve"); - - // has description file - let resolver = Resolver::default(); - assert_eq!( - resolver.resolve(&f, ".").map(Resolution::into_path_buf), - Ok(f.join("lib/index.js")) - ); - - // without description file - let resolver = - Resolver::new(ResolveOptions { description_files: vec![], ..ResolveOptions::default() }); - assert_eq!(resolver.resolve(&f, "."), Err(ResolveError::NotFound(f.into_boxed_path()))); -} diff --git a/crates/oxc_resolver/tests/enhanced_resolve/mod.rs b/crates/oxc_resolver/tests/enhanced_resolve/mod.rs deleted file mode 100644 index 585722915..000000000 --- a/crates/oxc_resolver/tests/enhanced_resolve/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod test; diff --git a/crates/oxc_resolver/tests/enhanced_resolve/test/mod.rs b/crates/oxc_resolver/tests/enhanced_resolve/test/mod.rs deleted file mode 100644 index f0e2516c6..000000000 --- a/crates/oxc_resolver/tests/enhanced_resolve/test/mod.rs +++ /dev/null @@ -1,19 +0,0 @@ -mod alias; -mod browser_field; -mod exports_field; -mod extension_alias; -mod extensions; -mod fallback; -mod imports_field; -mod incorrect_description_file; -mod resolve; -mod roots; -mod scoped_packages; -mod simple; -mod symlink; - -use std::{env, path::PathBuf}; - -pub fn fixture() -> PathBuf { - env::current_dir().unwrap().join("tests/enhanced_resolve/test/fixtures") -} diff --git a/crates/oxc_resolver/tests/mod.rs b/crates/oxc_resolver/tests/mod.rs deleted file mode 100644 index 3df6fd24a..000000000 --- a/crates/oxc_resolver/tests/mod.rs +++ /dev/null @@ -1,24 +0,0 @@ -mod description_file; -mod enhanced_resolve; -mod memory_fs; - -use std::{env, sync::Arc, thread}; - -pub(crate) use memory_fs::MemoryFS; -use oxc_resolver::Resolver; - -#[test] -fn threaded_environment() { - let cwd = env::current_dir().unwrap(); - let resolver = Arc::new(Resolver::default()); - for _ in 0..2 { - _ = thread::spawn({ - let cwd = cwd.clone(); - let resolver = Arc::clone(&resolver); - move || { - _ = resolver.resolve(cwd, "."); - } - }) - .join(); - } -}