Skip to content

Commit e8b7371

Browse files
committed
Unix path::absolute: Fix leading "." component
Testing leading `.` and `..` components were missing from the unix tests.
1 parent c8a49fc commit e8b7371

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

library/std/src/path/tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,11 @@ fn test_unix_absolute() {
17191719
assert_eq!(absolute("///a/b/c").unwrap(), Path::new("/a/b/c"));
17201720
assert_eq!(absolute("/a/b/c/").unwrap(), Path::new("/a/b/c/"));
17211721
assert_eq!(absolute("/a/./b/../c/.././..").unwrap(), Path::new("/a/b/../c/../.."));
1722+
1723+
// Test leading `.` and `..` components
1724+
let curdir = crate::env::current_dir().unwrap();
1725+
assert_eq!(absolute("./a").unwrap().as_os_str(), curdir.join("a").as_os_str());
1726+
assert_eq!(absolute("../a").unwrap().as_os_str(), curdir.join("../a").as_os_str()); // return /pwd/../a
17221727
}
17231728

17241729
#[test]

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

Lines changed: 2 additions & 1 deletion
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)