Skip to content

Commit d3edfd7

Browse files
committed
Add test for multiple OUT_DIR
1 parent a6c58d4 commit d3edfd7

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

tests/testsuite/build_scripts_multiple.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,135 @@ fn bar() {
764764
"#]])
765765
.run();
766766
}
767+
768+
#[cargo_test]
769+
fn multiple_out_dirs() {
770+
// Test to verify access to the `OUT_DIR` of the respective build scripts.
771+
772+
let p = project()
773+
.file(
774+
"Cargo.toml",
775+
r#"
776+
cargo-features = ["multiple-build-scripts"]
777+
778+
[package]
779+
name = "foo"
780+
version = "0.1.0"
781+
edition = "2024"
782+
build = ["build1.rs", "build2.rs"]
783+
"#,
784+
)
785+
.file(
786+
"src/main.rs",
787+
r#"
788+
include!(concat!(env!("OUT_DIR"), "/foo.rs"));
789+
fn main() {
790+
println!("{}", message());
791+
}
792+
"#,
793+
)
794+
.file(
795+
"build1.rs",
796+
r#"
797+
use std::env;
798+
use std::fs;
799+
use std::path::Path;
800+
801+
fn main() {
802+
let out_dir = env::var_os("OUT_DIR").unwrap();
803+
let dest_path = Path::new(&out_dir).join("foo.rs");
804+
fs::write(
805+
&dest_path,
806+
"pub fn message() -> &'static str {
807+
\"Hello, from Build Script 1!\"
808+
}
809+
"
810+
).unwrap();
811+
}"#,
812+
)
813+
.file(
814+
"build2.rs",
815+
r#"
816+
use std::env;
817+
use std::fs;
818+
use std::path::Path;
819+
820+
fn main() {
821+
let out_dir = env::var_os("OUT_DIR").unwrap();
822+
let dest_path = Path::new(&out_dir).join("foo.rs");
823+
fs::write(
824+
&dest_path,
825+
"pub fn message() -> &'static str {
826+
\"Hello, from Build Script 2!\"
827+
}
828+
"
829+
).unwrap();
830+
}"#,
831+
)
832+
.build();
833+
834+
p.cargo("run -v")
835+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
836+
.with_status(0)
837+
.with_stdout_data(str![[r#"
838+
Hello, from Build Script 2!
839+
840+
"#]])
841+
.run();
842+
}
843+
844+
#[cargo_test]
845+
fn duplicate_build_script_stems() {
846+
// Test to verify that duplicate build script file stems throws error.
847+
848+
let p = project()
849+
.file(
850+
"Cargo.toml",
851+
r#"
852+
cargo-features = ["multiple-build-scripts"]
853+
854+
[package]
855+
name = "foo"
856+
version = "0.1.0"
857+
edition = "2024"
858+
build = ["build1.rs", "foo/build1.rs"]
859+
"#,
860+
)
861+
.file("src/main.rs", "fn main() {}")
862+
.file("build1.rs", "fn main() {}")
863+
.file("foo/build1.rs", "fn main() {}")
864+
.build();
865+
866+
p.cargo("check -v")
867+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
868+
.with_status(101)
869+
.with_stderr_data(str![[r#"
870+
[WARNING] output filename collision.
871+
The build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)` has the same output filename as the build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)`.
872+
Colliding filename is: [ROOT]/foo/target/debug/build/foo-[HASH]/build_script_build1-[HASH]
873+
The targets should have unique names.
874+
Consider changing their names to be unique or compiling them separately.
875+
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
876+
[WARNING] output filename collision.
877+
The build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)` has the same output filename as the build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)`.
878+
Colliding filename is: [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build1
879+
The targets should have unique names.
880+
Consider changing their names to be unique or compiling them separately.
881+
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
882+
[WARNING] output filename collision.
883+
The build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)` has the same output filename as the build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)`.
884+
Colliding filename is: [ROOT]/foo/target/debug/build/foo-[HASH]/build_script_build1-[HASH].dwp
885+
The targets should have unique names.
886+
Consider changing their names to be unique or compiling them separately.
887+
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
888+
[WARNING] output filename collision.
889+
The build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)` has the same output filename as the build-script target `build-script-build1` in package `foo v0.1.0 ([ROOT]/foo)`.
890+
Colliding filename is: [ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build1.dwp
891+
The targets should have unique names.
892+
Consider changing their names to be unique or compiling them separately.
893+
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
894+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
895+
...
896+
"#]])
897+
.run();
898+
}

0 commit comments

Comments
 (0)