windows: Fix tests on Windows (#22616)

Release Notes:

- N/A

---------

Co-authored-by: Mikayla <mikayla.c.maki@gmail.com>
This commit is contained in:
张小白 2025-02-05 22:30:09 +08:00 committed by GitHub
parent c252b5db16
commit 74c4dbd237
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
56 changed files with 1540 additions and 856 deletions

View file

@ -23,6 +23,7 @@ pub trait PathExt {
fn compact(&self) -> PathBuf;
fn icon_stem_or_suffix(&self) -> Option<&str>;
fn extension_or_hidden_file_name(&self) -> Option<&str>;
fn to_sanitized_string(&self) -> String;
fn try_from_bytes<'a>(bytes: &'a [u8]) -> anyhow::Result<Self>
where
Self: From<&'a Path>,
@ -94,6 +95,20 @@ impl<T: AsRef<Path>> PathExt for T {
self.as_ref().file_name()?.to_str()?.split('.').last()
}
/// Returns a sanitized string representation of the path.
/// Note, on Windows, this assumes that the path is a valid UTF-8 string and
/// is not a UNC path.
fn to_sanitized_string(&self) -> String {
#[cfg(target_os = "windows")]
{
self.as_ref().to_string_lossy().replace("/", "\\")
}
#[cfg(not(target_os = "windows"))]
{
self.as_ref().to_string_lossy().to_string()
}
}
}
/// Due to the issue of UNC paths on Windows, which can cause bugs in various parts of Zed, introducing this `SanitizedPath`
@ -115,6 +130,17 @@ impl SanitizedPath {
self.0.to_string_lossy().to_string()
}
pub fn to_glob_string(&self) -> String {
#[cfg(target_os = "windows")]
{
self.0.to_string_lossy().replace("/", "\\")
}
#[cfg(not(target_os = "windows"))]
{
self.0.to_string_lossy().to_string()
}
}
pub fn join(&self, path: &Self) -> Self {
self.0.join(&path.0).into()
}
@ -448,14 +474,6 @@ pub fn compare_paths(
}
}
#[cfg(any(test, feature = "test-support"))]
pub fn replace_path_separator(path: &str) -> String {
#[cfg(target_os = "windows")]
return path.replace("/", std::path::MAIN_SEPARATOR_STR);
#[cfg(not(target_os = "windows"))]
return path.to_string();
}
#[cfg(test)]
mod tests {
use super::*;