Skip to content

Commit ea015a2

Browse files
committed
test(compile-time-deps): Add tests for --compile-time-deps
1 parent 2251525 commit ea015a2

File tree

2 files changed

+382
-0
lines changed

2 files changed

+382
-0
lines changed

tests/testsuite/compile_time_deps.rs

Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
use cargo_test_support::prelude::*;
2+
use cargo_test_support::{project, str};
3+
4+
#[should_panic]
5+
#[cargo_test]
6+
fn gated_by_unstable_opts() {
7+
let p = project()
8+
.file("src/main.rs", r#"fn main() { println!("Hello, World!") }"#)
9+
.build();
10+
11+
p.cargo("check --compile-time-deps").run();
12+
}
13+
14+
#[cargo_test]
15+
fn non_comp_time_dep() {
16+
let p = project()
17+
.file(
18+
"Cargo.toml",
19+
r#"
20+
[package]
21+
name = "foo"
22+
version = "0.0.1"
23+
edition = "2021"
24+
25+
[dependencies]
26+
bar.path = "bar"
27+
"#,
28+
)
29+
.file(
30+
"src/main.rs",
31+
r#"
32+
fn main() {
33+
bar::bar();
34+
}
35+
"#,
36+
)
37+
.file(
38+
"bar/Cargo.toml",
39+
r#"
40+
[package]
41+
name = "bar"
42+
version = "0.0.1"
43+
edition = "2021"
44+
"#,
45+
)
46+
.file("bar/src/lib.rs", r#"pub fn bar() {}"#)
47+
.build();
48+
49+
p.cargo("check")
50+
.with_stderr_data(str![[r#"
51+
[LOCKING] 1 package to latest compatible version
52+
[CHECKING] bar v0.0.1 ([ROOT]/foo/bar)
53+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
54+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
55+
56+
"#]])
57+
.run();
58+
}
59+
60+
#[cargo_test]
61+
fn proc_macro_dep() {
62+
let p = project()
63+
.file(
64+
"Cargo.toml",
65+
r#"
66+
[workspace]
67+
resolver = "2"
68+
members = ["foo", "bar", "baz"]
69+
70+
[workspace.dependencies]
71+
bar.path = "bar"
72+
baz.path = "baz"
73+
"#,
74+
)
75+
.file(
76+
"foo/Cargo.toml",
77+
r#"
78+
[package]
79+
name = "foo"
80+
version = "0.0.1"
81+
edition = "2021"
82+
83+
[dependencies]
84+
bar.workspace = true
85+
"#,
86+
)
87+
.file(
88+
"foo/src/main.rs",
89+
r#"
90+
fn main() {
91+
bar::bar!();
92+
}
93+
"#,
94+
)
95+
.file(
96+
"bar/Cargo.toml",
97+
r#"
98+
[package]
99+
name = "bar"
100+
version = "0.0.1"
101+
edition = "2021"
102+
103+
[lib]
104+
proc-macro = true
105+
106+
[dependencies]
107+
baz.workspace = true
108+
"#,
109+
)
110+
.file(
111+
"bar/src/lib.rs",
112+
r#"
113+
extern crate proc_macro;
114+
115+
use proc_macro::TokenStream;
116+
117+
#[proc_macro]
118+
pub fn bar(input: TokenStream) -> TokenStream {
119+
baz::baz();
120+
input
121+
}
122+
"#,
123+
)
124+
.file(
125+
"bar/tests/simple.rs",
126+
r#"
127+
#[test]
128+
fn test_bar() {
129+
let _x: bool = bar::bar!(true);
130+
}
131+
"#,
132+
)
133+
.file(
134+
"baz/Cargo.toml",
135+
r#"
136+
[package]
137+
name = "baz"
138+
version = "0.0.1"
139+
edition = "2021"
140+
"#,
141+
)
142+
.file("baz/src/lib.rs", r#"pub fn baz() {}"#)
143+
.build();
144+
145+
p.cargo("check --package foo")
146+
.with_stderr_data(str![[r#"
147+
[COMPILING] baz v0.0.1 ([ROOT]/foo/baz)
148+
[COMPILING] bar v0.0.1 ([ROOT]/foo/bar)
149+
[CHECKING] foo v0.0.1 ([ROOT]/foo/foo)
150+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
151+
152+
"#]])
153+
.run();
154+
155+
p.cargo("clean").run();
156+
157+
p.cargo("check --package bar")
158+
.with_stderr_data(str![[r#"
159+
[CHECKING] baz v0.0.1 ([ROOT]/foo/baz)
160+
[CHECKING] bar v0.0.1 ([ROOT]/foo/bar)
161+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
162+
163+
"#]])
164+
.run();
165+
}
166+
167+
#[cargo_test]
168+
fn build_dep() {
169+
let p = project()
170+
.file(
171+
"Cargo.toml",
172+
r#"
173+
[package]
174+
name = "foo"
175+
version = "0.0.1"
176+
edition = "2021"
177+
178+
[build-dependencies]
179+
bar.path = "bar"
180+
"#,
181+
)
182+
.file("src/main.rs", r#"fn main() {}"#)
183+
.file(
184+
"build.rs",
185+
r#"
186+
fn main() {
187+
bar::bar();
188+
std::fs::write("check-script-output", "build script run").unwrap();
189+
}
190+
"#,
191+
)
192+
.file(
193+
"bar/Cargo.toml",
194+
r#"
195+
[package]
196+
name = "bar"
197+
version = "0.0.1"
198+
edition = "2021"
199+
200+
[dependencies]
201+
baz.path = "baz"
202+
"#,
203+
)
204+
.file(
205+
"bar/src/lib.rs",
206+
r#"
207+
pub fn bar() {
208+
baz::baz();
209+
}
210+
"#,
211+
)
212+
.file(
213+
"bar/baz/Cargo.toml",
214+
r#"
215+
[package]
216+
name = "baz"
217+
version = "0.0.1"
218+
edition = "2021"
219+
"#,
220+
)
221+
.file("bar/baz/src/lib.rs", r#"pub fn baz() {}"#)
222+
.build();
223+
224+
p.cargo("check")
225+
.with_stderr_data(str![[r#"
226+
[LOCKING] 2 packages to latest compatible versions
227+
[COMPILING] baz v0.0.1 ([ROOT]/foo/bar/baz)
228+
[COMPILING] bar v0.0.1 ([ROOT]/foo/bar)
229+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
230+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
231+
232+
"#]])
233+
.run();
234+
235+
assert_eq!(p.read_file("check-script-output"), "build script run");
236+
}
237+
238+
#[cargo_test]
239+
fn indirect_comp_time_dep() {
240+
let p = project()
241+
.file(
242+
"Cargo.toml",
243+
r#"
244+
[package]
245+
name = "foo"
246+
version = "0.0.1"
247+
edition = "2021"
248+
249+
[dependencies]
250+
bar.path = "bar"
251+
"#,
252+
)
253+
.file("src/main.rs", r#"fn main() {}"#)
254+
.file(
255+
"bar/Cargo.toml",
256+
r#"
257+
[package]
258+
name = "bar"
259+
version = "0.0.1"
260+
edition = "2021"
261+
262+
[build-dependencies]
263+
baz.path = "baz"
264+
"#,
265+
)
266+
.file("bar/src/lib.rs", r#"pub fn bar() {}"#)
267+
.file(
268+
"bar/build.rs",
269+
r#"
270+
fn main() {
271+
baz::baz();
272+
}
273+
"#,
274+
)
275+
.file(
276+
"bar/baz/Cargo.toml",
277+
r#"
278+
[package]
279+
name = "baz"
280+
version = "0.0.1"
281+
edition = "2021"
282+
"#,
283+
)
284+
.file("bar/src/lib.rs", r#"pub fn baz() {}"#)
285+
.file(
286+
"bar/baz/Cargo.toml",
287+
r#"
288+
[package]
289+
name = "baz"
290+
version = "0.0.1"
291+
edition = "2021"
292+
"#,
293+
)
294+
.file("bar/baz/src/lib.rs", r#"pub fn baz() {}"#)
295+
.build();
296+
297+
p.cargo("check")
298+
.with_stderr_data(str![[r#"
299+
[LOCKING] 2 packages to latest compatible versions
300+
[COMPILING] baz v0.0.1 ([ROOT]/foo/bar/baz)
301+
[COMPILING] bar v0.0.1 ([ROOT]/foo/bar)
302+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
303+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
304+
305+
"#]])
306+
.run();
307+
}
308+
309+
#[cargo_test]
310+
fn tests_target() {
311+
let p = project()
312+
.file(
313+
"Cargo.toml",
314+
r#"
315+
[package]
316+
name = "foo"
317+
version = "0.0.1"
318+
edition = "2021"
319+
320+
[dev-dependencies]
321+
bar.path = "bar"
322+
"#,
323+
)
324+
.file(
325+
"src/main.rs",
326+
r#"
327+
fn main() {}
328+
329+
#[test]
330+
fn foo() {
331+
bar::bar!();
332+
}
333+
"#,
334+
)
335+
.file(
336+
"bar/Cargo.toml",
337+
r#"
338+
[package]
339+
name = "bar"
340+
version = "0.0.1"
341+
edition = "2021"
342+
343+
[lib]
344+
proc-macro = true
345+
"#,
346+
)
347+
.file(
348+
"bar/src/lib.rs",
349+
r#"
350+
extern crate proc_macro;
351+
352+
use proc_macro::TokenStream;
353+
354+
#[proc_macro]
355+
pub fn bar(input: TokenStream) -> TokenStream {
356+
input
357+
}
358+
"#,
359+
)
360+
.build();
361+
362+
p.cargo("check --tests")
363+
.with_stderr_data(str![[r#"
364+
[LOCKING] 1 package to latest compatible version
365+
[COMPILING] bar v0.0.1 ([ROOT]/foo/bar)
366+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
367+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
368+
369+
"#]])
370+
.run();
371+
372+
p.cargo("clean").run();
373+
374+
p.cargo("check")
375+
.with_stderr_data(str![[r#"
376+
[CHECKING] foo v0.0.1 ([ROOT]/foo)
377+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
378+
379+
"#]])
380+
.run();
381+
}

tests/testsuite/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ mod check;
6868
mod check_cfg;
6969
mod clean;
7070
mod collisions;
71+
mod compile_time_deps;
7172
mod concurrent;
7273
mod config;
7374
mod config_cli;

0 commit comments

Comments
 (0)