From 3118ce7211aee107b2e509b95dc6b9574566eeda Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Sun, 27 Feb 2022 19:09:31 -0800 Subject: [PATCH] Make path_exists() relative to current directory (#1122) --- src/function.rs | 17 ++++++++++++----- tests/functions.rs | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/function.rs b/src/function.rs index eba04e7539..943c534707 100644 --- a/src/function.rs +++ b/src/function.rs @@ -154,7 +154,7 @@ fn just_executable(_context: &FunctionContext) -> Result { exe_path.to_str().map(str::to_owned).ok_or_else(|| { format!( "Executable path is not valid unicode: {}", - exe_path.to_string_lossy() + exe_path.display() ) }) } @@ -168,7 +168,7 @@ fn justfile(context: &FunctionContext) -> Result { .ok_or_else(|| { format!( "Justfile path is not valid unicode: {}", - context.search.justfile.to_string_lossy() + context.search.justfile.display() ) }) } @@ -187,7 +187,7 @@ fn justfile_directory(context: &FunctionContext) -> Result { .ok_or_else(|| { format!( "Justfile directory is not valid unicode: {}", - justfile_directory.to_string_lossy() + justfile_directory.display() ) }) } @@ -211,8 +211,15 @@ fn parent_directory(_context: &FunctionContext, path: &str) -> Result Result { - Ok(Utf8Path::new(path).exists().to_string()) +fn path_exists(context: &FunctionContext, path: &str) -> Result { + Ok( + context + .search + .working_directory + .join(path) + .exists() + .to_string(), + ) } fn quote(_context: &FunctionContext, s: &str) -> Result { diff --git a/tests/functions.rs b/tests/functions.rs index e499ea21e2..ffbf2f60f2 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -403,3 +403,18 @@ fn test_path_exists_filepath_doesnt_exist() { .stdout("false") .run(); } + +#[test] +fn path_exists_subdir() { + Test::new() + .tree(tree! { + foo: "", + bar: { + } + }) + .justfile("x := path_exists('foo')") + .current_dir("bar") + .args(&["--evaluate", "x"]) + .stdout("true") + .run(); +}