Merge pull request #1060 from naufraghi/issues-972-expand-tilde-as-home-in-external-commands

Expand tilde as home in external commands
This commit is contained in:
Jonathan Turner 2019-12-13 08:46:08 -08:00 committed by GitHub
commit 550bda477b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 11 deletions

View File

@ -7,6 +7,7 @@ use nu_errors::ShellError;
use nu_parser::ExternalCommand;
use nu_protocol::{UntaggedValue, Value};
use std::io::{Error, ErrorKind};
use std::ops::Deref;
use subprocess::Exec;
use super::ClassifiedInputStream;
@ -107,11 +108,7 @@ pub(crate) async fn run_external_command(
None
} else {
// Let's also replace ~ as we shell out
let arg = if let Some(ref home_dir) = home_dir {
arg.replace("~", home_dir.to_str().unwrap())
} else {
arg.replace("~", "~")
};
let arg = shellexpand::tilde_with_context(arg.deref(), || home_dir.as_ref());
Some(arg.replace("$it", &i))
}
@ -125,11 +122,7 @@ pub(crate) async fn run_external_command(
process = Exec::cmd(&command.name);
for arg in command.args.iter() {
// Let's also replace ~ as we shell out
let arg = if let Some(ref home_dir) = home_dir {
arg.replace("~", home_dir.to_str().unwrap())
} else {
arg.replace("~", "~")
};
let arg = shellexpand::tilde_with_context(arg.deref(), || home_dir.as_ref());
let arg_chars: Vec<_> = arg.chars().collect();
if arg_chars.len() > 1 && arg_chars[0] == '"' && arg_chars[arg_chars.len() - 1] == '"' {
@ -137,7 +130,7 @@ pub(crate) async fn run_external_command(
let new_arg: String = arg_chars[1..arg_chars.len() - 1].iter().collect();
process = process.arg(new_arg);
} else {
process = process.arg(arg.clone());
process = process.arg(arg.as_ref());
}
}
}

View File

@ -1,5 +1,7 @@
mod helpers;
use helpers::Playground;
#[test]
fn external_command() {
let actual = nu!(
@ -9,3 +11,32 @@ fn external_command() {
assert!(actual.contains("1"));
}
#[test]
fn spawn_external_process_with_home_in_arguments() {
Playground::setup("echo_tilde", |dirs, _| {
let actual = nu!(
cwd: dirs.test(),
r#"
sh -c "echo ~"
"#
);
assert!(
!actual.contains("~"),
format!("'{}' should not contain ~", actual)
);
})
}
#[test]
fn spawn_external_process_with_tilde_in_arguments() {
let actual = nu!(
cwd: "tests/fixtures",
r#"
sh -c "echo 1~1"
"#
);
assert_eq!(actual, "1~1");
}