Skip to content

Commit

Permalink
Improve cbindgen detection in tests
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jschwe committed Mar 11, 2023
1 parent 84a54d0 commit 01ab9e8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 39 deletions.
16 changes: 1 addition & 15 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -53,8 +40,7 @@ fn generate_tests() {

writeln!(
dst,
"test_file!({:?}, test_{}, {:?}, {:?});",
cbindgen_path,
"test_file!(test_{}, {:?}, {:?});",
identifier,
path_segment,
entry.path(),
Expand Down
16 changes: 2 additions & 14 deletions tests/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 7 additions & 10 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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 => {}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -183,7 +184,6 @@ fn run_compile_test(
generated_file.push(source_file);

let cbindgen_output = run_cbindgen(
cbindgen_path,
path,
&generated_file,
language,
Expand Down Expand Up @@ -234,7 +234,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")
Expand All @@ -247,7 +247,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,
Expand All @@ -260,7 +259,6 @@ fn test_file(cbindgen_path: &'static str, name: &'static str, filename: &'static
}

run_compile_test(
cbindgen_path,
name,
test,
tmp_dir,
Expand All @@ -274,7 +272,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,
Expand All @@ -287,10 +284,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);
}
};
}
Expand Down

0 comments on commit 01ab9e8

Please sign in to comment.