Add invocation_directory_native() (#1507)

This commit is contained in:
Casey Rodarmor 2023-01-13 11:03:14 -08:00 committed by GitHub
parent 1d02f0ef80
commit 182ef134d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 50 additions and 19 deletions

View File

@ -31,6 +31,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"file_name" => Unary(file_name), "file_name" => Unary(file_name),
"file_stem" => Unary(file_stem), "file_stem" => Unary(file_stem),
"invocation_directory" => Nullary(invocation_directory), "invocation_directory" => Nullary(invocation_directory),
"invocation_directory_native" => Nullary(invocation_directory_native),
"join" => BinaryPlus(join), "join" => BinaryPlus(join),
"just_executable" => Nullary(just_executable), "just_executable" => Nullary(just_executable),
"justfile" => Nullary(justfile), "justfile" => Nullary(justfile),
@ -181,6 +182,19 @@ fn invocation_directory(context: &FunctionContext) -> Result<String, String> {
.map_err(|e| format!("Error getting shell path: {e}")) .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( fn join(
_context: &FunctionContext, _context: &FunctionContext,
base: &str, base: &str,

View File

@ -1,6 +1,6 @@
use super::*; 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_success(output);
assert_eq!(String::from_utf8_lossy(&output.stdout), stdout); assert_eq!(String::from_utf8_lossy(&output.stdout), stdout);
} }

View File

@ -1,6 +1,4 @@
use super::*; pub(crate) fn assert_success(output: &std::process::Output) {
pub(crate) fn assert_success(output: &Output) {
if !output.status.success() { if !output.status.success() {
eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr)); eprintln!("stderr: {}", String::from_utf8_lossy(&output.stderr));
eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout)); eprintln!("stdout: {}", String::from_utf8_lossy(&output.stdout));

View File

@ -22,13 +22,13 @@ fn current_dir() {
#[test] #[test]
fn exists() { fn exists() {
let tempdir = Test::new() let output = Test::new()
.no_justfile() .no_justfile()
.arg("--init") .arg("--init")
.stderr_regex("Wrote justfile to `.*`\n") .stderr_regex("Wrote justfile to `.*`\n")
.run(); .run();
Test::with_tempdir(tempdir) Test::with_tempdir(output.tempdir)
.no_justfile() .no_justfile()
.arg("--init") .arg("--init")
.status(EXIT_FAILURE) .status(EXIT_FAILURE)
@ -191,12 +191,12 @@ fn justfile_and_working_directory() {
#[test] #[test]
fn fmt_compatibility() { fn fmt_compatibility() {
let tempdir = Test::new() let output = Test::new()
.no_justfile() .no_justfile()
.arg("--init") .arg("--init")
.stderr_regex("Wrote justfile to `.*`\n") .stderr_regex("Wrote justfile to `.*`\n")
.run(); .run();
Test::with_tempdir(tempdir) Test::with_tempdir(output.tempdir)
.no_justfile() .no_justfile()
.arg("--unstable") .arg("--unstable")
.arg("--check") .arg("--check")

View File

@ -88,3 +88,18 @@ fn test_invocation_directory() {
panic!("test failed"); 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());
}
}

View File

@ -1,6 +1,9 @@
pub(crate) use { pub(crate) use {
crate::{ 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, cradle::input::Input,
executable_path::executable_path, executable_path::executable_path,
@ -18,7 +21,7 @@ pub(crate) use {
io::Write, io::Write,
iter, iter,
path::{Path, PathBuf, MAIN_SEPARATOR}, path::{Path, PathBuf, MAIN_SEPARATOR},
process::{Command, Output, Stdio}, process::{Command, Stdio},
str, str,
}, },
tempfile::TempDir, tempfile::TempDir,

View File

@ -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) struct Test {
pub(crate) args: Vec<String>, pub(crate) args: Vec<String>,
pub(crate) current_dir: PathBuf, pub(crate) current_dir: PathBuf,
@ -171,7 +176,7 @@ impl Test {
} }
impl Test { impl Test {
pub(crate) fn run(self) -> TempDir { pub(crate) fn run(self) -> Output {
if let Some(justfile) = &self.justfile { if let Some(justfile) = &self.justfile {
let justfile = unindent(justfile); let justfile = unindent(justfile);
fs::write(self.justfile_path(), justfile).unwrap(); fs::write(self.justfile_path(), justfile).unwrap();
@ -256,15 +261,11 @@ impl Test {
test_round_trip(self.tempdir.path()); 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) { fn test_round_trip(tmpdir: &Path) {