Skip to content

Commit

Permalink
Add path_exists() function (#1106)
Browse files Browse the repository at this point in the history
  • Loading branch information
heavelock authored Feb 21, 2022
1 parent b4a0a80 commit 6271e94
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 5 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -210,6 +211,10 @@ 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 quote(_context: &FunctionContext, s: &str) -> Result<String, String> {
Ok(format!("'{}'", s.replace('\'', "'\\''")))
}
Expand Down
21 changes: 21 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 6271e94

Please sign in to comment.