1 //! Tests for the `cargo test` command.
2 
3 use cargo_test_support::paths::CargoPathExt;
4 use cargo_test_support::registry::Package;
5 use cargo_test_support::{
6     basic_bin_manifest, basic_lib_manifest, basic_manifest, cargo_exe, project,
7 };
8 use cargo_test_support::{cross_compile, is_nightly, paths};
9 use cargo_test_support::{rustc_host, sleep_ms};
10 use std::fs;
11 
12 #[cargo_test]
cargo_test_simple()13 fn cargo_test_simple() {
14     let p = project()
15         .file("Cargo.toml", &basic_bin_manifest("foo"))
16         .file(
17             "src/main.rs",
18             r#"
19             fn hello() -> &'static str {
20                 "hello"
21             }
22 
23             pub fn main() {
24                 println!("{}", hello())
25             }
26 
27             #[test]
28             fn test_hello() {
29                 assert_eq!(hello(), "hello")
30             }
31             "#,
32         )
33         .build();
34 
35     p.cargo("build").run();
36     assert!(p.bin("foo").is_file());
37 
38     p.process(&p.bin("foo")).with_stdout("hello\n").run();
39 
40     p.cargo("test")
41         .with_stderr(
42             "\
43 [COMPILING] foo v0.5.0 ([CWD])
44 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
45 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])",
46         )
47         .with_stdout_contains("test test_hello ... ok")
48         .run();
49 }
50 
51 #[cargo_test]
cargo_test_release()52 fn cargo_test_release() {
53     let p = project()
54         .file(
55             "Cargo.toml",
56             r#"
57                 [package]
58                 name = "foo"
59                 authors = []
60                 version = "0.1.0"
61 
62                 [dependencies]
63                 bar = { path = "bar" }
64             "#,
65         )
66         .file(
67             "src/lib.rs",
68             r#"
69                 extern crate bar;
70                 pub fn foo() { bar::bar(); }
71 
72                 #[test]
73                 fn test() { foo(); }
74             "#,
75         )
76         .file(
77             "tests/test.rs",
78             r#"
79                 extern crate foo;
80 
81                 #[test]
82                 fn test() { foo::foo(); }
83             "#,
84         )
85         .file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
86         .file("bar/src/lib.rs", "pub fn bar() {}")
87         .build();
88 
89     p.cargo("test -v --release")
90         .with_stderr(
91             "\
92 [COMPILING] bar v0.0.1 ([CWD]/bar)
93 [RUNNING] [..] -C opt-level=3 [..]
94 [COMPILING] foo v0.1.0 ([CWD])
95 [RUNNING] [..] -C opt-level=3 [..]
96 [RUNNING] [..] -C opt-level=3 [..]
97 [RUNNING] [..] -C opt-level=3 [..]
98 [FINISHED] release [optimized] target(s) in [..]
99 [RUNNING] `[..]target/release/deps/foo-[..][EXE]`
100 [RUNNING] `[..]target/release/deps/test-[..][EXE]`
101 [DOCTEST] foo
102 [RUNNING] `rustdoc [..]--test [..]lib.rs[..]`",
103         )
104         .with_stdout_contains_n("test test ... ok", 2)
105         .with_stdout_contains("running 0 tests")
106         .run();
107 }
108 
109 #[cargo_test]
cargo_test_overflow_checks()110 fn cargo_test_overflow_checks() {
111     let p = project()
112         .file(
113             "Cargo.toml",
114             r#"
115             [package]
116             name = "foo"
117             version = "0.5.0"
118             authors = []
119 
120             [[bin]]
121             name = "foo"
122 
123             [profile.release]
124             overflow-checks = true
125             "#,
126         )
127         .file(
128             "src/foo.rs",
129             r#"
130             use std::panic;
131             pub fn main() {
132                 let r = panic::catch_unwind(|| {
133                     [1, i32::MAX].iter().sum::<i32>();
134                 });
135                 assert!(r.is_err());
136             }
137             "#,
138         )
139         .build();
140 
141     p.cargo("build --release").run();
142     assert!(p.release_bin("foo").is_file());
143 
144     p.process(&p.release_bin("foo")).with_stdout("").run();
145 }
146 
147 #[cargo_test]
cargo_test_quiet_with_harness()148 fn cargo_test_quiet_with_harness() {
149     let p = project()
150         .file(
151             "Cargo.toml",
152             r#"
153                 [package]
154                 name = "foo"
155                 version = "0.1.0"
156                 authors = []
157 
158                 [[test]]
159                 name = "foo"
160                 path = "src/foo.rs"
161                 harness = true
162             "#,
163         )
164         .file(
165             "src/foo.rs",
166             r#"
167                 fn main() {}
168                 #[test] fn test_hello() {}
169             "#,
170         )
171         .build();
172 
173     p.cargo("test -q")
174         .with_stdout(
175             "
176 running 1 test
177 .
178 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
179 
180 ",
181         )
182         .with_stderr("")
183         .run();
184 }
185 
186 #[cargo_test]
cargo_test_quiet_no_harness()187 fn cargo_test_quiet_no_harness() {
188     let p = project()
189         .file(
190             "Cargo.toml",
191             r#"
192                 [package]
193                 name = "foo"
194                 version = "0.1.0"
195                 authors = []
196 
197                 [[bin]]
198                 name = "foo"
199                 test = false
200 
201                 [[test]]
202                 name = "foo"
203                 path = "src/main.rs"
204                 harness = false
205             "#,
206         )
207         .file(
208             "src/main.rs",
209             r#"
210                 fn main() {}
211                 #[test] fn test_hello() {}
212             "#,
213         )
214         .build();
215 
216     p.cargo("test -q").with_stdout("").with_stderr("").run();
217 }
218 
219 #[cargo_test]
cargo_doc_test_quiet()220 fn cargo_doc_test_quiet() {
221     let p = project()
222         .file(
223             "Cargo.toml",
224             r#"
225                 [package]
226                 name = "foo"
227                 version = "0.1.0"
228                 authors = []
229             "#,
230         )
231         .file(
232             "src/lib.rs",
233             r#"
234                 /// ```
235                 /// let result = foo::add(2, 3);
236                 /// assert_eq!(result, 5);
237                 /// ```
238                 pub fn add(a: i32, b: i32) -> i32 {
239                     a + b
240                 }
241 
242                 /// ```
243                 /// let result = foo::div(10, 2);
244                 /// assert_eq!(result, 5);
245                 /// ```
246                 ///
247                 /// # Panics
248                 ///
249                 /// The function panics if the second argument is zero.
250                 ///
251                 /// ```rust,should_panic
252                 /// // panics on division by zero
253                 /// foo::div(10, 0);
254                 /// ```
255                 pub fn div(a: i32, b: i32) -> i32 {
256                     if b == 0 {
257                         panic!("Divide-by-zero error");
258                     }
259 
260                     a / b
261                 }
262 
263                 #[test] fn test_hello() {}
264             "#,
265         )
266         .build();
267 
268     p.cargo("test -q")
269         .with_stdout(
270             "
271 running 1 test
272 .
273 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
274 
275 
276 running 3 tests
277 ...
278 test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
279 
280 ",
281         )
282         .with_stderr("")
283         .run();
284 }
285 
286 #[cargo_test]
cargo_test_verbose()287 fn cargo_test_verbose() {
288     let p = project()
289         .file("Cargo.toml", &basic_bin_manifest("foo"))
290         .file(
291             "src/main.rs",
292             r#"
293                 fn main() {}
294                 #[test] fn test_hello() {}
295             "#,
296         )
297         .build();
298 
299     p.cargo("test -v hello")
300         .with_stderr(
301             "\
302 [COMPILING] foo v0.5.0 ([CWD])
303 [RUNNING] `rustc [..] src/main.rs [..]`
304 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
305 [RUNNING] `[CWD]/target/debug/deps/foo-[..] hello`
306 ",
307         )
308         .with_stdout_contains("test test_hello ... ok")
309         .run();
310 }
311 
312 #[cargo_test]
many_similar_names()313 fn many_similar_names() {
314     let p = project()
315         .file(
316             "src/lib.rs",
317             "
318             pub fn foo() {}
319             #[test] fn lib_test() {}
320         ",
321         )
322         .file(
323             "src/main.rs",
324             "
325             extern crate foo;
326             fn main() {}
327             #[test] fn bin_test() { foo::foo() }
328         ",
329         )
330         .file(
331             "tests/foo.rs",
332             r#"
333                 extern crate foo;
334                 #[test] fn test_test() { foo::foo() }
335             "#,
336         )
337         .build();
338 
339     p.cargo("test -v")
340         .with_stdout_contains("test bin_test ... ok")
341         .with_stdout_contains("test lib_test ... ok")
342         .with_stdout_contains("test test_test ... ok")
343         .run();
344 }
345 
346 #[cargo_test]
cargo_test_failing_test_in_bin()347 fn cargo_test_failing_test_in_bin() {
348     let p = project()
349         .file("Cargo.toml", &basic_bin_manifest("foo"))
350         .file(
351             "src/main.rs",
352             r#"
353             fn hello() -> &'static str {
354                 "hello"
355             }
356 
357             pub fn main() {
358                 println!("{}", hello())
359             }
360 
361             #[test]
362             fn test_hello() {
363                 assert_eq!(hello(), "nope")
364             }
365             "#,
366         )
367         .build();
368 
369     p.cargo("build").run();
370     assert!(p.bin("foo").is_file());
371 
372     p.process(&p.bin("foo")).with_stdout("hello\n").run();
373 
374     p.cargo("test")
375         .with_stderr(
376             "\
377 [COMPILING] foo v0.5.0 ([CWD])
378 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
379 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
380 [ERROR] test failed, to rerun pass '--bin foo'",
381         )
382         .with_stdout_contains(
383             "
384 running 1 test
385 test test_hello ... FAILED
386 
387 failures:
388 
389 ---- test_hello stdout ----
390 [..]thread '[..]' panicked at 'assertion failed:[..]",
391         )
392         .with_stdout_contains("[..]`(left == right)`[..]")
393         .with_stdout_contains("[..]left: `\"hello\"`,[..]")
394         .with_stdout_contains("[..]right: `\"nope\"`[..]")
395         .with_stdout_contains("[..]src/main.rs:12[..]")
396         .with_stdout_contains(
397             "\
398 failures:
399     test_hello
400 ",
401         )
402         .with_status(101)
403         .run();
404 }
405 
406 #[cargo_test]
cargo_test_failing_test_in_test()407 fn cargo_test_failing_test_in_test() {
408     let p = project()
409         .file("Cargo.toml", &basic_bin_manifest("foo"))
410         .file("src/main.rs", r#"pub fn main() { println!("hello"); }"#)
411         .file(
412             "tests/footest.rs",
413             "#[test] fn test_hello() { assert!(false) }",
414         )
415         .build();
416 
417     p.cargo("build").run();
418     assert!(p.bin("foo").is_file());
419 
420     p.process(&p.bin("foo")).with_stdout("hello\n").run();
421 
422     p.cargo("test")
423         .with_stderr(
424             "\
425 [COMPILING] foo v0.5.0 ([CWD])
426 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
427 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
428 [RUNNING] [..] (target/debug/deps/footest-[..][EXE])
429 [ERROR] test failed, to rerun pass '--test footest'",
430         )
431         .with_stdout_contains("running 0 tests")
432         .with_stdout_contains(
433             "\
434 running 1 test
435 test test_hello ... FAILED
436 
437 failures:
438 
439 ---- test_hello stdout ----
440 [..]thread '[..]' panicked at 'assertion failed: false', \
441       tests/footest.rs:1[..]
442 ",
443         )
444         .with_stdout_contains(
445             "\
446 failures:
447     test_hello
448 ",
449         )
450         .with_status(101)
451         .run();
452 }
453 
454 #[cargo_test]
cargo_test_failing_test_in_lib()455 fn cargo_test_failing_test_in_lib() {
456     let p = project()
457         .file("Cargo.toml", &basic_lib_manifest("foo"))
458         .file("src/lib.rs", "#[test] fn test_hello() { assert!(false) }")
459         .build();
460 
461     p.cargo("test")
462         .with_stderr(
463             "\
464 [COMPILING] foo v0.5.0 ([CWD])
465 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
466 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
467 [ERROR] test failed, to rerun pass '--lib'",
468         )
469         .with_stdout_contains(
470             "\
471 test test_hello ... FAILED
472 
473 failures:
474 
475 ---- test_hello stdout ----
476 [..]thread '[..]' panicked at 'assertion failed: false', \
477       src/lib.rs:1[..]
478 ",
479         )
480         .with_stdout_contains(
481             "\
482 failures:
483     test_hello
484 ",
485         )
486         .with_status(101)
487         .run();
488 }
489 
490 #[cargo_test]
test_with_lib_dep()491 fn test_with_lib_dep() {
492     let p = project()
493         .file(
494             "Cargo.toml",
495             r#"
496                 [project]
497                 name = "foo"
498                 version = "0.0.1"
499                 authors = []
500 
501                 [[bin]]
502                 name = "baz"
503                 path = "src/main.rs"
504             "#,
505         )
506         .file(
507             "src/lib.rs",
508             r#"
509                 ///
510                 /// ```rust
511                 /// extern crate foo;
512                 /// fn main() {
513                 ///     println!("{:?}", foo::foo());
514                 /// }
515                 /// ```
516                 ///
517                 pub fn foo(){}
518                 #[test] fn lib_test() {}
519             "#,
520         )
521         .file(
522             "src/main.rs",
523             "
524             #[allow(unused_extern_crates)]
525             extern crate foo;
526 
527             fn main() {}
528 
529             #[test]
530             fn bin_test() {}
531         ",
532         )
533         .build();
534 
535     p.cargo("test")
536         .with_stderr(
537             "\
538 [COMPILING] foo v0.0.1 ([CWD])
539 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
540 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
541 [RUNNING] [..] (target/debug/deps/baz-[..][EXE])
542 [DOCTEST] foo",
543         )
544         .with_stdout_contains("test lib_test ... ok")
545         .with_stdout_contains("test bin_test ... ok")
546         .with_stdout_contains_n("test [..] ... ok", 3)
547         .run();
548 }
549 
550 #[cargo_test]
test_with_deep_lib_dep()551 fn test_with_deep_lib_dep() {
552     let p = project()
553         .file(
554             "Cargo.toml",
555             r#"
556                 [package]
557                 name = "foo"
558                 version = "0.0.1"
559                 authors = []
560 
561                 [dependencies.bar]
562                 path = "../bar"
563             "#,
564         )
565         .file(
566             "src/lib.rs",
567             "
568             #[cfg(test)]
569             extern crate bar;
570             /// ```
571             /// foo::foo();
572             /// ```
573             pub fn foo() {}
574 
575             #[test]
576             fn bar_test() {
577                 bar::bar();
578             }
579         ",
580         )
581         .build();
582     let _p2 = project()
583         .at("bar")
584         .file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
585         .file("src/lib.rs", "pub fn bar() {} #[test] fn foo_test() {}")
586         .build();
587 
588     p.cargo("test")
589         .with_stderr(
590             "\
591 [COMPILING] bar v0.0.1 ([..])
592 [COMPILING] foo v0.0.1 ([CWD])
593 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
594 [RUNNING] [..] (target[..])
595 [DOCTEST] foo",
596         )
597         .with_stdout_contains("test bar_test ... ok")
598         .with_stdout_contains_n("test [..] ... ok", 2)
599         .run();
600 }
601 
602 #[cargo_test]
external_test_explicit()603 fn external_test_explicit() {
604     let p = project()
605         .file(
606             "Cargo.toml",
607             r#"
608                 [project]
609                 name = "foo"
610                 version = "0.0.1"
611                 authors = []
612 
613                 [[test]]
614                 name = "test"
615                 path = "src/test.rs"
616             "#,
617         )
618         .file(
619             "src/lib.rs",
620             r#"
621                 pub fn get_hello() -> &'static str { "Hello" }
622 
623                 #[test]
624                 fn internal_test() {}
625             "#,
626         )
627         .file(
628             "src/test.rs",
629             r#"
630                 extern crate foo;
631 
632                 #[test]
633                 fn external_test() { assert_eq!(foo::get_hello(), "Hello") }
634             "#,
635         )
636         .build();
637 
638     p.cargo("test")
639         .with_stderr(
640             "\
641 [COMPILING] foo v0.0.1 ([CWD])
642 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
643 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
644 [RUNNING] [..] (target/debug/deps/test-[..][EXE])
645 [DOCTEST] foo",
646         )
647         .with_stdout_contains("test internal_test ... ok")
648         .with_stdout_contains("test external_test ... ok")
649         .with_stdout_contains("running 0 tests")
650         .run();
651 }
652 
653 #[cargo_test]
external_test_named_test()654 fn external_test_named_test() {
655     let p = project()
656         .file(
657             "Cargo.toml",
658             r#"
659                 [project]
660                 name = "foo"
661                 version = "0.0.1"
662                 authors = []
663 
664                 [[test]]
665                 name = "test"
666             "#,
667         )
668         .file("src/lib.rs", "")
669         .file("tests/test.rs", "#[test] fn foo() {}")
670         .build();
671 
672     p.cargo("test").run();
673 }
674 
675 #[cargo_test]
external_test_implicit()676 fn external_test_implicit() {
677     let p = project()
678         .file(
679             "src/lib.rs",
680             r#"
681                 pub fn get_hello() -> &'static str { "Hello" }
682 
683                 #[test]
684                 fn internal_test() {}
685             "#,
686         )
687         .file(
688             "tests/external.rs",
689             r#"
690                 extern crate foo;
691 
692                 #[test]
693                 fn external_test() { assert_eq!(foo::get_hello(), "Hello") }
694             "#,
695         )
696         .build();
697 
698     p.cargo("test")
699         .with_stderr(
700             "\
701 [COMPILING] foo v0.0.1 ([CWD])
702 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
703 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
704 [RUNNING] [..] (target/debug/deps/external-[..][EXE])
705 [DOCTEST] foo",
706         )
707         .with_stdout_contains("test internal_test ... ok")
708         .with_stdout_contains("test external_test ... ok")
709         .with_stdout_contains("running 0 tests")
710         .run();
711 }
712 
713 #[cargo_test]
dont_run_examples()714 fn dont_run_examples() {
715     let p = project()
716         .file("src/lib.rs", "")
717         .file(
718             "examples/dont-run-me-i-will-fail.rs",
719             r#"
720                 fn main() { panic!("Examples should not be run by 'cargo test'"); }
721             "#,
722         )
723         .build();
724     p.cargo("test").run();
725 }
726 
727 #[cargo_test]
pass_through_command_line()728 fn pass_through_command_line() {
729     let p = project()
730         .file(
731             "src/lib.rs",
732             "
733             #[test] fn foo() {}
734             #[test] fn bar() {}
735         ",
736         )
737         .build();
738 
739     p.cargo("test bar")
740         .with_stderr(
741             "\
742 [COMPILING] foo v0.0.1 ([CWD])
743 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
744 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
745 ",
746         )
747         .with_stdout_contains("running 1 test")
748         .with_stdout_contains("test bar ... ok")
749         .run();
750 
751     p.cargo("test foo")
752         .with_stderr(
753             "\
754 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
755 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
756 ",
757         )
758         .with_stdout_contains("running 1 test")
759         .with_stdout_contains("test foo ... ok")
760         .run();
761 }
762 
763 // Regression test for running cargo-test twice with
764 // tests in an rlib
765 #[cargo_test]
cargo_test_twice()766 fn cargo_test_twice() {
767     let p = project()
768         .file("Cargo.toml", &basic_lib_manifest("foo"))
769         .file(
770             "src/foo.rs",
771             r#"
772             #![crate_type = "rlib"]
773 
774             #[test]
775             fn dummy_test() { }
776             "#,
777         )
778         .build();
779 
780     for _ in 0..2 {
781         p.cargo("test").run();
782     }
783 }
784 
785 #[cargo_test]
lib_bin_same_name()786 fn lib_bin_same_name() {
787     let p = project()
788         .file(
789             "Cargo.toml",
790             r#"
791                 [project]
792                 name = "foo"
793                 version = "0.0.1"
794                 authors = []
795 
796                 [lib]
797                 name = "foo"
798                 [[bin]]
799                 name = "foo"
800             "#,
801         )
802         .file("src/lib.rs", "#[test] fn lib_test() {}")
803         .file(
804             "src/main.rs",
805             "
806             #[allow(unused_extern_crates)]
807             extern crate foo;
808 
809             #[test]
810             fn bin_test() {}
811         ",
812         )
813         .build();
814 
815     p.cargo("test")
816         .with_stderr(
817             "\
818 [COMPILING] foo v0.0.1 ([CWD])
819 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
820 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
821 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
822 [DOCTEST] foo",
823         )
824         .with_stdout_contains_n("test [..] ... ok", 2)
825         .with_stdout_contains("running 0 tests")
826         .run();
827 }
828 
829 #[cargo_test]
lib_with_standard_name()830 fn lib_with_standard_name() {
831     let p = project()
832         .file("Cargo.toml", &basic_manifest("syntax", "0.0.1"))
833         .file(
834             "src/lib.rs",
835             "
836             /// ```
837             /// syntax::foo();
838             /// ```
839             pub fn foo() {}
840 
841             #[test]
842             fn foo_test() {}
843         ",
844         )
845         .file(
846             "tests/test.rs",
847             "
848             extern crate syntax;
849 
850             #[test]
851             fn test() { syntax::foo() }
852         ",
853         )
854         .build();
855 
856     p.cargo("test")
857         .with_stderr(
858             "\
859 [COMPILING] syntax v0.0.1 ([CWD])
860 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
861 [RUNNING] [..] (target/debug/deps/syntax-[..][EXE])
862 [RUNNING] [..] (target/debug/deps/test-[..][EXE])
863 [DOCTEST] syntax",
864         )
865         .with_stdout_contains("test foo_test ... ok")
866         .with_stdout_contains("test test ... ok")
867         .with_stdout_contains_n("test [..] ... ok", 3)
868         .run();
869 }
870 
871 #[cargo_test]
lib_with_standard_name2()872 fn lib_with_standard_name2() {
873     let p = project()
874         .file(
875             "Cargo.toml",
876             r#"
877                 [package]
878                 name = "syntax"
879                 version = "0.0.1"
880                 authors = []
881 
882                 [lib]
883                 name = "syntax"
884                 test = false
885                 doctest = false
886             "#,
887         )
888         .file("src/lib.rs", "pub fn foo() {}")
889         .file(
890             "src/main.rs",
891             "
892             extern crate syntax;
893 
894             fn main() {}
895 
896             #[test]
897             fn test() { syntax::foo() }
898         ",
899         )
900         .build();
901 
902     p.cargo("test")
903         .with_stderr(
904             "\
905 [COMPILING] syntax v0.0.1 ([CWD])
906 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
907 [RUNNING] [..] (target/debug/deps/syntax-[..][EXE])",
908         )
909         .with_stdout_contains("test test ... ok")
910         .run();
911 }
912 
913 #[cargo_test]
lib_without_name()914 fn lib_without_name() {
915     let p = project()
916         .file(
917             "Cargo.toml",
918             r#"
919                 [package]
920                 name = "syntax"
921                 version = "0.0.1"
922                 authors = []
923 
924                 [lib]
925                 test = false
926                 doctest = false
927             "#,
928         )
929         .file("src/lib.rs", "pub fn foo() {}")
930         .file(
931             "src/main.rs",
932             "
933             extern crate syntax;
934 
935             fn main() {}
936 
937             #[test]
938             fn test() { syntax::foo() }
939         ",
940         )
941         .build();
942 
943     p.cargo("test")
944         .with_stderr(
945             "\
946 [COMPILING] syntax v0.0.1 ([CWD])
947 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
948 [RUNNING] [..] (target/debug/deps/syntax-[..][EXE])",
949         )
950         .with_stdout_contains("test test ... ok")
951         .run();
952 }
953 
954 #[cargo_test]
bin_without_name()955 fn bin_without_name() {
956     let p = project()
957         .file(
958             "Cargo.toml",
959             r#"
960                 [package]
961                 name = "syntax"
962                 version = "0.0.1"
963                 authors = []
964 
965                 [lib]
966                 test = false
967                 doctest = false
968 
969                 [[bin]]
970                 path = "src/main.rs"
971             "#,
972         )
973         .file("src/lib.rs", "pub fn foo() {}")
974         .file(
975             "src/main.rs",
976             "
977             extern crate syntax;
978 
979             fn main() {}
980 
981             #[test]
982             fn test() { syntax::foo() }
983         ",
984         )
985         .build();
986 
987     p.cargo("test")
988         .with_status(101)
989         .with_stderr(
990             "\
991 [ERROR] failed to parse manifest at `[..]`
992 
993 Caused by:
994   binary target bin.name is required",
995         )
996         .run();
997 }
998 
999 #[cargo_test]
bench_without_name()1000 fn bench_without_name() {
1001     let p = project()
1002         .file(
1003             "Cargo.toml",
1004             r#"
1005                 [package]
1006                 name = "syntax"
1007                 version = "0.0.1"
1008                 authors = []
1009 
1010                 [lib]
1011                 test = false
1012                 doctest = false
1013 
1014                 [[bench]]
1015                 path = "src/bench.rs"
1016             "#,
1017         )
1018         .file("src/lib.rs", "pub fn foo() {}")
1019         .file(
1020             "src/main.rs",
1021             "
1022             extern crate syntax;
1023 
1024             fn main() {}
1025 
1026             #[test]
1027             fn test() { syntax::foo() }
1028         ",
1029         )
1030         .file(
1031             "src/bench.rs",
1032             "
1033             #![feature(test)]
1034             extern crate syntax;
1035             extern crate test;
1036 
1037             #[bench]
1038             fn external_bench(_b: &mut test::Bencher) {}
1039         ",
1040         )
1041         .build();
1042 
1043     p.cargo("test")
1044         .with_status(101)
1045         .with_stderr(
1046             "\
1047 [ERROR] failed to parse manifest at `[..]`
1048 
1049 Caused by:
1050   benchmark target bench.name is required",
1051         )
1052         .run();
1053 }
1054 
1055 #[cargo_test]
test_without_name()1056 fn test_without_name() {
1057     let p = project()
1058         .file(
1059             "Cargo.toml",
1060             r#"
1061                 [package]
1062                 name = "syntax"
1063                 version = "0.0.1"
1064                 authors = []
1065 
1066                 [lib]
1067                 test = false
1068                 doctest = false
1069 
1070                 [[test]]
1071                 path = "src/test.rs"
1072             "#,
1073         )
1074         .file(
1075             "src/lib.rs",
1076             r#"
1077                 pub fn foo() {}
1078                 pub fn get_hello() -> &'static str { "Hello" }
1079             "#,
1080         )
1081         .file(
1082             "src/main.rs",
1083             "
1084             extern crate syntax;
1085 
1086             fn main() {}
1087 
1088             #[test]
1089             fn test() { syntax::foo() }
1090         ",
1091         )
1092         .file(
1093             "src/test.rs",
1094             r#"
1095                 extern crate syntax;
1096 
1097                 #[test]
1098                 fn external_test() { assert_eq!(syntax::get_hello(), "Hello") }
1099             "#,
1100         )
1101         .build();
1102 
1103     p.cargo("test")
1104         .with_status(101)
1105         .with_stderr(
1106             "\
1107 [ERROR] failed to parse manifest at `[..]`
1108 
1109 Caused by:
1110   test target test.name is required",
1111         )
1112         .run();
1113 }
1114 
1115 #[cargo_test]
example_without_name()1116 fn example_without_name() {
1117     let p = project()
1118         .file(
1119             "Cargo.toml",
1120             r#"
1121                 [package]
1122                 name = "syntax"
1123                 version = "0.0.1"
1124                 authors = []
1125 
1126                 [lib]
1127                 test = false
1128                 doctest = false
1129 
1130                 [[example]]
1131                 path = "examples/example.rs"
1132             "#,
1133         )
1134         .file("src/lib.rs", "pub fn foo() {}")
1135         .file(
1136             "src/main.rs",
1137             "
1138             extern crate syntax;
1139 
1140             fn main() {}
1141 
1142             #[test]
1143             fn test() { syntax::foo() }
1144         ",
1145         )
1146         .file(
1147             "examples/example.rs",
1148             r#"
1149                 extern crate syntax;
1150 
1151                 fn main() {
1152                     println!("example1");
1153                 }
1154             "#,
1155         )
1156         .build();
1157 
1158     p.cargo("test")
1159         .with_status(101)
1160         .with_stderr(
1161             "\
1162 [ERROR] failed to parse manifest at `[..]`
1163 
1164 Caused by:
1165   example target example.name is required",
1166         )
1167         .run();
1168 }
1169 
1170 #[cargo_test]
bin_there_for_integration()1171 fn bin_there_for_integration() {
1172     let p = project()
1173         .file(
1174             "src/main.rs",
1175             "
1176             fn main() { std::process::exit(101); }
1177             #[test] fn main_test() {}
1178         ",
1179         )
1180         .file(
1181             "tests/foo.rs",
1182             r#"
1183                 use std::process::Command;
1184                 #[test]
1185                 fn test_test() {
1186                     let status = Command::new("target/debug/foo").status().unwrap();
1187                     assert_eq!(status.code(), Some(101));
1188                 }
1189             "#,
1190         )
1191         .build();
1192 
1193     p.cargo("test -v")
1194         .with_stdout_contains("test main_test ... ok")
1195         .with_stdout_contains("test test_test ... ok")
1196         .run();
1197 }
1198 
1199 #[cargo_test]
test_dylib()1200 fn test_dylib() {
1201     let p = project()
1202         .file(
1203             "Cargo.toml",
1204             r#"
1205                 [package]
1206                 name = "foo"
1207                 version = "0.0.1"
1208                 authors = []
1209 
1210                 [lib]
1211                 name = "foo"
1212                 crate_type = ["dylib"]
1213 
1214                 [dependencies.bar]
1215                 path = "bar"
1216             "#,
1217         )
1218         .file(
1219             "src/lib.rs",
1220             r#"
1221                 extern crate bar as the_bar;
1222 
1223                 pub fn bar() { the_bar::baz(); }
1224 
1225                 #[test]
1226                 fn foo() { bar(); }
1227             "#,
1228         )
1229         .file(
1230             "tests/test.rs",
1231             r#"
1232                 extern crate foo as the_foo;
1233 
1234                 #[test]
1235                 fn foo() { the_foo::bar(); }
1236             "#,
1237         )
1238         .file(
1239             "bar/Cargo.toml",
1240             r#"
1241                 [package]
1242                 name = "bar"
1243                 version = "0.0.1"
1244                 authors = []
1245 
1246                 [lib]
1247                 name = "bar"
1248                 crate_type = ["dylib"]
1249             "#,
1250         )
1251         .file("bar/src/lib.rs", "pub fn baz() {}")
1252         .build();
1253 
1254     p.cargo("test")
1255         .with_stderr(
1256             "\
1257 [COMPILING] bar v0.0.1 ([CWD]/bar)
1258 [COMPILING] foo v0.0.1 ([CWD])
1259 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1260 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
1261 [RUNNING] [..] (target/debug/deps/test-[..][EXE])",
1262         )
1263         .with_stdout_contains_n("test foo ... ok", 2)
1264         .run();
1265 
1266     p.root().move_into_the_past();
1267     p.cargo("test")
1268         .with_stderr(
1269             "\
1270 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1271 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
1272 [RUNNING] [..] (target/debug/deps/test-[..][EXE])",
1273         )
1274         .with_stdout_contains_n("test foo ... ok", 2)
1275         .run();
1276 }
1277 
1278 #[cargo_test]
test_twice_with_build_cmd()1279 fn test_twice_with_build_cmd() {
1280     let p = project()
1281         .file(
1282             "Cargo.toml",
1283             r#"
1284                 [package]
1285                 name = "foo"
1286                 version = "0.0.1"
1287                 authors = []
1288                 build = "build.rs"
1289             "#,
1290         )
1291         .file("build.rs", "fn main() {}")
1292         .file("src/lib.rs", "#[test] fn foo() {}")
1293         .build();
1294 
1295     p.cargo("test")
1296         .with_stderr(
1297             "\
1298 [COMPILING] foo v0.0.1 ([CWD])
1299 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1300 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
1301 [DOCTEST] foo",
1302         )
1303         .with_stdout_contains("test foo ... ok")
1304         .with_stdout_contains("running 0 tests")
1305         .run();
1306 
1307     p.cargo("test")
1308         .with_stderr(
1309             "\
1310 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1311 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
1312 [DOCTEST] foo",
1313         )
1314         .with_stdout_contains("test foo ... ok")
1315         .with_stdout_contains("running 0 tests")
1316         .run();
1317 }
1318 
1319 #[cargo_test]
test_then_build()1320 fn test_then_build() {
1321     let p = project().file("src/lib.rs", "#[test] fn foo() {}").build();
1322 
1323     p.cargo("test")
1324         .with_stderr(
1325             "\
1326 [COMPILING] foo v0.0.1 ([CWD])
1327 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1328 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
1329 [DOCTEST] foo",
1330         )
1331         .with_stdout_contains("test foo ... ok")
1332         .with_stdout_contains("running 0 tests")
1333         .run();
1334 
1335     p.cargo("build").with_stdout("").run();
1336 }
1337 
1338 #[cargo_test]
test_no_run()1339 fn test_no_run() {
1340     let p = project()
1341         .file("src/lib.rs", "#[test] fn foo() { panic!() }")
1342         .build();
1343 
1344     p.cargo("test --no-run")
1345         .with_stderr(
1346             "\
1347 [COMPILING] foo v0.0.1 ([CWD])
1348 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1349 ",
1350         )
1351         .run();
1352 }
1353 
1354 #[cargo_test]
test_run_specific_bin_target()1355 fn test_run_specific_bin_target() {
1356     let prj = project()
1357         .file(
1358             "Cargo.toml",
1359             r#"
1360                 [package]
1361                 name = "foo"
1362                 version = "0.0.1"
1363                 authors = []
1364 
1365                 [[bin]]
1366                 name="bin1"
1367                 path="src/bin1.rs"
1368 
1369                 [[bin]]
1370                 name="bin2"
1371                 path="src/bin2.rs"
1372             "#,
1373         )
1374         .file("src/bin1.rs", "#[test] fn test1() { }")
1375         .file("src/bin2.rs", "#[test] fn test2() { }")
1376         .build();
1377 
1378     prj.cargo("test --bin bin2")
1379         .with_stderr(
1380             "\
1381 [COMPILING] foo v0.0.1 ([CWD])
1382 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1383 [RUNNING] [..] (target/debug/deps/bin2-[..][EXE])",
1384         )
1385         .with_stdout_contains("test test2 ... ok")
1386         .run();
1387 }
1388 
1389 #[cargo_test]
test_run_implicit_bin_target()1390 fn test_run_implicit_bin_target() {
1391     let prj = project()
1392         .file(
1393             "Cargo.toml",
1394             r#"
1395                 [package]
1396                 name = "foo"
1397                 version = "0.0.1"
1398                 authors = []
1399 
1400                 [[bin]]
1401                 name="mybin"
1402                 path="src/mybin.rs"
1403             "#,
1404         )
1405         .file(
1406             "src/mybin.rs",
1407             "#[test] fn test_in_bin() { }
1408                fn main() { panic!(\"Don't execute me!\"); }",
1409         )
1410         .file("tests/mytest.rs", "#[test] fn test_in_test() { }")
1411         .file("benches/mybench.rs", "#[test] fn test_in_bench() { }")
1412         .file(
1413             "examples/myexm.rs",
1414             "#[test] fn test_in_exm() { }
1415                fn main() { panic!(\"Don't execute me!\"); }",
1416         )
1417         .build();
1418 
1419     prj.cargo("test --bins")
1420         .with_stderr(
1421             "\
1422 [COMPILING] foo v0.0.1 ([CWD])
1423 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1424 [RUNNING] [..] (target/debug/deps/mybin-[..][EXE])",
1425         )
1426         .with_stdout_contains("test test_in_bin ... ok")
1427         .run();
1428 }
1429 
1430 #[cargo_test]
test_run_specific_test_target()1431 fn test_run_specific_test_target() {
1432     let prj = project()
1433         .file("src/bin/a.rs", "fn main() { }")
1434         .file("src/bin/b.rs", "#[test] fn test_b() { } fn main() { }")
1435         .file("tests/a.rs", "#[test] fn test_a() { }")
1436         .file("tests/b.rs", "#[test] fn test_b() { }")
1437         .build();
1438 
1439     prj.cargo("test --test b")
1440         .with_stderr(
1441             "\
1442 [COMPILING] foo v0.0.1 ([CWD])
1443 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1444 [RUNNING] [..] (target/debug/deps/b-[..][EXE])",
1445         )
1446         .with_stdout_contains("test test_b ... ok")
1447         .run();
1448 }
1449 
1450 #[cargo_test]
test_run_implicit_test_target()1451 fn test_run_implicit_test_target() {
1452     let prj = project()
1453         .file(
1454             "Cargo.toml",
1455             r#"
1456                 [package]
1457                 name = "foo"
1458                 version = "0.0.1"
1459                 authors = []
1460 
1461                 [[bin]]
1462                 name="mybin"
1463                 path="src/mybin.rs"
1464             "#,
1465         )
1466         .file(
1467             "src/mybin.rs",
1468             "#[test] fn test_in_bin() { }
1469                fn main() { panic!(\"Don't execute me!\"); }",
1470         )
1471         .file("tests/mytest.rs", "#[test] fn test_in_test() { }")
1472         .file("benches/mybench.rs", "#[test] fn test_in_bench() { }")
1473         .file(
1474             "examples/myexm.rs",
1475             "fn main() { compile_error!(\"Don't build me!\"); }",
1476         )
1477         .build();
1478 
1479     prj.cargo("test --tests")
1480         .with_stderr(
1481             "\
1482 [COMPILING] foo v0.0.1 ([CWD])
1483 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1484 [RUNNING] [..] (target/debug/deps/mybin-[..][EXE])
1485 [RUNNING] [..] (target/debug/deps/mytest-[..][EXE])",
1486         )
1487         .with_stdout_contains("test test_in_test ... ok")
1488         .run();
1489 }
1490 
1491 #[cargo_test]
test_run_implicit_bench_target()1492 fn test_run_implicit_bench_target() {
1493     let prj = project()
1494         .file(
1495             "Cargo.toml",
1496             r#"
1497                 [package]
1498                 name = "foo"
1499                 version = "0.0.1"
1500                 authors = []
1501 
1502                 [[bin]]
1503                 name="mybin"
1504                 path="src/mybin.rs"
1505             "#,
1506         )
1507         .file(
1508             "src/mybin.rs",
1509             "#[test] fn test_in_bin() { }
1510                fn main() { panic!(\"Don't execute me!\"); }",
1511         )
1512         .file("tests/mytest.rs", "#[test] fn test_in_test() { }")
1513         .file("benches/mybench.rs", "#[test] fn test_in_bench() { }")
1514         .file(
1515             "examples/myexm.rs",
1516             "fn main() { compile_error!(\"Don't build me!\"); }",
1517         )
1518         .build();
1519 
1520     prj.cargo("test --benches")
1521         .with_stderr(
1522             "\
1523 [COMPILING] foo v0.0.1 ([CWD])
1524 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1525 [RUNNING] [..] (target/debug/deps/mybin-[..][EXE])
1526 [RUNNING] [..] (target/debug/deps/mybench-[..][EXE])",
1527         )
1528         .with_stdout_contains("test test_in_bench ... ok")
1529         .run();
1530 }
1531 
1532 #[cargo_test]
test_run_implicit_example_target()1533 fn test_run_implicit_example_target() {
1534     let prj = project()
1535         .file(
1536             "Cargo.toml",
1537             r#"
1538                 [package]
1539                 name = "foo"
1540                 version = "0.0.1"
1541                 authors = []
1542 
1543                 [[bin]]
1544                 name = "mybin"
1545                 path = "src/mybin.rs"
1546 
1547                 [[example]]
1548                 name = "myexm1"
1549 
1550                 [[example]]
1551                 name = "myexm2"
1552                 test = true
1553             "#,
1554         )
1555         .file(
1556             "src/mybin.rs",
1557             "#[test] fn test_in_bin() { }
1558                fn main() { panic!(\"Don't execute me!\"); }",
1559         )
1560         .file("tests/mytest.rs", "#[test] fn test_in_test() { }")
1561         .file("benches/mybench.rs", "#[test] fn test_in_bench() { }")
1562         .file(
1563             "examples/myexm1.rs",
1564             "#[test] fn test_in_exm() { }
1565                fn main() { panic!(\"Don't execute me!\"); }",
1566         )
1567         .file(
1568             "examples/myexm2.rs",
1569             "#[test] fn test_in_exm() { }
1570                fn main() { panic!(\"Don't execute me!\"); }",
1571         )
1572         .build();
1573 
1574     // Compiles myexm1 as normal, but does not run it.
1575     prj.cargo("test -v")
1576         .with_stderr_contains("[RUNNING] `rustc [..]myexm1.rs [..]--crate-type bin[..]")
1577         .with_stderr_contains("[RUNNING] `rustc [..]myexm2.rs [..]--test[..]")
1578         .with_stderr_does_not_contain("[RUNNING] [..]myexm1-[..]")
1579         .with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm2-[..]")
1580         .run();
1581 
1582     // Only tests myexm2.
1583     prj.cargo("test --tests")
1584         .with_stderr_does_not_contain("[RUNNING] [..]myexm1-[..]")
1585         .with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm2-[..]")
1586         .run();
1587 
1588     // Tests all examples.
1589     prj.cargo("test --examples")
1590         .with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm1-[..]")
1591         .with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm2-[..]")
1592         .run();
1593 
1594     // Test an example, even without `test` set.
1595     prj.cargo("test --example myexm1")
1596         .with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm1-[..]")
1597         .run();
1598 
1599     // Tests all examples.
1600     prj.cargo("test --all-targets")
1601         .with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm1-[..]")
1602         .with_stderr_contains("[RUNNING] [..]target/debug/examples/myexm2-[..]")
1603         .run();
1604 }
1605 
1606 #[cargo_test]
test_filtered_excludes_compiling_examples()1607 fn test_filtered_excludes_compiling_examples() {
1608     let p = project()
1609         .file(
1610             "src/lib.rs",
1611             "#[cfg(test)] mod tests { #[test] fn foo() { assert!(true); } }",
1612         )
1613         .file("examples/ex1.rs", "fn main() {}")
1614         .build();
1615 
1616     p.cargo("test -v foo")
1617         .with_stdout(
1618             "
1619 running 1 test
1620 test tests::foo ... ok
1621 
1622 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
1623 
1624 ",
1625         )
1626         .with_stderr(
1627             "\
1628 [COMPILING] foo v0.0.1 ([CWD])
1629 [RUNNING] `rustc --crate-name foo src/lib.rs [..] --test [..]`
1630 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1631 [RUNNING] `[CWD]/target/debug/deps/foo-[..] foo`
1632 ",
1633         )
1634         .with_stderr_does_not_contain("[RUNNING][..]rustc[..]ex1[..]")
1635         .run();
1636 }
1637 
1638 #[cargo_test]
test_no_harness()1639 fn test_no_harness() {
1640     let p = project()
1641         .file(
1642             "Cargo.toml",
1643             r#"
1644                 [package]
1645                 name = "foo"
1646                 version = "0.0.1"
1647                 authors = []
1648 
1649                 [[bin]]
1650                 name = "foo"
1651                 test = false
1652 
1653                 [[test]]
1654                 name = "bar"
1655                 path = "foo.rs"
1656                 harness = false
1657             "#,
1658         )
1659         .file("src/main.rs", "fn main() {}")
1660         .file("foo.rs", "fn main() {}")
1661         .build();
1662 
1663     p.cargo("test -- --nocapture")
1664         .with_stderr(
1665             "\
1666 [COMPILING] foo v0.0.1 ([CWD])
1667 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1668 [RUNNING] [..] (target/debug/deps/bar-[..][EXE])
1669 ",
1670         )
1671         .run();
1672 }
1673 
1674 #[cargo_test]
selective_testing()1675 fn selective_testing() {
1676     let p = project()
1677         .file(
1678             "Cargo.toml",
1679             r#"
1680                 [package]
1681                 name = "foo"
1682                 version = "0.0.1"
1683                 authors = []
1684 
1685                 [dependencies.d1]
1686                     path = "d1"
1687                 [dependencies.d2]
1688                     path = "d2"
1689 
1690                 [lib]
1691                     name = "foo"
1692                     doctest = false
1693             "#,
1694         )
1695         .file("src/lib.rs", "")
1696         .file(
1697             "d1/Cargo.toml",
1698             r#"
1699                 [package]
1700                 name = "d1"
1701                 version = "0.0.1"
1702                 authors = []
1703 
1704                 [lib]
1705                     name = "d1"
1706                     doctest = false
1707             "#,
1708         )
1709         .file("d1/src/lib.rs", "")
1710         .file(
1711             "d1/src/main.rs",
1712             "#[allow(unused_extern_crates)] extern crate d1; fn main() {}",
1713         )
1714         .file(
1715             "d2/Cargo.toml",
1716             r#"
1717                 [package]
1718                 name = "d2"
1719                 version = "0.0.1"
1720                 authors = []
1721 
1722                 [lib]
1723                     name = "d2"
1724                     doctest = false
1725             "#,
1726         )
1727         .file("d2/src/lib.rs", "")
1728         .file(
1729             "d2/src/main.rs",
1730             "#[allow(unused_extern_crates)] extern crate d2; fn main() {}",
1731         );
1732     let p = p.build();
1733 
1734     println!("d1");
1735     p.cargo("test -p d1")
1736         .with_stderr(
1737             "\
1738 [COMPILING] d1 v0.0.1 ([CWD]/d1)
1739 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1740 [RUNNING] [..] (target/debug/deps/d1-[..][EXE])
1741 [RUNNING] [..] (target/debug/deps/d1-[..][EXE])",
1742         )
1743         .with_stdout_contains_n("running 0 tests", 2)
1744         .run();
1745 
1746     println!("d2");
1747     p.cargo("test -p d2")
1748         .with_stderr(
1749             "\
1750 [COMPILING] d2 v0.0.1 ([CWD]/d2)
1751 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1752 [RUNNING] [..] (target/debug/deps/d2-[..][EXE])
1753 [RUNNING] [..] (target/debug/deps/d2-[..][EXE])",
1754         )
1755         .with_stdout_contains_n("running 0 tests", 2)
1756         .run();
1757 
1758     println!("whole");
1759     p.cargo("test")
1760         .with_stderr(
1761             "\
1762 [COMPILING] foo v0.0.1 ([CWD])
1763 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1764 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])",
1765         )
1766         .with_stdout_contains("running 0 tests")
1767         .run();
1768 }
1769 
1770 #[cargo_test]
almost_cyclic_but_not_quite()1771 fn almost_cyclic_but_not_quite() {
1772     let p = project()
1773         .file(
1774             "Cargo.toml",
1775             r#"
1776                 [package]
1777                 name = "foo"
1778                 version = "0.0.1"
1779                 authors = []
1780 
1781                 [dev-dependencies.b]
1782                 path = "b"
1783                 [dev-dependencies.c]
1784                 path = "c"
1785             "#,
1786         )
1787         .file(
1788             "src/lib.rs",
1789             r#"
1790                 #[cfg(test)] extern crate b;
1791                 #[cfg(test)] extern crate c;
1792             "#,
1793         )
1794         .file(
1795             "b/Cargo.toml",
1796             r#"
1797                 [package]
1798                 name = "b"
1799                 version = "0.0.1"
1800                 authors = []
1801 
1802                 [dependencies.foo]
1803                 path = ".."
1804             "#,
1805         )
1806         .file(
1807             "b/src/lib.rs",
1808             r#"
1809                 #[allow(unused_extern_crates)]
1810                 extern crate foo;
1811             "#,
1812         )
1813         .file("c/Cargo.toml", &basic_manifest("c", "0.0.1"))
1814         .file("c/src/lib.rs", "")
1815         .build();
1816 
1817     p.cargo("build").run();
1818     p.cargo("test").run();
1819 }
1820 
1821 #[cargo_test]
build_then_selective_test()1822 fn build_then_selective_test() {
1823     let p = project()
1824         .file(
1825             "Cargo.toml",
1826             r#"
1827                 [package]
1828                 name = "foo"
1829                 version = "0.0.1"
1830                 authors = []
1831 
1832                 [dependencies.b]
1833                 path = "b"
1834             "#,
1835         )
1836         .file(
1837             "src/lib.rs",
1838             "#[allow(unused_extern_crates)] extern crate b;",
1839         )
1840         .file(
1841             "src/main.rs",
1842             r#"
1843                 #[allow(unused_extern_crates)]
1844                 extern crate b;
1845                 #[allow(unused_extern_crates)]
1846                 extern crate foo;
1847                 fn main() {}
1848             "#,
1849         )
1850         .file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
1851         .file("b/src/lib.rs", "")
1852         .build();
1853 
1854     p.cargo("build").run();
1855     p.root().move_into_the_past();
1856     p.cargo("test -p b").run();
1857 }
1858 
1859 #[cargo_test]
example_dev_dep()1860 fn example_dev_dep() {
1861     let p = project()
1862         .file(
1863             "Cargo.toml",
1864             r#"
1865                 [project]
1866                 name = "foo"
1867                 version = "0.0.1"
1868                 authors = []
1869 
1870                 [dev-dependencies.bar]
1871                 path = "bar"
1872             "#,
1873         )
1874         .file("src/lib.rs", "")
1875         .file("examples/e1.rs", "extern crate bar; fn main() {}")
1876         .file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
1877         .file(
1878             "bar/src/lib.rs",
1879             r#"
1880                 // make sure this file takes awhile to compile
1881                 macro_rules! f0( () => (1) );
1882                 macro_rules! f1( () => ({(f0!()) + (f0!())}) );
1883                 macro_rules! f2( () => ({(f1!()) + (f1!())}) );
1884                 macro_rules! f3( () => ({(f2!()) + (f2!())}) );
1885                 macro_rules! f4( () => ({(f3!()) + (f3!())}) );
1886                 macro_rules! f5( () => ({(f4!()) + (f4!())}) );
1887                 macro_rules! f6( () => ({(f5!()) + (f5!())}) );
1888                 macro_rules! f7( () => ({(f6!()) + (f6!())}) );
1889                 macro_rules! f8( () => ({(f7!()) + (f7!())}) );
1890                 pub fn bar() {
1891                     f8!();
1892                 }
1893             "#,
1894         )
1895         .build();
1896     p.cargo("test").run();
1897     p.cargo("run --example e1 --release -v").run();
1898 }
1899 
1900 #[cargo_test]
selective_testing_with_docs()1901 fn selective_testing_with_docs() {
1902     let p = project()
1903         .file(
1904             "Cargo.toml",
1905             r#"
1906                 [package]
1907                 name = "foo"
1908                 version = "0.0.1"
1909                 authors = []
1910 
1911                 [dependencies.d1]
1912                     path = "d1"
1913             "#,
1914         )
1915         .file(
1916             "src/lib.rs",
1917             r#"
1918                 /// ```
1919                 /// not valid rust
1920                 /// ```
1921                 pub fn foo() {}
1922             "#,
1923         )
1924         .file(
1925             "d1/Cargo.toml",
1926             r#"
1927                 [package]
1928                 name = "d1"
1929                 version = "0.0.1"
1930                 authors = []
1931 
1932                 [lib]
1933                 name = "d1"
1934                 path = "d1.rs"
1935             "#,
1936         )
1937         .file("d1/d1.rs", "");
1938     let p = p.build();
1939 
1940     p.cargo("test -p d1")
1941         .with_stderr(
1942             "\
1943 [COMPILING] d1 v0.0.1 ([CWD]/d1)
1944 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1945 [RUNNING] [..] (target/debug/deps/d1[..][EXE])
1946 [DOCTEST] d1",
1947         )
1948         .with_stdout_contains_n("running 0 tests", 2)
1949         .run();
1950 }
1951 
1952 #[cargo_test]
example_bin_same_name()1953 fn example_bin_same_name() {
1954     let p = project()
1955         .file("src/bin/foo.rs", r#"fn main() { println!("bin"); }"#)
1956         .file("examples/foo.rs", r#"fn main() { println!("example"); }"#)
1957         .build();
1958 
1959     p.cargo("test --no-run -v")
1960         .with_stderr(
1961             "\
1962 [COMPILING] foo v0.0.1 ([CWD])
1963 [RUNNING] `rustc [..]`
1964 [RUNNING] `rustc [..]`
1965 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
1966 ",
1967         )
1968         .run();
1969 
1970     assert!(!p.bin("foo").is_file());
1971     assert!(p.bin("examples/foo").is_file());
1972 
1973     p.process(&p.bin("examples/foo"))
1974         .with_stdout("example\n")
1975         .run();
1976 
1977     p.cargo("run")
1978         .with_stderr(
1979             "\
1980 [COMPILING] foo v0.0.1 ([..])
1981 [FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1982 [RUNNING] [..]",
1983         )
1984         .with_stdout("bin")
1985         .run();
1986     assert!(p.bin("foo").is_file());
1987 }
1988 
1989 #[cargo_test]
test_with_example_twice()1990 fn test_with_example_twice() {
1991     let p = project()
1992         .file("src/bin/foo.rs", r#"fn main() { println!("bin"); }"#)
1993         .file("examples/foo.rs", r#"fn main() { println!("example"); }"#)
1994         .build();
1995 
1996     println!("first");
1997     p.cargo("test -v").run();
1998     assert!(p.bin("examples/foo").is_file());
1999     println!("second");
2000     p.cargo("test -v").run();
2001     assert!(p.bin("examples/foo").is_file());
2002 }
2003 
2004 #[cargo_test]
example_with_dev_dep()2005 fn example_with_dev_dep() {
2006     let p = project()
2007         .file(
2008             "Cargo.toml",
2009             r#"
2010                 [package]
2011                 name = "foo"
2012                 version = "0.0.1"
2013                 authors = []
2014 
2015                 [lib]
2016                 name = "foo"
2017                 test = false
2018                 doctest = false
2019 
2020                 [dev-dependencies.a]
2021                 path = "a"
2022             "#,
2023         )
2024         .file("src/lib.rs", "")
2025         .file(
2026             "examples/ex.rs",
2027             "#[allow(unused_extern_crates)] extern crate a; fn main() {}",
2028         )
2029         .file("a/Cargo.toml", &basic_manifest("a", "0.0.1"))
2030         .file("a/src/lib.rs", "")
2031         .build();
2032 
2033     p.cargo("test -v")
2034         .with_stderr(
2035             "\
2036 [..]
2037 [..]
2038 [..]
2039 [..]
2040 [RUNNING] `rustc --crate-name ex [..] --extern a=[..]`
2041 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2042 ",
2043         )
2044         .run();
2045 }
2046 
2047 #[cargo_test]
bin_is_preserved()2048 fn bin_is_preserved() {
2049     let p = project()
2050         .file("src/lib.rs", "")
2051         .file("src/main.rs", "fn main() {}")
2052         .build();
2053 
2054     p.cargo("build -v").run();
2055     assert!(p.bin("foo").is_file());
2056 
2057     println!("test");
2058     p.cargo("test -v").run();
2059     assert!(p.bin("foo").is_file());
2060 }
2061 
2062 #[cargo_test]
bad_example()2063 fn bad_example() {
2064     let p = project().file("src/lib.rs", "");
2065     let p = p.build();
2066 
2067     p.cargo("run --example foo")
2068         .with_status(101)
2069         .with_stderr("[ERROR] no example target named `foo`")
2070         .run();
2071     p.cargo("run --bin foo")
2072         .with_status(101)
2073         .with_stderr("[ERROR] no bin target named `foo`")
2074         .run();
2075 }
2076 
2077 #[cargo_test]
doctest_feature()2078 fn doctest_feature() {
2079     let p = project()
2080         .file(
2081             "Cargo.toml",
2082             r#"
2083                 [package]
2084                 name = "foo"
2085                 version = "0.0.1"
2086                 authors = []
2087                 [features]
2088                 bar = []
2089             "#,
2090         )
2091         .file(
2092             "src/lib.rs",
2093             r#"
2094                 /// ```rust
2095                 /// assert_eq!(foo::foo(), 1);
2096                 /// ```
2097                 #[cfg(feature = "bar")]
2098                 pub fn foo() -> i32 { 1 }
2099             "#,
2100         )
2101         .build();
2102 
2103     p.cargo("test --features bar")
2104         .with_stderr(
2105             "\
2106 [COMPILING] foo [..]
2107 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2108 [RUNNING] [..] (target/debug/deps/foo[..][EXE])
2109 [DOCTEST] foo",
2110         )
2111         .with_stdout_contains("running 0 tests")
2112         .with_stdout_contains("test [..] ... ok")
2113         .run();
2114 }
2115 
2116 #[cargo_test]
dashes_to_underscores()2117 fn dashes_to_underscores() {
2118     let p = project()
2119         .file("Cargo.toml", &basic_manifest("foo-bar", "0.0.1"))
2120         .file(
2121             "src/lib.rs",
2122             r#"
2123                 /// ```
2124                 /// assert_eq!(foo_bar::foo(), 1);
2125                 /// ```
2126                 pub fn foo() -> i32 { 1 }
2127             "#,
2128         )
2129         .build();
2130 
2131     p.cargo("test -v").run();
2132 }
2133 
2134 #[cargo_test]
doctest_dev_dep()2135 fn doctest_dev_dep() {
2136     let p = project()
2137         .file(
2138             "Cargo.toml",
2139             r#"
2140                 [package]
2141                 name = "foo"
2142                 version = "0.0.1"
2143                 authors = []
2144 
2145                 [dev-dependencies]
2146                 b = { path = "b" }
2147             "#,
2148         )
2149         .file(
2150             "src/lib.rs",
2151             r#"
2152                 /// ```
2153                 /// extern crate b;
2154                 /// ```
2155                 pub fn foo() {}
2156             "#,
2157         )
2158         .file("b/Cargo.toml", &basic_manifest("b", "0.0.1"))
2159         .file("b/src/lib.rs", "")
2160         .build();
2161 
2162     p.cargo("test -v").run();
2163 }
2164 
2165 #[cargo_test]
filter_no_doc_tests()2166 fn filter_no_doc_tests() {
2167     let p = project()
2168         .file(
2169             "src/lib.rs",
2170             r#"
2171                 /// ```
2172                 /// extern crate b;
2173                 /// ```
2174                 pub fn foo() {}
2175             "#,
2176         )
2177         .file("tests/foo.rs", "")
2178         .build();
2179 
2180     p.cargo("test --test=foo")
2181         .with_stderr(
2182             "\
2183 [COMPILING] foo v0.0.1 ([..])
2184 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2185 [RUNNING] [..] (target/debug/deps/foo[..][EXE])",
2186         )
2187         .with_stdout_contains("running 0 tests")
2188         .run();
2189 }
2190 
2191 #[cargo_test]
dylib_doctest()2192 fn dylib_doctest() {
2193     let p = project()
2194         .file(
2195             "Cargo.toml",
2196             r#"
2197                 [package]
2198                 name = "foo"
2199                 version = "0.0.1"
2200                 authors = []
2201 
2202                 [lib]
2203                 name = "foo"
2204                 crate-type = ["rlib", "dylib"]
2205                 test = false
2206             "#,
2207         )
2208         .file(
2209             "src/lib.rs",
2210             r#"
2211                 /// ```
2212                 /// foo::foo();
2213                 /// ```
2214                 pub fn foo() {}
2215             "#,
2216         )
2217         .build();
2218 
2219     p.cargo("test")
2220         .with_stderr(
2221             "\
2222 [COMPILING] foo v0.0.1 ([..])
2223 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2224 [DOCTEST] foo",
2225         )
2226         .with_stdout_contains("test [..] ... ok")
2227         .run();
2228 }
2229 
2230 #[cargo_test]
dylib_doctest2()2231 fn dylib_doctest2() {
2232     // Can't doc-test dylibs, as they're statically linked together.
2233     let p = project()
2234         .file(
2235             "Cargo.toml",
2236             r#"
2237                 [package]
2238                 name = "foo"
2239                 version = "0.0.1"
2240                 authors = []
2241 
2242                 [lib]
2243                 name = "foo"
2244                 crate-type = ["dylib"]
2245                 test = false
2246             "#,
2247         )
2248         .file(
2249             "src/lib.rs",
2250             r#"
2251                 /// ```
2252                 /// foo::foo();
2253                 /// ```
2254                 pub fn foo() {}
2255             "#,
2256         )
2257         .build();
2258 
2259     p.cargo("test").with_stdout("").run();
2260 }
2261 
2262 #[cargo_test]
cyclic_dev_dep_doc_test()2263 fn cyclic_dev_dep_doc_test() {
2264     let p = project()
2265         .file(
2266             "Cargo.toml",
2267             r#"
2268                 [package]
2269                 name = "foo"
2270                 version = "0.0.1"
2271                 authors = []
2272 
2273                 [dev-dependencies]
2274                 bar = { path = "bar" }
2275             "#,
2276         )
2277         .file(
2278             "src/lib.rs",
2279             r#"
2280                 //! ```
2281                 //! extern crate bar;
2282                 //! ```
2283             "#,
2284         )
2285         .file(
2286             "bar/Cargo.toml",
2287             r#"
2288                 [package]
2289                 name = "bar"
2290                 version = "0.0.1"
2291                 authors = []
2292 
2293                 [dependencies]
2294                 foo = { path = ".." }
2295             "#,
2296         )
2297         .file(
2298             "bar/src/lib.rs",
2299             r#"
2300                 #[allow(unused_extern_crates)]
2301                 extern crate foo;
2302             "#,
2303         )
2304         .build();
2305     p.cargo("test")
2306         .with_stderr(
2307             "\
2308 [COMPILING] foo v0.0.1 ([..])
2309 [COMPILING] bar v0.0.1 ([..])
2310 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2311 [RUNNING] [..] (target/debug/deps/foo[..][EXE])
2312 [DOCTEST] foo",
2313         )
2314         .with_stdout_contains("running 0 tests")
2315         .with_stdout_contains("test [..] ... ok")
2316         .run();
2317 }
2318 
2319 #[cargo_test]
dev_dep_with_build_script()2320 fn dev_dep_with_build_script() {
2321     let p = project()
2322         .file(
2323             "Cargo.toml",
2324             r#"
2325                 [package]
2326                 name = "foo"
2327                 version = "0.0.1"
2328                 authors = []
2329 
2330                 [dev-dependencies]
2331                 bar = { path = "bar" }
2332             "#,
2333         )
2334         .file("src/lib.rs", "")
2335         .file("examples/foo.rs", "fn main() {}")
2336         .file(
2337             "bar/Cargo.toml",
2338             r#"
2339                 [package]
2340                 name = "bar"
2341                 version = "0.0.1"
2342                 authors = []
2343                 build = "build.rs"
2344             "#,
2345         )
2346         .file("bar/src/lib.rs", "")
2347         .file("bar/build.rs", "fn main() {}")
2348         .build();
2349     p.cargo("test").run();
2350 }
2351 
2352 #[cargo_test]
no_fail_fast()2353 fn no_fail_fast() {
2354     let p = project()
2355         .file(
2356             "src/lib.rs",
2357             r#"
2358             pub fn add_one(x: i32) -> i32{
2359                 x + 1
2360             }
2361 
2362             /// ```rust
2363             /// use foo::sub_one;
2364             /// assert_eq!(sub_one(101), 100);
2365             /// ```
2366             pub fn sub_one(x: i32) -> i32{
2367                 x - 1
2368             }
2369             "#,
2370         )
2371         .file(
2372             "tests/test_add_one.rs",
2373             r#"
2374             extern crate foo;
2375             use foo::*;
2376 
2377             #[test]
2378             fn add_one_test() {
2379                 assert_eq!(add_one(1), 2);
2380             }
2381 
2382             #[test]
2383             fn fail_add_one_test() {
2384                 assert_eq!(add_one(1), 1);
2385             }
2386             "#,
2387         )
2388         .file(
2389             "tests/test_sub_one.rs",
2390             r#"
2391             extern crate foo;
2392             use foo::*;
2393 
2394             #[test]
2395             fn sub_one_test() {
2396                 assert_eq!(sub_one(1), 0);
2397             }
2398             "#,
2399         )
2400         .build();
2401     p.cargo("test --no-fail-fast")
2402         .with_status(101)
2403         .with_stderr_contains(
2404             "\
2405 [COMPILING] foo v0.0.1 ([..])
2406 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2407 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])
2408 [RUNNING] [..] (target/debug/deps/test_add_one-[..][EXE])",
2409         )
2410         .with_stdout_contains("running 0 tests")
2411         .with_stderr_contains(
2412             "\
2413 [RUNNING] [..] (target/debug/deps/test_sub_one-[..][EXE])
2414 [DOCTEST] foo",
2415         )
2416         .with_stdout_contains("test result: FAILED. [..]")
2417         .with_stdout_contains("test sub_one_test ... ok")
2418         .with_stdout_contains_n("test [..] ... ok", 3)
2419         .run();
2420 }
2421 
2422 #[cargo_test]
test_multiple_packages()2423 fn test_multiple_packages() {
2424     let p = project()
2425         .file(
2426             "Cargo.toml",
2427             r#"
2428                 [package]
2429                 name = "foo"
2430                 version = "0.0.1"
2431                 authors = []
2432 
2433                 [dependencies.d1]
2434                     path = "d1"
2435                 [dependencies.d2]
2436                     path = "d2"
2437 
2438                 [lib]
2439                     name = "foo"
2440                     doctest = false
2441             "#,
2442         )
2443         .file("src/lib.rs", "")
2444         .file(
2445             "d1/Cargo.toml",
2446             r#"
2447                 [package]
2448                 name = "d1"
2449                 version = "0.0.1"
2450                 authors = []
2451 
2452                 [lib]
2453                     name = "d1"
2454                     doctest = false
2455             "#,
2456         )
2457         .file("d1/src/lib.rs", "")
2458         .file(
2459             "d2/Cargo.toml",
2460             r#"
2461                 [package]
2462                 name = "d2"
2463                 version = "0.0.1"
2464                 authors = []
2465 
2466                 [lib]
2467                     name = "d2"
2468                     doctest = false
2469             "#,
2470         )
2471         .file("d2/src/lib.rs", "");
2472     let p = p.build();
2473 
2474     p.cargo("test -p d1 -p d2")
2475         .with_stderr_contains("[RUNNING] [..] (target/debug/deps/d1-[..][EXE])")
2476         .with_stderr_contains("[RUNNING] [..] (target/debug/deps/d2-[..][EXE])")
2477         .with_stdout_contains_n("running 0 tests", 2)
2478         .run();
2479 }
2480 
2481 #[cargo_test]
bin_does_not_rebuild_tests()2482 fn bin_does_not_rebuild_tests() {
2483     let p = project()
2484         .file("src/lib.rs", "")
2485         .file("src/main.rs", "fn main() {}")
2486         .file("tests/foo.rs", "");
2487     let p = p.build();
2488 
2489     p.cargo("test -v").run();
2490 
2491     sleep_ms(1000);
2492     fs::write(p.root().join("src/main.rs"), "fn main() { 3; }").unwrap();
2493 
2494     p.cargo("test -v --no-run")
2495         .with_stderr(
2496             "\
2497 [COMPILING] foo v0.0.1 ([..])
2498 [RUNNING] `rustc [..] src/main.rs [..]`
2499 [RUNNING] `rustc [..] src/main.rs [..]`
2500 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2501 ",
2502         )
2503         .run();
2504 }
2505 
2506 #[cargo_test]
selective_test_wonky_profile()2507 fn selective_test_wonky_profile() {
2508     let p = project()
2509         .file(
2510             "Cargo.toml",
2511             r#"
2512                 [package]
2513                 name = "foo"
2514                 version = "0.0.1"
2515                 authors = []
2516 
2517                 [profile.release]
2518                 opt-level = 2
2519 
2520                 [dependencies]
2521                 a = { path = "a" }
2522             "#,
2523         )
2524         .file("src/lib.rs", "")
2525         .file("a/Cargo.toml", &basic_manifest("a", "0.0.1"))
2526         .file("a/src/lib.rs", "");
2527     let p = p.build();
2528 
2529     p.cargo("test -v --no-run --release -p foo -p a").run();
2530 }
2531 
2532 #[cargo_test]
selective_test_optional_dep()2533 fn selective_test_optional_dep() {
2534     let p = project()
2535         .file(
2536             "Cargo.toml",
2537             r#"
2538                 [package]
2539                 name = "foo"
2540                 version = "0.0.1"
2541                 authors = []
2542 
2543                 [dependencies]
2544                 a = { path = "a", optional = true }
2545             "#,
2546         )
2547         .file("src/lib.rs", "")
2548         .file("a/Cargo.toml", &basic_manifest("a", "0.0.1"))
2549         .file("a/src/lib.rs", "");
2550     let p = p.build();
2551 
2552     p.cargo("test -v --no-run --features a -p a")
2553         .with_stderr(
2554             "\
2555 [COMPILING] a v0.0.1 ([..])
2556 [RUNNING] `rustc [..] a/src/lib.rs [..]`
2557 [RUNNING] `rustc [..] a/src/lib.rs [..]`
2558 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2559 ",
2560         )
2561         .run();
2562 }
2563 
2564 #[cargo_test]
only_test_docs()2565 fn only_test_docs() {
2566     let p = project()
2567         .file(
2568             "src/lib.rs",
2569             r#"
2570                 #[test]
2571                 fn foo() {
2572                     let a: u32 = "hello";
2573                 }
2574 
2575                 /// ```
2576                 /// foo::bar();
2577                 /// println!("ok");
2578                 /// ```
2579                 pub fn bar() {
2580                 }
2581             "#,
2582         )
2583         .file("tests/foo.rs", "this is not rust");
2584     let p = p.build();
2585 
2586     p.cargo("test --doc")
2587         .with_stderr(
2588             "\
2589 [COMPILING] foo v0.0.1 ([..])
2590 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2591 [DOCTEST] foo",
2592         )
2593         .with_stdout_contains("test [..] ... ok")
2594         .run();
2595 }
2596 
2597 #[cargo_test]
test_panic_abort_with_dep()2598 fn test_panic_abort_with_dep() {
2599     let p = project()
2600         .file(
2601             "Cargo.toml",
2602             r#"
2603                 [package]
2604                 name = "foo"
2605                 version = "0.0.1"
2606                 authors = []
2607 
2608                 [dependencies]
2609                 bar = { path = "bar" }
2610 
2611                 [profile.dev]
2612                 panic = 'abort'
2613             "#,
2614         )
2615         .file(
2616             "src/lib.rs",
2617             r#"
2618                 extern crate bar;
2619 
2620                 #[test]
2621                 fn foo() {}
2622             "#,
2623         )
2624         .file("bar/Cargo.toml", &basic_manifest("bar", "0.0.1"))
2625         .file("bar/src/lib.rs", "")
2626         .build();
2627     p.cargo("test -v").run();
2628 }
2629 
2630 #[cargo_test]
cfg_test_even_with_no_harness()2631 fn cfg_test_even_with_no_harness() {
2632     let p = project()
2633         .file(
2634             "Cargo.toml",
2635             r#"
2636                 [package]
2637                 name = "foo"
2638                 version = "0.0.1"
2639                 authors = []
2640 
2641                 [lib]
2642                 harness = false
2643                 doctest = false
2644             "#,
2645         )
2646         .file(
2647             "src/lib.rs",
2648             r#"#[cfg(test)] fn main() { println!("hello!"); }"#,
2649         )
2650         .build();
2651     p.cargo("test -v")
2652         .with_stdout("hello!\n")
2653         .with_stderr(
2654             "\
2655 [COMPILING] foo v0.0.1 ([..])
2656 [RUNNING] `rustc [..]`
2657 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
2658 [RUNNING] `[..]`
2659 ",
2660         )
2661         .run();
2662 }
2663 
2664 #[cargo_test]
panic_abort_multiple()2665 fn panic_abort_multiple() {
2666     let p = project()
2667         .file(
2668             "Cargo.toml",
2669             r#"
2670                 [package]
2671                 name = "foo"
2672                 version = "0.0.1"
2673                 authors = []
2674 
2675                 [dependencies]
2676                 a = { path = "a" }
2677 
2678                 [profile.release]
2679                 panic = 'abort'
2680             "#,
2681         )
2682         .file(
2683             "src/lib.rs",
2684             "#[allow(unused_extern_crates)] extern crate a;",
2685         )
2686         .file("a/Cargo.toml", &basic_manifest("a", "0.0.1"))
2687         .file("a/src/lib.rs", "")
2688         .build();
2689     p.cargo("test --release -v -p foo -p a").run();
2690 }
2691 
2692 #[cargo_test]
pass_correct_cfgs_flags_to_rustdoc()2693 fn pass_correct_cfgs_flags_to_rustdoc() {
2694     let p = project()
2695         .file(
2696             "Cargo.toml",
2697             r#"
2698                 [package]
2699                 name = "foo"
2700                 version = "0.1.0"
2701                 authors = []
2702 
2703                 [features]
2704                 default = ["feature_a/default"]
2705                 nightly = ["feature_a/nightly"]
2706 
2707                 [dependencies.feature_a]
2708                 path = "libs/feature_a"
2709                 default-features = false
2710             "#,
2711         )
2712         .file(
2713             "src/lib.rs",
2714             r#"
2715                 #[cfg(test)]
2716                 mod tests {
2717                     #[test]
2718                     fn it_works() {
2719                       assert!(true);
2720                     }
2721                 }
2722             "#,
2723         )
2724         .file(
2725             "libs/feature_a/Cargo.toml",
2726             r#"
2727                 [package]
2728                 name = "feature_a"
2729                 version = "0.1.0"
2730                 authors = []
2731 
2732                 [features]
2733                 default = ["mock_serde_codegen"]
2734                 nightly = ["mock_serde_derive"]
2735 
2736                 [dependencies]
2737                 mock_serde_derive = { path = "../mock_serde_derive", optional = true }
2738 
2739                 [build-dependencies]
2740                 mock_serde_codegen = { path = "../mock_serde_codegen", optional = true }
2741             "#,
2742         )
2743         .file(
2744             "libs/feature_a/src/lib.rs",
2745             r#"
2746                 #[cfg(feature = "mock_serde_derive")]
2747                 const MSG: &'static str = "This is safe";
2748 
2749                 #[cfg(feature = "mock_serde_codegen")]
2750                 const MSG: &'static str = "This is risky";
2751 
2752                 pub fn get() -> &'static str {
2753                     MSG
2754                 }
2755             "#,
2756         )
2757         .file(
2758             "libs/mock_serde_derive/Cargo.toml",
2759             &basic_manifest("mock_serde_derive", "0.1.0"),
2760         )
2761         .file("libs/mock_serde_derive/src/lib.rs", "")
2762         .file(
2763             "libs/mock_serde_codegen/Cargo.toml",
2764             &basic_manifest("mock_serde_codegen", "0.1.0"),
2765         )
2766         .file("libs/mock_serde_codegen/src/lib.rs", "");
2767     let p = p.build();
2768 
2769     p.cargo("test --package feature_a --verbose")
2770         .with_stderr_contains(
2771             "\
2772 [DOCTEST] feature_a
2773 [RUNNING] `rustdoc [..]--test [..]mock_serde_codegen[..]`",
2774         )
2775         .run();
2776 
2777     p.cargo("test --verbose")
2778         .with_stderr_contains(
2779             "\
2780 [DOCTEST] foo
2781 [RUNNING] `rustdoc [..]--test [..]feature_a[..]`",
2782         )
2783         .run();
2784 }
2785 
2786 #[cargo_test]
test_release_ignore_panic()2787 fn test_release_ignore_panic() {
2788     let p = project()
2789         .file(
2790             "Cargo.toml",
2791             r#"
2792                 [package]
2793                 name = "foo"
2794                 version = "0.0.1"
2795                 authors = []
2796 
2797                 [dependencies]
2798                 a = { path = "a" }
2799 
2800                 [profile.test]
2801                 panic = 'abort'
2802                 [profile.release]
2803                 panic = 'abort'
2804             "#,
2805         )
2806         .file(
2807             "src/lib.rs",
2808             "#[allow(unused_extern_crates)] extern crate a;",
2809         )
2810         .file("a/Cargo.toml", &basic_manifest("a", "0.0.1"))
2811         .file("a/src/lib.rs", "");
2812     let p = p.build();
2813     println!("test");
2814     p.cargo("test -v").run();
2815     println!("bench");
2816     p.cargo("bench -v").run();
2817 }
2818 
2819 #[cargo_test]
test_many_with_features()2820 fn test_many_with_features() {
2821     let p = project()
2822         .file(
2823             "Cargo.toml",
2824             r#"
2825                 [package]
2826                 name = "foo"
2827                 version = "0.0.1"
2828                 authors = []
2829 
2830                 [dependencies]
2831                 a = { path = "a" }
2832 
2833                 [features]
2834                 foo = []
2835 
2836                 [workspace]
2837             "#,
2838         )
2839         .file("src/lib.rs", "")
2840         .file("a/Cargo.toml", &basic_manifest("a", "0.0.1"))
2841         .file("a/src/lib.rs", "")
2842         .build();
2843 
2844     p.cargo("test -v -p a -p foo --features foo").run();
2845 }
2846 
2847 #[cargo_test]
test_all_workspace()2848 fn test_all_workspace() {
2849     let p = project()
2850         .file(
2851             "Cargo.toml",
2852             r#"
2853                 [project]
2854                 name = "foo"
2855                 version = "0.1.0"
2856 
2857                 [dependencies]
2858                 bar = { path = "bar" }
2859 
2860                 [workspace]
2861             "#,
2862         )
2863         .file("src/main.rs", "#[test] fn foo_test() {}")
2864         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
2865         .file("bar/src/lib.rs", "#[test] fn bar_test() {}")
2866         .build();
2867 
2868     p.cargo("test --workspace")
2869         .with_stdout_contains("test foo_test ... ok")
2870         .with_stdout_contains("test bar_test ... ok")
2871         .run();
2872 }
2873 
2874 #[cargo_test]
test_all_exclude()2875 fn test_all_exclude() {
2876     let p = project()
2877         .file(
2878             "Cargo.toml",
2879             r#"
2880                 [project]
2881                 name = "foo"
2882                 version = "0.1.0"
2883 
2884                 [workspace]
2885                 members = ["bar", "baz"]
2886             "#,
2887         )
2888         .file("src/main.rs", "fn main() {}")
2889         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
2890         .file("bar/src/lib.rs", "#[test] pub fn bar() {}")
2891         .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
2892         .file("baz/src/lib.rs", "#[test] pub fn baz() { assert!(false); }")
2893         .build();
2894 
2895     p.cargo("test --workspace --exclude baz")
2896         .with_stdout_contains(
2897             "running 1 test
2898 test bar ... ok",
2899         )
2900         .run();
2901 }
2902 
2903 #[cargo_test]
test_all_exclude_not_found()2904 fn test_all_exclude_not_found() {
2905     let p = project()
2906         .file(
2907             "Cargo.toml",
2908             r#"
2909                 [project]
2910                 name = "foo"
2911                 version = "0.1.0"
2912 
2913                 [workspace]
2914                 members = ["bar"]
2915             "#,
2916         )
2917         .file("src/main.rs", "fn main() {}")
2918         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
2919         .file("bar/src/lib.rs", "#[test] pub fn bar() {}")
2920         .build();
2921 
2922     p.cargo("test --workspace --exclude baz")
2923         .with_stderr_contains("[WARNING] excluded package(s) `baz` not found in workspace [..]")
2924         .with_stdout_contains(
2925             "running 1 test
2926 test bar ... ok",
2927         )
2928         .run();
2929 }
2930 
2931 #[cargo_test]
test_all_exclude_glob()2932 fn test_all_exclude_glob() {
2933     let p = project()
2934         .file(
2935             "Cargo.toml",
2936             r#"
2937                 [project]
2938                 name = "foo"
2939                 version = "0.1.0"
2940 
2941                 [workspace]
2942                 members = ["bar", "baz"]
2943             "#,
2944         )
2945         .file("src/main.rs", "fn main() {}")
2946         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
2947         .file("bar/src/lib.rs", "#[test] pub fn bar() {}")
2948         .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
2949         .file("baz/src/lib.rs", "#[test] pub fn baz() { assert!(false); }")
2950         .build();
2951 
2952     p.cargo("test --workspace --exclude '*z'")
2953         .with_stdout_contains(
2954             "running 1 test
2955 test bar ... ok",
2956         )
2957         .run();
2958 }
2959 
2960 #[cargo_test]
test_all_exclude_glob_not_found()2961 fn test_all_exclude_glob_not_found() {
2962     let p = project()
2963         .file(
2964             "Cargo.toml",
2965             r#"
2966                 [project]
2967                 name = "foo"
2968                 version = "0.1.0"
2969 
2970                 [workspace]
2971                 members = ["bar"]
2972             "#,
2973         )
2974         .file("src/main.rs", "fn main() {}")
2975         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
2976         .file("bar/src/lib.rs", "#[test] pub fn bar() {}")
2977         .build();
2978 
2979     p.cargo("test --workspace --exclude '*z'")
2980         .with_stderr_contains(
2981             "[WARNING] excluded package pattern(s) `*z` not found in workspace [..]",
2982         )
2983         .with_stdout_contains(
2984             "running 1 test
2985 test bar ... ok",
2986         )
2987         .run();
2988 }
2989 
2990 #[cargo_test]
test_all_exclude_broken_glob()2991 fn test_all_exclude_broken_glob() {
2992     let p = project().file("src/main.rs", "fn main() {}").build();
2993 
2994     p.cargo("test --workspace --exclude '[*z'")
2995         .with_status(101)
2996         .with_stderr_contains("[ERROR] cannot build glob pattern from `[*z`")
2997         .run();
2998 }
2999 
3000 #[cargo_test]
test_all_virtual_manifest()3001 fn test_all_virtual_manifest() {
3002     let p = project()
3003         .file(
3004             "Cargo.toml",
3005             r#"
3006                 [workspace]
3007                 members = ["a", "b"]
3008             "#,
3009         )
3010         .file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
3011         .file("a/src/lib.rs", "#[test] fn a() {}")
3012         .file("b/Cargo.toml", &basic_manifest("b", "0.1.0"))
3013         .file("b/src/lib.rs", "#[test] fn b() {}")
3014         .build();
3015 
3016     p.cargo("test --workspace")
3017         .with_stdout_contains("running 1 test\ntest a ... ok")
3018         .with_stdout_contains("running 1 test\ntest b ... ok")
3019         .run();
3020 }
3021 
3022 #[cargo_test]
test_virtual_manifest_all_implied()3023 fn test_virtual_manifest_all_implied() {
3024     let p = project()
3025         .file(
3026             "Cargo.toml",
3027             r#"
3028                 [workspace]
3029                 members = ["a", "b"]
3030             "#,
3031         )
3032         .file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
3033         .file("a/src/lib.rs", "#[test] fn a() {}")
3034         .file("b/Cargo.toml", &basic_manifest("b", "0.1.0"))
3035         .file("b/src/lib.rs", "#[test] fn b() {}")
3036         .build();
3037 
3038     p.cargo("test")
3039         .with_stdout_contains("running 1 test\ntest a ... ok")
3040         .with_stdout_contains("running 1 test\ntest b ... ok")
3041         .run();
3042 }
3043 
3044 #[cargo_test]
test_virtual_manifest_one_project()3045 fn test_virtual_manifest_one_project() {
3046     let p = project()
3047         .file(
3048             "Cargo.toml",
3049             r#"
3050                 [workspace]
3051                 members = ["bar", "baz"]
3052             "#,
3053         )
3054         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
3055         .file("bar/src/lib.rs", "#[test] fn bar() {}")
3056         .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
3057         .file("baz/src/lib.rs", "#[test] fn baz() { assert!(false); }")
3058         .build();
3059 
3060     p.cargo("test -p bar")
3061         .with_stdout_contains("running 1 test\ntest bar ... ok")
3062         .with_stdout_does_not_contain("running 1 test\ntest baz ... ok")
3063         .run();
3064 }
3065 
3066 #[cargo_test]
test_virtual_manifest_glob()3067 fn test_virtual_manifest_glob() {
3068     let p = project()
3069         .file(
3070             "Cargo.toml",
3071             r#"
3072                 [workspace]
3073                 members = ["bar", "baz"]
3074             "#,
3075         )
3076         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
3077         .file("bar/src/lib.rs", "#[test] fn bar() { assert!(false); }")
3078         .file("baz/Cargo.toml", &basic_manifest("baz", "0.1.0"))
3079         .file("baz/src/lib.rs", "#[test] fn baz() {}")
3080         .build();
3081 
3082     p.cargo("test -p '*z'")
3083         .with_stdout_does_not_contain("running 1 test\ntest bar ... ok")
3084         .with_stdout_contains("running 1 test\ntest baz ... ok")
3085         .run();
3086 }
3087 
3088 #[cargo_test]
test_virtual_manifest_glob_not_found()3089 fn test_virtual_manifest_glob_not_found() {
3090     let p = project()
3091         .file(
3092             "Cargo.toml",
3093             r#"
3094                 [workspace]
3095                 members = ["bar"]
3096             "#,
3097         )
3098         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
3099         .file("bar/src/lib.rs", "#[test] fn bar() {}")
3100         .build();
3101 
3102     p.cargo("test -p bar -p '*z'")
3103         .with_status(101)
3104         .with_stderr("[ERROR] package pattern(s) `*z` not found in workspace [..]")
3105         .run();
3106 }
3107 
3108 #[cargo_test]
test_virtual_manifest_broken_glob()3109 fn test_virtual_manifest_broken_glob() {
3110     let p = project()
3111         .file(
3112             "Cargo.toml",
3113             r#"
3114                 [workspace]
3115                 members = ["bar"]
3116             "#,
3117         )
3118         .file("bar/Cargo.toml", &basic_manifest("bar", "0.1.0"))
3119         .file("bar/src/lib.rs", "#[test] fn bar() {}")
3120         .build();
3121 
3122     p.cargo("test -p '[*z'")
3123         .with_status(101)
3124         .with_stderr_contains("[ERROR] cannot build glob pattern from `[*z`")
3125         .run();
3126 }
3127 
3128 #[cargo_test]
test_all_member_dependency_same_name()3129 fn test_all_member_dependency_same_name() {
3130     let p = project()
3131         .file(
3132             "Cargo.toml",
3133             r#"
3134                 [workspace]
3135                 members = ["a"]
3136             "#,
3137         )
3138         .file(
3139             "a/Cargo.toml",
3140             r#"
3141                 [project]
3142                 name = "a"
3143                 version = "0.1.0"
3144 
3145                 [dependencies]
3146                 a = "0.1.0"
3147             "#,
3148         )
3149         .file("a/src/lib.rs", "#[test] fn a() {}")
3150         .build();
3151 
3152     Package::new("a", "0.1.0").publish();
3153 
3154     p.cargo("test --workspace")
3155         .with_stdout_contains("test a ... ok")
3156         .run();
3157 }
3158 
3159 #[cargo_test]
doctest_only_with_dev_dep()3160 fn doctest_only_with_dev_dep() {
3161     let p = project()
3162         .file(
3163             "Cargo.toml",
3164             r#"
3165                 [project]
3166                 name = "a"
3167                 version = "0.1.0"
3168 
3169                 [dev-dependencies]
3170                 b = { path = "b" }
3171             "#,
3172         )
3173         .file(
3174             "src/lib.rs",
3175             r#"
3176                 /// ```
3177                 /// extern crate b;
3178                 ///
3179                 /// b::b();
3180                 /// ```
3181                 pub fn a() {}
3182             "#,
3183         )
3184         .file("b/Cargo.toml", &basic_manifest("b", "0.1.0"))
3185         .file("b/src/lib.rs", "pub fn b() {}")
3186         .build();
3187 
3188     p.cargo("test --doc -v").run();
3189 }
3190 
3191 #[cargo_test]
test_many_targets()3192 fn test_many_targets() {
3193     let p = project()
3194         .file(
3195             "src/bin/a.rs",
3196             r#"
3197                 fn main() {}
3198                 #[test] fn bin_a() {}
3199             "#,
3200         )
3201         .file(
3202             "src/bin/b.rs",
3203             r#"
3204                 fn main() {}
3205                 #[test] fn bin_b() {}
3206             "#,
3207         )
3208         .file(
3209             "src/bin/c.rs",
3210             r#"
3211                 fn main() {}
3212                 #[test] fn bin_c() { panic!(); }
3213             "#,
3214         )
3215         .file(
3216             "examples/a.rs",
3217             r#"
3218                 fn main() {}
3219                 #[test] fn example_a() {}
3220             "#,
3221         )
3222         .file(
3223             "examples/b.rs",
3224             r#"
3225                 fn main() {}
3226                 #[test] fn example_b() {}
3227             "#,
3228         )
3229         .file("examples/c.rs", "#[test] fn example_c() { panic!(); }")
3230         .file("tests/a.rs", "#[test] fn test_a() {}")
3231         .file("tests/b.rs", "#[test] fn test_b() {}")
3232         .file("tests/c.rs", "does not compile")
3233         .build();
3234 
3235     p.cargo("test --verbose --bin a --bin b --example a --example b --test a --test b")
3236         .with_stdout_contains("test bin_a ... ok")
3237         .with_stdout_contains("test bin_b ... ok")
3238         .with_stdout_contains("test test_a ... ok")
3239         .with_stdout_contains("test test_b ... ok")
3240         .with_stderr_contains("[RUNNING] `rustc --crate-name a examples/a.rs [..]`")
3241         .with_stderr_contains("[RUNNING] `rustc --crate-name b examples/b.rs [..]`")
3242         .run();
3243 }
3244 
3245 #[cargo_test]
doctest_and_registry()3246 fn doctest_and_registry() {
3247     let p = project()
3248         .file(
3249             "Cargo.toml",
3250             r#"
3251                 [project]
3252                 name = "a"
3253                 version = "0.1.0"
3254 
3255                 [dependencies]
3256                 b = { path = "b" }
3257                 c = { path = "c" }
3258 
3259                 [workspace]
3260             "#,
3261         )
3262         .file("src/lib.rs", "")
3263         .file("b/Cargo.toml", &basic_manifest("b", "0.1.0"))
3264         .file(
3265             "b/src/lib.rs",
3266             "
3267             /// ```
3268             /// b::foo();
3269             /// ```
3270             pub fn foo() {}
3271         ",
3272         )
3273         .file(
3274             "c/Cargo.toml",
3275             r#"
3276                 [project]
3277                 name = "c"
3278                 version = "0.1.0"
3279 
3280                 [dependencies]
3281                 b = "0.1"
3282             "#,
3283         )
3284         .file("c/src/lib.rs", "")
3285         .build();
3286 
3287     Package::new("b", "0.1.0").publish();
3288 
3289     p.cargo("test --workspace -v").run();
3290 }
3291 
3292 #[cargo_test]
cargo_test_env()3293 fn cargo_test_env() {
3294     let src = format!(
3295         r#"
3296         #![crate_type = "rlib"]
3297 
3298         #[test]
3299         fn env_test() {{
3300             use std::env;
3301             eprintln!("{{}}", env::var("{}").unwrap());
3302         }}
3303         "#,
3304         cargo::CARGO_ENV
3305     );
3306 
3307     let p = project()
3308         .file("Cargo.toml", &basic_lib_manifest("foo"))
3309         .file("src/lib.rs", &src)
3310         .build();
3311 
3312     let cargo = cargo_exe().canonicalize().unwrap();
3313     p.cargo("test --lib -- --nocapture")
3314         .with_stderr_contains(cargo.to_str().unwrap())
3315         .with_stdout_contains("test env_test ... ok")
3316         .run();
3317 }
3318 
3319 #[cargo_test]
test_order()3320 fn test_order() {
3321     let p = project()
3322         .file("src/lib.rs", "#[test] fn test_lib() {}")
3323         .file("tests/a.rs", "#[test] fn test_a() {}")
3324         .file("tests/z.rs", "#[test] fn test_z() {}")
3325         .build();
3326 
3327     p.cargo("test --workspace")
3328         .with_stdout_contains(
3329             "
3330 running 1 test
3331 test test_lib ... ok
3332 
3333 test result: ok. [..]
3334 
3335 
3336 running 1 test
3337 test test_a ... ok
3338 
3339 test result: ok. [..]
3340 
3341 
3342 running 1 test
3343 test test_z ... ok
3344 
3345 test result: ok. [..]
3346 ",
3347         )
3348         .run();
3349 }
3350 
3351 #[cargo_test]
cyclic_dev()3352 fn cyclic_dev() {
3353     let p = project()
3354         .file(
3355             "Cargo.toml",
3356             r#"
3357                 [project]
3358                 name = "foo"
3359                 version = "0.1.0"
3360 
3361                 [dev-dependencies]
3362                 foo = { path = "." }
3363             "#,
3364         )
3365         .file("src/lib.rs", "#[test] fn test_lib() {}")
3366         .file("tests/foo.rs", "extern crate foo;")
3367         .build();
3368 
3369     p.cargo("test --workspace").run();
3370 }
3371 
3372 #[cargo_test]
publish_a_crate_without_tests()3373 fn publish_a_crate_without_tests() {
3374     Package::new("testless", "0.1.0")
3375         .file(
3376             "Cargo.toml",
3377             r#"
3378                 [project]
3379                 name = "testless"
3380                 version = "0.1.0"
3381                 exclude = ["tests/*"]
3382 
3383                 [[test]]
3384                 name = "a_test"
3385             "#,
3386         )
3387         .file("src/lib.rs", "")
3388         // In real life, the package will have a test,
3389         // which would be excluded from .crate file by the
3390         // `exclude` field. Our test harness does not honor
3391         // exclude though, so let's just not add the file!
3392         // .file("tests/a_test.rs", "")
3393         .publish();
3394 
3395     let p = project()
3396         .file(
3397             "Cargo.toml",
3398             r#"
3399                 [project]
3400                 name = "foo"
3401                 version = "0.1.0"
3402 
3403                 [dependencies]
3404                 testless = "0.1.0"
3405             "#,
3406         )
3407         .file("src/lib.rs", "")
3408         .build();
3409 
3410     p.cargo("test").run();
3411     p.cargo("test --package testless").run();
3412 }
3413 
3414 #[cargo_test]
find_dependency_of_proc_macro_dependency_with_target()3415 fn find_dependency_of_proc_macro_dependency_with_target() {
3416     let p = project()
3417         .file(
3418             "Cargo.toml",
3419             r#"
3420                 [workspace]
3421                 members = ["root", "proc_macro_dep"]
3422             "#,
3423         )
3424         .file(
3425             "root/Cargo.toml",
3426             r#"
3427                 [project]
3428                 name = "root"
3429                 version = "0.1.0"
3430                 authors = []
3431 
3432                 [dependencies]
3433                 proc_macro_dep = { path = "../proc_macro_dep" }
3434             "#,
3435         )
3436         .file(
3437             "root/src/lib.rs",
3438             r#"
3439                 #[macro_use]
3440                 extern crate proc_macro_dep;
3441 
3442                 #[derive(Noop)]
3443                 pub struct X;
3444             "#,
3445         )
3446         .file(
3447             "proc_macro_dep/Cargo.toml",
3448             r#"
3449                 [project]
3450                 name = "proc_macro_dep"
3451                 version = "0.1.0"
3452                 authors = []
3453 
3454                 [lib]
3455                 proc-macro = true
3456 
3457                 [dependencies]
3458                 baz = "^0.1"
3459             "#,
3460         )
3461         .file(
3462             "proc_macro_dep/src/lib.rs",
3463             r#"
3464                 extern crate baz;
3465                 extern crate proc_macro;
3466                 use proc_macro::TokenStream;
3467 
3468                 #[proc_macro_derive(Noop)]
3469                 pub fn noop(_input: TokenStream) -> TokenStream {
3470                     "".parse().unwrap()
3471                 }
3472             "#,
3473         )
3474         .build();
3475     Package::new("bar", "0.1.0").publish();
3476     Package::new("baz", "0.1.0")
3477         .dep("bar", "0.1")
3478         .file("src/lib.rs", "extern crate bar;")
3479         .publish();
3480     p.cargo("test --workspace --target").arg(rustc_host()).run();
3481 }
3482 
3483 #[cargo_test]
test_hint_not_masked_by_doctest()3484 fn test_hint_not_masked_by_doctest() {
3485     let p = project()
3486         .file(
3487             "src/lib.rs",
3488             r#"
3489                 /// ```
3490                 /// assert_eq!(1, 1);
3491                 /// ```
3492                 pub fn this_works() {}
3493             "#,
3494         )
3495         .file(
3496             "tests/integ.rs",
3497             r#"
3498                 #[test]
3499                 fn this_fails() {
3500                     panic!();
3501                 }
3502             "#,
3503         )
3504         .build();
3505     p.cargo("test --no-fail-fast")
3506         .with_status(101)
3507         .with_stdout_contains("test this_fails ... FAILED")
3508         .with_stdout_contains("[..]this_works (line [..]ok")
3509         .with_stderr_contains(
3510             "[ERROR] test failed, to rerun pass \
3511              '--test integ'",
3512         )
3513         .run();
3514 }
3515 
3516 #[cargo_test]
test_hint_workspace_virtual()3517 fn test_hint_workspace_virtual() {
3518     let p = project()
3519         .file(
3520             "Cargo.toml",
3521             r#"
3522                 [workspace]
3523                 members = ["a", "b"]
3524             "#,
3525         )
3526         .file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
3527         .file("a/src/lib.rs", "#[test] fn t1() {}")
3528         .file("b/Cargo.toml", &basic_manifest("b", "0.1.0"))
3529         .file("b/src/lib.rs", "#[test] fn t1() {assert!(false)}")
3530         .build();
3531 
3532     p.cargo("test")
3533         .with_stderr_contains("[ERROR] test failed, to rerun pass '-p b --lib'")
3534         .with_status(101)
3535         .run();
3536     p.cargo("test")
3537         .cwd("b")
3538         .with_stderr_contains("[ERROR] test failed, to rerun pass '--lib'")
3539         .with_status(101)
3540         .run();
3541 }
3542 
3543 #[cargo_test]
test_hint_workspace_nonvirtual()3544 fn test_hint_workspace_nonvirtual() {
3545     let p = project()
3546         .file(
3547             "Cargo.toml",
3548             r#"
3549             [package]
3550             name = "foo"
3551             version = "0.1.0"
3552 
3553             [workspace]
3554             members = ["a"]
3555             "#,
3556         )
3557         .file("src/lib.rs", "")
3558         .file("a/Cargo.toml", &basic_manifest("a", "0.1.0"))
3559         .file("a/src/lib.rs", "#[test] fn t1() {assert!(false)}")
3560         .build();
3561 
3562     p.cargo("test --workspace")
3563         .with_stderr_contains("[ERROR] test failed, to rerun pass '-p a --lib'")
3564         .with_status(101)
3565         .run();
3566     p.cargo("test -p a")
3567         .with_stderr_contains("[ERROR] test failed, to rerun pass '-p a --lib'")
3568         .with_status(101)
3569         .run();
3570 }
3571 
3572 #[cargo_test]
json_artifact_includes_test_flag()3573 fn json_artifact_includes_test_flag() {
3574     // Verify that the JSON artifact output includes `test` flag.
3575     let p = project()
3576         .file(
3577             "Cargo.toml",
3578             r#"
3579                 [package]
3580                 name = "foo"
3581                 version = "0.0.1"
3582                 authors = []
3583 
3584                 [profile.test]
3585                 opt-level = 1
3586             "#,
3587         )
3588         .file("src/lib.rs", "")
3589         .build();
3590 
3591     p.cargo("test --lib -v --message-format=json")
3592         .with_json(
3593             r#"
3594                 {
3595                     "reason":"compiler-artifact",
3596                     "profile": {
3597                         "debug_assertions": true,
3598                         "debuginfo": 2,
3599                         "opt_level": "1",
3600                         "overflow_checks": true,
3601                         "test": true
3602                     },
3603                     "executable": "[..]/foo-[..]",
3604                     "features": [],
3605                     "package_id":"foo 0.0.1 ([..])",
3606                     "manifest_path": "[..]",
3607                     "target":{
3608                         "kind":["lib"],
3609                         "crate_types":["lib"],
3610                         "doc": true,
3611                         "doctest": true,
3612                         "edition": "2015",
3613                         "name":"foo",
3614                         "src_path":"[..]lib.rs",
3615                         "test": true
3616                     },
3617                     "filenames":"{...}",
3618                     "fresh": false
3619                 }
3620 
3621                 {"reason": "build-finished", "success": true}
3622             "#,
3623         )
3624         .run();
3625 }
3626 
3627 #[cargo_test]
json_artifact_includes_executable_for_library_tests()3628 fn json_artifact_includes_executable_for_library_tests() {
3629     let p = project()
3630         .file("src/main.rs", "fn main() { }")
3631         .file("src/lib.rs", r#"#[test] fn lib_test() {}"#)
3632         .build();
3633 
3634     p.cargo("test --lib -v --no-run --message-format=json")
3635         .with_json(
3636             r#"
3637                 {
3638                     "executable": "[..]/foo/target/debug/deps/foo-[..][EXE]",
3639                     "features": [],
3640                     "filenames": "{...}",
3641                     "fresh": false,
3642                     "package_id": "foo 0.0.1 ([..])",
3643                     "manifest_path": "[..]",
3644                     "profile": "{...}",
3645                     "reason": "compiler-artifact",
3646                     "target": {
3647                         "crate_types": [ "lib" ],
3648                         "kind": [ "lib" ],
3649                         "doc": true,
3650                         "doctest": true,
3651                         "edition": "2015",
3652                         "name": "foo",
3653                         "src_path": "[..]/foo/src/lib.rs",
3654                         "test": true
3655                     }
3656                 }
3657 
3658                 {"reason": "build-finished", "success": true}
3659             "#,
3660         )
3661         .run();
3662 }
3663 
3664 #[cargo_test]
json_artifact_includes_executable_for_integration_tests()3665 fn json_artifact_includes_executable_for_integration_tests() {
3666     let p = project()
3667         .file(
3668             "tests/integration_test.rs",
3669             r#"#[test] fn integration_test() {}"#,
3670         )
3671         .build();
3672 
3673     p.cargo("test -v --no-run --message-format=json --test integration_test")
3674         .with_json(
3675             r#"
3676                 {
3677                     "executable": "[..]/foo/target/debug/deps/integration_test-[..][EXE]",
3678                     "features": [],
3679                     "filenames": "{...}",
3680                     "fresh": false,
3681                     "package_id": "foo 0.0.1 ([..])",
3682                     "manifest_path": "[..]",
3683                     "profile": "{...}",
3684                     "reason": "compiler-artifact",
3685                     "target": {
3686                         "crate_types": [ "bin" ],
3687                         "kind": [ "test" ],
3688                         "doc": false,
3689                         "doctest": false,
3690                         "edition": "2015",
3691                         "name": "integration_test",
3692                         "src_path": "[..]/foo/tests/integration_test.rs",
3693                         "test": true
3694                     }
3695                 }
3696 
3697                 {"reason": "build-finished", "success": true}
3698             "#,
3699         )
3700         .run();
3701 }
3702 
3703 #[cargo_test]
test_build_script_links()3704 fn test_build_script_links() {
3705     let p = project()
3706         .file(
3707             "Cargo.toml",
3708             r#"
3709                 [package]
3710                 name = "foo"
3711                 version = "0.0.1"
3712                 links = 'something'
3713 
3714                 [lib]
3715                 test = false
3716             "#,
3717         )
3718         .file("build.rs", "fn main() {}")
3719         .file("src/lib.rs", "")
3720         .build();
3721 
3722     p.cargo("test --no-run").run();
3723 }
3724 
3725 #[cargo_test]
doctest_skip_staticlib()3726 fn doctest_skip_staticlib() {
3727     let p = project()
3728         .file(
3729             "Cargo.toml",
3730             r#"
3731                 [package]
3732                 name = "foo"
3733                 version = "0.0.1"
3734 
3735                 [lib]
3736                 crate-type = ["staticlib"]
3737             "#,
3738         )
3739         .file(
3740             "src/lib.rs",
3741             r#"
3742             //! ```
3743             //! assert_eq!(1,2);
3744             //! ```
3745             "#,
3746         )
3747         .build();
3748 
3749     p.cargo("test --doc")
3750         .with_status(101)
3751         .with_stderr(
3752             "\
3753 [WARNING] doc tests are not supported for crate type(s) `staticlib` in package `foo`
3754 [ERROR] no library targets found in package `foo`",
3755         )
3756         .run();
3757 
3758     p.cargo("test")
3759         .with_stderr(
3760             "\
3761 [COMPILING] foo [..]
3762 [FINISHED] test [..]
3763 [RUNNING] [..] (target/debug/deps/foo-[..])",
3764         )
3765         .run();
3766 }
3767 
3768 #[cargo_test]
can_not_mix_doc_tests_and_regular_tests()3769 fn can_not_mix_doc_tests_and_regular_tests() {
3770     let p = project()
3771         .file(
3772             "src/lib.rs",
3773             "\
3774 /// ```
3775 /// assert_eq!(1, 1)
3776 /// ```
3777 pub fn foo() -> u8 { 1 }
3778 
3779 #[cfg(test)] mod tests {
3780     #[test] fn it_works() { assert_eq!(2 + 2, 4); }
3781 }
3782 ",
3783         )
3784         .build();
3785 
3786     p.cargo("test")
3787         .with_stderr(
3788             "\
3789 [COMPILING] foo v0.0.1 ([CWD])
3790 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
3791 [RUNNING] [..] (target/debug/deps/foo-[..])
3792 [DOCTEST] foo
3793 ",
3794         )
3795         .with_stdout(
3796             "
3797 running 1 test
3798 test tests::it_works ... ok
3799 
3800 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
3801 
3802 
3803 running 1 test
3804 test src/lib.rs - foo (line 1) ... ok
3805 
3806 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
3807 \n",
3808         )
3809         .run();
3810 
3811     p.cargo("test --lib")
3812         .with_stderr(
3813             "\
3814 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
3815 [RUNNING] [..] (target/debug/deps/foo-[..])\n",
3816         )
3817         .with_stdout(
3818             "
3819 running 1 test
3820 test tests::it_works ... ok
3821 
3822 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
3823 \n",
3824         )
3825         .run();
3826 
3827     // This has been modified to attempt to diagnose spurious errors on CI.
3828     // For some reason, this is recompiling the lib when it shouldn't. If the
3829     // root cause is ever found, the changes here should be reverted.
3830     // See https://github.com/rust-lang/cargo/issues/6887
3831     p.cargo("test --doc -vv")
3832         .with_stderr_does_not_contain("[COMPILING] foo [..]")
3833         .with_stderr_contains("[DOCTEST] foo")
3834         .with_stdout(
3835             "
3836 running 1 test
3837 test src/lib.rs - foo (line 1) ... ok
3838 
3839 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]
3840 
3841 ",
3842         )
3843         .env("CARGO_LOG", "cargo=trace")
3844         .run();
3845 
3846     p.cargo("test --lib --doc")
3847         .with_status(101)
3848         .with_stderr("[ERROR] Can't mix --doc with other target selecting options\n")
3849         .run();
3850 }
3851 
3852 #[cargo_test]
can_not_no_run_doc_tests()3853 fn can_not_no_run_doc_tests() {
3854     let p = project()
3855         .file(
3856             "src/lib.rs",
3857             r#"
3858             /// ```
3859             /// let _x = 1 + "foo";
3860             /// ```
3861             pub fn foo() -> u8 { 1 }
3862             "#,
3863         )
3864         .build();
3865 
3866     p.cargo("test --doc --no-run")
3867         .with_status(101)
3868         .with_stderr("[ERROR] Can't skip running doc tests with --no-run")
3869         .run();
3870 }
3871 
3872 #[cargo_test]
test_all_targets_lib()3873 fn test_all_targets_lib() {
3874     let p = project().file("src/lib.rs", "").build();
3875 
3876     p.cargo("test --all-targets")
3877         .with_stderr(
3878             "\
3879 [COMPILING] foo [..]
3880 [FINISHED] test [..]
3881 [RUNNING] [..]foo[..]
3882 ",
3883         )
3884         .run();
3885 }
3886 
3887 #[cargo_test]
test_dep_with_dev()3888 fn test_dep_with_dev() {
3889     Package::new("devdep", "0.1.0").publish();
3890     let p = project()
3891         .file(
3892             "Cargo.toml",
3893             r#"
3894                 [package]
3895                 name = "foo"
3896                 version = "0.0.1"
3897 
3898                 [dependencies]
3899                 bar = { path = "bar" }
3900             "#,
3901         )
3902         .file("src/lib.rs", "")
3903         .file(
3904             "bar/Cargo.toml",
3905             r#"
3906                 [package]
3907                 name = "bar"
3908                 version = "0.0.1"
3909 
3910                 [dev-dependencies]
3911                 devdep = "0.1"
3912             "#,
3913         )
3914         .file("bar/src/lib.rs", "")
3915         .build();
3916 
3917     p.cargo("test -p bar")
3918         .with_status(101)
3919         .with_stderr(
3920             "[ERROR] package `bar` cannot be tested because it requires dev-dependencies \
3921              and is not a member of the workspace",
3922         )
3923         .run();
3924 }
3925 
3926 #[cargo_test]
cargo_test_doctest_xcompile_ignores()3927 fn cargo_test_doctest_xcompile_ignores() {
3928     if !is_nightly() {
3929         // -Zdoctest-xcompile is unstable
3930         return;
3931     }
3932     // -Zdoctest-xcompile also enables --enable-per-target-ignores which
3933     // allows the ignore-TARGET syntax.
3934     let p = project()
3935         .file("Cargo.toml", &basic_lib_manifest("foo"))
3936         .file(
3937             "src/lib.rs",
3938             r#"
3939             ///```ignore-x86_64
3940             ///assert!(cfg!(not(target_arch = "x86_64")));
3941             ///```
3942             pub fn foo() -> u8 {
3943                 4
3944             }
3945             "#,
3946         )
3947         .build();
3948 
3949     p.cargo("build").run();
3950     #[cfg(not(target_arch = "x86_64"))]
3951     p.cargo("test")
3952         .with_stdout_contains(
3953             "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]",
3954         )
3955         .run();
3956     #[cfg(target_arch = "x86_64")]
3957     p.cargo("test")
3958         .with_status(101)
3959         .with_stdout_contains(
3960             "test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out[..]",
3961         )
3962         .run();
3963 
3964     #[cfg(not(target_arch = "x86_64"))]
3965     p.cargo("test -Zdoctest-xcompile")
3966         .masquerade_as_nightly_cargo()
3967         .with_stdout_contains(
3968             "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]",
3969         )
3970         .run();
3971 
3972     #[cfg(target_arch = "x86_64")]
3973     p.cargo("test -Zdoctest-xcompile")
3974         .masquerade_as_nightly_cargo()
3975         .with_stdout_contains(
3976             "test result: ok. 0 passed; 0 failed; 1 ignored; 0 measured; 0 filtered out[..]",
3977         )
3978         .run();
3979 }
3980 
3981 #[cargo_test]
cargo_test_doctest_xcompile()3982 fn cargo_test_doctest_xcompile() {
3983     if !cross_compile::can_run_on_host() {
3984         return;
3985     }
3986     if !is_nightly() {
3987         // -Zdoctest-xcompile is unstable
3988         return;
3989     }
3990     let p = project()
3991         .file("Cargo.toml", &basic_lib_manifest("foo"))
3992         .file(
3993             "src/lib.rs",
3994             r#"
3995 
3996             ///```
3997             ///assert!(1 == 1);
3998             ///```
3999             pub fn foo() -> u8 {
4000                 4
4001             }
4002             "#,
4003         )
4004         .build();
4005 
4006     p.cargo("build").run();
4007     p.cargo(&format!("test --target {}", cross_compile::alternate()))
4008         .with_stdout_contains("running 0 tests")
4009         .run();
4010     p.cargo(&format!(
4011         "test --target {} -Zdoctest-xcompile",
4012         cross_compile::alternate()
4013     ))
4014     .masquerade_as_nightly_cargo()
4015     .with_stdout_contains(
4016         "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]",
4017     )
4018     .run();
4019 }
4020 
4021 #[cargo_test]
cargo_test_doctest_xcompile_runner()4022 fn cargo_test_doctest_xcompile_runner() {
4023     if !cross_compile::can_run_on_host() {
4024         return;
4025     }
4026     if !is_nightly() {
4027         // -Zdoctest-xcompile is unstable
4028         return;
4029     }
4030 
4031     let runner = project()
4032         .file("Cargo.toml", &basic_bin_manifest("runner"))
4033         .file(
4034             "src/main.rs",
4035             r#"
4036             pub fn main() {
4037                 eprintln!("this is a runner");
4038                 let args: Vec<String> = std::env::args().collect();
4039                 std::process::Command::new(&args[1]).spawn();
4040             }
4041             "#,
4042         )
4043         .build();
4044 
4045     runner.cargo("build").run();
4046     assert!(runner.bin("runner").is_file());
4047     let runner_path = paths::root().join("runner");
4048     fs::copy(&runner.bin("runner"), &runner_path).unwrap();
4049 
4050     let config = paths::root().join(".cargo/config");
4051 
4052     fs::create_dir_all(config.parent().unwrap()).unwrap();
4053     // Escape Windows backslashes for TOML config.
4054     let runner_str = runner_path.to_str().unwrap().replace('\\', "\\\\");
4055     fs::write(
4056         config,
4057         format!(
4058             r#"
4059             [target.'cfg(target_arch = "{}")']
4060             runner = "{}"
4061             "#,
4062             cross_compile::alternate_arch(),
4063             runner_str
4064         ),
4065     )
4066     .unwrap();
4067 
4068     let p = project()
4069         .file("Cargo.toml", &basic_lib_manifest("foo"))
4070         .file(
4071             "src/lib.rs",
4072             &format!(
4073                 r#"
4074                 ///```
4075                 ///assert!(cfg!(target_arch = "{}"));
4076                 ///```
4077                 pub fn foo() -> u8 {{
4078                     4
4079                 }}
4080                 "#,
4081                 cross_compile::alternate_arch()
4082             ),
4083         )
4084         .build();
4085 
4086     p.cargo("build").run();
4087     p.cargo(&format!("test --target {}", cross_compile::alternate()))
4088         .with_stdout_contains("running 0 tests")
4089         .run();
4090     p.cargo(&format!(
4091         "test --target {} -Zdoctest-xcompile",
4092         cross_compile::alternate()
4093     ))
4094     .masquerade_as_nightly_cargo()
4095     .with_stdout_contains(
4096         "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]",
4097     )
4098     .with_stderr_contains("this is a runner")
4099     .run();
4100 }
4101 
4102 #[cargo_test]
4103 fn cargo_test_doctest_xcompile_no_runner() {
4104     if !cross_compile::can_run_on_host() {
4105         return;
4106     }
4107     if !is_nightly() {
4108         // -Zdoctest-xcompile is unstable
4109         return;
4110     }
4111 
4112     let p = project()
4113         .file("Cargo.toml", &basic_lib_manifest("foo"))
4114         .file(
4115             "src/lib.rs",
4116             &format!(
4117                 r#"
4118                 ///```
4119                 ///assert!(cfg!(target_arch = "{}"));
4120                 ///```
foo() -> u84121                 pub fn foo() -> u8 {{
4122                     4
4123                 }}
4124                 "#,
4125                 cross_compile::alternate_arch()
4126             ),
4127         )
4128         .build();
4129 
4130     p.cargo("build").run();
4131     p.cargo(&format!("test --target {}", cross_compile::alternate()))
4132         .with_stdout_contains("running 0 tests")
4133         .run();
4134     p.cargo(&format!(
4135         "test --target {} -Zdoctest-xcompile",
4136         cross_compile::alternate()
4137     ))
4138     .masquerade_as_nightly_cargo()
4139     .with_stdout_contains(
4140         "test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out[..]",
4141     )
4142     .run();
4143 }
4144 
4145 #[cargo_test]
4146 fn panic_abort_tests() {
4147     if !is_nightly() {
4148         // -Zpanic-abort-tests in rustc is unstable
4149         return;
4150     }
4151 
4152     let p = project()
4153         .file(
4154             "Cargo.toml",
4155             r#"
4156                 [package]
4157                 name = 'foo'
4158                 version = '0.1.0'
4159 
4160                 [dependencies]
4161                 a = { path = 'a' }
4162 
4163                 [profile.dev]
4164                 panic = 'abort'
4165                 [profile.test]
4166                 panic = 'abort'
4167             "#,
4168         )
4169         .file(
4170             "src/lib.rs",
4171             r#"
4172                 #[test]
foo()4173                 fn foo() {
4174                     a::foo();
4175                 }
4176             "#,
4177         )
4178         .file("a/Cargo.toml", &basic_lib_manifest("a"))
4179         .file("a/src/lib.rs", "pub fn foo() {}")
4180         .build();
4181 
4182     p.cargo("test -Z panic-abort-tests -v")
4183         .with_stderr_contains("[..]--crate-name a [..]-C panic=abort[..]")
4184         .with_stderr_contains("[..]--crate-name foo [..]-C panic=abort[..]")
4185         .with_stderr_contains("[..]--crate-name foo [..]-C panic=abort[..]--test[..]")
4186         .masquerade_as_nightly_cargo()
4187         .run();
4188 }
4189 
4190 #[cargo_test]
4191 fn panic_abort_only_test() {
4192     if !is_nightly() {
4193         // -Zpanic-abort-tests in rustc is unstable
4194         return;
4195     }
4196 
4197     let p = project()
4198         .file(
4199             "Cargo.toml",
4200             r#"
4201                 [package]
4202                 name = 'foo'
4203                 version = '0.1.0'
4204 
4205                 [dependencies]
4206                 a = { path = 'a' }
4207 
4208                 [profile.test]
4209                 panic = 'abort'
4210             "#,
4211         )
4212         .file(
4213             "src/lib.rs",
4214             r#"
4215                 #[test]
foo()4216                 fn foo() {
4217                     a::foo();
4218                 }
4219             "#,
4220         )
4221         .file("a/Cargo.toml", &basic_lib_manifest("a"))
4222         .file("a/src/lib.rs", "pub fn foo() {}")
4223         .build();
4224 
4225     p.cargo("test -Z panic-abort-tests -v")
4226         .with_stderr_contains("warning: `panic` setting is ignored for `test` profile")
4227         .masquerade_as_nightly_cargo()
4228         .run();
4229 }
4230 
4231 #[cargo_test]
4232 fn panic_abort_test_profile_inherits() {
4233     if !is_nightly() {
4234         // -Zpanic-abort-tests in rustc is unstable
4235         return;
4236     }
4237 
4238     let p = project()
4239         .file(
4240             "Cargo.toml",
4241             r#"
4242                 [package]
4243                 name = 'foo'
4244                 version = '0.1.0'
4245 
4246                 [dependencies]
4247                 a = { path = 'a' }
4248 
4249                 [profile.dev]
4250                 panic = 'abort'
4251             "#,
4252         )
4253         .file(
4254             "src/lib.rs",
4255             r#"
4256                 #[test]
foo()4257                 fn foo() {
4258                     a::foo();
4259                 }
4260             "#,
4261         )
4262         .file("a/Cargo.toml", &basic_lib_manifest("a"))
4263         .file("a/src/lib.rs", "pub fn foo() {}")
4264         .build();
4265 
4266     p.cargo("test -Z panic-abort-tests -v")
4267         .masquerade_as_nightly_cargo()
4268         .with_status(0)
4269         .run();
4270 }
4271 
4272 #[cargo_test]
4273 fn bin_env_for_test() {
4274     // Test for the `CARGO_BIN_` environment variables for tests.
4275     //
4276     // Note: The Unicode binary uses a `[[bin]]` definition because different
4277     // filesystems normalize utf-8 in different ways. For example, HFS uses
4278     // "gru\u{308}ßen" and APFS uses "gr\u{fc}ßen". Defining it in TOML forces
4279     // one form to be used.
4280     let p = project()
4281         .file(
4282             "Cargo.toml",
4283             r#"
4284                 [package]
4285                 name = "foo"
4286                 version = "0.1.0"
4287                 edition = "2018"
4288 
4289                 [[bin]]
4290                 name = 'grüßen'
4291                 path = 'src/bin/grussen.rs'
4292             "#,
4293         )
main()4294         .file("src/bin/foo.rs", "fn main() {}")
4295         .file("src/bin/with-dash.rs", "fn main() {}")
4296         .file("src/bin/grussen.rs", "fn main() {}")
4297         .build();
4298 
4299     let bin_path = |name| p.bin(name).to_string_lossy().replace("\\", "\\\\");
4300     p.change_file(
4301         "tests/check_env.rs",
4302         &r#"
4303             #[test]
run_bins()4304             fn run_bins() {
4305                 assert_eq!(env!("CARGO_BIN_EXE_foo"), "<FOO_PATH>");
4306                 assert_eq!(env!("CARGO_BIN_EXE_with-dash"), "<WITH_DASH_PATH>");
4307                 assert_eq!(env!("CARGO_BIN_EXE_grüßen"), "<GRÜSSEN_PATH>");
4308             }
4309         "#
4310         .replace("<FOO_PATH>", &bin_path("foo"))
4311         .replace("<WITH_DASH_PATH>", &bin_path("with-dash"))
4312         .replace("<GRÜSSEN_PATH>", &bin_path("grüßen")),
4313     );
4314 
4315     p.cargo("test --test check_env").run();
4316     p.cargo("check --test check_env").run();
4317 }
4318 
4319 #[cargo_test]
4320 fn test_workspaces_cwd() {
4321     // This tests that all the different test types are executed from the
4322     // crate directory (manifest_dir), and not from the workspace root.
4323 
4324     let make_lib_file = |expected| {
4325         format!(
4326             r#"
4327                 //! ```
4328                 //! assert_eq!("{expected}", std::fs::read_to_string("file.txt").unwrap());
4329                 //! assert_eq!("{expected}", include_str!("../file.txt"));
4330                 //! assert_eq!(
4331                 //!     std::path::PathBuf::from(std::env!("CARGO_MANIFEST_DIR")),
4332                 //!     std::env::current_dir().unwrap(),
4333                 //! );
4334                 //! ```
4335 
4336                 #[test]
4337                 fn test_unit_{expected}_cwd() {{
4338                     assert_eq!("{expected}", std::fs::read_to_string("file.txt").unwrap());
4339                     assert_eq!("{expected}", include_str!("../file.txt"));
4340                     assert_eq!(
4341                         std::path::PathBuf::from(std::env!("CARGO_MANIFEST_DIR")),
4342                         std::env::current_dir().unwrap(),
4343                     );
4344                 }}
4345             "#,
4346             expected = expected
4347         )
4348     };
4349     let make_test_file = |expected| {
4350         format!(
4351             r#"
4352                 #[test]
4353                 fn test_integration_{expected}_cwd() {{
4354                     assert_eq!("{expected}", std::fs::read_to_string("file.txt").unwrap());
4355                     assert_eq!("{expected}", include_str!("../file.txt"));
4356                     assert_eq!(
4357                         std::path::PathBuf::from(std::env!("CARGO_MANIFEST_DIR")),
4358                         std::env::current_dir().unwrap(),
4359                     );
4360                 }}
4361             "#,
4362             expected = expected
4363         )
4364     };
4365 
4366     let p = project()
4367         .file(
4368             "Cargo.toml",
4369             r#"
4370                 [package]
4371                 name = "root-crate"
4372                 version = "0.0.0"
4373 
4374                 [workspace]
4375                 members = [".", "nested-crate", "very/deeply/nested/deep-crate"]
4376             "#,
4377         )
4378         .file("file.txt", "root")
4379         .file("src/lib.rs", &make_lib_file("root"))
4380         .file("tests/integration.rs", &make_test_file("root"))
4381         .file(
4382             "nested-crate/Cargo.toml",
4383             r#"
4384                 [package]
4385                 name = "nested-crate"
4386                 version = "0.0.0"
4387             "#,
4388         )
4389         .file("nested-crate/file.txt", "nested")
4390         .file("nested-crate/src/lib.rs", &make_lib_file("nested"))
4391         .file(
4392             "nested-crate/tests/integration.rs",
4393             &make_test_file("nested"),
4394         )
4395         .file(
4396             "very/deeply/nested/deep-crate/Cargo.toml",
4397             r#"
4398                 [package]
4399                 name = "deep-crate"
4400                 version = "0.0.0"
4401             "#,
4402         )
4403         .file("very/deeply/nested/deep-crate/file.txt", "deep")
4404         .file(
4405             "very/deeply/nested/deep-crate/src/lib.rs",
4406             &make_lib_file("deep"),
4407         )
4408         .file(
4409             "very/deeply/nested/deep-crate/tests/integration.rs",
4410             &make_test_file("deep"),
4411         )
4412         .build();
4413 
4414     p.cargo("test --workspace --all")
4415         .with_stderr_contains("[DOCTEST] root-crate")
4416         .with_stderr_contains("[DOCTEST] nested-crate")
4417         .with_stderr_contains("[DOCTEST] deep-crate")
4418         .with_stdout_contains("test test_unit_root_cwd ... ok")
4419         .with_stdout_contains("test test_unit_nested_cwd ... ok")
4420         .with_stdout_contains("test test_unit_deep_cwd ... ok")
4421         .with_stdout_contains("test test_integration_root_cwd ... ok")
4422         .with_stdout_contains("test test_integration_nested_cwd ... ok")
4423         .with_stdout_contains("test test_integration_deep_cwd ... ok")
4424         .run();
4425 
4426     p.cargo("test -p root-crate --all")
4427         .with_stderr_contains("[DOCTEST] root-crate")
4428         .with_stdout_contains("test test_unit_root_cwd ... ok")
4429         .with_stdout_contains("test test_integration_root_cwd ... ok")
4430         .run();
4431 
4432     p.cargo("test -p nested-crate --all")
4433         .with_stderr_contains("[DOCTEST] nested-crate")
4434         .with_stdout_contains("test test_unit_nested_cwd ... ok")
4435         .with_stdout_contains("test test_integration_nested_cwd ... ok")
4436         .run();
4437 
4438     p.cargo("test -p deep-crate --all")
4439         .with_stderr_contains("[DOCTEST] deep-crate")
4440         .with_stdout_contains("test test_unit_deep_cwd ... ok")
4441         .with_stdout_contains("test test_integration_deep_cwd ... ok")
4442         .run();
4443 
4444     p.cargo("test --all")
4445         .cwd("nested-crate")
4446         .with_stderr_contains("[DOCTEST] nested-crate")
4447         .with_stdout_contains("test test_unit_nested_cwd ... ok")
4448         .with_stdout_contains("test test_integration_nested_cwd ... ok")
4449         .run();
4450 
4451     p.cargo("test --all")
4452         .cwd("very/deeply/nested/deep-crate")
4453         .with_stderr_contains("[DOCTEST] deep-crate")
4454         .with_stdout_contains("test test_unit_deep_cwd ... ok")
4455         .with_stdout_contains("test test_integration_deep_cwd ... ok")
4456         .run();
4457 }
4458