Skip to content

Commit 8ea3f23

Browse files
authored
Rollup merge of #94649 - ChrisDenton:unix-absolute-fix, r=Dylan-DPC
Unix path::absolute: Fix leading "." component Testing leading `.` and `..` components were missing from the unix tests. This PR adds them and fixes the leading `.` case. It also fixes the test cases so that they do an exact comparison. This problem reported by ``@axetroy``
2 parents 02e8839 + 0421af9 commit 8ea3f23

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

library/std/src/path/tests.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -1710,15 +1710,23 @@ fn test_unix_absolute() {
17101710
let relative = "a/b";
17111711
let mut expected = crate::env::current_dir().unwrap();
17121712
expected.push(relative);
1713-
assert_eq!(absolute(relative).unwrap(), expected);
1713+
assert_eq!(absolute(relative).unwrap().as_os_str(), expected.as_os_str());
17141714

17151715
// Test how components are collected.
1716-
assert_eq!(absolute("/a/b/c").unwrap(), Path::new("/a/b/c"));
1717-
assert_eq!(absolute("/a//b/c").unwrap(), Path::new("/a/b/c"));
1718-
assert_eq!(absolute("//a/b/c").unwrap(), Path::new("//a/b/c"));
1719-
assert_eq!(absolute("///a/b/c").unwrap(), Path::new("/a/b/c"));
1720-
assert_eq!(absolute("/a/b/c/").unwrap(), Path::new("/a/b/c/"));
1721-
assert_eq!(absolute("/a/./b/../c/.././..").unwrap(), Path::new("/a/b/../c/../.."));
1716+
assert_eq!(absolute("/a/b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
1717+
assert_eq!(absolute("/a//b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
1718+
assert_eq!(absolute("//a/b/c").unwrap().as_os_str(), Path::new("//a/b/c").as_os_str());
1719+
assert_eq!(absolute("///a/b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
1720+
assert_eq!(absolute("/a/b/c/").unwrap().as_os_str(), Path::new("/a/b/c/").as_os_str());
1721+
assert_eq!(
1722+
absolute("/a/./b/../c/.././..").unwrap().as_os_str(),
1723+
Path::new("/a/b/../c/../..").as_os_str()
1724+
);
1725+
1726+
// Test leading `.` and `..` components
1727+
let curdir = crate::env::current_dir().unwrap();
1728+
assert_eq!(absolute("./a").unwrap().as_os_str(), curdir.join("a").as_os_str());
1729+
assert_eq!(absolute("../a").unwrap().as_os_str(), curdir.join("../a").as_os_str()); // return /pwd/../a
17221730
}
17231731

17241732
#[test]

library/std/src/sys/unix/path.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
2828
// See 4.13 Pathname Resolution, IEEE Std 1003.1-2017
2929
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
3030

31-
let mut components = path.components();
31+
// Get the components, skipping the redundant leading "." component if it exists.
32+
let mut components = path.strip_prefix(".").unwrap_or(path).components();
3233
let path_os = path.as_os_str().bytes();
3334

3435
let mut normalized = if path.is_absolute() {

0 commit comments

Comments
 (0)