nushell/crates/nu-path/src/assert_path_eq.rs
YizhePKU f184a77fe1
Path expansion no longer removes trailing slashes (#12662)
This PR changes `nu_path::expand_path_with()` to no longer remove
trailing slashes. It also fixes bugs in the current implementation due
to ineffective tests (Fixes #12602).
2024-05-01 17:28:54 -05:00

51 lines
1.9 KiB
Rust

//! Path equality in Rust is defined by comparing their `components()`. However,
//! `Path::components()` will perform its own normalization, which makes
//! `assert_eq!` not suitable testing.
//!
//! This module provides two macros, `assert_path_eq!` and `assert_path_ne!`,
//! which converts path to string before comparison. They accept PathBuf, Path,
//! String, and &str as parameters.
#[macro_export]
macro_rules! assert_path_eq {
($left:expr, $right:expr $(,)?) => {
assert_eq!(
AsRef::<Path>::as_ref(&$left).to_str().unwrap(),
AsRef::<Path>::as_ref(&$right).to_str().unwrap()
)
};
}
#[macro_export]
macro_rules! assert_path_ne {
($left:expr, $right:expr $(,)?) => {
assert_ne!(
AsRef::<Path>::as_ref(&$left).to_str().unwrap(),
AsRef::<Path>::as_ref(&$right).to_str().unwrap()
)
};
}
#[cfg(test)]
mod test {
use std::path::{Path, PathBuf};
#[test]
fn assert_path_eq_works() {
assert_path_eq!(PathBuf::from("/foo/bar"), Path::new("/foo/bar"));
assert_path_eq!(PathBuf::from("/foo/bar"), String::from("/foo/bar"));
assert_path_eq!(PathBuf::from("/foo/bar"), "/foo/bar");
assert_path_eq!(Path::new("/foo/bar"), String::from("/foo/bar"));
assert_path_eq!(Path::new("/foo/bar"), "/foo/bar");
assert_path_eq!(Path::new(r"\foo\bar"), r"\foo\bar");
assert_path_ne!(PathBuf::from("/foo/bar/."), Path::new("/foo/bar"));
assert_path_ne!(PathBuf::from("/foo/bar/."), String::from("/foo/bar"));
assert_path_ne!(PathBuf::from("/foo/bar/."), "/foo/bar");
assert_path_ne!(Path::new("/foo/./bar"), String::from("/foo/bar"));
assert_path_ne!(Path::new("/foo/./bar"), "/foo/bar");
assert_path_ne!(Path::new(r"\foo\bar"), r"/foo/bar");
assert_path_ne!(Path::new(r"/foo/bar"), r"\foo\bar");
}
}