Skip to content

Commit

Permalink
Added build_dir tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ranger-ross committed Feb 1, 2025
1 parent c14c2f9 commit dd06427
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 0 deletions.
110 changes: 110 additions & 0 deletions tests/testsuite/build_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! Tests for `build.build-dir` config property.
use std::path::PathBuf;

use cargo_test_support::prelude::*;
use cargo_test_support::project;

#[cargo_test]
fn binary_with_debug() {
let p = project()
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
.file(
".cargo/config.toml",
r#"
[build]
build-dir = "build"
"#,
)
.build();

p.cargo("build -Z unstable-options -Z build-dir")
.masquerade_as_nightly_cargo(&["build-dir"])
.enable_mac_dsym()
.run();

assert_build_dir(p.root().join("build"), "debug", true);
assert_build_dir(p.root().join("target"), "debug", false);

// Verify the binary was copied to the `target` dir
assert!(p.root().join("target/debug/foo").is_file());
}

#[cargo_test]
fn binary_with_release() {
let p = project()
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
.file(
".cargo/config.toml",
r#"
[build]
build-dir = "build"
"#,
)
.build();

p.cargo("build -Z unstable-options -Z build-dir --release")
.masquerade_as_nightly_cargo(&["build-dir"])
.enable_mac_dsym()
.run();

assert_build_dir(p.root().join("build"), "release", true);
assert_build_dir(p.root().join("target"), "release", false);

// Verify the binary was copied to the `target` dir
assert!(p.root().join("target/release/foo").is_file());
}

#[cargo_test]
fn should_default_to_target() {
let p = project()
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
.build();

p.cargo("build -Z unstable-options -Z build-dir")
.masquerade_as_nightly_cargo(&["build-dir"])
.enable_mac_dsym()
.run();

assert_build_dir(p.root().join("target"), "debug", true);
// Verify the binary exists in the correct location
assert!(p.root().join("target/debug/foo").is_file());
}

#[track_caller]
fn assert_build_dir(path: PathBuf, profile: &str, is_build_dir: bool) {
println!("checking if {path:?} is a build directory ({is_build_dir})");
// For things that are in both `target` and the build directory we only check if they are
// present if `is_build_dir` is true.
if is_build_dir {
assert!(path.join("CACHEDIR.TAG").is_file());
assert_eq!(is_build_dir, path.join(profile).is_dir());
}

let error_message = |dir: &str| {
if is_build_dir {
format!("`{dir}` dir was expected but not found")
} else {
format!("`{dir}` dir was not expected but was found")
}
};

assert_eq!(
is_build_dir,
path.join(profile).join("deps").is_dir(),
"{}",
error_message("deps")
);
assert_eq!(
is_build_dir,
path.join(profile).join("build").is_dir(),
"{}",
error_message("build")
);
assert_eq!(
is_build_dir,
path.join(profile).join("incremental").is_dir(),
"{}",
error_message("incremental")
);
}
1 change: 1 addition & 0 deletions tests/testsuite/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod bad_manifest_path;
mod bench;
mod binary_name;
mod build;
mod build_dir;
mod build_plan;
mod build_script;
mod build_script_env;
Expand Down

0 comments on commit dd06427

Please sign in to comment.