Skip to content

Commit f12ba84

Browse files
committed
Fixes relative drive paths
1 parent 92470fe commit f12ba84

File tree

6 files changed

+57
-31
lines changed

6 files changed

+57
-31
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
target
1+
target

Cargo.lock

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ concurrent_lru = "^0.2"
1313
fancy-regex = { version = "^0.16.0", default-features = false, features = ["std"] }
1414
miniz_oxide = "^0.8"
1515
mmap-rs = { version = "^0.6", optional = true }
16-
path-slash = "0.2.1"
1716
pathdiff = "^0.2"
1817
radix_trie = "0.2.1"
1918
serde = { version = "1", features = ["derive"] }

fixtures/global-cache/.yarnrc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
enableGlobalCache: true
22

3+
globalFolder: "D:\\yarn-global"
4+
35
nodeLinker: pnp

src/lib_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,7 @@ mod tests {
212212
panic!("Unexpected resolve failed");
213213
}
214214
}
215+
216+
panic!("test");
215217
}
216218
}

src/util.rs

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use fancy_regex::Regex;
22
use serde::{Deserialize, Deserializer, de::Error};
3-
use std::{borrow::Cow, path::Component};
3+
use std::borrow::Cow;
44

5-
use path_slash::PathBufExt;
65
use std::path::{MAIN_SEPARATOR_STR, Path, PathBuf};
76

87
#[derive(Debug, Default, Clone)]
@@ -33,35 +32,61 @@ impl<T> Trie<T> {
3332
}
3433
}
3534

36-
fn clean_path(path: PathBuf) -> PathBuf {
37-
let mut out = Vec::new();
35+
pub fn normalize_path<P: AsRef<str>>(original: P) -> String {
36+
let original_str
37+
= original.as_ref();
38+
39+
let check_str_root
40+
= original_str.strip_prefix("/");
41+
let str_minus_root
42+
= check_str_root.unwrap_or(original_str);
43+
44+
let components
45+
= str_minus_root.split(&['/', '\\'][..]);
46+
47+
let mut out: Vec<&str>
48+
= Vec::new();
3849

39-
for comp in path.components() {
40-
println!("Component: {:?}", comp);
50+
for comp in components {
4151
match comp {
42-
Component::CurDir => (),
43-
Component::ParentDir => match out.last() {
44-
Some(Component::RootDir) => (),
45-
Some(Component::Normal(_)) => {
52+
"" | "." => {
53+
// Those components don't progress the path
54+
},
55+
56+
".." => match out.last() {
57+
None if check_str_root.is_some() => {
58+
// No need to add a ".." since we're already at the root
59+
},
60+
61+
Some(&"..") | None => {
62+
out.push(comp);
63+
},
64+
65+
Some(_) => {
4666
out.pop();
47-
}
48-
None
49-
| Some(Component::CurDir)
50-
| Some(Component::ParentDir)
51-
| Some(Component::Prefix(_)) => out.push(comp),
67+
},
68+
},
69+
70+
comp => {
71+
out.push(comp)
5272
},
53-
comp => out.push(comp),
5473
}
5574
}
5675

57-
if !out.is_empty() { out.iter().collect() } else { PathBuf::from(".") }
58-
}
76+
if check_str_root.is_some() {
77+
if out.is_empty() {
78+
return "/".to_string();
79+
} else {
80+
out.insert(0, "");
81+
}
82+
}
5983

60-
pub fn normalize_path<P: AsRef<str>>(original: P) -> String {
61-
let original_str = original.as_ref();
84+
let mut str
85+
= out.join("/");
6286

63-
let p = PathBuf::from(original_str);
64-
let mut str = clean_path(p).to_slash_lossy().to_string();
87+
if out.is_empty() {
88+
return ".".to_string();
89+
}
6590

6691
if (original_str.ends_with('/') || original_str.ends_with(MAIN_SEPARATOR_STR))
6792
&& !str.ends_with('/')
@@ -85,12 +110,17 @@ mod tests {
85110
assert_eq!(normalize_path("foo//bar"), "foo/bar");
86111
assert_eq!(normalize_path("foo/./bar"), "foo/bar");
87112
assert_eq!(normalize_path("foo/../bar"), "bar");
113+
assert_eq!(normalize_path("foo/..//bar"), "bar");
88114
assert_eq!(normalize_path("foo/bar/.."), "foo");
89115
assert_eq!(normalize_path("foo/../../bar"), "../bar");
90116
assert_eq!(normalize_path("../foo/../../bar"), "../../bar");
91117
assert_eq!(normalize_path("./foo"), "foo");
92118
assert_eq!(normalize_path("../foo"), "../foo");
119+
assert_eq!(normalize_path("../D:/foo"), "../D:/foo");
93120
assert_eq!(normalize_path("/foo/bar"), "/foo/bar");
121+
assert_eq!(normalize_path("/../foo/bar"), "/foo/bar");
122+
assert_eq!(normalize_path("/foo/../../bar/baz"), "/bar/baz");
123+
assert_eq!(normalize_path("/../foo/bar"), "/foo/bar");
94124
assert_eq!(normalize_path("/foo/bar/"), "/foo/bar/");
95125
}
96126
}

0 commit comments

Comments
 (0)