windows: Fix tests on Windows (#22616)
Release Notes: - N/A --------- Co-authored-by: Mikayla <mikayla.c.maki@gmail.com>
This commit is contained in:
@@ -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::*;
|
||||
|
||||
@@ -28,6 +28,8 @@ use unicase::UniCase;
|
||||
use anyhow::{anyhow, Context as _};
|
||||
|
||||
pub use take_until::*;
|
||||
#[cfg(any(test, feature = "test-support"))]
|
||||
pub use util_macros::{separator, uri};
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! debug_panic {
|
||||
@@ -41,6 +43,50 @@ macro_rules! debug_panic {
|
||||
};
|
||||
}
|
||||
|
||||
/// A macro to add "C:" to the beginning of a path literal on Windows, and replace all
|
||||
/// the separator from `/` to `\`.
|
||||
/// But on non-Windows platforms, it will return the path literal as is.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// use util::path;
|
||||
///
|
||||
/// let path = path!("/Users/user/file.txt");
|
||||
/// #[cfg(target_os = "windows")]
|
||||
/// assert_eq!(path, "C:\\Users\\user\\file.txt");
|
||||
/// #[cfg(not(target_os = "windows"))]
|
||||
/// assert_eq!(path, "/Users/user/file.txt");
|
||||
/// ```
|
||||
#[cfg(all(any(test, feature = "test-support"), target_os = "windows"))]
|
||||
#[macro_export]
|
||||
macro_rules! path {
|
||||
($path:literal) => {
|
||||
concat!("C:", util::separator!($path))
|
||||
};
|
||||
}
|
||||
|
||||
/// A macro to add "C:" to the beginning of a path literal on Windows, and replace all
|
||||
/// the separator from `/` to `\`.
|
||||
/// But on non-Windows platforms, it will return the path literal as is.
|
||||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// use util::path;
|
||||
///
|
||||
/// let path = path!("/Users/user/file.txt");
|
||||
/// #[cfg(target_os = "windows")]
|
||||
/// assert_eq!(path, "C:\\Users\\user\\file.txt");
|
||||
/// #[cfg(not(target_os = "windows"))]
|
||||
/// assert_eq!(path, "/Users/user/file.txt");
|
||||
/// ```
|
||||
#[cfg(all(any(test, feature = "test-support"), not(target_os = "windows")))]
|
||||
#[macro_export]
|
||||
macro_rules! path {
|
||||
($path:literal) => {
|
||||
$path
|
||||
};
|
||||
}
|
||||
|
||||
pub fn truncate(s: &str, max_chars: usize) -> &str {
|
||||
match s.char_indices().nth(max_chars) {
|
||||
None => s,
|
||||
|
||||
Reference in New Issue
Block a user