Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(ui): avoid using Path::exists for better robustness #44

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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