@@ -452,3 +452,142 @@ fn rerun_if_env_newly_added_in_config() {
452452"# ] ] )
453453 . run ( ) ;
454454}
455+
456+ #[ cargo_test]
457+ fn build_script_debug_assertions_dev ( ) {
458+ // Test that CARGO_CFG_DEBUG_ASSERTIONS is set in dev profile (default)
459+ let build_rs = r#"
460+ fn main() {
461+ let has_debug_assertions = std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_some();
462+ assert!(has_debug_assertions, "CARGO_CFG_DEBUG_ASSERTIONS should be set in dev profile");
463+ }
464+ "# ;
465+
466+ let p = project ( )
467+ . file ( "src/lib.rs" , r#""# )
468+ . file ( "build.rs" , build_rs)
469+ . build ( ) ;
470+
471+ // Default dev profile has debug-assertions enabled
472+ p. cargo ( "check" ) . run ( ) ;
473+ }
474+
475+ #[ cargo_test]
476+ fn build_script_debug_assertions_release ( ) {
477+ // Test that CARGO_CFG_DEBUG_ASSERTIONS is NOT set in release profile (default)
478+ let build_rs = r#"
479+ fn main() {
480+ let has_debug_assertions = std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_some();
481+ assert!(!has_debug_assertions, "CARGO_CFG_DEBUG_ASSERTIONS should NOT be set in release profile");
482+ }
483+ "# ;
484+
485+ let p = project ( )
486+ . file ( "src/lib.rs" , r#""# )
487+ . file ( "build.rs" , build_rs)
488+ . build ( ) ;
489+
490+ // Release profile has debug-assertions disabled by default
491+ p. cargo ( "check --release" ) . run ( ) ;
492+ }
493+
494+ #[ cargo_test]
495+ fn build_script_debug_assertions_override_dev ( ) {
496+ // Test that CARGO_CFG_DEBUG_ASSERTIONS respects profile overrides
497+ // Dev profile with debug-assertions explicitly DISABLED
498+ let build_rs = r#"
499+ fn main() {
500+ let has_debug_assertions = std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_some();
501+ assert!(!has_debug_assertions, "CARGO_CFG_DEBUG_ASSERTIONS should NOT be set when dev profile disables it");
502+ }
503+ "# ;
504+
505+ let p = project ( )
506+ . file (
507+ "Cargo.toml" ,
508+ r#"
509+ [package]
510+ name = "foo"
511+ version = "0.1.0"
512+ edition = "2024"
513+
514+ [profile.dev]
515+ debug-assertions = false
516+ "# ,
517+ )
518+ . file ( "src/lib.rs" , r#""# )
519+ . file ( "build.rs" , build_rs)
520+ . build ( ) ;
521+
522+ // Dev profile with debug-assertions explicitly disabled
523+ p. cargo ( "check" ) . run ( ) ;
524+ }
525+
526+ #[ cargo_test]
527+ fn build_script_debug_assertions_override_release ( ) {
528+ // Test that CARGO_CFG_DEBUG_ASSERTIONS respects profile overrides
529+ // Release profile with debug-assertions explicitly ENABLED
530+ let build_rs = r#"
531+ fn main() {
532+ let has_debug_assertions = std::env::var_os("CARGO_CFG_DEBUG_ASSERTIONS").is_some();
533+ assert!(has_debug_assertions, "CARGO_CFG_DEBUG_ASSERTIONS should be set when release profile enables it");
534+ }
535+ "# ;
536+
537+ let p = project ( )
538+ . file (
539+ "Cargo.toml" ,
540+ r#"
541+ [package]
542+ name = "foo"
543+ version = "0.1.0"
544+ edition = "2024"
545+
546+ [profile.release]
547+ debug-assertions = true
548+ "# ,
549+ )
550+ . file ( "src/lib.rs" , r#""# )
551+ . file ( "build.rs" , build_rs)
552+ . build ( ) ;
553+
554+ // Release profile with debug-assertions explicitly enabled
555+ p. cargo ( "check --release" ) . run ( ) ;
556+ }
557+
558+ #[ cargo_test]
559+ fn build_script_debug_assertions_build_override ( ) {
560+ let build_rs = r#"
561+ fn main() {
562+ let profile = std::env::var("PROFILE").unwrap();
563+ if profile == "debug" {
564+ assert!(!cfg!(debug_assertions));
565+ } else if profile == "release" {
566+ assert!(cfg!(debug_assertions));
567+ }
568+ }
569+ "# ;
570+
571+ let p = project ( )
572+ . file (
573+ "Cargo.toml" ,
574+ r#"
575+ [package]
576+ name = "foo"
577+ version = "0.1.0"
578+ edition = "2024"
579+
580+ [profile.dev.build-override]
581+ debug-assertions = false
582+
583+ [profile.release.build-override]
584+ debug-assertions = true
585+ "# ,
586+ )
587+ . file ( "src/lib.rs" , r#""# )
588+ . file ( "build.rs" , build_rs)
589+ . build ( ) ;
590+
591+ p. cargo ( "check" ) . run ( ) ;
592+ p. cargo ( "check --release" ) . run ( ) ;
593+ }
0 commit comments