Do not evaluate backticks in assignments during dry runs (#253)

This commit is contained in:
Travis snɯǝᗡɔW 2017-11-17 23:21:37 -05:00 committed by Casey Rodarmor
parent 13a124d659
commit acb5d6f98b
4 changed files with 37 additions and 32 deletions

View File

@ -7,6 +7,7 @@ pub fn evaluate_assignments<'a>(
overrides: &Map<&str, &str>, overrides: &Map<&str, &str>,
quiet: bool, quiet: bool,
shell: &'a str, shell: &'a str,
dry_run: bool,
) -> RunResult<'a, Map<&'a str, String>> { ) -> RunResult<'a, Map<&'a str, String>> {
let mut evaluator = AssignmentEvaluator { let mut evaluator = AssignmentEvaluator {
assignments: assignments, assignments: assignments,
@ -16,6 +17,7 @@ pub fn evaluate_assignments<'a>(
quiet: quiet, quiet: quiet,
scope: &empty(), scope: &empty(),
shell: shell, shell: shell,
dry_run: dry_run,
}; };
for name in assignments.keys() { for name in assignments.keys() {
@ -25,31 +27,6 @@ pub fn evaluate_assignments<'a>(
Ok(evaluator.evaluated) Ok(evaluator.evaluated)
} }
fn run_backtick<'a, 'b>(
raw: &str,
token: &Token<'a>,
scope: &Map<&'a str, String>,
exports: &Set<&'a str>,
quiet: bool,
shell: &'b str,
) -> RunResult<'a, String> {
let mut cmd = Command::new(shell);
cmd.export_environment_variables(scope, exports)?;
cmd.arg("-cu")
.arg(raw);
cmd.stderr(if quiet {
process::Stdio::null()
} else {
process::Stdio::inherit()
});
brev::output(cmd)
.map_err(|output_error| RuntimeError::Backtick{token: token.clone(), output_error})
}
pub struct AssignmentEvaluator<'a: 'b, 'b> { pub struct AssignmentEvaluator<'a: 'b, 'b> {
pub assignments: &'b Map<&'a str, Expression<'a>>, pub assignments: &'b Map<&'a str, Expression<'a>>,
pub evaluated: Map<&'a str, String>, pub evaluated: Map<&'a str, String>,
@ -58,6 +35,7 @@ pub struct AssignmentEvaluator<'a: 'b, 'b> {
pub quiet: bool, pub quiet: bool,
pub scope: &'b Map<&'a str, String>, pub scope: &'b Map<&'a str, String>,
pub shell: &'b str, pub shell: &'b str,
pub dry_run: bool,
} }
impl<'a, 'b> AssignmentEvaluator<'a, 'b> { impl<'a, 'b> AssignmentEvaluator<'a, 'b> {
@ -123,7 +101,11 @@ impl<'a, 'b> AssignmentEvaluator<'a, 'b> {
} }
Expression::String{ref cooked_string} => cooked_string.cooked.clone(), Expression::String{ref cooked_string} => cooked_string.cooked.clone(),
Expression::Backtick{raw, ref token} => { Expression::Backtick{raw, ref token} => {
run_backtick(raw, token, self.scope, self.exports, self.quiet, self.shell)? if self.dry_run {
format!("`{}`", raw)
} else {
self.run_backtick(raw, token)?
}
} }
Expression::Concatination{ref lhs, ref rhs} => { Expression::Concatination{ref lhs, ref rhs} => {
self.evaluate_expression(lhs, arguments)? self.evaluate_expression(lhs, arguments)?
@ -132,6 +114,28 @@ impl<'a, 'b> AssignmentEvaluator<'a, 'b> {
} }
}) })
} }
fn run_backtick(
&self,
raw: &str,
token: &Token<'a>,
) -> RunResult<'a, String> {
let mut cmd = Command::new(self.shell);
cmd.export_environment_variables(self.scope, self.exports)?;
cmd.arg("-cu")
.arg(raw);
cmd.stderr(if self.quiet {
process::Stdio::null()
} else {
process::Stdio::inherit()
});
brev::output(cmd)
.map_err(|output_error| RuntimeError::Backtick{token: token.clone(), output_error})
}
} }

View File

@ -60,6 +60,7 @@ impl<'a, 'b> Justfile<'a> where 'a: 'b {
&options.overrides, &options.overrides,
options.quiet, options.quiet,
options.shell, options.shell,
options.dry_run,
)?; )?;
if options.evaluate { if options.evaluate {

View File

@ -91,6 +91,7 @@ impl<'a> Recipe<'a> {
overrides: &empty(), overrides: &empty(),
quiet: options.quiet, quiet: options.quiet,
shell: options.shell, shell: options.shell,
dry_run: options.dry_run,
}; };
if self.shebang { if self.shebang {

View File

@ -497,14 +497,13 @@ shebang:
echo {{`echo shebang interpolation`}}"#, echo {{`echo shebang interpolation`}}"#,
args: ("--dry-run", "shebang", "command"), args: ("--dry-run", "shebang", "command"),
stdout: "", stdout: "",
stderr: "stderr stderr: "#!/bin/sh
#!/bin/sh
touch /this/is/not/a/file touch /this/is/not/a/file
backtick `echo stderr 1>&2; echo backtick`
echo shebang interpolation echo `echo shebang interpolation`
touch /this/is/not/a/file touch /this/is/not/a/file
backtick `echo stderr 1>&2; echo backtick`
echo command interpolation echo `echo command interpolation`
", ",
status: EXIT_SUCCESS, status: EXIT_SUCCESS,
} }