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

View file

@ -1,5 +1,7 @@
mod helpers; mod helpers;
use helpers::Playground;
#[test] #[test]
fn external_command() { fn external_command() {
let actual = nu!( let actual = nu!(
@ -9,3 +11,32 @@ fn external_command() {
assert!(actual.contains("1")); 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");
}