mirror of
https://github.com/nushell/nushell.git
synced 2025-04-02 12:19:48 +02:00
add -n for path expand, so it doesn't follow symlink (#6255)
* add -p for path expand, so it doesn't follow symlink * fix arg name * rename from no-dereferenct to no-follow-link * rename from no-follow-link to no-symlink, and change short -p to -n * follow strict first * fix * simplify test * fix clippy * fix test on windows
This commit is contained in:
parent
2e5d981a09
commit
0f10d984c3
@ -57,9 +57,9 @@ impl Command for Cp {
|
|||||||
// .switch("force", "suppress error when no file", Some('f'))
|
// .switch("force", "suppress error when no file", Some('f'))
|
||||||
.switch("interactive", "ask user to confirm action", Some('i'))
|
.switch("interactive", "ask user to confirm action", Some('i'))
|
||||||
.switch(
|
.switch(
|
||||||
"no-dereference",
|
"no-symlink",
|
||||||
"If the -r option is specified, no symbolic links are followed.",
|
"no symbolic links are followed, only works if -r is active",
|
||||||
Some('p'),
|
Some('n'),
|
||||||
)
|
)
|
||||||
.category(Category::FileSystem)
|
.category(Category::FileSystem)
|
||||||
}
|
}
|
||||||
@ -218,7 +218,7 @@ impl Command for Cp {
|
|||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
let not_follow_symlink = call.has_flag("no-dereference");
|
let not_follow_symlink = call.has_flag("no-symlink");
|
||||||
let sources = sources.paths_applying_with(|(source_file, depth_level)| {
|
let sources = sources.paths_applying_with(|(source_file, depth_level)| {
|
||||||
let mut dest = destination.clone();
|
let mut dest = destination.clone();
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ struct Arguments {
|
|||||||
strict: bool,
|
strict: bool,
|
||||||
columns: Option<Vec<String>>,
|
columns: Option<Vec<String>>,
|
||||||
cwd: String,
|
cwd: String,
|
||||||
|
not_follow_symlink: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PathSubcommandArguments for Arguments {
|
impl PathSubcommandArguments for Arguments {
|
||||||
@ -34,6 +35,7 @@ impl Command for SubCommand {
|
|||||||
"Throw an error if the path could not be expanded",
|
"Throw an error if the path could not be expanded",
|
||||||
Some('s'),
|
Some('s'),
|
||||||
)
|
)
|
||||||
|
.switch("no-symlink", "Do not resolve symbolic links", Some('n'))
|
||||||
.named(
|
.named(
|
||||||
"columns",
|
"columns",
|
||||||
SyntaxShape::Table,
|
SyntaxShape::Table,
|
||||||
@ -58,6 +60,7 @@ impl Command for SubCommand {
|
|||||||
strict: call.has_flag("strict"),
|
strict: call.has_flag("strict"),
|
||||||
columns: call.get_flag(engine_state, stack, "columns")?,
|
columns: call.get_flag(engine_state, stack, "columns")?,
|
||||||
cwd: current_dir_str(engine_state, stack)?,
|
cwd: current_dir_str(engine_state, stack)?,
|
||||||
|
not_follow_symlink: call.has_flag("no-symlink"),
|
||||||
};
|
};
|
||||||
|
|
||||||
input.map(
|
input.map(
|
||||||
@ -84,6 +87,11 @@ impl Command for SubCommand {
|
|||||||
example: r"'foo\..\bar' | path expand",
|
example: r"'foo\..\bar' | path expand",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Expand an absolute path without following symlink",
|
||||||
|
example: r"'foo\..\bar' | path expand -n",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,22 +118,35 @@ impl Command for SubCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn expand(path: &Path, span: Span, args: &Arguments) -> Value {
|
fn expand(path: &Path, span: Span, args: &Arguments) -> Value {
|
||||||
if let Ok(p) = canonicalize_with(path, &args.cwd) {
|
if args.strict {
|
||||||
Value::string(p.to_string_lossy(), span)
|
match canonicalize_with(path, &args.cwd) {
|
||||||
} else if args.strict {
|
Ok(p) => {
|
||||||
Value::Error {
|
if args.not_follow_symlink {
|
||||||
error: ShellError::GenericError(
|
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)
|
||||||
"Could not expand path".into(),
|
} else {
|
||||||
"could not be expanded (path might not exist, non-final \
|
Value::string(p.to_string_lossy(), span)
|
||||||
component is not a directory, or other cause)"
|
}
|
||||||
.into(),
|
}
|
||||||
Some(span),
|
Err(_) => Value::Error {
|
||||||
None,
|
error: ShellError::GenericError(
|
||||||
Vec::new(),
|
"Could not expand path".into(),
|
||||||
),
|
"could not be expanded (path might not exist, non-final \
|
||||||
|
component is not a directory, or other cause)"
|
||||||
|
.into(),
|
||||||
|
Some(span),
|
||||||
|
None,
|
||||||
|
Vec::new(),
|
||||||
|
),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
} else {
|
} else if args.not_follow_symlink {
|
||||||
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)
|
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)
|
||||||
|
} else {
|
||||||
|
canonicalize_with(path, &args.cwd)
|
||||||
|
.map(|p| Value::string(p.to_string_lossy(), span))
|
||||||
|
.unwrap_or_else(|_| {
|
||||||
|
Value::string(expand_path_with(path, &args.cwd).to_string_lossy(), span)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -286,7 +286,7 @@ fn copy_dir_contains_symlink() {
|
|||||||
// make symbolic link and copy.
|
// make symbolic link and copy.
|
||||||
nu!(
|
nu!(
|
||||||
cwd: sandbox.cwd(),
|
cwd: sandbox.cwd(),
|
||||||
"rm tmp_dir/good_bye; cp -r -p tmp_dir tmp_dir_2",
|
"rm tmp_dir/good_bye; cp -r -n tmp_dir tmp_dir_2",
|
||||||
);
|
);
|
||||||
|
|
||||||
// check hello_there exists inside `tmp_dir_2`, and `dangle_symlink` also exists inside `tmp_dir_2`.
|
// check hello_there exists inside `tmp_dir_2`, and `dangle_symlink` also exists inside `tmp_dir_2`.
|
||||||
@ -309,7 +309,7 @@ fn copy_dir_symlink_file_body_not_changed() {
|
|||||||
// make symbolic link and copy.
|
// make symbolic link and copy.
|
||||||
nu!(
|
nu!(
|
||||||
cwd: sandbox.cwd(),
|
cwd: sandbox.cwd(),
|
||||||
"rm tmp_dir/good_bye; cp -r -p tmp_dir tmp_dir_2; rm -r tmp_dir; cp -r -p tmp_dir_2 tmp_dir; echo hello_data | save tmp_dir/good_bye",
|
"rm tmp_dir/good_bye; cp -r -n tmp_dir tmp_dir_2; rm -r tmp_dir; cp -r -n tmp_dir_2 tmp_dir; echo hello_data | save tmp_dir/good_bye",
|
||||||
);
|
);
|
||||||
|
|
||||||
// check dangle_symlink in tmp_dir is no longer dangling.
|
// check dangle_symlink in tmp_dir is no longer dangling.
|
||||||
|
@ -24,6 +24,28 @@ fn expands_path_with_dot() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(unix)]
|
||||||
|
#[test]
|
||||||
|
fn expands_path_without_follow_symlink() {
|
||||||
|
Playground::setup("path_expand_3", |dirs, sandbox| {
|
||||||
|
sandbox
|
||||||
|
.within("menu")
|
||||||
|
.with_files(vec![EmptyFile("spam.txt")]);
|
||||||
|
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: dirs.test(), pipeline(
|
||||||
|
r#"
|
||||||
|
ln -s spam.txt menu/spam_link.ln;
|
||||||
|
echo "menu/./spam_link.ln"
|
||||||
|
| path expand -n
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
let expected = dirs.test.join("menu").join("spam_link.ln");
|
||||||
|
assert_eq!(PathBuf::from(actual.out), expected);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn expands_path_with_double_dot() {
|
fn expands_path_with_double_dot() {
|
||||||
Playground::setup("path_expand_2", |dirs, sandbox| {
|
Playground::setup("path_expand_2", |dirs, sandbox| {
|
||||||
@ -75,4 +97,31 @@ mod windows {
|
|||||||
assert!(!PathBuf::from(actual.out).starts_with("~"));
|
assert!(!PathBuf::from(actual.out).starts_with("~"));
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn expands_path_without_follow_symlink() {
|
||||||
|
Playground::setup("path_expand_3", |dirs, sandbox| {
|
||||||
|
sandbox
|
||||||
|
.within("menu")
|
||||||
|
.with_files(vec![EmptyFile("spam.txt")]);
|
||||||
|
|
||||||
|
let cwd = dirs.test();
|
||||||
|
std::os::windows::fs::symlink_file(
|
||||||
|
cwd.join("menu").join("spam.txt"),
|
||||||
|
cwd.join("menu").join("spam_link.ln"),
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: dirs.test(), pipeline(
|
||||||
|
r#"
|
||||||
|
echo "menu/./spam_link.ln"
|
||||||
|
| path expand -n
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
let expected = dirs.test.join("menu").join("spam_link.ln");
|
||||||
|
assert_eq!(PathBuf::from(actual.out), expected);
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user