Skip to content

Commit

Permalink
test(ui): avoid using Path::exists for better robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardosm committed Jun 7, 2024
1 parent 41b69c6 commit 8aa5d9a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 23 deletions.
25 changes: 14 additions & 11 deletions rsjsonnet/tests/ui/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,21 @@ fn gather_tests(root_path: &Path, tests_path: &Path) -> BTreeMap<PathBuf, defs::
params_file_name.push(".params.toml");
let params_path = current_dir.join(&params_file_name);

let params = if params_path.exists() {
let params = std::fs::read(&params_path).unwrap_or_else(|e| {
let params = match std::fs::read(&params_path) {
Ok(params) => {
let params = String::from_utf8(params).unwrap_or_else(|_| {
panic!("{params_path:?} is not valid UTF-8");
});
toml::from_str(&params).unwrap_or_else(|e| {
panic!("failed to parse {params_path:?}: {e}");
})
}
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
defs::TestParams::default()
}
Err(e) => {
panic!("failed to read {params_path:?}: {e}");
});
let params = String::from_utf8(params).unwrap_or_else(|_| {
panic!("{params_path:?} is not valid UTF-8");
});
toml::from_str(&params).unwrap_or_else(|e| {
panic!("failed to parse {params_path:?}: {e}");
})
} else {
defs::TestParams::default()
}
};

if !params.not_test {
Expand Down
34 changes: 22 additions & 12 deletions rsjsonnet/tests/ui/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ pub(crate) fn run(
stderr_name.push(".stderr");
let stderr_path = test_dir.join(stderr_name);

let expected_stderr = if stderr_path.exists() {
std::fs::read(&stderr_path).map_err(|e| format!("failed to read {stderr_path:?}: {e}"))?
} else {
Vec::new()
let expected_stderr = match std::fs::read(&stderr_path) {
Ok(data) => data,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Vec::new(),
Err(e) => return Err(format!("failed to read {stderr_path:?}: {e}")),
};

let expected_stdout = if stdout_path.exists() {
std::fs::read(&stdout_path).map_err(|e| format!("failed to read {stdout_path:?}: {e}"))?
} else if expected_stderr.is_empty() {
b"true\n".to_vec()
} else {
Vec::new()
let expected_stdout = match std::fs::read(&stdout_path) {
Ok(data) => data,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
if expected_stderr.is_empty() {
b"true\n".to_vec()
} else {
Vec::new()
}
}
Err(e) => return Err(format!("failed to read {stdout_path:?}: {e}")),
};

let mut expected_exit_code = test_params
Expand Down Expand Up @@ -93,7 +97,10 @@ pub(crate) fn run(
if (!cmd_output.stderr.is_empty() && cmd_output.stdout.is_empty())
|| (cmd_output.stderr.is_empty() && cmd_output.stdout == b"true\n")
{
if stdout_path.exists() {
if stdout_path
.try_exists()
.map_err(|e| format!("failed to check {stdout_path:?}: {e}"))?
{
std::fs::remove_file(&stdout_path)
.map_err(|e| format!("failed to remove {stdout_path:?}: {e}"))?;
}
Expand All @@ -103,7 +110,10 @@ pub(crate) fn run(
}

if cmd_output.stderr.is_empty() {
if stderr_path.exists() {
if stderr_path
.try_exists()
.map_err(|e| format!("failed to check {stderr_path:?}: {e}"))?
{
std::fs::remove_file(&stderr_path)
.map_err(|e| format!("failed to remove {stderr_path:?}: {e}"))?;
}
Expand Down

0 comments on commit 8aa5d9a

Please sign in to comment.