2022-02-04 03:01:45 +01:00
|
|
|
use nu_test_support::fs::Stub::EmptyFile;
|
2020-05-07 13:03:43 +02:00
|
|
|
use nu_test_support::nu;
|
2019-12-17 19:54:39 +01:00
|
|
|
use nu_test_support::playground::Playground;
|
2019-09-08 10:09:05 +02:00
|
|
|
use std::path::PathBuf;
|
2019-09-08 07:10:08 +02:00
|
|
|
|
2022-10-13 11:04:34 +02:00
|
|
|
#[test]
|
|
|
|
fn cd_works_with_in_var() {
|
|
|
|
Playground::setup("cd_test_1", |dirs, _| {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.root(),
|
|
|
|
r#"
|
|
|
|
"cd_test_1" | cd $in; $env.PWD | path split | last
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!("cd_test_1", actual.out);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-06 09:05:47 +02:00
|
|
|
#[test]
|
2019-09-08 12:15:55 +02:00
|
|
|
fn filesystem_change_from_current_directory_using_relative_path() {
|
2019-09-08 10:09:05 +02:00
|
|
|
Playground::setup("cd_test_1", |dirs, _| {
|
2023-04-07 23:09:55 +02:00
|
|
|
let actual = nu!( cwd: dirs.root(), "cd cd_test_1; $env.PWD");
|
2019-08-06 09:05:47 +02:00
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(PathBuf::from(actual.out), *dirs.test());
|
2019-09-08 10:09:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesystem_change_from_current_directory_using_absolute_path() {
|
|
|
|
Playground::setup("cd_test_2", |dirs, _| {
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
r#"
|
2023-01-28 21:56:47 +01:00
|
|
|
cd '{}'
|
|
|
|
$env.PWD
|
2019-09-08 10:09:05 +02:00
|
|
|
"#,
|
2022-08-13 04:13:50 +02:00
|
|
|
dirs.formats().display()
|
2019-09-08 10:09:05 +02:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(PathBuf::from(actual.out), dirs.formats());
|
2019-09-11 16:36:50 +02:00
|
|
|
})
|
2019-08-16 00:18:18 +02:00
|
|
|
}
|
2019-09-08 07:10:08 +02:00
|
|
|
|
|
|
|
#[test]
|
2019-09-08 10:09:05 +02:00
|
|
|
fn filesystem_switch_back_to_previous_working_directory() {
|
|
|
|
Playground::setup("cd_test_3", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("odin");
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test().join("odin"),
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2019-09-08 10:09:05 +02:00
|
|
|
cd {}
|
|
|
|
cd -
|
2023-01-28 21:56:47 +01:00
|
|
|
$env.PWD
|
2023-04-07 23:09:55 +02:00
|
|
|
",
|
2022-08-13 04:13:50 +02:00
|
|
|
dirs.test().display()
|
2019-09-08 10:09:05 +02:00
|
|
|
);
|
2019-09-08 07:10:08 +02:00
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(PathBuf::from(actual.out), dirs.test().join("odin"));
|
2019-09-08 10:09:05 +02:00
|
|
|
})
|
|
|
|
}
|
2019-09-08 07:10:08 +02:00
|
|
|
|
2019-09-08 11:55:49 +02:00
|
|
|
#[test]
|
2023-01-16 12:43:46 +01:00
|
|
|
fn filesystem_change_from_current_directory_using_relative_path_and_dash() {
|
2019-09-08 11:55:49 +02:00
|
|
|
Playground::setup("cd_test_4", |dirs, sandbox| {
|
2019-09-08 12:15:55 +02:00
|
|
|
sandbox.within("odin").mkdir("-");
|
2019-09-08 11:55:49 +02:00
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2019-09-08 11:55:49 +02:00
|
|
|
cd odin/-
|
2023-01-28 21:56:47 +01:00
|
|
|
$env.PWD
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2019-09-08 11:55:49 +02:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(
|
|
|
|
PathBuf::from(actual.out),
|
|
|
|
dirs.test().join("odin").join("-")
|
|
|
|
);
|
2019-09-08 11:55:49 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-08 10:09:05 +02:00
|
|
|
#[test]
|
|
|
|
fn filesystem_change_current_directory_to_parent_directory() {
|
2019-09-08 11:55:49 +02:00
|
|
|
Playground::setup("cd_test_5", |dirs, _| {
|
2019-09-08 10:09:05 +02:00
|
|
|
let actual = nu!(
|
2019-09-08 07:10:08 +02:00
|
|
|
cwd: dirs.test(),
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2019-09-08 10:09:05 +02:00
|
|
|
cd ..
|
2023-01-28 21:56:47 +01:00
|
|
|
$env.PWD
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2019-09-08 07:10:08 +02:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(PathBuf::from(actual.out), *dirs.root());
|
2019-09-08 07:10:08 +02:00
|
|
|
})
|
|
|
|
}
|
2019-09-08 10:09:05 +02:00
|
|
|
|
2020-04-06 13:28:56 +02:00
|
|
|
#[test]
|
|
|
|
fn filesystem_change_current_directory_to_two_parents_up_using_multiple_dots() {
|
|
|
|
Playground::setup("cd_test_6", |dirs, sandbox| {
|
|
|
|
sandbox.within("foo").mkdir("bar");
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test().join("foo/bar"),
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2020-04-06 13:28:56 +02:00
|
|
|
cd ...
|
2023-01-28 21:56:47 +01:00
|
|
|
$env.PWD
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2020-04-06 13:28:56 +02:00
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(PathBuf::from(actual.out), *dirs.test());
|
2020-04-06 13:28:56 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-08 10:09:05 +02:00
|
|
|
#[test]
|
2019-09-08 12:15:55 +02:00
|
|
|
fn filesystem_change_to_home_directory() {
|
2020-04-06 13:28:56 +02:00
|
|
|
Playground::setup("cd_test_8", |dirs, _| {
|
2019-09-08 10:09:05 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2019-09-08 10:09:05 +02:00
|
|
|
cd ~
|
2023-01-28 21:56:47 +01:00
|
|
|
$env.PWD
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2019-09-08 10:09:05 +02:00
|
|
|
);
|
|
|
|
|
2021-01-19 21:24:27 +01:00
|
|
|
assert_eq!(Some(PathBuf::from(actual.out)), dirs_next::home_dir());
|
2019-09-08 10:09:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesystem_change_to_a_directory_containing_spaces() {
|
2020-04-06 13:28:56 +02:00
|
|
|
Playground::setup("cd_test_9", |dirs, sandbox| {
|
2019-09-08 10:09:05 +02:00
|
|
|
sandbox.mkdir("robalino turner katz");
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
r#"
|
|
|
|
cd "robalino turner katz"
|
2023-01-28 21:56:47 +01:00
|
|
|
$env.PWD
|
2019-09-08 10:09:05 +02:00
|
|
|
"#
|
|
|
|
);
|
|
|
|
|
2019-09-11 16:36:50 +02:00
|
|
|
assert_eq!(
|
2020-05-07 13:03:43 +02:00
|
|
|
PathBuf::from(actual.out),
|
2019-09-11 16:36:50 +02:00
|
|
|
dirs.test().join("robalino turner katz")
|
|
|
|
);
|
2019-09-08 10:09:05 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-24 22:34:30 +02:00
|
|
|
#[test]
|
|
|
|
fn filesystem_not_a_directory() {
|
2020-04-06 13:28:56 +02:00
|
|
|
Playground::setup("cd_test_10", |dirs, sandbox| {
|
2024-05-04 02:53:15 +02:00
|
|
|
sandbox.with_files(&[EmptyFile("ferris_did_it.txt")]);
|
2019-09-24 22:34:30 +02:00
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(
|
2019-09-24 22:34:30 +02:00
|
|
|
cwd: dirs.test(),
|
|
|
|
"cd ferris_did_it.txt"
|
|
|
|
);
|
|
|
|
|
2020-05-07 13:03:43 +02:00
|
|
|
assert!(
|
|
|
|
actual.err.contains("ferris_did_it.txt"),
|
|
|
|
"actual={:?}",
|
|
|
|
actual.err
|
|
|
|
);
|
|
|
|
assert!(
|
|
|
|
actual.err.contains("is not a directory"),
|
|
|
|
"actual={:?}",
|
|
|
|
actual.err
|
|
|
|
);
|
2019-09-24 22:34:30 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-08 10:09:05 +02:00
|
|
|
#[test]
|
|
|
|
fn filesystem_directory_not_found() {
|
2020-04-25 08:09:00 +02:00
|
|
|
Playground::setup("cd_test_11", |dirs, _| {
|
2020-05-07 13:03:43 +02:00
|
|
|
let actual = nu!(
|
2020-04-25 08:09:00 +02:00
|
|
|
cwd: dirs.test(),
|
|
|
|
"cd dir_that_does_not_exist"
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
assert!(
|
2020-05-07 13:03:43 +02:00
|
|
|
actual.err.contains("dir_that_does_not_exist"),
|
2020-04-25 08:09:00 +02:00
|
|
|
"actual={:?}",
|
2020-05-07 13:03:43 +02:00
|
|
|
actual.err
|
2020-04-25 08:09:00 +02:00
|
|
|
);
|
|
|
|
assert!(
|
2020-05-07 13:03:43 +02:00
|
|
|
actual.err.contains("directory not found"),
|
2020-04-25 08:09:00 +02:00
|
|
|
"actual={:?}",
|
2020-05-07 13:03:43 +02:00
|
|
|
actual.err
|
2020-04-25 08:09:00 +02:00
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filesystem_change_directory_to_symlink_relative() {
|
|
|
|
Playground::setup("cd_test_12", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("foo");
|
|
|
|
sandbox.mkdir("boo");
|
|
|
|
sandbox.symlink("foo", "foo_link");
|
|
|
|
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test().join("boo"),
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2020-04-25 08:09:00 +02:00
|
|
|
cd ../foo_link
|
2023-01-28 21:56:47 +01:00
|
|
|
$env.PWD
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2020-04-25 08:09:00 +02:00
|
|
|
);
|
Migrate to a new PWD API (#12603)
This is the first PR towards migrating to a new `$env.PWD` API that
returns potentially un-canonicalized paths. Refer to PR #12515 for
motivations.
## New API: `EngineState::cwd()`
The goal of the new API is to cover both parse-time and runtime use
case, and avoid unintentional misuse. It takes an `Option<Stack>` as
argument, which if supplied, will search for `$env.PWD` on the stack in
additional to the engine state. I think with this design, there's less
confusion over parse-time and runtime environments. If you have access
to a stack, just supply it; otherwise supply `None`.
## Deprecation of other PWD-related APIs
Other APIs are re-implemented using `EngineState::cwd()` and properly
documented. They're marked deprecated, but their behavior is unchanged.
Unused APIs are deleted, and code that accesses `$env.PWD` directly
without using an API is rewritten.
Deprecated APIs:
* `EngineState::current_work_dir()`
* `StateWorkingSet::get_cwd()`
* `env::current_dir()`
* `env::current_dir_str()`
* `env::current_dir_const()`
* `env::current_dir_str_const()`
Other changes:
* `EngineState::get_cwd()` (deleted)
* `StateWorkingSet::list_env()` (deleted)
* `repl::do_run_cmd()` (rewritten with `env::current_dir_str()`)
## `cd` and `pwd` now use logical paths by default
This pulls the changes from PR #12515. It's currently somewhat broken
because using non-canonicalized paths exposed a bug in our path
normalization logic (Issue #12602). Once that is fixed, this should
work.
## Future plans
This PR needs some tests. Which test helpers should I use, and where
should I put those tests?
I noticed that unquoted paths are expanded within `eval_filepath()` and
`eval_directory()` before they even reach the `cd` command. This means
every paths is expanded twice. Is this intended?
Once this PR lands, the plan is to review all usages of the deprecated
APIs and migrate them to `EngineState::cwd()`. In the meantime, these
usages are annotated with `#[allow(deprecated)]` to avoid breaking CI.
---------
Co-authored-by: Jakub Žádník <kubouch@gmail.com>
2024-05-03 13:33:09 +02:00
|
|
|
assert_eq!(PathBuf::from(actual.out), dirs.test().join("foo_link"));
|
2020-04-25 08:09:00 +02:00
|
|
|
|
Migrate to a new PWD API (#12603)
This is the first PR towards migrating to a new `$env.PWD` API that
returns potentially un-canonicalized paths. Refer to PR #12515 for
motivations.
## New API: `EngineState::cwd()`
The goal of the new API is to cover both parse-time and runtime use
case, and avoid unintentional misuse. It takes an `Option<Stack>` as
argument, which if supplied, will search for `$env.PWD` on the stack in
additional to the engine state. I think with this design, there's less
confusion over parse-time and runtime environments. If you have access
to a stack, just supply it; otherwise supply `None`.
## Deprecation of other PWD-related APIs
Other APIs are re-implemented using `EngineState::cwd()` and properly
documented. They're marked deprecated, but their behavior is unchanged.
Unused APIs are deleted, and code that accesses `$env.PWD` directly
without using an API is rewritten.
Deprecated APIs:
* `EngineState::current_work_dir()`
* `StateWorkingSet::get_cwd()`
* `env::current_dir()`
* `env::current_dir_str()`
* `env::current_dir_const()`
* `env::current_dir_str_const()`
Other changes:
* `EngineState::get_cwd()` (deleted)
* `StateWorkingSet::list_env()` (deleted)
* `repl::do_run_cmd()` (rewritten with `env::current_dir_str()`)
## `cd` and `pwd` now use logical paths by default
This pulls the changes from PR #12515. It's currently somewhat broken
because using non-canonicalized paths exposed a bug in our path
normalization logic (Issue #12602). Once that is fixed, this should
work.
## Future plans
This PR needs some tests. Which test helpers should I use, and where
should I put those tests?
I noticed that unquoted paths are expanded within `eval_filepath()` and
`eval_directory()` before they even reach the `cd` command. This means
every paths is expanded twice. Is this intended?
Once this PR lands, the plan is to review all usages of the deprecated
APIs and migrate them to `EngineState::cwd()`. In the meantime, these
usages are annotated with `#[allow(deprecated)]` to avoid breaking CI.
---------
Co-authored-by: Jakub Žádník <kubouch@gmail.com>
2024-05-03 13:33:09 +02:00
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test().join("boo"),
|
|
|
|
"
|
|
|
|
cd -P ../foo_link
|
|
|
|
$env.PWD
|
|
|
|
"
|
|
|
|
);
|
2020-05-07 13:03:43 +02:00
|
|
|
assert_eq!(PathBuf::from(actual.out), dirs.test().join("foo"));
|
2020-04-25 08:09:00 +02:00
|
|
|
})
|
2019-09-08 12:15:55 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 03:01:45 +01:00
|
|
|
// FIXME: jt: needs more work
|
|
|
|
#[ignore]
|
2020-06-21 21:02:58 +02:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
#[test]
|
|
|
|
fn test_change_windows_drive() {
|
|
|
|
Playground::setup("cd_test_20", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("test_folder");
|
|
|
|
|
|
|
|
let _actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
|
|
|
r#"
|
|
|
|
subst Z: test_folder
|
|
|
|
Z:
|
|
|
|
echo "some text" | save test_file.txt
|
|
|
|
cd ~
|
|
|
|
subst Z: /d
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
assert!(dirs
|
|
|
|
.test()
|
|
|
|
.join("test_folder")
|
|
|
|
.join("test_file.txt")
|
|
|
|
.exists());
|
|
|
|
})
|
|
|
|
}
|
2022-11-04 19:38:39 +01:00
|
|
|
|
|
|
|
#[cfg(unix)]
|
|
|
|
#[test]
|
2023-01-16 12:43:46 +01:00
|
|
|
fn cd_permission_denied_folder() {
|
2022-11-04 19:38:39 +01:00
|
|
|
Playground::setup("cd_test_21", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("banned");
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2022-11-04 19:38:39 +01:00
|
|
|
chmod -x banned
|
|
|
|
cd banned
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2022-11-04 19:38:39 +01:00
|
|
|
);
|
|
|
|
assert!(actual.err.contains("Cannot change directory to"));
|
|
|
|
nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2022-11-04 19:38:39 +01:00
|
|
|
chmod +x banned
|
|
|
|
rm banned
|
2023-04-07 23:09:55 +02:00
|
|
|
"
|
2022-11-04 19:38:39 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
2023-01-16 12:43:46 +01:00
|
|
|
// FIXME: cd_permission_denied_folder on windows
|
2022-11-04 19:38:39 +01:00
|
|
|
#[ignore]
|
|
|
|
#[cfg(windows)]
|
|
|
|
#[test]
|
2023-01-16 12:43:46 +01:00
|
|
|
fn cd_permission_denied_folder() {
|
2022-11-04 19:38:39 +01:00
|
|
|
Playground::setup("cd_test_21", |dirs, sandbox| {
|
|
|
|
sandbox.mkdir("banned");
|
|
|
|
let actual = nu!(
|
|
|
|
cwd: dirs.test(),
|
2023-11-16 22:14:45 +01:00
|
|
|
r"
|
2022-11-04 19:38:39 +01:00
|
|
|
icacls banned /deny BUILTIN\Administrators:F
|
|
|
|
cd banned
|
2023-11-16 22:14:45 +01:00
|
|
|
"
|
2022-11-04 19:38:39 +01:00
|
|
|
);
|
|
|
|
assert!(actual.err.contains("Folder is not able to read"));
|
|
|
|
});
|
|
|
|
}
|