Unix path::absolute: Fix leading "." component

Testing leading `.` and `..` components were missing from the unix tests.
This commit is contained in:
Chris Denton 2022-03-05 17:57:12 +00:00
parent c8a49fc902
commit e8b7371a23
No known key found for this signature in database
GPG Key ID: 713472F2F45627DE
2 changed files with 7 additions and 1 deletions

View File

@ -1719,6 +1719,11 @@ fn test_unix_absolute() {
assert_eq!(absolute("///a/b/c").unwrap(), Path::new("/a/b/c"));
assert_eq!(absolute("/a/b/c/").unwrap(), Path::new("/a/b/c/"));
assert_eq!(absolute("/a/./b/../c/.././..").unwrap(), Path::new("/a/b/../c/../.."));
// Test leading `.` and `..` components
let curdir = crate::env::current_dir().unwrap();
assert_eq!(absolute("./a").unwrap().as_os_str(), curdir.join("a").as_os_str());
assert_eq!(absolute("../a").unwrap().as_os_str(), curdir.join("../a").as_os_str()); // return /pwd/../a
}
#[test]

View File

@ -28,7 +28,8 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
// See 4.13 Pathname Resolution, IEEE Std 1003.1-2017
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
let mut components = path.components();
// Get the components, skipping the redundant leading "." component if it exists.
let mut components = path.strip_prefix(".").unwrap_or(path).components();
let path_os = path.as_os_str().bytes();
let mut normalized = if path.is_absolute() {