From 182ef134d9776ec8b307839172749216984b4b76 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 13 Jan 2023 11:03:14 -0800 Subject: [PATCH] Add invocation_directory_native() (#1507) --- src/function.rs | 14 ++++++++++++++ tests/assert_stdout.rs | 2 +- tests/assert_success.rs | 4 +--- tests/init.rs | 8 ++++---- tests/invocation_directory.rs | 15 +++++++++++++++ tests/lib.rs | 7 +++++-- tests/test.rs | 19 ++++++++++--------- 7 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/function.rs b/src/function.rs index 525e72e..768db98 100644 --- a/src/function.rs +++ b/src/function.rs @@ -31,6 +31,7 @@ pub(crate) fn get(name: &str) -> Option { "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 { .map_err(|e| format!("Error getting shell path: {e}")) } +fn invocation_directory_native(context: &FunctionContext) -> Result { + 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, diff --git a/tests/assert_stdout.rs b/tests/assert_stdout.rs index dd1b2d5..7435cc4 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 3026b67..bcb364f 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 deab5bd..118d053 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 9dbacaa..58b4609 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 6dbc733..bdcdccf 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 8271074..1963b8f 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, 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...");