mirror of
https://github.com/nushell/nushell.git
synced 2025-08-18 01:30:49 +02:00
ls, rm, cp, open, touch, mkdir: Don't expand tilde if input path is quoted string or a variable. (#12232)
# Description Fixes: #11887 Fixes: #11626 This pr unify the tilde expand behavior over several filesystem relative commands. It follows the same rule with glob expansion: | command | result | | ----------- | ------ | | ls ~/aaa | expand tilde | ls "~/aaa" | don't expand tilde | let f = "~/aaa"; ls $f | don't expand tilde, if you want to: use `ls ($f \| path expand)` | let f: glob = "~/aaa"; ls $f | expand tilde, they don't expand on `mkdir`, `touch` comamnd. Actually I'm not sure for 4th item, currently it's expanding is just because it followes the same rule with glob expansion. ### About the change It changes `expand_path_with` to accept a new argument called `expand_tilde`, if it's true, expand it, if not, just keep it as `~` itself. # User-Facing Changes After this change, `ls "~/aaa"` won't expand tilde. # Tests + Formatting Done
This commit is contained in:
@@ -12,8 +12,8 @@ fn expand_path_with_and_without_relative() {
|
||||
|
||||
let cwd = std::env::current_dir().expect("Could not get current directory");
|
||||
assert_eq!(
|
||||
expand_path_with(full_path, cwd),
|
||||
expand_path_with(path, relative_to),
|
||||
expand_path_with(full_path, cwd, true),
|
||||
expand_path_with(path, relative_to, true),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -22,7 +22,10 @@ fn expand_path_with_relative() {
|
||||
let relative_to = "/foo/bar";
|
||||
let path = "../..";
|
||||
|
||||
assert_eq!(PathBuf::from("/"), expand_path_with(path, relative_to),);
|
||||
assert_eq!(
|
||||
PathBuf::from("/"),
|
||||
expand_path_with(path, relative_to, true),
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(not(windows))]
|
||||
@@ -31,7 +34,7 @@ fn expand_path_no_change() {
|
||||
let path = "/foo/bar";
|
||||
|
||||
let cwd = std::env::current_dir().expect("Could not get current directory");
|
||||
let actual = expand_path_with(path, cwd);
|
||||
let actual = expand_path_with(path, cwd, true);
|
||||
|
||||
assert_eq!(actual, PathBuf::from(path));
|
||||
}
|
||||
@@ -43,7 +46,7 @@ fn expand_unicode_path_no_change() {
|
||||
spam.push("🚒.txt");
|
||||
|
||||
let cwd = std::env::current_dir().expect("Could not get current directory");
|
||||
let actual = expand_path_with(spam, cwd);
|
||||
let actual = expand_path_with(spam, cwd, true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("🚒.txt");
|
||||
|
||||
@@ -60,7 +63,7 @@ fn expand_non_utf8_path() {
|
||||
#[test]
|
||||
fn expand_path_relative_to() {
|
||||
Playground::setup("nu_path_test_1", |dirs, _| {
|
||||
let actual = expand_path_with("spam.txt", dirs.test());
|
||||
let actual = expand_path_with("spam.txt", dirs.test(), true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
|
||||
@@ -74,7 +77,7 @@ fn expand_unicode_path_relative_to_unicode_path_with_spaces() {
|
||||
let mut relative_to = dirs.test().to_owned();
|
||||
relative_to.push("e-$ èрт🚒♞中片-j");
|
||||
|
||||
let actual = expand_path_with("🚒.txt", relative_to);
|
||||
let actual = expand_path_with("🚒.txt", relative_to, true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("e-$ èрт🚒♞中片-j/🚒.txt");
|
||||
|
||||
@@ -94,7 +97,7 @@ fn expand_absolute_path_relative_to() {
|
||||
let mut absolute_path = dirs.test().to_owned();
|
||||
absolute_path.push("spam.txt");
|
||||
|
||||
let actual = expand_path_with(&absolute_path, "non/existent/directory");
|
||||
let actual = expand_path_with(&absolute_path, "non/existent/directory", true);
|
||||
let expected = absolute_path;
|
||||
|
||||
assert_eq!(actual, expected);
|
||||
@@ -104,7 +107,7 @@ fn expand_absolute_path_relative_to() {
|
||||
#[test]
|
||||
fn expand_path_with_dot_relative_to() {
|
||||
Playground::setup("nu_path_test_1", |dirs, _| {
|
||||
let actual = expand_path_with("./spam.txt", dirs.test());
|
||||
let actual = expand_path_with("./spam.txt", dirs.test(), true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
|
||||
@@ -115,7 +118,7 @@ fn expand_path_with_dot_relative_to() {
|
||||
#[test]
|
||||
fn expand_path_with_many_dots_relative_to() {
|
||||
Playground::setup("nu_path_test_1", |dirs, _| {
|
||||
let actual = expand_path_with("././/.//////./././//.////spam.txt", dirs.test());
|
||||
let actual = expand_path_with("././/.//////./././//.////spam.txt", dirs.test(), true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
|
||||
@@ -126,7 +129,7 @@ fn expand_path_with_many_dots_relative_to() {
|
||||
#[test]
|
||||
fn expand_path_with_double_dot_relative_to() {
|
||||
Playground::setup("nu_path_test_1", |dirs, _| {
|
||||
let actual = expand_path_with("foo/../spam.txt", dirs.test());
|
||||
let actual = expand_path_with("foo/../spam.txt", dirs.test(), true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
|
||||
@@ -137,7 +140,7 @@ fn expand_path_with_double_dot_relative_to() {
|
||||
#[test]
|
||||
fn expand_path_with_many_double_dots_relative_to() {
|
||||
Playground::setup("nu_path_test_1", |dirs, _| {
|
||||
let actual = expand_path_with("foo/bar/baz/../../../spam.txt", dirs.test());
|
||||
let actual = expand_path_with("foo/bar/baz/../../../spam.txt", dirs.test(), true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
|
||||
@@ -148,7 +151,7 @@ fn expand_path_with_many_double_dots_relative_to() {
|
||||
#[test]
|
||||
fn expand_path_with_3_ndots_relative_to() {
|
||||
Playground::setup("nu_path_test_1", |dirs, _| {
|
||||
let actual = expand_path_with("foo/bar/.../spam.txt", dirs.test());
|
||||
let actual = expand_path_with("foo/bar/.../spam.txt", dirs.test(), true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
|
||||
@@ -162,6 +165,7 @@ fn expand_path_with_many_3_ndots_relative_to() {
|
||||
let actual = expand_path_with(
|
||||
"foo/bar/baz/eggs/sausage/bacon/.../.../.../spam.txt",
|
||||
dirs.test(),
|
||||
true,
|
||||
);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
@@ -173,7 +177,7 @@ fn expand_path_with_many_3_ndots_relative_to() {
|
||||
#[test]
|
||||
fn expand_path_with_4_ndots_relative_to() {
|
||||
Playground::setup("nu_path_test_1", |dirs, _| {
|
||||
let actual = expand_path_with("foo/bar/baz/..../spam.txt", dirs.test());
|
||||
let actual = expand_path_with("foo/bar/baz/..../spam.txt", dirs.test(), true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
|
||||
@@ -187,6 +191,7 @@ fn expand_path_with_many_4_ndots_relative_to() {
|
||||
let actual = expand_path_with(
|
||||
"foo/bar/baz/eggs/sausage/bacon/..../..../spam.txt",
|
||||
dirs.test(),
|
||||
true,
|
||||
);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
@@ -201,7 +206,11 @@ fn expand_path_with_way_too_many_dots_relative_to() {
|
||||
let mut relative_to = dirs.test().to_owned();
|
||||
relative_to.push("foo/bar/baz/eggs/sausage/bacon/vikings");
|
||||
|
||||
let actual = expand_path_with("././..////././...///././.....///spam.txt", relative_to);
|
||||
let actual = expand_path_with(
|
||||
"././..////././...///././.....///spam.txt",
|
||||
relative_to,
|
||||
true,
|
||||
);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("spam.txt");
|
||||
|
||||
@@ -215,7 +224,7 @@ fn expand_unicode_path_with_way_too_many_dots_relative_to_unicode_path_with_spac
|
||||
let mut relative_to = dirs.test().to_owned();
|
||||
relative_to.push("foo/áčěéí +šř=é/baz/eggs/e-$ èрт🚒♞中片-j/bacon/öäöä öäöä");
|
||||
|
||||
let actual = expand_path_with("././..////././...///././.....///🚒.txt", relative_to);
|
||||
let actual = expand_path_with("././..////././...///././.....///🚒.txt", relative_to, true);
|
||||
let mut expected = dirs.test().to_owned();
|
||||
expected.push("🚒.txt");
|
||||
|
||||
@@ -228,7 +237,7 @@ fn expand_path_tilde() {
|
||||
let tilde_path = "~";
|
||||
|
||||
let cwd = std::env::current_dir().expect("Could not get current directory");
|
||||
let actual = expand_path_with(tilde_path, cwd);
|
||||
let actual = expand_path_with(tilde_path, cwd, true);
|
||||
|
||||
assert!(actual.is_absolute());
|
||||
assert!(!actual.starts_with("~"));
|
||||
@@ -238,7 +247,7 @@ fn expand_path_tilde() {
|
||||
fn expand_path_tilde_relative_to() {
|
||||
let tilde_path = "~";
|
||||
|
||||
let actual = expand_path_with(tilde_path, "non/existent/path");
|
||||
let actual = expand_path_with(tilde_path, "non/existent/path", true);
|
||||
|
||||
assert!(actual.is_absolute());
|
||||
assert!(!actual.starts_with("~"));
|
||||
|
Reference in New Issue
Block a user