Skip to content

Commit e9df793

Browse files
committed
Add tests for multiple build scripts execution
1 parent 8f80388 commit e9df793

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

tests/testsuite/build_scripts_multiple.rs

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,205 @@ path = "src/main.rs"
409409
.run();
410410
}
411411

412+
#[cargo_test]
413+
fn custom_build_script_first_index_script_failed() {
414+
// In this, the script that is at first index in the build script array fails
415+
let p = project()
416+
.file(
417+
"Cargo.toml",
418+
r#"
419+
cargo-features = ["multiple-build-scripts"]
420+
421+
[package]
422+
name = "foo"
423+
version = "0.1.0"
424+
edition = "2024"
425+
426+
build = ["build1.rs", "build2.rs"]
427+
"#,
428+
)
429+
.file("src/main.rs", "fn main() {}")
430+
.file("build1.rs", "fn main() { std::process::exit(101); }")
431+
.file("build2.rs", "fn main() {}")
432+
.build();
433+
434+
p.cargo("check -v")
435+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
436+
.with_status(101)
437+
.with_stderr_data(str![[r#"
438+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
439+
[RUNNING] `rustc --crate-name build_script_build1 --edition=2024 build1.rs [..]--crate-type bin [..]`
440+
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build1`
441+
[ERROR] failed to run custom build command for `foo v0.1.0 ([ROOT]/foo)`
442+
443+
Caused by:
444+
process didn't exit successfully: `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build1` ([EXIT_STATUS]: 101)
445+
446+
"#]])
447+
.run();
448+
}
449+
450+
#[cargo_test]
451+
fn custom_build_script_second_index_script_failed() {
452+
// In this, the script that is at second index in the build script array fails
453+
// This test was necessary because earlier, the program failed only if first script failed.
454+
let p = project()
455+
.file(
456+
"Cargo.toml",
457+
r#"
458+
cargo-features = ["multiple-build-scripts"]
459+
460+
[package]
461+
name = "foo"
462+
version = "0.1.0"
463+
edition = "2024"
464+
465+
build = ["build1.rs", "build2.rs"]
466+
"#,
467+
)
468+
.file("src/main.rs", "fn main() {}")
469+
.file("build1.rs", "fn main() {}")
470+
.file("build2.rs", "fn main() { std::process::exit(101); }")
471+
.build();
472+
473+
p.cargo("check -v")
474+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
475+
.with_status(0)
476+
.with_stderr_data(str![[r#"
477+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
478+
[RUNNING] `rustc --crate-name build_script_build1 --edition=2024 build1.rs [..]--crate-type bin [..]`
479+
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build1`
480+
[RUNNING] `rustc --crate-name foo --edition=2024 src/main.rs [..] --crate-type bin [..]`
481+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
482+
483+
"#]])
484+
.run();
485+
}
486+
487+
#[cargo_test]
488+
fn build_script_with_conflicting_environment_variables() {
489+
// In this, multiple scripts set different values to same environment variables
490+
let p = project()
491+
.file(
492+
"Cargo.toml",
493+
r#"
494+
cargo-features = ["multiple-build-scripts"]
495+
496+
[package]
497+
name = "foo"
498+
version = "0.1.0"
499+
edition = "2024"
500+
501+
build = ["build1.rs", "build2.rs"]
502+
"#,
503+
)
504+
.file(
505+
"src/main.rs",
506+
r#"
507+
const FOO: &'static str = env!("FOO");
508+
fn main() {
509+
println!("{}", FOO);
510+
}
511+
"#,
512+
)
513+
.file(
514+
"build1.rs",
515+
r#"fn main() { println!("cargo::rustc-env=FOO=bar1"); }"#,
516+
)
517+
.file(
518+
"build2.rs",
519+
r#"fn main() { println!("cargo::rustc-env=FOO=bar2"); }"#,
520+
)
521+
.build();
522+
523+
p.cargo("run -v")
524+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
525+
.with_status(0)
526+
.with_stdout_data(str![[r#"
527+
bar1
528+
529+
"#]])
530+
.run();
531+
}
532+
533+
#[cargo_test]
534+
fn build_script_with_conflicting_out_dirs() {
535+
// In this, multiple scripts create file with same name in their respective OUT_DIR.
536+
537+
let p = project()
538+
.file(
539+
"Cargo.toml",
540+
r#"
541+
cargo-features = ["multiple-build-scripts"]
542+
543+
[package]
544+
name = "foo"
545+
version = "0.1.0"
546+
edition = "2024"
547+
548+
build = ["build1.rs", "build2.rs"]
549+
"#,
550+
)
551+
// OUT_DIR is set to the lexicographically largest build script's OUT_DIR by default
552+
.file(
553+
"src/main.rs",
554+
r#"
555+
include!(concat!(env!("OUT_DIR"), "/foo.rs"));
556+
fn main() {
557+
println!("{}", message());
558+
}
559+
"#,
560+
)
561+
.file(
562+
"build1.rs",
563+
r#"
564+
use std::env;
565+
use std::fs;
566+
use std::path::Path;
567+
568+
fn main() {
569+
let out_dir = env::var_os("OUT_DIR").unwrap();
570+
let dest_path = Path::new(&out_dir).join("foo.rs");
571+
fs::write(
572+
&dest_path,
573+
"pub fn message() -> &'static str {
574+
\"Hello, from Build Script 1!\"
575+
}
576+
"
577+
).unwrap();
578+
}"#,
579+
)
580+
.file(
581+
"build2.rs",
582+
r#"
583+
use std::env;
584+
use std::fs;
585+
use std::path::Path;
586+
587+
fn main() {
588+
let out_dir = env::var_os("OUT_DIR").unwrap();
589+
let dest_path = Path::new(&out_dir).join("foo.rs");
590+
fs::write(
591+
&dest_path,
592+
"pub fn message() -> &'static str {
593+
\"Hello, from Build Script 2!\"
594+
}
595+
"
596+
).unwrap();
597+
}"#,
598+
)
599+
.build();
600+
601+
p.cargo("run -v")
602+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
603+
.with_status(0)
604+
.with_stdout_data(str![[r#"
605+
Hello, from Build Script 1!
606+
607+
"#]])
608+
.run();
609+
}
610+
412611
#[cargo_test]
413612
fn rerun_untracks_other_files() {
414613
let p = project()

0 commit comments

Comments
 (0)