Skip to content

Commit

Permalink
Fix: Ignore CARGO_BUILD_TARGET in tests
Browse files Browse the repository at this point in the history
When build target triples are explicitly specified, the layout of the
target directory is changed from `target/...` to `target/<triple>/...`.

See: https://doc.rust-lang.org/cargo/guide/build-cache.html

This caused bin tests failures in `tests/profile.rs`. This patch
proposes that we simply ignore the `CARGO_BUILD_TARGET` env var in
tests to avoid such problems.

A new test is introduced to ensure the correct behavior.
  • Loading branch information
bryango committed Oct 11, 2024
1 parent 3ed9434 commit f8efe01
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ fn build_using_bin(extra_args: &[&str]) -> tempfile::TempDir {
Command::new(cbindgen_path)
.current_dir(expand_dep_test_dir)
.env("CARGO_EXPAND_TARGET_DIR", tmp_dir.path())
.env_remove("CARGO_BUILD_TARGET")
// ^ avoid unexpected change of layout of the target directory;
// ... see: https://doc.rust-lang.org/cargo/guide/build-cache.html
.args(extra_args)
.output()
.expect("build should succeed");
Expand Down Expand Up @@ -87,6 +90,18 @@ fn bin_default_uses_debug_build() {
assert_eq!(get_contents_of_dir(target_dir.path()), &["debug"]);
}

#[test]
fn bin_ignore_cargo_build_target_in_tests() {
use std::env;
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();
}

#[test]
fn bin_explicit_debug_build() {
let target_dir = build_using_bin(&["--profile", "debug"]);
Expand Down

0 comments on commit f8efe01

Please sign in to comment.