Skip to content

Commit

Permalink
Tweak tests
Browse files Browse the repository at this point in the history
  • Loading branch information
casey committed Dec 10, 2024
1 parent 68562e8 commit 966261a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 44 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1835,7 +1835,8 @@ which will halt execution.
- `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.
- `file_content(path)` - Returns the content of the file as a string.
- `read_to_string(path)`<sup>master</sup> - Returns the content of file at
`path` as a string.

##### Error Reporting

Expand Down
16 changes: 6 additions & 10 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"extension" => Unary(extension),
"file_name" => Unary(file_name),
"file_stem" => Unary(file_stem),
"file_content" => Unary(file_content),
"home_directory" => Nullary(|_| dir("home", dirs::home_dir)),
"invocation_directory" => Nullary(invocation_directory),
"invocation_directory_native" => Nullary(invocation_directory_native),
Expand All @@ -88,6 +87,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"path_exists" => Unary(path_exists),
"prepend" => Binary(prepend),
"quote" => Unary(quote),
"read_to_string" => Unary(read_to_string),
"replace" => Ternary(replace),
"replace_regex" => Ternary(replace_regex),
"semver_matches" => Binary(semver_matches),
Expand Down Expand Up @@ -173,15 +173,6 @@ fn blake3_file(context: Context, path: &str) -> FunctionResult {
Ok(hasher.finalize().to_string())
}

fn file_content(context: Context, filename: &str) -> FunctionResult {
let path = context.evaluator.context.working_directory().join(filename);
let contents = fs::read_to_string(path);
if let Err(err) = &contents {
return Err(format!("error reading file `{filename}`: {err}"));
}
Ok(contents.unwrap())
}

fn canonicalize(context: Context, path: &str) -> FunctionResult {
let canonical = std::fs::canonicalize(context.evaluator.context.working_directory().join(path))
.map_err(|err| format!("I/O error canonicalizing path: {err}"))?;
Expand Down Expand Up @@ -538,6 +529,11 @@ fn quote(_context: Context, s: &str) -> FunctionResult {
Ok(format!("'{}'", s.replace('\'', "'\\''")))
}

fn read_to_string(context: Context, filename: &str) -> FunctionResult {
fs::read_to_string(context.evaluator.context.working_directory().join(filename))
.map_err(|err| format!("I/O error reading `{filename}`: {err}"))
}

fn replace(_context: Context, s: &str, from: &str, to: &str) -> FunctionResult {
Ok(s.replace(from, to))
}
Expand Down
53 changes: 20 additions & 33 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -748,39 +748,6 @@ fn sha256_file() {
.run();
}

#[test]
fn file_content() {
Test::new()
.justfile("x := sha256(file_content('sub/shafile'))")
.tree(tree! {
sub: {
shafile: "just is great\n",
}
})
.current_dir("sub")
.args(["--evaluate", "x"])
.stdout("177b3d79aaafb53a7a4d7aaba99a82f27c73370e8cb0295571aade1e4fea1cd2")
.run();
}

#[test]
fn file_content_file_not_found() {
Test::new()
.justfile("x := sha256(file_content('sub/shafile_NON_EXISTENT'))")
.args(["--evaluate", "x"])
.stderr(
"
error: Call to function `file_content` failed: error reading file `sub/shafile_NON_EXISTENT`: No such file or directory (os error 2)
——▶ justfile:1:13
1 │ x := sha256(file_content('sub/shafile_NON_EXISTENT'))
│ ^^^^^^^^^^^^
",
)
.status(EXIT_FAILURE)
.run();
}

#[test]
fn just_pid() {
let Output { stdout, pid, .. } = Test::new()
Expand Down Expand Up @@ -1291,3 +1258,23 @@ fn style_unknown() {
.status(EXIT_FAILURE)
.run();
}

#[test]
fn read_to_string() {
Test::new()
.justfile("foo := read_to_string('bar')")
.write("bar", "baz")
.args(["--evaluate", "foo"])
.stdout("baz")
.run();
}

#[test]
fn read_to_string_not_found() {
Test::new()
.justfile("foo := read_to_string('bar')")
.args(["--evaluate", "foo"])
.stderr_regex(r"error: Call to function `read_to_string` failed: I/O error reading `bar`: .*")
.status(EXIT_FAILURE)
.run();
}

0 comments on commit 966261a

Please sign in to comment.