Fix trailing slash in PWD set by cd (#12760)

# Description

Fixes #12758.

#12662 introduced a bug where calling `cd` with a path with a trailing
slash would cause `PWD` to be set to a path including a trailing slash,
which is not allowed. This adds a helper to `nu_path` to remove this,
and uses it in the `cd` command to clean it up before setting `PWD`.

# Tests + Formatting
I added some tests to make sure we don't regress on this in the future.

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`
This commit is contained in:
Devyn Cairns
2024-05-04 02:38:37 -07:00
committed by GitHub
parent 35a0f7a369
commit 709b2479d9
5 changed files with 142 additions and 15 deletions

View File

@ -26,6 +26,16 @@ fn filesystem_change_from_current_directory_using_relative_path() {
})
}
#[test]
fn filesystem_change_from_current_directory_using_relative_path_with_trailing_slash() {
Playground::setup("cd_test_1_slash", |dirs, _| {
// Intentionally not using correct path sep because this should work on Windows
let actual = nu!( cwd: dirs.root(), "cd cd_test_1_slash/; $env.PWD");
assert_eq!(PathBuf::from(actual.out), *dirs.test());
})
}
#[test]
fn filesystem_change_from_current_directory_using_absolute_path() {
Playground::setup("cd_test_2", |dirs, _| {
@ -42,6 +52,23 @@ fn filesystem_change_from_current_directory_using_absolute_path() {
})
}
#[test]
fn filesystem_change_from_current_directory_using_absolute_path_with_trailing_slash() {
Playground::setup("cd_test_2", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
r#"
cd '{}{}'
$env.PWD
"#,
dirs.formats().display(),
std::path::MAIN_SEPARATOR_STR,
);
assert_eq!(PathBuf::from(actual.out), dirs.formats());
})
}
#[test]
fn filesystem_switch_back_to_previous_working_directory() {
Playground::setup("cd_test_3", |dirs, sandbox| {