Skip to content

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

Closed

Conversation

Owen-CH-Leung
Copy link

Description

As per #4369 , the generate function should return a Result instead of returning nothing

Checklist

  • Relevant tests in libc-test/semver have been updated
  • No placeholder or unstable values like *LAST or *MAX are
    included (see #3131)
  • Tested locally (cd libc-test && cargo test --target mytarget);
    especially relevant for platforms that may not be checked in CI

@rustbot
Copy link
Collaborator

rustbot commented Apr 19, 2025

r? @tgross35

rustbot has assigned @tgross35.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added ctest Issues relating to the ctest crate S-waiting-on-review labels Apr 19, 2025
@Owen-CH-Leung Owen-CH-Leung changed the title Revise generate function to return Result. Add unit test in ctest-tes… Revise generate function to return Result Apr 19, 2025
@Owen-CH-Leung Owen-CH-Leung marked this pull request as ready for review April 19, 2025 12:25
devnexen and others added 15 commits April 19, 2025 22:16
Fix an `unnecessary_transmutes` from a recent nightly
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
Update pidfd constants and types (Linux 6.9-6.15)
chore: apply some clippy lints
Copy link
Contributor

@tgross35 tgross35 left a 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.

Comment on lines 56 to 107
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");
Copy link
Contributor

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?

Copy link
Author

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> {
Copy link
Contributor

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.

Copy link
Author

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

Copy link
Contributor

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.

Copy link
Author

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
Comment on lines 856 to 864
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"))?;
Copy link
Contributor

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"))) {
Copy link
Contributor

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.

@tgross35 tgross35 added the stable-unneeded This PR is applied to main but already exists on libc-0.2 label Apr 25, 2025
@rustbot
Copy link
Collaborator

rustbot commented Apr 26, 2025

Some changes occurred in solarish module

cc @jclulow, @pfmooney

Some changes occurred in the Android module

cc @maurer

Some changes occurred in OpenBSD module

cc @semarie

@Owen-CH-Leung
Copy link
Author

Sorry - bad rebase here. Will close this MR and create a new one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants