Skip to content

Commit 4dc5db2

Browse files
committed
Auto merge of #5464 - ehuss:restore-example-test, r=alexcrichton
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) ```
2 parents 66b0ffa + dffc5ba commit 4dc5db2

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

src/cargo/ops/cargo_compile.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ fn generate_targets<'a>(
603603
};
604604
let target_mode = match target_mode {
605605
CompileMode::Test => {
606-
if target.is_example() {
606+
if target.is_example() && !filter.is_specific() && !target.tested() {
607607
// Examples are included as regular binaries to verify
608608
// that they compile.
609609
CompileMode::Build

tests/testsuite/test.rs

+60-12
Original file line numberDiff line numberDiff line change
@@ -1721,8 +1721,15 @@ fn test_run_implicit_example_target() {
17211721
authors = []
17221722
17231723
[[bin]]
1724-
name="mybin"
1725-
path="src/mybin.rs"
1724+
name = "mybin"
1725+
path = "src/mybin.rs"
1726+
1727+
[[example]]
1728+
name = "myexm1"
1729+
1730+
[[example]]
1731+
name = "myexm2"
1732+
test = true
17261733
"#,
17271734
)
17281735
.file(
@@ -1733,20 +1740,61 @@ fn test_run_implicit_example_target() {
17331740
.file("tests/mytest.rs", "#[test] fn test_in_test() { }")
17341741
.file("benches/mybench.rs", "#[test] fn test_in_bench() { }")
17351742
.file(
1736-
"examples/myexm.rs",
1737-
r#"#[test] fn test_in_exm() { panic!("Don't even test me."); }
1738-
fn main() { panic!("Don't execute me!"); }"#,
1743+
"examples/myexm1.rs",
1744+
"#[test] fn test_in_exm() { }
1745+
fn main() { panic!(\"Don't execute me!\"); }",
1746+
)
1747+
.file(
1748+
"examples/myexm2.rs",
1749+
"#[test] fn test_in_exm() { }
1750+
fn main() { panic!(\"Don't execute me!\"); }",
17391751
)
17401752
.build();
17411753

1754+
// Compiles myexm1 as normal, but does not run it.
17421755
assert_that(
1743-
prj.cargo("test").arg("--examples"),
1744-
execs().with_status(0).with_stderr(format!(
1745-
"\
1746-
[COMPILING] foo v0.0.1 ({dir})
1747-
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]",
1748-
dir = prj.url()
1749-
)),
1756+
prj.cargo("test -v"),
1757+
execs()
1758+
.with_status(0)
1759+
.with_stderr_contains("[RUNNING] `rustc [..]myexm1.rs --crate-type bin[..]")
1760+
.with_stderr_contains("[RUNNING] `rustc [..]myexm2.rs [..]--test[..]")
1761+
.with_stderr_does_not_contain("[RUNNING] [..]myexm1-[..]")
1762+
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm2-[..]"),
1763+
);
1764+
1765+
// Only tests myexm2.
1766+
assert_that(
1767+
prj.cargo("test --tests"),
1768+
execs()
1769+
.with_status(0)
1770+
.with_stderr_does_not_contain("[RUNNING] [..]myexm1-[..]")
1771+
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm2-[..]"),
1772+
);
1773+
1774+
// Tests all examples.
1775+
assert_that(
1776+
prj.cargo("test --examples"),
1777+
execs()
1778+
.with_status(0)
1779+
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm1-[..]")
1780+
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm2-[..]"),
1781+
);
1782+
1783+
// Test an example, even without `test` set.
1784+
assert_that(
1785+
prj.cargo("test --example myexm1"),
1786+
execs()
1787+
.with_status(0)
1788+
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm1-[..]"),
1789+
);
1790+
1791+
// Tests all examples.
1792+
assert_that(
1793+
prj.cargo("test --all-targets"),
1794+
execs()
1795+
.with_status(0)
1796+
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm1-[..]")
1797+
.with_stderr_contains("[RUNNING] [..]target[/]debug[/]examples[/]myexm2-[..]"),
17501798
);
17511799
}
17521800

0 commit comments

Comments
 (0)