Skip to content

Commit

Permalink
Fix: Ignore CARGO_BUILD_TARGET in lib tests too
Browse files Browse the repository at this point in the history
This follows from the previous commit. It turns out that
`std::env::set_var` is unsafe: the `set_var` in bin tests leaked
through the threads and caused `lib` tests to fail.

To mitigate this problem we simply remove the `CARGO_BUILD_TARGET`
variable in lib tests as well. This also improves correctness.
Meanwhile we put `std::env::set_var` and `remove_var` in unsafe blocks
as it would be required by Rust 2024.
  • Loading branch information
bryango committed Oct 11, 2024
1 parent f8efe01 commit c009064
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions tests/profile.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cbindgen::*;

use serial_test::serial;
use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;

Expand All @@ -17,7 +18,12 @@ fn build_using_lib(config: fn(Builder) -> Builder) -> tempfile::TempDir {
.tempdir()
.expect("Creating tmp dir failed");

std::env::set_var("CARGO_EXPAND_TARGET_DIR", tmp_dir.path());
unsafe {
env::set_var("CARGO_EXPAND_TARGET_DIR", tmp_dir.path());
env::remove_var("CARGO_BUILD_TARGET");
// ^ avoid unexpected change of layout of the target directory;
// ... see: https://doc.rust-lang.org/cargo/guide/build-cache.html
}
let builder = Builder::new()
.with_config(Config::from_file(expand_dep_test_dir.join("cbindgen.toml")).unwrap())
.with_crate(expand_dep_test_dir);
Expand Down Expand Up @@ -92,14 +98,16 @@ fn bin_default_uses_debug_build() {

#[test]
fn bin_ignore_cargo_build_target_in_tests() {
use std::env;
env::set_var("CARGO_BUILD_TARGET", "x86_64-unknown-linux-gnu");
unsafe {
env::set_var("CARGO_BUILD_TARGET", "x86_64-unknown-linux-gnu");
}
assert_eq!(
env::var("CARGO_BUILD_TARGET"),
Ok("x86_64-unknown-linux-gnu".into())
);
// ^ this env var should be ignored:
bin_default_uses_debug_build();
lib_default_uses_debug_build();
}

#[test]
Expand Down

0 comments on commit c009064

Please sign in to comment.