Skip to content

Commit 1c9529c

Browse files
committed
wip
1 parent d378451 commit 1c9529c

File tree

4 files changed

+91
-22
lines changed

4 files changed

+91
-22
lines changed

src/lib.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
mod util;
22

3-
use std::{path::{Path, PathBuf, Component}, fs, collections::{HashSet, HashMap}};
4-
use crate::util::RegexDef;
3+
use fancy_regex::Regex;
54
use lazy_static::lazy_static;
65
use radix_trie::Trie;
7-
use fancy_regex::Regex;
86
use serde::Deserialize;
97
use serde_with::{serde_as, DefaultOnNull};
108
use simple_error::{self, bail, SimpleError};
9+
use std::{path::{Path, PathBuf, Component}, fs, collections::{HashSet, HashMap}};
10+
use util::RegexDef;
1111

1212
pub enum Resolution {
1313
Specifier(String),
@@ -32,6 +32,7 @@ pub struct PnpResolutionConfig {
3232
pub host: PnpResolutionHost,
3333
}
3434

35+
#[derive(Clone)]
3536
#[derive(Deserialize)]
3637
pub struct PackageLocator {
3738
name: String,
@@ -47,6 +48,7 @@ enum PackageDependency {
4748
}
4849

4950
#[serde_as]
51+
#[derive(Clone)]
5052
#[derive(Deserialize)]
5153
#[serde(rename_all = "camelCase")]
5254
pub struct PackageInformation {
@@ -60,15 +62,13 @@ pub struct PackageInformation {
6062
}
6163

6264
#[serde_as]
65+
#[derive(Clone)]
6366
#[derive(Deserialize)]
6467
#[serde(rename_all = "camelCase")]
6568
pub struct Manifest {
6669
#[serde(skip_deserializing)]
6770
manifest_path: PathBuf,
6871

69-
#[serde(skip_deserializing)]
70-
fallback_dependencies: HashMap<String, Option<PackageDependency>>,
71-
7272
#[serde(skip_deserializing)]
7373
location_trie: Trie<PathBuf, PackageLocator>,
7474

@@ -79,7 +79,8 @@ pub struct Manifest {
7979
// "@app/monorepo",
8080
// "workspace:.",
8181
// ]]
82-
fallback_pool: Vec<PackageLocator>,
82+
#[serde_as(as = "Vec<(_, _)>")]
83+
fallback_pool: HashMap<String, Option<PackageDependency>>,
8384

8485
// fallbackExclusionList: [[
8586
// "@app/server",
@@ -214,18 +215,6 @@ pub fn init_pnp_manifest(manifest: &mut Manifest, p: &Path) {
214215

215216
manifest.manifest_path = p.to_owned();
216217

217-
for locator in manifest.fallback_pool.iter() {
218-
let info = manifest.package_registry_data
219-
.get(&locator.name)
220-
.expect("Assertion failed: The locator should be registered")
221-
.get(&locator.reference)
222-
.expect("Assertion failed: The locator should be registered");
223-
224-
for (name, dependency) in info.package_dependencies.iter() {
225-
manifest.fallback_dependencies.insert(name.clone(), dependency.clone());
226-
}
227-
}
228-
229218
for (name, ranges) in manifest.package_registry_data.iter_mut() {
230219
for (reference, info) in ranges.iter_mut() {
231220
if info.discard_from_lookup {
@@ -309,7 +298,7 @@ pub fn resolve_to_unqualified(specifier: &str, parent: &Path, config: &PnpResolu
309298
}
310299

311300
if !is_set && manifest.enable_top_level_fallback && !is_excluded_from_fallback(&manifest, parent_locator) {
312-
if let Some(fallback_resolution) = manifest.fallback_dependencies.get(&ident) {
301+
if let Some(fallback_resolution) = manifest.fallback_pool.get(&ident) {
313302
reference_or_alias = fallback_resolution.clone();
314303
is_set = true;
315304
}
@@ -339,3 +328,6 @@ pub fn resolve_to_unqualified(specifier: &str, parent: &Path, config: &PnpResolu
339328
Ok(Resolution::Specifier(specifier.to_string()))
340329
}
341330
}
331+
332+
#[cfg(test)]
333+
mod lib_tests;

src/lib_tests.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
use serde::Deserialize;
2+
3+
use crate::{pnp_resolve, PnpResolutionConfig, Resolution, Manifest};
4+
5+
6+
#[derive(Deserialize)]
7+
struct Test {
8+
it: String,
9+
imported: String,
10+
importer: String,
11+
expected: String,
12+
}
13+
14+
#[derive(Deserialize)]
15+
struct TestSuite {
16+
manifest: Manifest,
17+
tests: Vec<Test>,
18+
}
19+
20+
#[cfg(test)]
21+
mod tests {
22+
use std::{fs, path::PathBuf};
23+
24+
use crate::{init_pnp_manifest, resolve_to_unqualified, PnpResolutionHost};
25+
26+
// Note this useful idiom: importing names from outer (for mod tests) scope.
27+
use super::*;
28+
29+
#[test]
30+
fn test_add() {
31+
let expectations_path = std::env::current_dir()
32+
.expect("Assertion failed: Expected a valid current working directory")
33+
.join("testExpectations.json");
34+
35+
let manifest_content = fs::read_to_string(&expectations_path)
36+
.expect("Assertion failed: Expected the expectations to be found");
37+
38+
let mut test_suites: Vec<TestSuite> = serde_json::from_str(&manifest_content)
39+
.expect("Assertion failed: Expected the expectations to be loaded");
40+
41+
for test_suite in test_suites.iter_mut() {
42+
let manifest = &mut test_suite.manifest;
43+
init_pnp_manifest(manifest, &PathBuf::from("/path/to/project/.pnp.cjs"));
44+
45+
for test in test_suite.tests.iter() {
46+
let specifier = &test.imported;
47+
let parent = &PathBuf::from(&test.importer).join("fooo");
48+
49+
let manifest_copy = manifest.clone();
50+
51+
let host = PnpResolutionHost {
52+
find_pnp_manifest: Box::new(|_| Ok(Some(manifest_copy))),
53+
..Default::default()
54+
};
55+
56+
let config = PnpResolutionConfig {
57+
host,
58+
..Default::default()
59+
};
60+
61+
let resolution = resolve_to_unqualified(specifier, parent, &config);
62+
63+
match resolution {
64+
Ok(Resolution::Path(path)) => {
65+
assert_eq!(path.to_string_lossy(), test.expected);
66+
},
67+
Ok(Resolution::Specifier(specifier)) => {
68+
assert_eq!(specifier, test.expected);
69+
},
70+
Err(_) => {
71+
assert_eq!(test.expected, "error!");
72+
},
73+
}
74+
75+
}
76+
}
77+
}
78+
}

src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
mod util;
2-
31
use std::path::PathBuf;
42

53
use pnp_rs::{pnp_resolve, PnpResolutionConfig, Resolution};

src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ fn strip_slash_escape(str: &str) -> String {
2626
res
2727
}
2828

29+
#[derive(Clone)]
2930
pub struct RegexDef(pub Regex);
3031

3132
impl<'de> Deserialize<'de> for RegexDef {

0 commit comments

Comments
 (0)