about summary refs log tree commit diff
path: root/library/std/src
diff options
context:
space:
mode:
authorfee1-dead <ent3rm4n@gmail.com>2022-03-06 22:35:31 +1100
committerGitHub <noreply@github.com>2022-03-06 22:35:31 +1100
commit8ea3f236dc45cd4bee67504ae4b1cf64ee5de7ad (patch)
treeb89494c840d78cf077479294aa42366e6f1cd068 /library/std/src
parent02e8839cbd6d5df2019cd22cd42aea48c3aecf2a (diff)
parent0421af9a4626638c71d59feebd7a35136d53bfb9 (diff)
downloadrust-8ea3f236dc45cd4bee67504ae4b1cf64ee5de7ad.tar.gz
rust-8ea3f236dc45cd4bee67504ae4b1cf64ee5de7ad.zip
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``
Diffstat (limited to 'library/std/src')
-rw-r--r--library/std/src/path/tests.rs22
-rw-r--r--library/std/src/sys/unix/path.rs3
2 files changed, 17 insertions, 8 deletions
diff --git a/library/std/src/path/tests.rs b/library/std/src/path/tests.rs
index 8e51433094a..6e863787b7f 100644
--- a/library/std/src/path/tests.rs
+++ b/library/std/src/path/tests.rs
@@ -1710,15 +1710,23 @@ fn test_unix_absolute() {
     let relative = "a/b";
     let mut expected = crate::env::current_dir().unwrap();
     expected.push(relative);
-    assert_eq!(absolute(relative).unwrap(), expected);
+    assert_eq!(absolute(relative).unwrap().as_os_str(), expected.as_os_str());
 
     // Test how components are collected.
-    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"));
-    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/../.."));
+    assert_eq!(absolute("/a/b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
+    assert_eq!(absolute("/a//b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
+    assert_eq!(absolute("//a/b/c").unwrap().as_os_str(), Path::new("//a/b/c").as_os_str());
+    assert_eq!(absolute("///a/b/c").unwrap().as_os_str(), Path::new("/a/b/c").as_os_str());
+    assert_eq!(absolute("/a/b/c/").unwrap().as_os_str(), Path::new("/a/b/c/").as_os_str());
+    assert_eq!(
+        absolute("/a/./b/../c/.././..").unwrap().as_os_str(),
+        Path::new("/a/b/../c/../..").as_os_str()
+    );
+
+    // 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() {