From 1a6a261cac15d970428c77cb603bf60700d78b00 Mon Sep 17 00:00:00 2001 From: Dexoron Date: Thu, 23 Jul 2026 21:35:59 +0800 Subject: [PATCH] fix(ci): fix tests --- src/core/deps/register.rs | 55 ++++++++++++++++++++++++++++++++++----- tests/cli_deps.rs | 24 +++++++++++------ 2 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/core/deps/register.rs b/src/core/deps/register.rs index ccea2c2..e03a175 100644 --- a/src/core/deps/register.rs +++ b/src/core/deps/register.rs @@ -85,12 +85,7 @@ pub fn package_root_from_registry_info(pkg_info: &JsonValue) -> Result Result PathBuf { + let s = raw.trim(); + if let Some(rest) = s.strip_prefix("file://") { + if rest.len() >= 3 && rest.as_bytes()[0] == b'/' && rest.as_bytes()[2] == b':' { + return PathBuf::from(&rest[1..]); + } + if rest.len() >= 2 && rest.as_bytes()[1] == b':' { + return PathBuf::from(rest); + } + if rest.starts_with('/') { + return PathBuf::from(rest); + } + return PathBuf::from(format!("/{rest}")); + } + + let mut s = s.replace('\\', "/"); + if let Some(rest) = s.strip_prefix("//?/") { + s = rest.to_string(); + } else if let Some(rest) = s.strip_prefix("//./") { + s = rest.to_string(); + } + if s.len() >= 3 && s.as_bytes()[0] == b'/' && s.as_bytes()[2] == b':' { + s = s[1..].to_string(); + } + + let path = PathBuf::from(&s); + if path.is_absolute() || (s.len() >= 2 && s.as_bytes()[1] == b':') || s.starts_with('/') { + path + } else { + get_registry_cache_root().join(path) + } +} + pub fn registry_include_dir(dep_root: &Path) -> PathBuf { dep_root.join("target").join("include") } @@ -244,4 +272,19 @@ mod tests { ); let _ = std::fs::remove_dir_all(&root); } + + #[test] + fn registry_path_strips_windows_extended_prefix() { + let p = registry_path_to_pathbuf("//?/D:/cache/mylib"); + let s = p.to_string_lossy().replace('\\', "/"); + assert_eq!(s, "D:/cache/mylib"); + + let p2 = registry_path_to_pathbuf("/D:/cache/mylib"); + let s2 = p2.to_string_lossy().replace('\\', "/"); + assert_eq!(s2, "D:/cache/mylib"); + + let p3 = registry_path_to_pathbuf("file:///D:/cache/mylib"); + let s3 = p3.to_string_lossy().replace('\\', "/"); + assert_eq!(s3, "D:/cache/mylib"); + } } diff --git a/tests/cli_deps.rs b/tests/cli_deps.rs index 29687a1..146f7a7 100644 --- a/tests/cli_deps.rs +++ b/tests/cli_deps.rs @@ -117,16 +117,24 @@ fn registry_dependency_is_built_from_cache() { "[registry.local]\nurl = \"file://local\"\npriority = 1\n", ) .expect("failed to write registry config"); - let dep_path = dep - .canonicalize() - .unwrap_or(dep.clone()) - .to_string_lossy() - .replace('\\', "/"); + let dep_abs = dep.canonicalize().unwrap_or(dep.clone()); + let mut dep_path = dep_abs.to_string_lossy().replace('\\', "/"); + if let Some(rest) = dep_path.strip_prefix("//?/") { + dep_path = rest.to_string(); + } + if dep_path.len() >= 3 && dep_path.as_bytes()[0] == b'/' && dep_path.as_bytes()[2] == b':' { + dep_path = dep_path[1..].to_string(); + } std::fs::write( dcr_home.join("index.json"), - format!( - "{{\"packages\":[{{\"name\":\"mylib\",\"latest_version\":\"0.1.0\",\"path\":\"{dep_path}\"}}]}}" - ), + serde_json::json!({ + "packages": [{ + "name": "mylib", + "latest_version": "0.1.0", + "path": dep_path + }] + }) + .to_string(), ) .expect("failed to write registry index");