mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(resolver): clean up the tests a little bit
This commit is contained in:
parent
fdded5e97c
commit
08a9ba3d5e
10 changed files with 34 additions and 63 deletions
|
|
@ -14,10 +14,10 @@ categories.workspace = true
|
|||
|
||||
[dependencies]
|
||||
dashmap = { workspace = true }
|
||||
serde = { workspace = true, features = ["derive"] }
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
rustc-hash = { workspace = true }
|
||||
indexmap = { workspace = true, features = ["serde"] }
|
||||
indexmap = { workspace = true, features = ["serde"] } # serde for Deserialize from package.json
|
||||
dunce = "1.0.4"
|
||||
# Use `std::sync::OnceLock::get_or_try_init` when it is stable.
|
||||
once_cell = "1.18.0"
|
||||
|
|
|
|||
|
|
@ -1,16 +1,10 @@
|
|||
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/browserField.test.js>
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{ResolveError, ResolveOptions, Resolver};
|
||||
|
||||
fn fixture() -> PathBuf {
|
||||
super::fixture().join("browser-module")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ignore() {
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("browser-module");
|
||||
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
alias_fields: vec!["browser".into(), "innerBrowser1".into(), "innerBrowser2".into()],
|
||||
|
|
@ -34,7 +28,7 @@ fn ignore() {
|
|||
|
||||
#[test]
|
||||
fn replace_file() {
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("browser-module");
|
||||
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
alias_fields: vec!["browser".into()],
|
||||
|
|
|
|||
|
|
@ -76,8 +76,8 @@ fn test() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
// resolve using exports field, not a browser field #1
|
||||
#[test]
|
||||
fn exports_not_browser_field1() {
|
||||
let f = super::fixture().join("exports-field");
|
||||
|
||||
|
|
@ -92,8 +92,8 @@ fn exports_not_browser_field1() {
|
|||
assert_eq!(resolved_path, Ok(f.join("node_modules/exports-field/lib/lib2/main.js")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
// resolve using exports field and a browser alias field #2
|
||||
#[test]
|
||||
fn exports_not_browser_field2() {
|
||||
let f2 = super::fixture().join("exports-field2");
|
||||
|
||||
|
|
@ -108,8 +108,8 @@ fn exports_not_browser_field2() {
|
|||
assert_eq!(resolved_path, Ok(f2.join("node_modules/exports-field/lib/browser.js")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
// should resolve extension without fullySpecified
|
||||
#[test]
|
||||
fn extension_without_fully_specified() {
|
||||
let f2 = super::fixture().join("exports-field2");
|
||||
|
||||
|
|
|
|||
|
|
@ -1,16 +1,10 @@
|
|||
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/extension-alias.test.js>
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{ResolveError, ResolveOptions, Resolver};
|
||||
|
||||
fn fixture() -> PathBuf {
|
||||
super::fixture().join("extension-alias")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extension_alias() {
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("extension-alias");
|
||||
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
extensions: vec![".js".into()],
|
||||
|
|
@ -46,17 +40,17 @@ fn extension_alias() {
|
|||
}
|
||||
}
|
||||
|
||||
// should not apply extension alias to extensions or mainFiles field
|
||||
#[test]
|
||||
fn not_apply_to_extension_nor_main_files() {
|
||||
// should not apply extension alias to extensions or mainFiles field
|
||||
let options = ResolveOptions {
|
||||
let f = super::fixture().join("extension-alias");
|
||||
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
extensions: vec![".js".into()],
|
||||
main_files: vec!["index.js".into()],
|
||||
extension_alias: vec![(".js".into(), vec![])],
|
||||
..ResolveOptions::default()
|
||||
};
|
||||
let resolver = Resolver::new(options);
|
||||
let f = fixture();
|
||||
});
|
||||
|
||||
#[rustfmt::skip]
|
||||
let pass = [
|
||||
|
|
|
|||
|
|
@ -1,16 +1,10 @@
|
|||
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/extensions.test.js>
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{EnforceExtension, Resolution, ResolveError, ResolveOptions, Resolver};
|
||||
|
||||
fn fixture() -> PathBuf {
|
||||
super::fixture().join("extensions")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn extensions() {
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("extensions");
|
||||
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
extensions: vec![".ts".into(), ".js".into()],
|
||||
|
|
@ -44,10 +38,10 @@ fn extensions() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
// should default enforceExtension to true when extensions includes an empty string
|
||||
#[test]
|
||||
fn default_enforce_extension() {
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("extensions");
|
||||
|
||||
let resolved = Resolver::new(ResolveOptions {
|
||||
extensions: vec![".ts".into(), String::new(), ".js".into()],
|
||||
|
|
@ -59,10 +53,10 @@ fn default_enforce_extension() {
|
|||
// TODO: need to match missingDependencies returned from the resolve function
|
||||
}
|
||||
|
||||
#[test]
|
||||
// should respect enforceExtension when extensions includes an empty string
|
||||
#[test]
|
||||
fn respect_enforce_extension() {
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("extensions");
|
||||
|
||||
let resolved = Resolver::new(ResolveOptions {
|
||||
enforce_extension: EnforceExtension::Disabled,
|
||||
|
|
@ -77,7 +71,7 @@ fn respect_enforce_extension() {
|
|||
// Test for `.d.ts`, not part of enhanced-resolve.
|
||||
#[test]
|
||||
fn multi_dot_extension() {
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("extensions");
|
||||
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
extensions: vec![".a.b.c".into(), ".d.ts".into(), ".ts".into(), ".js".into()],
|
||||
|
|
|
|||
|
|
@ -1,19 +1,15 @@
|
|||
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/incorrect-description-file.test.js>
|
||||
|
||||
use std::{env, path::PathBuf};
|
||||
use std::env;
|
||||
|
||||
use crate::{JSONError, Resolution, ResolveError, ResolveOptions, Resolver};
|
||||
|
||||
fn fixture() -> PathBuf {
|
||||
super::fixture().join("incorrect-package")
|
||||
}
|
||||
|
||||
// TODO: add `ctx with fileDependencies and then check file dependencies
|
||||
|
||||
// should not resolve main in incorrect description file #1
|
||||
#[test]
|
||||
fn incorrect_description_file_1() {
|
||||
// should not resolve main in incorrect description file #1
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("incorrect-package");
|
||||
let resolution = Resolver::default().resolve(f.join("pack1"), ".");
|
||||
let error = ResolveError::JSON(JSONError {
|
||||
path: f.join("pack1/package.json"),
|
||||
|
|
@ -24,10 +20,10 @@ fn incorrect_description_file_1() {
|
|||
assert_eq!(resolution, Err(error));
|
||||
}
|
||||
|
||||
// should not resolve main in incorrect description file #2
|
||||
#[test]
|
||||
fn incorrect_description_file_2() {
|
||||
// should not resolve main in incorrect description file #2
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("incorrect-package");
|
||||
let resolution = Resolver::default().resolve(f.join("pack2"), ".");
|
||||
let error = ResolveError::JSON(JSONError {
|
||||
path: f.join("pack2/package.json"),
|
||||
|
|
@ -38,10 +34,10 @@ fn incorrect_description_file_2() {
|
|||
assert_eq!(resolution, Err(error));
|
||||
}
|
||||
|
||||
// should not resolve main in incorrect description file #3
|
||||
#[test]
|
||||
fn incorrect_description_file_3() {
|
||||
// should not resolve main in incorrect description file #3
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("incorrect-package");
|
||||
let resolution = Resolver::default().resolve(f.join("pack2"), ".");
|
||||
assert!(resolution.is_err());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ use crate::{ResolveError, ResolveOptions, Resolver, Restriction};
|
|||
// * should try to find alternative #2
|
||||
// * should try to find alternative #3
|
||||
|
||||
#[test]
|
||||
// should respect string restriction
|
||||
#[test]
|
||||
fn restriction1() {
|
||||
let fixture = super::fixture();
|
||||
let f = fixture.join("restrictions");
|
||||
|
|
|
|||
|
|
@ -60,7 +60,6 @@ fn resolve_to_context() {
|
|||
#[test]
|
||||
fn prefer_absolute() {
|
||||
let f = super::fixture();
|
||||
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
extensions: vec![".js".into()],
|
||||
alias: vec![("foo".into(), vec![AliasValue::Path("/fixtures".into())])],
|
||||
|
|
|
|||
|
|
@ -1,16 +1,10 @@
|
|||
//! <https://github.com/webpack/enhanced-resolve/blob/main/test/scoped-packages.test.js>
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{ResolveOptions, Resolver};
|
||||
|
||||
fn fixture() -> PathBuf {
|
||||
super::fixture().join("scoped")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scoped_packages() {
|
||||
let f = fixture();
|
||||
let f = super::fixture().join("scoped");
|
||||
|
||||
let resolver = Resolver::new(ResolveOptions {
|
||||
alias_fields: vec!["browser".into()],
|
||||
|
|
|
|||
|
|
@ -7,20 +7,20 @@ use crate::Resolver;
|
|||
#[test]
|
||||
fn simple() {
|
||||
// mimic `enhanced-resolve/test/simple.test.js`
|
||||
let f = env::current_dir().unwrap().join("tests/enhanced_resolve/test/");
|
||||
let dirname = env::current_dir().unwrap().join("tests");
|
||||
let f = dirname.join("enhanced_resolve/test");
|
||||
|
||||
let resolver = Resolver::default();
|
||||
|
||||
let data = [
|
||||
("direct", f.clone(), "../lib/index"),
|
||||
("as directory", f.clone(), ".."),
|
||||
("as module", f.join("../../").canonicalize().unwrap(), "./enhanced_resolve"),
|
||||
("as directory", f, ".."),
|
||||
("as module", dirname.clone(), "./enhanced_resolve"),
|
||||
];
|
||||
|
||||
for (comment, path, request) in data {
|
||||
let resolved_path =
|
||||
resolver.resolve(&path, request).map(|f| f.full_path().canonicalize().unwrap());
|
||||
let expected = f.join("../lib/index.js").canonicalize().unwrap();
|
||||
let resolved_path = resolver.resolve(&path, request).map(|f| f.full_path());
|
||||
let expected = dirname.join("enhanced_resolve/lib/index.js");
|
||||
assert_eq!(resolved_path, Ok(expected), "{comment} {path:?} {request}");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue