Repository: casey/just
Pull request #1507 (2023-01-13 18:51:05)

Add invocation_directory_native function


Patch:
diff --git a/src/function.rs b/src/function.rs
index 525e72e7..768db98f 100644
--- a/src/function.rs
+++ b/src/function.rs
@@ -31,6 +31,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
     "file_name" => Unary(file_name),
     "file_stem" => Unary(file_stem),
     "invocation_directory" => Nullary(invocation_directory),
+    "invocation_directory_native" => Nullary(invocation_directory_native),
     "join" => BinaryPlus(join),
     "just_executable" => Nullary(just_executable),
     "justfile" => Nullary(justfile),
@@ -181,6 +182,19 @@ fn invocation_directory(context: &FunctionContext) -> Result<String, String> {
   .map_err(|e| format!("Error getting shell path: {e}"))
 }
 
+fn invocation_directory_native(context: &FunctionContext) -> Result<String, String> {
+  context
+    .invocation_directory
+    .to_str()
+    .map(str::to_owned)
+    .ok_or_else(|| {
+      format!(
+        "Invocation directory is not valid unicode: {}",
+        context.invocation_directory.display()
+      )
+    })
+}
+
 fn join(
   _context: &FunctionContext,
   base: &str,


Test patch:
diff --git a/tests/assert_stdout.rs b/tests/assert_stdout.rs
index dd1b2d5a..7435cc4b 100644
--- a/tests/assert_stdout.rs
+++ b/tests/assert_stdout.rs
@@ -1,6 +1,6 @@
 use super::*;
 
-pub(crate) fn assert_stdout(output: &Output, stdout: &str) {
+pub(crate) fn assert_stdout(output: &std::process::Output, stdout: &str) {
   assert_success(output);
   assert_eq!(String::from_utf8_lossy(&output.stdout), stdout);
 }
diff --git a/tests/assert_success.rs b/tests/assert_success.rs
index 3026b67b..bcb364f8 100644
--- a/tests/assert_success.rs
+++ b/tests/assert_success.rs
@@ -1,6 +1,4 @@
-use super::*;
-
-pub(crate) fn assert_success(output: &Output) {
+pub(crate) fn assert_success(output: &std::process::Output) {
   if !output.status.success() {
     eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
     eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));
diff --git a/tests/init.rs b/tests/init.rs
index deab5bda..118d0537 100644
--- a/tests/init.rs
+++ b/tests/init.rs
@@ -22,13 +22,13 @@ fn current_dir() {
 
 #[test]
 fn exists() {
-  let tempdir = Test::new()
+  let output = Test::new()
     .no_justfile()
     .arg("--init")
     .stderr_regex("Wrote justfile to `.*`\n")
     .run();
 
-  Test::with_tempdir(tempdir)
+  Test::with_tempdir(output.tempdir)
     .no_justfile()
     .arg("--init")
     .status(EXIT_FAILURE)
@@ -191,12 +191,12 @@ fn justfile_and_working_directory() {
 
 #[test]
 fn fmt_compatibility() {
-  let tempdir = Test::new()
+  let output = Test::new()
     .no_justfile()
     .arg("--init")
     .stderr_regex("Wrote justfile to `.*`\n")
     .run();
-  Test::with_tempdir(tempdir)
+  Test::with_tempdir(output.tempdir)
     .no_justfile()
     .arg("--unstable")
     .arg("--check")
diff --git a/tests/invocation_directory.rs b/tests/invocation_directory.rs
index 9dbacaa5..58b46092 100644
--- a/tests/invocation_directory.rs
+++ b/tests/invocation_directory.rs
@@ -88,3 +88,18 @@ fn test_invocation_directory() {
     panic!("test failed");
   }
 }
+
+#[test]
+fn invocation_directory_native() {
+  let Output { stdout, tempdir } = Test::new()
+    .justfile("x := invocation_directory_native()")
+    .args(["--evaluate", "x"])
+    .stdout_regex(".*")
+    .run();
+
+  if cfg!(windows) {
+    assert_eq!(Path::new(&stdout), tempdir.path());
+  } else {
+    assert_eq!(Path::new(&stdout), tempdir.path().canonicalize().unwrap());
+  }
+}
diff --git a/tests/lib.rs b/tests/lib.rs
index 6dbc733e..bdcdccfb 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -1,6 +1,9 @@
 pub(crate) use {
   crate::{
-    assert_stdout::assert_stdout, assert_success::assert_success, tempdir::tempdir, test::Test,
+    assert_stdout::assert_stdout,
+    assert_success::assert_success,
+    tempdir::tempdir,
+    test::{Output, Test},
   },
   cradle::input::Input,
   executable_path::executable_path,
@@ -18,7 +21,7 @@ pub(crate) use {
     io::Write,
     iter,
     path::{Path, PathBuf, MAIN_SEPARATOR},
-    process::{Command, Output, Stdio},
+    process::{Command, Stdio},
     str,
   },
   tempfile::TempDir,
diff --git a/tests/test.rs b/tests/test.rs
index 8271074f..1963b8f2 100644
--- a/tests/test.rs
+++ b/tests/test.rs
@@ -34,6 +34,11 @@ macro_rules! test {
   }
 }
 
+pub(crate) struct Output {
+  pub(crate) stdout: String,
+  pub(crate) tempdir: TempDir,
+}
+
 pub(crate) struct Test {
   pub(crate) args: Vec<String>,
   pub(crate) current_dir: PathBuf,
@@ -171,7 +176,7 @@ impl Test {
 }
 
 impl Test {
-  pub(crate) fn run(self) -> TempDir {
+  pub(crate) fn run(self) -> Output {
     if let Some(justfile) = &self.justfile {
       let justfile = unindent(justfile);
       fs::write(self.justfile_path(), justfile).unwrap();
@@ -256,17 +261,13 @@ impl Test {
       test_round_trip(self.tempdir.path());
     }
 
-    self.tempdir
+    Output {
+      tempdir: self.tempdir,
+      stdout: output_stdout.into(),
+    }
   }
 }
 
-#[derive(PartialEq, Debug)]
-struct Output<'a> {
-  stdout: &'a str,
-  stderr: &'a str,
-  status: i32,
-}
-
 fn test_round_trip(tmpdir: &Path) {
   println!("Reparsing...");