mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(resolver): change internal funcs to non-pub by moving to unit tests (#682)
This commit is contained in:
parent
2e3934db49
commit
702d5b0120
22 changed files with 82 additions and 97 deletions
|
|
@ -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~
|
||||
|
|
|
|||
|
|
@ -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<Fs: FileSystem> ResolverGeneric<Fs> {
|
|||
}
|
||||
|
||||
/// 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<Fs: FileSystem> ResolverGeneric<Fs> {
|
|||
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<Fs: FileSystem> ResolverGeneric<Fs> {
|
|||
}
|
||||
|
||||
/// 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,
|
||||
|
|
|
|||
|
|
@ -75,7 +75,6 @@ impl ExportsField {
|
|||
pub enum ExportsKey {
|
||||
Main,
|
||||
Pattern(String),
|
||||
Hash(String),
|
||||
CustomCondition(String),
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
@ -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")
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -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")
|
||||
|
|
@ -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")
|
||||
|
|
@ -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.
|
||||
|
|
@ -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]
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/incorrect-description-file.test.js>
|
||||
|
||||
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())));
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@ use std::{
|
|||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use oxc_resolver::{FileMetadata, FileSystem};
|
||||
use crate::{FileMetadata, FileSystem};
|
||||
|
||||
pub struct MemoryFS {
|
||||
fs: vfs::MemoryFS,
|
||||
37
crates/oxc_resolver/src/tests/mod.rs
Normal file
37
crates/oxc_resolver/src/tests/mod.rs
Normal file
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/resolve.test.js>
|
||||
|
||||
use oxc_resolver::{Resolution, ResolveOptions, Resolver};
|
||||
use crate::{Resolution, ResolveOptions, Resolver};
|
||||
|
||||
#[test]
|
||||
fn resolve() {
|
||||
|
|
@ -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() {
|
||||
|
|
@ -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")
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
use std::env;
|
||||
|
||||
use oxc_resolver::Resolver;
|
||||
use crate::Resolver;
|
||||
|
||||
#[test]
|
||||
fn simple() {
|
||||
|
|
@ -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 {
|
||||
|
|
@ -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())));
|
||||
}
|
||||
|
|
@ -1 +0,0 @@
|
|||
mod test;
|
||||
|
|
@ -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")
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue