From c613aff97affce386ab4209b6b06f5efa1ba030f Mon Sep 17 00:00:00 2001 From: Jonathan Schwender Date: Sat, 11 Mar 2023 09:29:46 +0100 Subject: [PATCH 1/2] Improve cbindgen detection in tests Since our MSRV is now Rust 1.54, we can rely on cargo setting `CARGO_BIN_EXE_cbindgen` to the cbindgen path in integration tests. This is more reliable than guessing the path, since cargo knows where it placed the bin. --- build.rs | 16 +--------------- tests/profile.rs | 16 ++-------------- tests/tests.rs | 25 ++++++++----------------- 3 files changed, 11 insertions(+), 46 deletions(-) diff --git a/build.rs b/build.rs index 5644b02c1..e8257068d 100644 --- a/build.rs +++ b/build.rs @@ -16,19 +16,6 @@ fn generate_tests() { println!("cargo:rerun-if-changed={}", tests_dir.display()); - // Try to make a decent guess at where our binary will end up in. - // - // TODO(emilio): Ideally running tests will just use the library-version of - // cbindgen instead of the built binary. - let cbindgen_path = out_dir - .parent() - .unwrap() - .parent() - .unwrap() - .parent() - .unwrap() - .join("cbindgen"); - for entry in entries { let path_segment = if entry.file_type().unwrap().is_file() { match entry.path().extension().and_then(OsStr::to_str) { @@ -53,8 +40,7 @@ fn generate_tests() { writeln!( dst, - "test_file!({:?}, test_{}, {:?}, {:?});", - cbindgen_path, + "test_file!(test_{}, {:?}, {:?});", identifier, path_segment, entry.path(), diff --git a/tests/profile.rs b/tests/profile.rs index 4e8b489d2..69433a2be 100644 --- a/tests/profile.rs +++ b/tests/profile.rs @@ -40,26 +40,14 @@ fn build_using_bin(extra_args: &[&str]) -> tempfile::TempDir { .tempdir() .expect("Creating tmp dir failed"); - let cbindgen_path = match option_env!("CARGO_BIN_EXE_cbindgen") { - Some(path) => path.into(), - None => { - // We must be on an older version of Rust. - // Guess where cbindgen would be relative to OUT_DIR. - let mut path = PathBuf::from(env!("OUT_DIR")); - path.pop(); - path.pop(); - path.pop(); - path.push("cbindgen"); - path.into_os_string() - } - }; + let cbindgen_path = env!("CARGO_BIN_EXE_cbindgen"); Command::new(cbindgen_path) .current_dir(expand_dep_test_dir) .env("CARGO_EXPAND_TARGET_DIR", tmp_dir.path()) .args(extra_args) .output() - .expect("build should succed"); + .expect("build should succeed"); tmp_dir } diff --git a/tests/tests.rs b/tests/tests.rs index 88a1c400e..a027a295d 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -6,6 +6,9 @@ use std::path::Path; use std::process::Command; use std::{env, fs, str}; +// Set automatically by cargo for integration tests +static CBINDGEN_PATH: &str = env!("CARGO_BIN_EXE_cbindgen"); + fn style_str(style: Style) -> &'static str { match style { Style::Both => "both", @@ -15,14 +18,13 @@ fn style_str(style: Style) -> &'static str { } fn run_cbindgen( - cbindgen_path: &'static str, path: &Path, output: &Path, language: Language, cpp_compat: bool, style: Option