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.
This commit is contained in:
Jonathan Schwender 2023-03-11 09:29:46 +01:00 committed by Emilio Cobos Álvarez
parent ea6e886f7b
commit cb42a00ab6
3 changed files with 11 additions and 46 deletions

View File

@ -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(),

View File

@ -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
}

View File

@ -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<Style>,
) -> Vec<u8> {
let program = Path::new(cbindgen_path);
let program = Path::new(CBINDGEN_PATH);
let mut command = Command::new(program);
match language {
Language::Cxx => {}
@ -144,7 +146,6 @@ const SKIP_WARNING_AS_ERROR_SUFFIX: &str = ".skip_warning_as_error";
#[allow(clippy::too_many_arguments)]
fn run_compile_test(
cbindgen_path: &'static str,
name: &'static str,
path: &Path,
tmp_dir: &Path,
@ -182,14 +183,7 @@ fn run_compile_test(
generated_file.push(source_file);
let cbindgen_output = run_cbindgen(
cbindgen_path,
path,
&generated_file,
language,
cpp_compat,
style,
);
let cbindgen_output = run_cbindgen(path, &generated_file, language, cpp_compat, style);
if cbindgen_outputs.contains(&cbindgen_output) {
// We already generated an identical file previously.
@ -234,7 +228,7 @@ fn run_compile_test(
}
}
fn test_file(cbindgen_path: &'static str, name: &'static str, filename: &'static str) {
fn test_file(name: &'static str, filename: &'static str) {
let test = Path::new(filename);
let tmp_dir = tempfile::Builder::new()
.prefix("cbindgen-test-output")
@ -247,7 +241,6 @@ fn test_file(cbindgen_path: &'static str, name: &'static str, filename: &'static
for cpp_compat in &[true, false] {
for style in &[Style::Type, Style::Tag, Style::Both] {
run_compile_test(
cbindgen_path,
name,
test,
tmp_dir,
@ -260,7 +253,6 @@ fn test_file(cbindgen_path: &'static str, name: &'static str, filename: &'static
}
run_compile_test(
cbindgen_path,
name,
test,
tmp_dir,
@ -274,7 +266,6 @@ fn test_file(cbindgen_path: &'static str, name: &'static str, filename: &'static
let mut cbindgen_outputs = HashSet::new();
for style in &[Style::Type, Style::Tag] {
run_compile_test(
cbindgen_path,
name,
test,
tmp_dir,
@ -287,10 +278,10 @@ fn test_file(cbindgen_path: &'static str, name: &'static str, filename: &'static
}
macro_rules! test_file {
($cbindgen_path:expr, $test_function_name:ident, $name:expr, $file:tt) => {
($test_function_name:ident, $name:expr, $file:tt) => {
#[test]
fn $test_function_name() {
test_file($cbindgen_path, $name, $file);
test_file($name, $file);
}
};
}