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.
This commit is contained in:
Xiretza
2024-07-28 16:40:14 +00:00
committed by Peter Ammon
parent 3d816174fd
commit fd006e02da
2 changed files with 20 additions and 2 deletions

View File

@@ -331,8 +331,12 @@ pub fn path_normalize_for_cd(wd: &wstr, path: &wstr) -> WString {
paths.extend(path_comps); paths.extend(path_comps);
let mut result = let mut result =
WString::with_capacity(paths.iter().fold(0, |sum, s| sum + s.len()) + paths.len() + 1); 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.push_utfstr(*p);
} }
result result
@@ -402,6 +406,14 @@ fn root_directory() {
assert_eq!(path_normalize_for_cd(wd, path), L!("/..")); 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] #[test]
fn empty_path() { fn empty_path() {
let wd = L!("/home/user/"); let wd = L!("/home/user/");

View File

@@ -194,6 +194,12 @@ cd $old_path
cd file cd file
cd $old_path 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 back before removing the test directory again.
cd $oldpwd cd $oldpwd
rm -Rf $base rm -Rf $base