]> git.proxmox.com Git - cargo.git/blob - tests/testsuite/check.rs
Add RegistryBuilder to help initializing test registries.
[cargo.git] / tests / testsuite / check.rs
1 //! Tests for the `cargo check` command.
2
3 use std::fmt::{self, Write};
4
5 use cargo_test_support::install::exe;
6 use cargo_test_support::paths::CargoPathExt;
7 use cargo_test_support::registry::Package;
8 use cargo_test_support::{basic_manifest, project};
9
10 #[cargo_test]
11 fn check_success() {
12 let foo = project()
13 .file(
14 "Cargo.toml",
15 r#"
16 [package]
17 name = "foo"
18 version = "0.0.1"
19 authors = []
20
21 [dependencies.bar]
22 path = "../bar"
23 "#,
24 )
25 .file(
26 "src/main.rs",
27 "extern crate bar; fn main() { ::bar::baz(); }",
28 )
29 .build();
30 let _bar = project()
31 .at("bar")
32 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
33 .file("src/lib.rs", "pub fn baz() {}")
34 .build();
35
36 foo.cargo("check").run();
37 }
38
39 #[cargo_test]
40 fn check_fail() {
41 let foo = project()
42 .file(
43 "Cargo.toml",
44 r#"
45 [package]
46 name = "foo"
47 version = "0.0.1"
48 authors = []
49
50 [dependencies.bar]
51 path = "../bar"
52 "#,
53 )
54 .file(
55 "src/main.rs",
56 "extern crate bar; fn main() { ::bar::baz(42); }",
57 )
58 .build();
59 let _bar = project()
60 .at("bar")
61 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
62 .file("src/lib.rs", "pub fn baz() {}")
63 .build();
64
65 foo.cargo("check")
66 .with_status(101)
67 .with_stderr_contains("[..]this function takes 0[..]")
68 .run();
69 }
70
71 #[cargo_test]
72 fn custom_derive() {
73 let foo = project()
74 .file(
75 "Cargo.toml",
76 r#"
77 [package]
78 name = "foo"
79 version = "0.0.1"
80 authors = []
81
82 [dependencies.bar]
83 path = "../bar"
84 "#,
85 )
86 .file(
87 "src/main.rs",
88 r#"
89 #[macro_use]
90 extern crate bar;
91
92 trait B {
93 fn b(&self);
94 }
95
96 #[derive(B)]
97 struct A;
98
99 fn main() {
100 let a = A;
101 a.b();
102 }
103 "#,
104 )
105 .build();
106 let _bar = project()
107 .at("bar")
108 .file(
109 "Cargo.toml",
110 r#"
111 [package]
112 name = "bar"
113 version = "0.1.0"
114 authors = []
115 [lib]
116 proc-macro = true
117 "#,
118 )
119 .file(
120 "src/lib.rs",
121 r#"
122 extern crate proc_macro;
123
124 use proc_macro::TokenStream;
125
126 #[proc_macro_derive(B)]
127 pub fn derive(_input: TokenStream) -> TokenStream {
128 format!("impl B for A {{ fn b(&self) {{}} }}").parse().unwrap()
129 }
130 "#,
131 )
132 .build();
133
134 foo.cargo("check").run();
135 }
136
137 #[cargo_test]
138 fn check_build() {
139 let foo = project()
140 .file(
141 "Cargo.toml",
142 r#"
143 [package]
144 name = "foo"
145 version = "0.0.1"
146 authors = []
147
148 [dependencies.bar]
149 path = "../bar"
150 "#,
151 )
152 .file(
153 "src/main.rs",
154 "extern crate bar; fn main() { ::bar::baz(); }",
155 )
156 .build();
157
158 let _bar = project()
159 .at("bar")
160 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
161 .file("src/lib.rs", "pub fn baz() {}")
162 .build();
163
164 foo.cargo("check").run();
165 foo.cargo("build").run();
166 }
167
168 #[cargo_test]
169 fn build_check() {
170 let foo = project()
171 .file(
172 "Cargo.toml",
173 r#"
174 [package]
175 name = "foo"
176 version = "0.0.1"
177 authors = []
178
179 [dependencies.bar]
180 path = "../bar"
181 "#,
182 )
183 .file(
184 "src/main.rs",
185 "extern crate bar; fn main() { ::bar::baz(); }",
186 )
187 .build();
188
189 let _bar = project()
190 .at("bar")
191 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
192 .file("src/lib.rs", "pub fn baz() {}")
193 .build();
194
195 foo.cargo("build -v").run();
196 foo.cargo("check -v").run();
197 }
198
199 // Checks that where a project has both a lib and a bin, the lib is only checked
200 // not built.
201 #[cargo_test]
202 fn issue_3418() {
203 let foo = project()
204 .file("src/lib.rs", "")
205 .file("src/main.rs", "fn main() {}")
206 .build();
207
208 foo.cargo("check -v")
209 .with_stderr_contains("[..] --emit=[..]metadata [..]")
210 .run();
211 }
212
213 // Some weirdness that seems to be caused by a crate being built as well as
214 // checked, but in this case with a proc macro too.
215 #[cargo_test]
216 fn issue_3419() {
217 let p = project()
218 .file(
219 "Cargo.toml",
220 r#"
221 [package]
222 name = "foo"
223 version = "0.0.1"
224 authors = []
225
226 [dependencies]
227 rustc-serialize = "*"
228 "#,
229 )
230 .file(
231 "src/lib.rs",
232 r#"
233 extern crate rustc_serialize;
234
235 use rustc_serialize::Decodable;
236
237 pub fn take<T: Decodable>() {}
238 "#,
239 )
240 .file(
241 "src/main.rs",
242 r#"
243 extern crate rustc_serialize;
244
245 extern crate foo;
246
247 #[derive(RustcDecodable)]
248 pub struct Foo;
249
250 fn main() {
251 foo::take::<Foo>();
252 }
253 "#,
254 )
255 .build();
256
257 Package::new("rustc-serialize", "1.0.0")
258 .file(
259 "src/lib.rs",
260 r#"
261 pub trait Decodable: Sized {
262 fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::Error>;
263 }
264 pub trait Decoder {
265 type Error;
266 fn read_struct<T, F>(&mut self, s_name: &str, len: usize, f: F)
267 -> Result<T, Self::Error>
268 where F: FnOnce(&mut Self) -> Result<T, Self::Error>;
269 }
270 "#,
271 )
272 .publish();
273
274 p.cargo("check").run();
275 }
276
277 // Check on a dylib should have a different metadata hash than build.
278 #[cargo_test]
279 fn dylib_check_preserves_build_cache() {
280 let p = project()
281 .file(
282 "Cargo.toml",
283 r#"
284 [package]
285 name = "foo"
286 version = "0.1.0"
287 authors = []
288
289 [lib]
290 crate-type = ["dylib"]
291
292 [dependencies]
293 "#,
294 )
295 .file("src/lib.rs", "")
296 .build();
297
298 p.cargo("build")
299 .with_stderr(
300 "\
301 [..]Compiling foo v0.1.0 ([..])
302 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
303 ",
304 )
305 .run();
306
307 p.cargo("check").run();
308
309 p.cargo("build")
310 .with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
311 .run();
312 }
313
314 // test `cargo rustc --profile check`
315 #[cargo_test]
316 fn rustc_check() {
317 let foo = project()
318 .file(
319 "Cargo.toml",
320 r#"
321 [package]
322 name = "foo"
323 version = "0.0.1"
324 authors = []
325
326 [dependencies.bar]
327 path = "../bar"
328 "#,
329 )
330 .file(
331 "src/main.rs",
332 "extern crate bar; fn main() { ::bar::baz(); }",
333 )
334 .build();
335 let _bar = project()
336 .at("bar")
337 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
338 .file("src/lib.rs", "pub fn baz() {}")
339 .build();
340
341 foo.cargo("rustc --profile check -- --emit=metadata").run();
342
343 // Verify compatible usage of --profile with --release, issue #7488
344 foo.cargo("rustc --profile check --release -- --emit=metadata")
345 .run();
346 foo.cargo("rustc --profile test --release -- --emit=metadata")
347 .run();
348 }
349
350 #[cargo_test]
351 fn rustc_check_err() {
352 let foo = project()
353 .file(
354 "Cargo.toml",
355 r#"
356 [package]
357 name = "foo"
358 version = "0.0.1"
359 authors = []
360
361 [dependencies.bar]
362 path = "../bar"
363 "#,
364 )
365 .file(
366 "src/main.rs",
367 "extern crate bar; fn main() { ::bar::qux(); }",
368 )
369 .build();
370 let _bar = project()
371 .at("bar")
372 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
373 .file("src/lib.rs", "pub fn baz() {}")
374 .build();
375
376 foo.cargo("rustc --profile check -- --emit=metadata")
377 .with_status(101)
378 .with_stderr_contains("[CHECKING] bar [..]")
379 .with_stderr_contains("[CHECKING] foo [..]")
380 .with_stderr_contains("[..]cannot find function `qux` in [..] `bar`")
381 .run();
382 }
383
384 #[cargo_test]
385 fn check_all() {
386 let p = project()
387 .file(
388 "Cargo.toml",
389 r#"
390 [package]
391 name = "foo"
392 version = "0.0.1"
393 authors = []
394
395 [workspace]
396 [dependencies]
397 b = { path = "b" }
398 "#,
399 )
400 .file("src/main.rs", "fn main() {}")
401 .file("examples/a.rs", "fn main() {}")
402 .file("tests/a.rs", "")
403 .file("src/lib.rs", "")
404 .file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
405 .file("b/src/main.rs", "fn main() {}")
406 .file("b/src/lib.rs", "")
407 .build();
408
409 p.cargo("check --workspace -v")
410 .with_stderr_contains("[..] --crate-name foo src/lib.rs [..]")
411 .with_stderr_contains("[..] --crate-name foo src/main.rs [..]")
412 .with_stderr_contains("[..] --crate-name b b/src/lib.rs [..]")
413 .with_stderr_contains("[..] --crate-name b b/src/main.rs [..]")
414 .run();
415 }
416
417 #[cargo_test]
418 fn check_all_exclude() {
419 let p = project()
420 .file(
421 "Cargo.toml",
422 r#"
423 [workspace]
424 members = ["bar", "baz"]
425 "#,
426 )
427 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
428 .file("bar/src/lib.rs", "pub fn bar() {}")
429 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
430 .file("baz/src/lib.rs", "pub fn baz() { break_the_build(); }")
431 .build();
432
433 p.cargo("check --workspace --exclude baz")
434 .with_stderr_does_not_contain("[CHECKING] baz v0.1.0 [..]")
435 .with_stderr(
436 "\
437 [CHECKING] bar v0.1.0 ([..])
438 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
439 ",
440 )
441 .run();
442 }
443
444 #[cargo_test]
445 fn check_all_exclude_glob() {
446 let p = project()
447 .file(
448 "Cargo.toml",
449 r#"
450 [workspace]
451 members = ["bar", "baz"]
452 "#,
453 )
454 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
455 .file("bar/src/lib.rs", "pub fn bar() {}")
456 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
457 .file("baz/src/lib.rs", "pub fn baz() { break_the_build(); }")
458 .build();
459
460 p.cargo("check --workspace --exclude '*z'")
461 .with_stderr_does_not_contain("[CHECKING] baz v0.1.0 [..]")
462 .with_stderr(
463 "\
464 [CHECKING] bar v0.1.0 ([..])
465 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
466 ",
467 )
468 .run();
469 }
470
471 #[cargo_test]
472 fn check_virtual_all_implied() {
473 let p = project()
474 .file(
475 "Cargo.toml",
476 r#"
477 [workspace]
478 members = ["bar", "baz"]
479 "#,
480 )
481 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
482 .file("bar/src/lib.rs", "pub fn bar() {}")
483 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
484 .file("baz/src/lib.rs", "pub fn baz() {}")
485 .build();
486
487 p.cargo("check -v")
488 .with_stderr_contains("[..] --crate-name bar bar/src/lib.rs [..]")
489 .with_stderr_contains("[..] --crate-name baz baz/src/lib.rs [..]")
490 .run();
491 }
492
493 #[cargo_test]
494 fn check_virtual_manifest_one_project() {
495 let p = project()
496 .file(
497 "Cargo.toml",
498 r#"
499 [workspace]
500 members = ["bar", "baz"]
501 "#,
502 )
503 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
504 .file("bar/src/lib.rs", "pub fn bar() {}")
505 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
506 .file("baz/src/lib.rs", "pub fn baz() { break_the_build(); }")
507 .build();
508
509 p.cargo("check -p bar")
510 .with_stderr_does_not_contain("[CHECKING] baz v0.1.0 [..]")
511 .with_stderr(
512 "\
513 [CHECKING] bar v0.1.0 ([..])
514 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
515 ",
516 )
517 .run();
518 }
519
520 #[cargo_test]
521 fn check_virtual_manifest_glob() {
522 let p = project()
523 .file(
524 "Cargo.toml",
525 r#"
526 [workspace]
527 members = ["bar", "baz"]
528 "#,
529 )
530 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
531 .file("bar/src/lib.rs", "pub fn bar() { break_the_build(); }")
532 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
533 .file("baz/src/lib.rs", "pub fn baz() {}")
534 .build();
535
536 p.cargo("check -p '*z'")
537 .with_stderr_does_not_contain("[CHECKING] bar v0.1.0 [..]")
538 .with_stderr(
539 "\
540 [CHECKING] baz v0.1.0 ([..])
541 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
542 ",
543 )
544 .run();
545 }
546
547 #[cargo_test]
548 fn exclude_warns_on_non_existing_package() {
549 let p = project().file("src/lib.rs", "").build();
550 p.cargo("check --workspace --exclude bar")
551 .with_stdout("")
552 .with_stderr(
553 "\
554 [WARNING] excluded package(s) `bar` not found in workspace `[CWD]`
555 [CHECKING] foo v0.0.1 ([CWD])
556 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
557 ",
558 )
559 .run();
560 }
561
562 #[cargo_test]
563 fn targets_selected_default() {
564 let foo = project()
565 .file("src/main.rs", "fn main() {}")
566 .file("src/lib.rs", "pub fn smth() {}")
567 .file("examples/example1.rs", "fn main() {}")
568 .file("tests/test2.rs", "#[test] fn t() {}")
569 .file("benches/bench3.rs", "")
570 .build();
571
572 foo.cargo("check -v")
573 .with_stderr_contains("[..] --crate-name foo src/lib.rs [..]")
574 .with_stderr_contains("[..] --crate-name foo src/main.rs [..]")
575 .with_stderr_does_not_contain("[..] --crate-name example1 examples/example1.rs [..]")
576 .with_stderr_does_not_contain("[..] --crate-name test2 tests/test2.rs [..]")
577 .with_stderr_does_not_contain("[..] --crate-name bench3 benches/bench3.rs [..]")
578 .run();
579 }
580
581 #[cargo_test]
582 fn targets_selected_all() {
583 let foo = project()
584 .file("src/main.rs", "fn main() {}")
585 .file("src/lib.rs", "pub fn smth() {}")
586 .file("examples/example1.rs", "fn main() {}")
587 .file("tests/test2.rs", "#[test] fn t() {}")
588 .file("benches/bench3.rs", "")
589 .build();
590
591 foo.cargo("check --all-targets -v")
592 .with_stderr_contains("[..] --crate-name foo src/lib.rs [..]")
593 .with_stderr_contains("[..] --crate-name foo src/main.rs [..]")
594 .with_stderr_contains("[..] --crate-name example1 examples/example1.rs [..]")
595 .with_stderr_contains("[..] --crate-name test2 tests/test2.rs [..]")
596 .with_stderr_contains("[..] --crate-name bench3 benches/bench3.rs [..]")
597 .run();
598 }
599
600 #[cargo_test]
601 fn check_unit_test_profile() {
602 let foo = project()
603 .file(
604 "src/lib.rs",
605 r#"
606 #[cfg(test)]
607 mod tests {
608 #[test]
609 fn it_works() {
610 badtext
611 }
612 }
613 "#,
614 )
615 .build();
616
617 foo.cargo("check").run();
618 foo.cargo("check --profile test")
619 .with_status(101)
620 .with_stderr_contains("[..]badtext[..]")
621 .run();
622 }
623
624 // Verify what is checked with various command-line filters.
625 #[cargo_test]
626 fn check_filters() {
627 let p = project()
628 .file(
629 "src/lib.rs",
630 r#"
631 fn unused_normal_lib() {}
632 #[cfg(test)]
633 mod tests {
634 fn unused_unit_lib() {}
635 }
636 "#,
637 )
638 .file(
639 "src/main.rs",
640 r#"
641 fn main() {}
642 fn unused_normal_bin() {}
643 #[cfg(test)]
644 mod tests {
645 fn unused_unit_bin() {}
646 }
647 "#,
648 )
649 .file(
650 "tests/t1.rs",
651 r#"
652 fn unused_normal_t1() {}
653 #[cfg(test)]
654 mod tests {
655 fn unused_unit_t1() {}
656 }
657 "#,
658 )
659 .file(
660 "examples/ex1.rs",
661 r#"
662 fn main() {}
663 fn unused_normal_ex1() {}
664 #[cfg(test)]
665 mod tests {
666 fn unused_unit_ex1() {}
667 }
668 "#,
669 )
670 .file(
671 "benches/b1.rs",
672 r#"
673 fn unused_normal_b1() {}
674 #[cfg(test)]
675 mod tests {
676 fn unused_unit_b1() {}
677 }
678 "#,
679 )
680 .build();
681
682 p.cargo("check")
683 .with_stderr_contains("[..]unused_normal_lib[..]")
684 .with_stderr_contains("[..]unused_normal_bin[..]")
685 .with_stderr_does_not_contain("[..]unused_normal_t1[..]")
686 .with_stderr_does_not_contain("[..]unused_normal_ex1[..]")
687 .with_stderr_does_not_contain("[..]unused_normal_b1[..]")
688 .with_stderr_does_not_contain("[..]unused_unit_[..]")
689 .run();
690 p.root().join("target").rm_rf();
691 p.cargo("check --tests -v")
692 .with_stderr_contains("[..] --crate-name foo src/lib.rs [..] --test [..]")
693 .with_stderr_contains("[..] --crate-name foo src/lib.rs [..] --crate-type lib [..]")
694 .with_stderr_contains("[..] --crate-name foo src/main.rs [..] --test [..]")
695 .with_stderr_contains("[..]unused_unit_lib[..]")
696 .with_stderr_contains("[..]unused_unit_bin[..]")
697 .with_stderr_contains("[..]unused_normal_lib[..]")
698 .with_stderr_contains("[..]unused_normal_bin[..]")
699 .with_stderr_contains("[..]unused_unit_t1[..]")
700 .with_stderr_does_not_contain("[..]unused_normal_ex1[..]")
701 .with_stderr_does_not_contain("[..]unused_unit_ex1[..]")
702 .with_stderr_does_not_contain("[..]unused_normal_b1[..]")
703 .with_stderr_does_not_contain("[..]unused_unit_b1[..]")
704 .with_stderr_does_not_contain("[..]--crate-type bin[..]")
705 .run();
706 p.root().join("target").rm_rf();
707 p.cargo("check --test t1 -v")
708 .with_stderr_contains("[..]unused_normal_lib[..]")
709 .with_stderr_contains("[..]unused_unit_t1[..]")
710 .with_stderr_does_not_contain("[..]unused_unit_lib[..]")
711 .with_stderr_does_not_contain("[..]unused_normal_bin[..]")
712 .with_stderr_does_not_contain("[..]unused_unit_bin[..]")
713 .with_stderr_does_not_contain("[..]unused_normal_ex1[..]")
714 .with_stderr_does_not_contain("[..]unused_normal_b1[..]")
715 .with_stderr_does_not_contain("[..]unused_unit_ex1[..]")
716 .with_stderr_does_not_contain("[..]unused_unit_b1[..]")
717 .run();
718 p.root().join("target").rm_rf();
719 p.cargo("check --all-targets -v")
720 .with_stderr_contains("[..]unused_normal_lib[..]")
721 .with_stderr_contains("[..]unused_normal_bin[..]")
722 .with_stderr_contains("[..]unused_normal_t1[..]")
723 .with_stderr_contains("[..]unused_normal_ex1[..]")
724 .with_stderr_contains("[..]unused_normal_b1[..]")
725 .with_stderr_contains("[..]unused_unit_b1[..]")
726 .with_stderr_contains("[..]unused_unit_t1[..]")
727 .with_stderr_contains("[..]unused_unit_lib[..]")
728 .with_stderr_contains("[..]unused_unit_bin[..]")
729 .with_stderr_does_not_contain("[..]unused_unit_ex1[..]")
730 .run();
731 }
732
733 #[cargo_test]
734 fn check_artifacts() {
735 // Verify which artifacts are created when running check (#4059).
736 let p = project()
737 .file("src/lib.rs", "")
738 .file("src/main.rs", "fn main() {}")
739 .file("tests/t1.rs", "")
740 .file("examples/ex1.rs", "fn main() {}")
741 .file("benches/b1.rs", "")
742 .build();
743
744 p.cargo("check").run();
745 assert!(!p.root().join("target/debug/libfoo.rmeta").is_file());
746 assert!(!p.root().join("target/debug/libfoo.rlib").is_file());
747 assert!(!p.root().join("target/debug").join(exe("foo")).is_file());
748 assert_eq!(p.glob("target/debug/deps/libfoo-*.rmeta").count(), 2);
749
750 p.root().join("target").rm_rf();
751 p.cargo("check --lib").run();
752 assert!(!p.root().join("target/debug/libfoo.rmeta").is_file());
753 assert!(!p.root().join("target/debug/libfoo.rlib").is_file());
754 assert!(!p.root().join("target/debug").join(exe("foo")).is_file());
755 assert_eq!(p.glob("target/debug/deps/libfoo-*.rmeta").count(), 1);
756
757 p.root().join("target").rm_rf();
758 p.cargo("check --bin foo").run();
759 assert!(!p.root().join("target/debug/libfoo.rmeta").is_file());
760 assert!(!p.root().join("target/debug/libfoo.rlib").is_file());
761 assert!(!p.root().join("target/debug").join(exe("foo")).is_file());
762 assert_eq!(p.glob("target/debug/deps/libfoo-*.rmeta").count(), 2);
763
764 p.root().join("target").rm_rf();
765 p.cargo("check --test t1").run();
766 assert!(!p.root().join("target/debug/libfoo.rmeta").is_file());
767 assert!(!p.root().join("target/debug/libfoo.rlib").is_file());
768 assert!(!p.root().join("target/debug").join(exe("foo")).is_file());
769 assert_eq!(p.glob("target/debug/t1-*").count(), 0);
770 assert_eq!(p.glob("target/debug/deps/libfoo-*.rmeta").count(), 1);
771 assert_eq!(p.glob("target/debug/deps/libt1-*.rmeta").count(), 1);
772
773 p.root().join("target").rm_rf();
774 p.cargo("check --example ex1").run();
775 assert!(!p.root().join("target/debug/libfoo.rmeta").is_file());
776 assert!(!p.root().join("target/debug/libfoo.rlib").is_file());
777 assert!(!p
778 .root()
779 .join("target/debug/examples")
780 .join(exe("ex1"))
781 .is_file());
782 assert_eq!(p.glob("target/debug/deps/libfoo-*.rmeta").count(), 1);
783 assert_eq!(p.glob("target/debug/examples/libex1-*.rmeta").count(), 1);
784
785 p.root().join("target").rm_rf();
786 p.cargo("check --bench b1").run();
787 assert!(!p.root().join("target/debug/libfoo.rmeta").is_file());
788 assert!(!p.root().join("target/debug/libfoo.rlib").is_file());
789 assert!(!p.root().join("target/debug").join(exe("foo")).is_file());
790 assert_eq!(p.glob("target/debug/b1-*").count(), 0);
791 assert_eq!(p.glob("target/debug/deps/libfoo-*.rmeta").count(), 1);
792 assert_eq!(p.glob("target/debug/deps/libb1-*.rmeta").count(), 1);
793 }
794
795 #[cargo_test]
796 fn short_message_format() {
797 let foo = project()
798 .file("src/lib.rs", "fn foo() { let _x: bool = 'a'; }")
799 .build();
800 foo.cargo("check --message-format=short")
801 .with_status(101)
802 .with_stderr_contains(
803 "\
804 src/lib.rs:1:27: error[E0308]: mismatched types
805 error: aborting due to previous error
806 error: could not compile `foo`
807 ",
808 )
809 .run();
810 }
811
812 #[cargo_test]
813 fn proc_macro() {
814 let p = project()
815 .file(
816 "Cargo.toml",
817 r#"
818 [package]
819 name = "demo"
820 version = "0.0.1"
821
822 [lib]
823 proc-macro = true
824 "#,
825 )
826 .file(
827 "src/lib.rs",
828 r#"
829 extern crate proc_macro;
830
831 use proc_macro::TokenStream;
832
833 #[proc_macro_derive(Foo)]
834 pub fn demo(_input: TokenStream) -> TokenStream {
835 "".parse().unwrap()
836 }
837 "#,
838 )
839 .file(
840 "src/main.rs",
841 r#"
842 #[macro_use]
843 extern crate demo;
844
845 #[derive(Foo)]
846 struct A;
847
848 fn main() {}
849 "#,
850 )
851 .build();
852 p.cargo("check -v").env("CARGO_LOG", "cargo=trace").run();
853 }
854
855 #[cargo_test]
856 fn does_not_use_empty_rustc_wrapper() {
857 let p = project().file("src/lib.rs", "").build();
858 p.cargo("check").env("RUSTC_WRAPPER", "").run();
859 }
860
861 #[cargo_test]
862 fn does_not_use_empty_rustc_workspace_wrapper() {
863 let p = project().file("src/lib.rs", "").build();
864 p.cargo("check").env("RUSTC_WORKSPACE_WRAPPER", "").run();
865 }
866
867 #[cargo_test]
868 fn error_from_deep_recursion() -> Result<(), fmt::Error> {
869 let mut big_macro = String::new();
870 writeln!(big_macro, "macro_rules! m {{")?;
871 for i in 0..130 {
872 writeln!(big_macro, "({}) => {{ m!({}); }};", i, i + 1)?;
873 }
874 writeln!(big_macro, "}}")?;
875 writeln!(big_macro, "m!(0);")?;
876
877 let p = project().file("src/lib.rs", &big_macro).build();
878 p.cargo("check --message-format=json")
879 .with_status(101)
880 .with_stdout_contains(
881 "[..]\"message\":\"recursion limit reached while expanding [..]`m[..]`\"[..]",
882 )
883 .run();
884
885 Ok(())
886 }
887
888 #[cargo_test]
889 fn rustc_workspace_wrapper_affects_all_workspace_members() {
890 use cargo_test_support::paths;
891 let p = project()
892 .file(
893 "Cargo.toml",
894 r#"
895 [workspace]
896 members = ["bar", "baz"]
897 "#,
898 )
899 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
900 .file("bar/src/lib.rs", "pub fn bar() {}")
901 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
902 .file("baz/src/lib.rs", "pub fn baz() {}")
903 .build();
904
905 p.cargo("check")
906 .env("RUSTC_WORKSPACE_WRAPPER", paths::echo_wrapper())
907 .with_stderr_contains("WRAPPER CALLED: rustc --crate-name bar [..]")
908 .with_stderr_contains("WRAPPER CALLED: rustc --crate-name baz [..]")
909 .run();
910 }
911
912 #[cargo_test]
913 fn rustc_workspace_wrapper_includes_path_deps() {
914 use cargo_test_support::paths;
915 let p = project()
916 .file(
917 "Cargo.toml",
918 r#"
919 [project]
920 name = "foo"
921 version = "0.1.0"
922 authors = []
923
924 [workspace]
925 members = ["bar"]
926
927 [dependencies]
928 baz = { path = "baz" }
929 "#,
930 )
931 .file("src/lib.rs", "")
932 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
933 .file("bar/src/lib.rs", "pub fn bar() {}")
934 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
935 .file("baz/src/lib.rs", "pub fn baz() {}")
936 .build();
937
938 p.cargo("check --workspace")
939 .env("RUSTC_WORKSPACE_WRAPPER", paths::echo_wrapper())
940 .with_stderr_contains("WRAPPER CALLED: rustc --crate-name foo [..]")
941 .with_stderr_contains("WRAPPER CALLED: rustc --crate-name bar [..]")
942 .with_stderr_contains("WRAPPER CALLED: rustc --crate-name baz [..]")
943 .run();
944 }
945
946 #[cargo_test]
947 fn rustc_workspace_wrapper_respects_primary_units() {
948 use cargo_test_support::paths;
949 let p = project()
950 .file(
951 "Cargo.toml",
952 r#"
953 [workspace]
954 members = ["bar", "baz"]
955 "#,
956 )
957 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
958 .file("bar/src/lib.rs", "pub fn bar() {}")
959 .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
960 .file("baz/src/lib.rs", "pub fn baz() {}")
961 .build();
962
963 p.cargo("check -p bar")
964 .env("RUSTC_WORKSPACE_WRAPPER", paths::echo_wrapper())
965 .with_stderr_contains("WRAPPER CALLED: rustc --crate-name bar [..]")
966 .with_stdout_does_not_contain("WRAPPER CALLED: rustc --crate-name baz [..]")
967 .run();
968 }
969
970 #[cargo_test]
971 fn rustc_workspace_wrapper_excludes_published_deps() {
972 use cargo_test_support::paths;
973 let p = project()
974 .file(
975 "Cargo.toml",
976 r#"
977 [project]
978 name = "foo"
979 version = "0.1.0"
980 authors = []
981
982 [workspace]
983 members = ["bar"]
984
985 [dependencies]
986 baz = "1.0.0"
987 "#,
988 )
989 .file("src/lib.rs", "")
990 .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
991 .file("bar/src/lib.rs", "pub fn bar() {}")
992 .build();
993
994 Package::new("baz", "1.0.0").publish();
995
996 p.cargo("check --workspace -v")
997 .env("RUSTC_WORKSPACE_WRAPPER", paths::echo_wrapper())
998 .with_stderr_contains("WRAPPER CALLED: rustc --crate-name foo [..]")
999 .with_stderr_contains("WRAPPER CALLED: rustc --crate-name bar [..]")
1000 .with_stderr_contains("[CHECKING] baz [..]")
1001 .with_stdout_does_not_contain("WRAPPER CALLED: rustc --crate-name baz [..]")
1002 .run();
1003 }