Skip to content

Commit

Permalink
refactor(resolver): change internal funcs to non-pub by moving to uni…
Browse files Browse the repository at this point in the history
…t tests (#682)
  • Loading branch information
Boshen authored Aug 2, 2023
1 parent 2e3934d commit 702d5b0
Show file tree
Hide file tree
Showing 22 changed files with 82 additions and 97 deletions.
2 changes: 2 additions & 0 deletions crates/oxc_resolver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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~
Expand Down
18 changes: 8 additions & 10 deletions crates/oxc_resolver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ mod path;
mod request;
mod resolution;

#[cfg(test)]
mod tests;

use std::{
borrow::Cow,
cmp::Ordering,
Expand All @@ -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,
};

Expand Down Expand Up @@ -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,
Expand All @@ -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"),
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_resolver/src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ impl ExportsField {
pub enum ExportsKey {
Main,
Pattern(String),
Hash(String),
CustomCondition(String),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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())));
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
path::{Path, PathBuf},
};

use oxc_resolver::{FileMetadata, FileSystem};
use crate::{FileMetadata, FileSystem};

pub struct MemoryFS {
fs: vfs::MemoryFS,
Expand Down
37 changes: 37 additions & 0 deletions crates/oxc_resolver/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::env;

use oxc_resolver::Resolver;
use crate::Resolver;

#[test]
fn simple() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
23 changes: 0 additions & 23 deletions crates/oxc_resolver/tests/description_file.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/oxc_resolver/tests/enhanced_resolve/mod.rs

This file was deleted.

19 changes: 0 additions & 19 deletions crates/oxc_resolver/tests/enhanced_resolve/test/mod.rs

This file was deleted.

24 changes: 0 additions & 24 deletions crates/oxc_resolver/tests/mod.rs

This file was deleted.

0 comments on commit 702d5b0

Please sign in to comment.