Skip to content

Commit 2c8b9fb

Browse files
committed
Add test for build script being run multiple times.
1 parent 3f06823 commit 2c8b9fb

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed

tests/testsuite/features2.rs

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,191 @@ fn decouple_dev_deps() {
421421
.masquerade_as_nightly_cargo()
422422
.run();
423423
}
424+
425+
#[cargo_test]
426+
fn build_script_runtime_features() {
427+
// Check that the CARGO_FEATURE_* environment variable is set correctly.
428+
//
429+
// This has a common dependency between build/normal/dev-deps, and it
430+
// queries which features it was built with in different circumstances.
431+
Package::new("common", "1.0.0")
432+
.feature("normal", &[])
433+
.feature("dev", &[])
434+
.feature("build", &[])
435+
.file(
436+
"build.rs",
437+
r#"
438+
fn is_set(name: &str) -> bool {
439+
std::env::var(name) == Ok("1".to_string())
440+
}
441+
442+
fn main() {
443+
let mut res = 0;
444+
if is_set("CARGO_FEATURE_NORMAL") {
445+
res |= 1;
446+
}
447+
if is_set("CARGO_FEATURE_DEV") {
448+
res |= 2;
449+
}
450+
if is_set("CARGO_FEATURE_BUILD") {
451+
res |= 4;
452+
}
453+
println!("cargo:rustc-cfg=RunCustomBuild=\"{}\"", res);
454+
455+
let mut res = 0;
456+
if cfg!(feature = "normal") {
457+
res |= 1;
458+
}
459+
if cfg!(feature = "dev") {
460+
res |= 2;
461+
}
462+
if cfg!(feature = "build") {
463+
res |= 4;
464+
}
465+
println!("cargo:rustc-cfg=CustomBuild=\"{}\"", res);
466+
}
467+
"#,
468+
)
469+
.file(
470+
"src/lib.rs",
471+
r#"
472+
pub fn foo() -> u32 {
473+
let mut res = 0;
474+
if cfg!(feature = "normal") {
475+
res |= 1;
476+
}
477+
if cfg!(feature = "dev") {
478+
res |= 2;
479+
}
480+
if cfg!(feature = "build") {
481+
res |= 4;
482+
}
483+
res
484+
}
485+
486+
pub fn build_time() -> u32 {
487+
#[cfg(RunCustomBuild="1")] return 1;
488+
#[cfg(RunCustomBuild="3")] return 3;
489+
#[cfg(RunCustomBuild="4")] return 4;
490+
#[cfg(RunCustomBuild="5")] return 5;
491+
#[cfg(RunCustomBuild="7")] return 7;
492+
}
493+
"#,
494+
)
495+
.publish();
496+
497+
let p = project()
498+
.file(
499+
"Cargo.toml",
500+
r#"
501+
[package]
502+
name = "foo"
503+
version = "0.1.0"
504+
edition = "2018"
505+
506+
[build-dependencies]
507+
common = {version="1.0", features=["build"]}
508+
509+
[dependencies]
510+
common = {version="1.0", features=["normal"]}
511+
512+
[dev-dependencies]
513+
common = {version="1.0", features=["dev"]}
514+
"#,
515+
)
516+
.file(
517+
"build.rs",
518+
r#"
519+
fn main() {
520+
assert_eq!(common::foo(), common::build_time());
521+
println!("cargo:rustc-cfg=from_build=\"{}\"", common::foo());
522+
}
523+
"#,
524+
)
525+
.file(
526+
"src/lib.rs",
527+
r#"
528+
pub fn foo() -> u32 {
529+
common::foo()
530+
}
531+
532+
pub fn build_time() -> u32 {
533+
common::build_time()
534+
}
535+
536+
#[test]
537+
fn test_lib() {
538+
assert_eq!(common::foo(), common::build_time());
539+
assert_eq!(common::foo(),
540+
std::env::var("CARGO_FEATURE_EXPECT").unwrap().parse().unwrap());
541+
}
542+
"#,
543+
)
544+
.file(
545+
"src/main.rs",
546+
r#"
547+
fn main() {
548+
assert_eq!(common::foo(), common::build_time());
549+
assert_eq!(common::foo(),
550+
std::env::var("CARGO_FEATURE_EXPECT").unwrap().parse().unwrap());
551+
}
552+
553+
#[test]
554+
fn test_bin() {
555+
assert_eq!(common::foo(), common::build_time());
556+
assert_eq!(common::foo(),
557+
std::env::var("CARGO_FEATURE_EXPECT").unwrap().parse().unwrap());
558+
}
559+
"#,
560+
)
561+
.file(
562+
"tests/t1.rs",
563+
r#"
564+
#[test]
565+
fn test_t1() {
566+
assert_eq!(common::foo(), common::build_time());
567+
assert_eq!(common::foo(),
568+
std::env::var("CARGO_FEATURE_EXPECT").unwrap().parse().unwrap());
569+
}
570+
571+
#[test]
572+
fn test_main() {
573+
// Features are unified for main when run with `cargo test`,
574+
// even with -Zfeatures=dev_dep.
575+
let s = std::process::Command::new("target/debug/foo")
576+
.status().unwrap();
577+
assert!(s.success());
578+
}
579+
"#,
580+
)
581+
.build();
582+
583+
// Old way, unifies all 3.
584+
p.cargo("run").env("CARGO_FEATURE_EXPECT", "7").run();
585+
586+
// normal + build unify
587+
p.cargo("run -Zfeatures=dev_dep")
588+
.env("CARGO_FEATURE_EXPECT", "5")
589+
.masquerade_as_nightly_cargo()
590+
.run();
591+
592+
// Normal only.
593+
p.cargo("run -Zfeatures=dev_dep,build_dep")
594+
.env("CARGO_FEATURE_EXPECT", "1")
595+
.masquerade_as_nightly_cargo()
596+
.run();
597+
598+
p.cargo("test").env("CARGO_FEATURE_EXPECT", "7").run();
599+
600+
// dev_deps are still unified with `cargo test`
601+
p.cargo("test -Zfeatures=dev_dep")
602+
.env("CARGO_FEATURE_EXPECT", "7")
603+
.masquerade_as_nightly_cargo()
604+
.run();
605+
606+
// normal + dev unify
607+
p.cargo("test -Zfeatures=build_dep")
608+
.env("CARGO_FEATURE_EXPECT", "3")
609+
.masquerade_as_nightly_cargo()
610+
.run();
611+
}

0 commit comments

Comments
 (0)