Partially revert change to testing examples.

Fixes #5437

I don't think changing the behavior was quite the correct thing to do.  This new behavior is very similar to the old with a few small differences:

```
cargo test
    ORGINAL: Only builds examples.
    NEW: Builds all examples.  Any example with `test` set is tested.

cargo test --tests
    ORIGINAL: Runs all examples as tests.
    NEW: Only runs examples as tests if `test` is set.

cargo test --examples
    ORIGINAL: Runs all examples as tests.
    NEW: (SAME)

cargo test --example foo
    ORIGINAL: Runs the given example as a test.
    NEW: (SAME)

cargo test --all-targets
    ORIGINAL: Runs all examples as tests.
    NEW: (SAME)
```
This commit is contained in:
Eric Huss 2018-05-02 20:17:53 -07:00
parent 6cd841fe4a
commit dffc5baeb2
2 changed files with 61 additions and 13 deletions

View File

@ -603,7 +603,7 @@ fn generate_targets<'a>(
}; };
let target_mode = match target_mode { let target_mode = match target_mode {
CompileMode::Test => { CompileMode::Test => {
if target.is_example() { if target.is_example() && !filter.is_specific() && !target.tested() {
// Examples are included as regular binaries to verify // Examples are included as regular binaries to verify
// that they compile. // that they compile.
CompileMode::Build CompileMode::Build

View File

@ -1723,6 +1723,13 @@ fn test_run_implicit_example_target() {
[[bin]] [[bin]]
name = "mybin" name = "mybin"
path = "src/mybin.rs" path = "src/mybin.rs"
[[example]]
name = "myexm1"
[[example]]
name = "myexm2"
test = true
"#, "#,
) )
.file( .file(
@ -1733,20 +1740,61 @@ fn test_run_implicit_example_target() {
.file("tests/mytest.rs", "#[test] fn test_in_test() { }") .file("tests/mytest.rs", "#[test] fn test_in_test() { }")
.file("benches/mybench.rs", "#[test] fn test_in_bench() { }") .file("benches/mybench.rs", "#[test] fn test_in_bench() { }")
.file( .file(
"examples/myexm.rs", "examples/myexm1.rs",
r#"#[test] fn test_in_exm() { panic!("Don't even test me."); } "#[test] fn test_in_exm() { }
fn main() { panic!("Don't execute me!"); }"#, fn main() { panic!(\"Don't execute me!\"); }",
)
.file(
"examples/myexm2.rs",
"#[test] fn test_in_exm() { }
fn main() { panic!(\"Don't execute me!\"); }",
) )
.build(); .build();
// Compiles myexm1 as normal, but does not run it.
assert_that( assert_that(
prj.cargo("test").arg("--examples"), prj.cargo("test -v"),
execs().with_status(0).with_stderr(format!( execs()
"\ .with_status(0)
[COMPILING] foo v0.0.1 ({dir}) .with_stderr_contains("[RUNNING] `rustc [..]myexm1.rs --crate-type bin[..]")
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]", .with_stderr_contains("[RUNNING] `rustc [..]myexm2.rs [..]--test[..]")
dir = prj.url() .with_stderr_does_not_contain("[RUNNING] [..]myexm1-[..]")
)), .with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm2-[..]"),
);
// Only tests myexm2.
assert_that(
prj.cargo("test --tests"),
execs()
.with_status(0)
.with_stderr_does_not_contain("[RUNNING] [..]myexm1-[..]")
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm2-[..]"),
);
// Tests all examples.
assert_that(
prj.cargo("test --examples"),
execs()
.with_status(0)
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm1-[..]")
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm2-[..]"),
);
// Test an example, even without `test` set.
assert_that(
prj.cargo("test --example myexm1"),
execs()
.with_status(0)
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm1-[..]"),
);
// Tests all examples.
assert_that(
prj.cargo("test --all-targets"),
execs()
.with_status(0)
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm1-[..]")
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm2-[..]"),
); );
} }