diff --git a/README.md b/README.md index b1fa158..8370fff 100644 --- a/README.md +++ b/README.md @@ -981,6 +981,10 @@ These functions can fail, for example if a path does not have an extension, whic - `clean(path)` - Simplify `path` by removing extra path separators, intermediate `.` components, and `..` where possible. `clean("foo//bar")` is `foo/bar`, `clean("foo/..")` is `.`, `clean("foo/./bar")` is `foo/bar`. +#### Filesystem Access + +- `path_exists(path)` - Returns `true` if the path points at an existing entity and `false` otherwise. Traverses symbolic links, and returns `false` if the path is inaccessible or points to a broken symlink. + ### Command Evaluation Using Backticks Backticks can be used to store the result of commands: diff --git a/src/function.rs b/src/function.rs index 0c044cf..eba04e7 100644 --- a/src/function.rs +++ b/src/function.rs @@ -30,6 +30,7 @@ lazy_static! { ("os", Nullary(os)), ("os_family", Nullary(os_family)), ("parent_directory", Unary(parent_directory)), + ("path_exists", Unary(path_exists)), ("quote", Unary(quote)), ("replace", Ternary(replace)), ("trim", Unary(trim)), @@ -210,6 +211,10 @@ fn parent_directory(_context: &FunctionContext, path: &str) -> Result Result { + Ok(Utf8Path::new(path).exists().to_string()) +} + fn quote(_context: &FunctionContext, s: &str) -> Result { Ok(format!("'{}'", s.replace('\'', "'\\''"))) } diff --git a/tests/functions.rs b/tests/functions.rs index abdcdf3..e499ea2 100644 --- a/tests/functions.rs +++ b/tests/functions.rs @@ -382,3 +382,24 @@ fn join_argument_count_error() { .status(EXIT_FAILURE) .run(); } + +#[test] +fn test_path_exists_filepath_exist() { + Test::new() + .tree(tree! { + testfile: "" + }) + .justfile("x := path_exists('testfile')") + .args(&["--evaluate", "x"]) + .stdout("true") + .run(); +} + +#[test] +fn test_path_exists_filepath_doesnt_exist() { + Test::new() + .justfile("x := path_exists('testfile')") + .args(&["--evaluate", "x"]) + .stdout("false") + .run(); +}