diff options
| author | Chris Denton <christophersdenton@gmail.com> | 2022-03-05 17:57:12 +0000 |
|---|---|---|
| committer | Chris Denton <christophersdenton@gmail.com> | 2022-03-05 17:57:12 +0000 |
| commit | e8b7371a237451cdc73547b27311fd8d5078521f (patch) | |
| tree | eee7817bddb94afc2db6716c573423ee9c346faa | |
| parent | c8a49fc90281d9a3227a547b5bac8e01d17325be (diff) | |
| download | rust-e8b7371a237451cdc73547b27311fd8d5078521f.tar.gz rust-e8b7371a237451cdc73547b27311fd8d5078521f.zip | |
Unix `path::absolute`: Fix leading "." component
Testing leading `.` and `..` components were missing from the unix tests.
| -rw-r--r-- | library/std/src/path/tests.rs | 5 | ||||
| -rw-r--r-- | library/std/src/sys/unix/path.rs | 3 |
2 files changed, 7 insertions, 1 deletions
diff --git a/library/std/src/path/tests.rs b/library/std/src/path/tests.rs index 8e51433094a..435e84f8cef 100644 --- a/library/std/src/path/tests.rs +++ b/library/std/src/path/tests.rs @@ -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] diff --git a/library/std/src/sys/unix/path.rs b/library/std/src/sys/unix/path.rs index 6d6f4c8b8dc..a98a69e2db8 100644 --- a/library/std/src/sys/unix/path.rs +++ b/library/std/src/sys/unix/path.rs @@ -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() { |
