Skip to content

Commit db8dbaf

Browse files
committed
Add initial tests for multiple build scripts (not implemented yet)
1 parent 126b3a1 commit db8dbaf

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed
Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
//! Tests for multiple build scripts feature.
2+
3+
use cargo_test_support::git;
4+
use cargo_test_support::prelude::*;
5+
use cargo_test_support::str;
6+
use cargo_test_support::{project, Project};
7+
8+
#[cargo_test]
9+
fn build_without_feature_enabled_aborts_with_error() {
10+
let p = project()
11+
.file(
12+
"Cargo.toml",
13+
r#"
14+
[package]
15+
name = "foo"
16+
version = "0.1.0"
17+
edition = "2024"
18+
build = ["build1.rs", "build2.rs"]
19+
"#,
20+
)
21+
.file("src/main.rs", "fn main() {}")
22+
.file("build1.rs", "fn main() {}")
23+
.file("build2.rs", "fn main() {}")
24+
.build();
25+
p.cargo("check")
26+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
27+
.with_status(101)
28+
.with_stderr_data(str![[r#"
29+
[ERROR] invalid type: sequence, expected a boolean or string
30+
--> Cargo.toml:6:25
31+
|
32+
6 | build = ["build1.rs", "build2.rs"]
33+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
|
35+
36+
"#]])
37+
.run();
38+
}
39+
40+
fn basic_empty_project() -> Project {
41+
project()
42+
.file(
43+
"Cargo.toml",
44+
r#"
45+
cargo-features = ["multiple-build-scripts"]
46+
47+
[package]
48+
name = "foo"
49+
version = "0.1.0"
50+
edition = "2024"
51+
build = ["build1.rs", "build2.rs"]
52+
"#,
53+
)
54+
.file("src/main.rs", "fn main() {}")
55+
.file("build1.rs", "fn main() {}")
56+
.file("build2.rs", "fn main() {}")
57+
.build()
58+
}
59+
60+
#[cargo_test]
61+
fn empty_multiple_build_script_project() {
62+
let p = basic_empty_project();
63+
p.cargo("check")
64+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
65+
.with_status(101)
66+
.with_stderr_data(str![[r#"
67+
[ERROR] invalid type: sequence, expected a boolean or string
68+
--> Cargo.toml:8:25
69+
|
70+
8 | build = ["build1.rs", "build2.rs"]
71+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
72+
|
73+
74+
"#]])
75+
.run();
76+
}
77+
78+
#[cargo_test]
79+
fn multiple_build_scripts_metadata() {
80+
let p = basic_empty_project();
81+
p.cargo("metadata --format-version=1")
82+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
83+
.with_status(101)
84+
.with_stderr_data(str![[r#"
85+
[ERROR] invalid type: sequence, expected a boolean or string
86+
--> Cargo.toml:8:25
87+
|
88+
8 | build = ["build1.rs", "build2.rs"]
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
90+
|
91+
92+
"#]])
93+
.run();
94+
}
95+
96+
#[cargo_test]
97+
fn verify_package_multiple_build_scripts() {
98+
let p = project()
99+
.file(
100+
"Cargo.toml",
101+
r#"
102+
cargo-features = ["multiple-build-scripts"]
103+
104+
[package]
105+
name = "foo"
106+
version = "0.1.0"
107+
edition = "2024"
108+
license = "MIT"
109+
description = "foo"
110+
documentation = "docs.rs/foo"
111+
authors = []
112+
113+
build = ["build1.rs", "build2.rs"]
114+
include = [ "src/main.rs", "build1.rs" ]
115+
"#,
116+
)
117+
.file("src/main.rs", "fn main() {}")
118+
.file("build1.rs", "fn main() {}")
119+
.file("build2.rs", "fn main() {}")
120+
.build();
121+
122+
p.cargo("package")
123+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
124+
.with_status(101)
125+
.with_stderr_data(str![[r#"
126+
[ERROR] invalid type: sequence, expected a boolean or string
127+
--> Cargo.toml:13:25
128+
|
129+
13 | build = ["build1.rs", "build2.rs"]
130+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
131+
|
132+
133+
"#]])
134+
.run();
135+
}
136+
137+
#[cargo_test]
138+
fn verify_vendor_multiple_build_scripts() {
139+
let git_project = git::new("dep", |project| {
140+
project
141+
.file(
142+
"Cargo.toml",
143+
r#"
144+
cargo-features = ["multiple-build-scripts"]
145+
146+
[package]
147+
name = "dep"
148+
version = "0.1.0"
149+
edition = "2024"
150+
license = "MIT"
151+
description = "dependency of foo"
152+
documentation = "docs.rs/dep"
153+
authors = []
154+
155+
build = ["build1.rs", "build2.rs"]
156+
include = [ "src/main.rs", "build1.rs" ]
157+
"#,
158+
)
159+
.file("src/main.rs", "fn main() {}")
160+
.file("build1.rs", "fn main() {}")
161+
.file("build2.rs", "fn main() {}")
162+
});
163+
164+
let p = project()
165+
.file(
166+
"Cargo.toml",
167+
&format!(
168+
r#"
169+
cargo-features = ["multiple-build-scripts"]
170+
171+
[package]
172+
name = "foo"
173+
version = "0.1.0"
174+
edition = "2024"
175+
176+
[dependencies.dep]
177+
git = '{}'
178+
"#,
179+
git_project.url()
180+
),
181+
)
182+
.file("src/main.rs", "fn main() {}")
183+
.build();
184+
185+
p.cargo("vendor --respect-source-config")
186+
.masquerade_as_nightly_cargo(&["multiple-build-scripts"])
187+
.with_status(101)
188+
.with_stderr_data(str![[r#"
189+
[UPDATING] git repository `[ROOTURL]/dep`
190+
[ERROR] invalid type: sequence, expected a boolean or string
191+
--> ../home/.cargo/git/checkouts/dep-[HASH]/[..]/Cargo.toml:13:25
192+
|
193+
13 | build = ["build1.rs", "build2.rs"]
194+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
195+
|
196+
[ERROR] failed to sync
197+
198+
Caused by:
199+
failed to load lockfile for [ROOT]/foo
200+
201+
Caused by:
202+
failed to get `dep` as a dependency of package `foo v0.1.0 ([ROOT]/foo)`
203+
204+
Caused by:
205+
failed to load source for dependency `dep`
206+
207+
Caused by:
208+
Unable to update [ROOTURL]/dep
209+
210+
"#]])
211+
.run();
212+
}
213+
214+
#[cargo_test]
215+
fn rerun_untracks_other_files() {
216+
let p = project()
217+
.file(
218+
"Cargo.toml",
219+
r#"
220+
[package]
221+
name = "foo"
222+
version = "0.1.0"
223+
edition = "2024"
224+
"#,
225+
)
226+
.file("src/main.rs", "fn main() {}")
227+
.file(
228+
"build.rs",
229+
r#"
230+
fn main() {
231+
foo();
232+
bar();
233+
}
234+
fn foo() {
235+
let _path = "assets/foo.txt";
236+
}
237+
fn bar() {
238+
let path = "assets/bar.txt";
239+
println!("cargo::rerun-if-changed={path}");
240+
}"#,
241+
)
242+
.file("assets/foo.txt", "foo")
243+
.file("assets/bar.txt", "bar")
244+
.build();
245+
p.cargo("build").run();
246+
247+
// Editing foo.txt won't recompile, leading to unnoticed changes
248+
249+
p.change_file("assets/foo.txt", "foo updated");
250+
p.cargo("build -v")
251+
.with_stderr_data(str![[r#"
252+
[FRESH] foo v0.1.0 ([ROOT]/foo)
253+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
254+
255+
"#]])
256+
.run();
257+
258+
// Editing bar.txt will recompile
259+
260+
p.change_file("assets/bar.txt", "bar updated");
261+
p.cargo("build -v")
262+
.with_stderr_data(str![[r#"
263+
[DIRTY] foo v0.1.0 ([ROOT]/foo): the file `assets/bar.txt` has changed ([TIME_DIFF_AFTER_LAST_BUILD])
264+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
265+
[RUNNING] `[ROOT]/foo/target/debug/build/foo-[HASH]/build-script-build`
266+
[RUNNING] `rustc --crate-name foo --edition=2024 src/main.rs [..] --crate-type bin [..]
267+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
268+
269+
"#]])
270+
.run();
271+
}

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod build_plan;
1616
mod build_script;
1717
mod build_script_env;
1818
mod build_script_extra_link_arg;
19+
mod build_scripts_multiple;
1920
mod cache_lock;
2021
mod cache_messages;
2122
mod cargo;

0 commit comments

Comments
 (0)