1 //! Tests for the `cargo check` command.
3 use std
::fmt
::{self, Write}
;
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}
;
27 "extern crate bar; fn main() { ::bar::baz(); }",
32 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
33 .file("src/lib.rs", "pub fn baz() {}")
36 foo
.cargo("check").run();
56 "extern crate bar; fn main() { ::bar::baz(42); }",
61 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
62 .file("src/lib.rs", "pub fn baz() {}")
67 .with_stderr_contains("[..]this function takes 0[..]")
122 extern crate proc_macro;
124 use proc_macro::TokenStream;
126 #[proc_macro_derive(B)]
127 pub fn derive(_input: TokenStream) -> TokenStream {
128 format!("impl B for A {{ fn b(&self) {{}} }}").parse().unwrap()
134 foo
.cargo("check").run();
154 "extern crate bar; fn main() { ::bar::baz(); }",
160 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
161 .file("src/lib.rs", "pub fn baz() {}")
164 foo
.cargo("check").run();
165 foo
.cargo("build").run();
185 "extern crate bar; fn main() { ::bar::baz(); }",
191 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
192 .file("src/lib.rs", "pub fn baz() {}")
195 foo
.cargo("build -v").run();
196 foo
.cargo("check -v").run();
199 // Checks that where a project has both a lib and a bin, the lib is only checked
204 .file("src/lib.rs", "")
205 .file("src/main.rs", "fn main() {}")
208 foo
.cargo("check -v")
209 .with_stderr_contains("[..] --emit=[..]metadata [..]")
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.
227 rustc-serialize = "*"
233 extern crate rustc_serialize;
235 use rustc_serialize::Decodable;
237 pub fn take<T: Decodable>() {}
243 extern crate rustc_serialize;
247 #[derive(RustcDecodable)]
257 Package
::new("rustc-serialize", "1.0.0")
261 pub trait Decodable: Sized {
262 fn decode<D: Decoder>(d: &mut D) -> Result<Self, D::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>;
274 p
.cargo("check").run();
277 // Check on a dylib should have a different metadata hash than build.
279 fn dylib_check_preserves_build_cache() {
290 crate-type = ["dylib"]
295 .file("src/lib.rs", "")
301 [..]Compiling foo v0.1.0 ([..])
302 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
307 p
.cargo("check").run();
310 .with_stderr("[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]")
314 // test `cargo rustc --profile check`
332 "extern crate bar; fn main() { ::bar::baz(); }",
337 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
338 .file("src/lib.rs", "pub fn baz() {}")
341 foo
.cargo("rustc --profile check -- --emit=metadata").run();
343 // Verify compatible usage of --profile with --release, issue #7488
344 foo
.cargo("rustc --profile check --release -- --emit=metadata")
346 foo
.cargo("rustc --profile test --release -- --emit=metadata")
351 fn rustc_check_err() {
367 "extern crate bar; fn main() { ::bar::qux(); }",
372 .file("Cargo.toml", &basic_manifest("bar", "0.1.0"))
373 .file("src/lib.rs", "pub fn baz() {}")
376 foo
.cargo("rustc --profile check -- --emit=metadata")
378 .with_stderr_contains("[CHECKING] bar [..]")
379 .with_stderr_contains("[CHECKING] foo [..]")
380 .with_stderr_contains("[..]cannot find function `qux` in [..] `bar`")
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", "")
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 [..]")
418 fn check_all_exclude() {
424 members = ["bar", "baz"]
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(); }")
433 p
.cargo("check --workspace --exclude baz")
434 .with_stderr_does_not_contain("[CHECKING] baz v0.1.0 [..]")
437 [CHECKING] bar v0.1.0 ([..])
438 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
445 fn check_all_exclude_glob() {
451 members = ["bar", "baz"]
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(); }")
460 p
.cargo("check --workspace --exclude '*z'")
461 .with_stderr_does_not_contain("[CHECKING] baz v0.1.0 [..]")
464 [CHECKING] bar v0.1.0 ([..])
465 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
472 fn check_virtual_all_implied() {
478 members = ["bar", "baz"]
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() {}")
488 .with_stderr_contains("[..] --crate-name bar bar/src/lib.rs [..]")
489 .with_stderr_contains("[..] --crate-name baz baz/src/lib.rs [..]")
494 fn check_virtual_manifest_one_project() {
500 members = ["bar", "baz"]
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(); }")
509 p
.cargo("check -p bar")
510 .with_stderr_does_not_contain("[CHECKING] baz v0.1.0 [..]")
513 [CHECKING] bar v0.1.0 ([..])
514 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
521 fn check_virtual_manifest_glob() {
527 members = ["bar", "baz"]
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() {}")
536 p
.cargo("check -p '*z'")
537 .with_stderr_does_not_contain("[CHECKING] bar v0.1.0 [..]")
540 [CHECKING] baz v0.1.0 ([..])
541 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
548 fn exclude_warns_on_non_existing_package() {
549 let p
= project().file("src/lib.rs", "").build();
550 p
.cargo("check --workspace --exclude bar")
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 [..]
563 fn targets_selected_default() {
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", "")
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 [..]")
582 fn targets_selected_all() {
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", "")
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 [..]")
601 fn check_unit_test_profile() {
617 foo
.cargo("check").run();
618 foo
.cargo("check --profile test")
620 .with_stderr_contains("[..]badtext[..]")
624 // Verify what is checked with various command-line filters.
631 fn unused_normal_lib() {}
634 fn unused_unit_lib() {}
642 fn unused_normal_bin() {}
645 fn unused_unit_bin() {}
652 fn unused_normal_t1() {}
655 fn unused_unit_t1() {}
663 fn unused_normal_ex1() {}
666 fn unused_unit_ex1() {}
673 fn unused_normal_b1() {}
676 fn unused_unit_b1() {}
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_[..]")
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[..]")
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[..]")
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[..]")
734 fn check_artifacts() {
735 // Verify which artifacts are created when running check (#4059).
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", "")
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);
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);
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);
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);
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());
779 .join("target/debug/examples")
782 assert_eq
!(p
.glob("target/debug/deps/libfoo-*.rmeta").count(), 1);
783 assert_eq
!(p
.glob("target/debug/examples/libex1-*.rmeta").count(), 1);
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);
796 fn short_message_format() {
798 .file("src/lib.rs", "fn foo() { let _x: bool = 'a'; }")
800 foo
.cargo("check --message-format=short")
802 .with_stderr_contains(
804 src/lib.rs:1:27: error[E0308]: mismatched types
805 error: aborting due to previous error
806 error: could not compile `foo`
829 extern crate proc_macro;
831 use proc_macro::TokenStream;
833 #[proc_macro_derive(Foo)]
834 pub fn demo(_input: TokenStream) -> TokenStream {
852 p
.cargo("check -v").env("CARGO_LOG", "cargo=trace").run();
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();
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();
868 fn error_from_deep_recursion() -> Result
<(), fmt
::Error
> {
869 let mut big_macro
= String
::new();
870 writeln
!(big_macro
, "macro_rules! m {{")?
;
872 writeln
!(big_macro
, "({}) => {{ m!({}); }};", i
, i
+ 1)?
;
874 writeln
!(big_macro
, "}}")?
;
875 writeln
!(big_macro
, "m!(0);")?
;
877 let p
= project().file("src/lib.rs", &big_macro
).build();
878 p
.cargo("check --message-format=json")
880 .with_stdout_contains(
881 "[..]\"message\":\"recursion limit reached while expanding [..]`m[..]`\"[..]",
889 fn rustc_workspace_wrapper_affects_all_workspace_members() {
890 use cargo_test_support
::paths
;
896 members = ["bar", "baz"]
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() {}")
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 [..]")
913 fn rustc_workspace_wrapper_includes_path_deps() {
914 use cargo_test_support
::paths
;
928 baz = { path = "baz" }
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() {}")
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 [..]")
947 fn rustc_workspace_wrapper_respects_primary_units() {
948 use cargo_test_support
::paths
;
954 members = ["bar", "baz"]
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() {}")
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 [..]")
971 fn rustc_workspace_wrapper_excludes_published_deps() {
972 use cargo_test_support
::paths
;
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() {}")
994 Package
::new("baz", "1.0.0").publish();
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 [..]")