-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c14c2f9
commit dd06427
Showing
2 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters