Make path_exists() relative to current directory (#1122)

This commit is contained in:
Casey Rodarmor 2022-02-27 19:09:31 -08:00 committed by GitHub
parent 88922b8974
commit 3118ce7211
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 5 deletions

View File

@ -154,7 +154,7 @@ fn just_executable(_context: &FunctionContext) -> Result<String, String> {
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<String, String> {
.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<String, String> {
.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<String, St
.ok_or_else(|| format!("Could not extract parent directory from `{}`", path))
}
fn path_exists(_context: &FunctionContext, path: &str) -> Result<String, String> {
Ok(Utf8Path::new(path).exists().to_string())
fn path_exists(context: &FunctionContext, path: &str) -> Result<String, String> {
Ok(
context
.search
.working_directory
.join(path)
.exists()
.to_string(),
)
}
fn quote(_context: &FunctionContext, s: &str) -> Result<String, String> {

View File

@ -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();
}