Repository: casey/just
Pull request #253 (2017-11-18 03:09:16)

 Do not evaluate backticks in assignments during dry runs


Patch:
diff --git a/src/assignment_evaluator.rs b/src/assignment_evaluator.rs
index e6363af4..fbde502c 100644
--- a/src/assignment_evaluator.rs
+++ b/src/assignment_evaluator.rs
@@ -7,6 +7,7 @@ pub fn evaluate_assignments<'a>(
   overrides:   &Map<&str, &str>,
   quiet:       bool,
   shell:       &'a str,
+  dry_run:     bool,
 ) -> RunResult<'a, Map<&'a str, String>> {
   let mut evaluator = AssignmentEvaluator {
     assignments: assignments,
@@ -16,6 +17,7 @@ pub fn evaluate_assignments<'a>(
     quiet:       quiet,
     scope:       &empty(),
     shell:       shell,
+    dry_run:     dry_run,
   };
 
   for name in assignments.keys() {
@@ -25,31 +27,6 @@ pub fn evaluate_assignments<'a>(
   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 assignments: &'b Map<&'a str, Expression<'a>>,
   pub evaluated:   Map<&'a str, String>,
@@ -58,6 +35,7 @@ pub struct AssignmentEvaluator<'a: 'b, 'b> {
   pub quiet:       bool,
   pub scope:       &'b Map<&'a str, String>,
   pub shell:       &'b str,
+  pub dry_run:     bool,
 }
 
 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::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} => {
         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})
+  }
 }
 
 
diff --git a/src/justfile.rs b/src/justfile.rs
index b777071e..0122461c 100644
--- a/src/justfile.rs
+++ b/src/justfile.rs
@@ -60,6 +60,7 @@ impl<'a, 'b> Justfile<'a> where 'a: 'b {
       &options.overrides,
       options.quiet,
       options.shell,
+      options.dry_run,
     )?;
 
     if options.evaluate {
diff --git a/src/recipe.rs b/src/recipe.rs
index ff8a7653..bece0adc 100644
--- a/src/recipe.rs
+++ b/src/recipe.rs
@@ -91,6 +91,7 @@ impl<'a> Recipe<'a> {
       overrides:   &empty(),
       quiet:       options.quiet,
       shell:       options.shell,
+      dry_run:     options.dry_run,
     };
 
     if self.shebang {


Test patch:
diff --git a/tests/integration.rs b/tests/integration.rs
index cb140890..ce6ce3a4 100644
--- a/tests/integration.rs
+++ b/tests/integration.rs
@@ -497,14 +497,13 @@ shebang:
   echo {{`echo shebang interpolation`}}"#,
   args:     ("--dry-run", "shebang", "command"),
   stdout:   "",
-  stderr:   "stderr
-#!/bin/sh
+  stderr:   "#!/bin/sh
 touch /this/is/not/a/file
-backtick
-echo shebang interpolation
+`echo stderr 1>&2; echo backtick`
+echo `echo shebang interpolation`
 touch /this/is/not/a/file
-backtick
-echo command interpolation
+`echo stderr 1>&2; echo backtick`
+echo `echo command interpolation`
 ",
   status:   EXIT_SUCCESS,
 }