Skip to content
37 changes: 37 additions & 0 deletions tests/testsuite/build_script_env.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test(add): add test for CARGO_CFG_DEBUG_ASSERTIONS in dev profile

For future reference, in Conventional commits, add is the scope, or what this applies to. In this case you are sayign you are doing tests for cargo add.

Original file line number Diff line number Diff line change
Expand Up @@ -554,3 +554,40 @@ fn build_script_debug_assertions_override_release() {
// Release profile with debug-assertions explicitly enabled
p.cargo("check --release").run();
}

#[cargo_test]
fn build_script_debug_assertions_build_override() {
let build_rs = r#"
fn main() {
let profile = std::env::var("PROFILE").unwrap();
if profile == "debug" {
assert!(!cfg!(debug_assertions));
} else if profile == "release" {
assert!(cfg!(debug_assertions));
}
}
"#;

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
edition = "2024"

[profile.dev.build-override]
debug-assertions = false

[profile.release.build-override]
debug-assertions = true
"#,
)
.file("src/lib.rs", r#""#)
.file("build.rs", build_rs)
.build();

p.cargo("check").run();
p.cargo("check --release").run();
}
Comment on lines +558 to +593
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this checking dev and release? The test is only relevant for the profile build-override is being used on

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test is checking both to verify that build-override correctly affects build script compilation in both profiles. It's showing the override works bidirectionally:
You can disable debug assertions for build scripts in dev (normally they're on)
You can enable debug assertions for build scripts in release (normally they're off)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, for some reason I had missed the use of build-override in both.

We don't really need tests for each of these two profiles. Once we test things in one profile, its sufficient for all.