-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Revise generate function to return Result #4424
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
Revise generate function to return Result #4424
Conversation
…t/build.rs to test out the error handling logic
Fix an `unnecessary_transmutes` from a recent nightly
netbsd move from e! marcro to c_enum!
run `cargo clippy --all-targets` on `libc-test/build.rs`, and fix all default issues. ### Notes * `copy_dir_hotfix` had a `replace` parameter that was never used
run `cargo clippy --all-targets` on `ctest-test`, and fix all default issues. Also added a generic
linux: add constant PACKET_IGNORE_OUTGOING
chore: lint `libc-test/build.rs`
chore: lint `ctest-test` crate
Fix querying emcc on windows
Update pidfd constants and types (Linux 6.9-6.15)
chore: apply some clippy lints
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for picking this up!
The results of the new tests look good, but unfortunately the internal functions also need to be updated to return Result
rather than just catching panics.
ctest-test/build.rs
Outdated
let orig_out_dir = std::env::var_os("OUT_DIR"); | ||
std::env::remove_var("OUT_DIR"); | ||
|
||
let err1 = ctest::TestGenerator::new() | ||
.header("t1.h") | ||
.generate("src/t1.rs", "out_dir_gen.rs"); | ||
|
||
// Restore OUT_DIR | ||
if let Some(dir) = orig_out_dir { | ||
std::env::set_var("OUT_DIR", dir); | ||
} | ||
|
||
assert!(err1.is_err(), "Expected error when OUT_DIR is missing"); | ||
|
||
// Test error handling for invalid output path | ||
let err = ctest::TestGenerator::new() | ||
.header("t1.h") | ||
.include("src") | ||
.out_dir("/nonexistent_dir") // Should fail with permission error | ||
.generate("src/t1.rs", "out_path_gen.rs"); | ||
|
||
assert!(err.is_err(), "Expected error with invalid output path"); | ||
|
||
// Test parsing error | ||
// Create a temporary file with invalid Rust syntax | ||
let temp_dir = env::temp_dir(); | ||
let invalid_file = temp_dir.join("invalid.rs"); | ||
std::fs::write(&invalid_file, "fn invalid_syntax {").unwrap(); | ||
|
||
let err = ctest::TestGenerator::new() | ||
.header("t1.h") | ||
.include("src") | ||
.generate(&invalid_file, "parse_gen.rs"); | ||
|
||
assert!(err.is_err(), "Expected error when parsing invalid syntax"); | ||
let _ = std::fs::remove_file(invalid_file); | ||
|
||
// Test non-existent header | ||
let err = ctest::TestGenerator::new() | ||
.header("nonexistent_header.h") | ||
.include("src") | ||
.generate("src/t1.rs", "missing_header_gen.rs"); | ||
|
||
assert!(err.is_err(), "Expected error with non-existent header"); | ||
|
||
// Test invalid include path | ||
let err = ctest::TestGenerator::new() | ||
.header("t1.h") | ||
.include("nonexistent_directory") | ||
.generate("src/t1.rs", "invalid_include_gen.rs"); | ||
|
||
assert!(err.is_err(), "Expected error with invalid include path"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
build.rs
works for the test that runs the whole crate, but these assert!
tests should be able to work on their own. Could you move them to ctest-test/tests/all.rs
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your review! I'll file a commit to move them to ctest-test/tests/all.rs
soon.
ctest/src/lib.rs
Outdated
@@ -793,9 +795,9 @@ impl TestGenerator { | |||
/// let mut cfg = TestGenerator::new(); | |||
/// cfg.generate("../path/to/libfoo-sys/lib.rs", "all.rs"); | |||
/// ``` | |||
pub fn generate<P: AsRef<Path>>(&mut self, krate: P, out_file: &str) { | |||
pub fn generate<P: AsRef<Path>>(&mut self, krate: P, out_file: &str) -> Result<PathBuf> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be best if we could do this in a way that doesn't break semver, just because this repo currently has a tricky branch setup. Could generate
keep its signature, but move the body to a new fn try_generate(...) -> Result
? The existing generate
can just call try_generate
and then panic if there is a failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. I'll keep the generate
function signature and move them to try_generate
instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be easiest to switch our Error
to anyhow
's Error
. This gives us .context()
and .with_context()
which should make using ?
a lot easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok. Yeah I think using anyhow's Error is a more idiomatic way to handle error. I'll add anyhow as part of dependencies and make the code change soon
ctest/src/lib.rs
Outdated
let stem = out | ||
.file_stem() | ||
.ok_or_else(|| Error::from("Output file has no stem"))? | ||
.to_str() | ||
.ok_or_else(|| Error::from("Output filename is not valid UTF-8"))?; | ||
|
||
let parent = out | ||
.parent() | ||
.ok_or_else(|| Error::from("Output file has no parent directory"))?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After switching to anyhow
this will be a bit easier to use .context
and ?
ctest/src/lib.rs
Outdated
|
||
cfg.target(&target).out_dir(parent); | ||
|
||
match catch_unwind(|| cfg.compile(&format!("lib{stem}.a"))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not quite :) we want to change all internal functions to return a Result
as well so it's easier to know what might fail and for simpler testing.
It should be pretty easy to find+replace any .unwrap()
with ?
, then update the function signatures anywhere it complains.
Update Redox SA_ constants.
…H-Leung/libc into revise_generate_signature
…Add anyhow error handling. Revert expect call in generate API
Sorry - bad rebase here. Will close this MR and create a new one |
Description
As per #4369 , the generate function should return a Result instead of returning nothing
Checklist
libc-test/semver
have been updated*LAST
or*MAX
areincluded (see #3131)
cd libc-test && cargo test --target mytarget
);especially relevant for platforms that may not be checked in CI