Skip to content

Commit

Permalink
Make path_exists() relative to current directory (#1122)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Feb 28, 2022
1 parent 88922b8 commit 3118ce7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
})
}
Expand All @@ -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()
)
})
}
Expand All @@ -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()
)
})
}
Expand All @@ -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> {
Expand Down
15 changes: 15 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

0 comments on commit 3118ce7

Please sign in to comment.