From fd006e02da53d41eb4ec24e63ae0a71bf1f23ad3 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sun, 28 Jul 2024 16:40:14 +0000 Subject: [PATCH] Fix `cd ..` to the root directory The leading slash always needs to be present, even if there aren't any other components. This was introduced by the Rust port. --- src/wutil/mod.rs | 16 ++++++++++++++-- tests/checks/cd.fish | 6 ++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/wutil/mod.rs b/src/wutil/mod.rs index f7d09dc96..0342f052a 100644 --- a/src/wutil/mod.rs +++ b/src/wutil/mod.rs @@ -331,8 +331,12 @@ pub fn path_normalize_for_cd(wd: &wstr, path: &wstr) -> WString { paths.extend(path_comps); let mut result = WString::with_capacity(paths.iter().fold(0, |sum, s| sum + s.len()) + paths.len() + 1); - for p in &paths { - result.push(SEP); + result.push(SEP); + // TODO: intersperse() https://github.com/rust-lang/rust/issues/79524 + for (i, p) in paths.iter().enumerate() { + if i != 0 { + result.push(SEP); + } result.push_utfstr(*p); } result @@ -402,6 +406,14 @@ mod path_cd_tests { assert_eq!(path_normalize_for_cd(wd, path), L!("/..")); } + #[test] + fn up_to_root_directory() { + let wd = L!("/foo/"); + let path = L!(".."); + eprintln!("({}, {})", wd, path); + assert_eq!(path_normalize_for_cd(wd, path), L!("/")); + } + #[test] fn empty_path() { let wd = L!("/home/user/"); diff --git a/tests/checks/cd.fish b/tests/checks/cd.fish index 83c3c3d98..102c94c3f 100644 --- a/tests/checks/cd.fish +++ b/tests/checks/cd.fish @@ -194,6 +194,12 @@ cd $old_path cd file cd $old_path +# Test that going up to the root directory using .. works +cd /(string split --no-empty -f 1 / (pwd)) +cd .. +pwd +#CHECK: / + # cd back before removing the test directory again. cd $oldpwd rm -Rf $base