Add invocation_directory_native() (#1507)
This commit is contained in:
parent
1d02f0ef80
commit
182ef134d9
@ -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,
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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));
|
||||
|
@ -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")
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -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...");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user