Skip to content

Commit 110c813

Browse files
committed
Cargo test quicker by not building untested examples when filtered
1 parent aef99e0 commit 110c813

File tree

3 files changed

+58
-16
lines changed

3 files changed

+58
-16
lines changed

src/bin/cargo/commands/test.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
9494

9595
let mut compile_opts = args.compile_options(config, CompileMode::Test, Some(&ws))?;
9696

97+
// `TESTNAME` is actually an argument of the test binary, but it's
98+
// important, so we explicitly mention it and reconfigure.
99+
let test_name: Option<&str> = args.value_of("TESTNAME");
100+
let mut test_args = vec![];
101+
test_args.extend(test_name.into_iter().map(|s| s.to_string()));
102+
test_args.extend(
103+
args.values_of("args")
104+
.unwrap_or_default()
105+
.map(|s| s.to_string()),
106+
);
107+
97108
let no_run = args.is_present("no-run");
98109
let doc = args.is_present("doc");
99110
if doc {
@@ -117,6 +128,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
117128
FilterRule::none(),
118129
FilterRule::none(),
119130
);
131+
} else if test_name.is_some() {
132+
if let CompileFilter::Default { .. } = compile_opts.filter {
133+
compile_opts.filter = ops::CompileFilter::new(
134+
LibRule::Default, // compile the library, so the unit tests can be run filtered
135+
FilterRule::All, // compile the binaries, so the unit tests in binaries can be run filtered
136+
FilterRule::All, // compile the tests, so the integration tests can be run filtered
137+
FilterRule::none(), // specify --examples to unit test binaries filtered
138+
FilterRule::none(), // specify --benches to unit test benchmarks filtered
139+
); // also, specify --doc to run doc tests filtered
140+
}
120141
}
121142

122143
let ops = ops::TestOptions {
@@ -125,16 +146,6 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
125146
compile_opts,
126147
};
127148

128-
// `TESTNAME` is actually an argument of the test binary, but it's
129-
// important, so we explicitly mention it and reconfigure.
130-
let mut test_args = vec![];
131-
test_args.extend(args.value_of("TESTNAME").into_iter().map(|s| s.to_string()));
132-
test_args.extend(
133-
args.values_of("args")
134-
.unwrap_or_default()
135-
.map(|s| s.to_string()),
136-
);
137-
138149
let err = ops::run_tests(&ws, &ops, &test_args)?;
139150
match err {
140151
None => Ok(()),

tests/testsuite/freshness.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ fn changing_profiles_caches_targets() {
247247
"\
248248
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
249249
[RUNNING] target[..]debug[..]deps[..]foo-[..][EXE]
250-
[DOCTEST] foo
251250
",
252251
)
253252
.run();

tests/testsuite/test.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ fn cargo_test_verbose() {
163163
[COMPILING] foo v0.5.0 ([CWD])
164164
[RUNNING] `rustc [..] src/main.rs [..]`
165165
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
166-
[RUNNING] `[..]target/debug/deps/foo-[..][EXE] hello`",
166+
[RUNNING] `[CWD]/target/debug/deps/foo-[..] hello`
167+
",
167168
)
168169
.with_stdout_contains("test test_hello ... ok")
169170
.run();
@@ -601,21 +602,21 @@ fn pass_through_command_line() {
601602
[COMPILING] foo v0.0.1 ([CWD])
602603
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
603604
[RUNNING] target/debug/deps/foo-[..][EXE]
604-
[DOCTEST] foo",
605+
",
605606
)
607+
.with_stdout_contains("running 1 test")
606608
.with_stdout_contains("test bar ... ok")
607-
.with_stdout_contains("running 0 tests")
608609
.run();
609610

610611
p.cargo("test foo")
611612
.with_stderr(
612613
"\
613614
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
614615
[RUNNING] target/debug/deps/foo-[..][EXE]
615-
[DOCTEST] foo",
616+
",
616617
)
618+
.with_stdout_contains("running 1 test")
617619
.with_stdout_contains("test foo ... ok")
618-
.with_stdout_contains("running 0 tests")
619620
.run();
620621
}
621622

@@ -1462,6 +1463,37 @@ fn test_run_implicit_example_target() {
14621463
.run();
14631464
}
14641465

1466+
#[test]
1467+
fn test_filtered_excludes_compiling_examples() {
1468+
let p = project()
1469+
.file(
1470+
"src/lib.rs",
1471+
"#[cfg(test)] mod tests { #[test] fn foo() { assert!(true); } }",
1472+
)
1473+
.file("examples/ex1.rs", "fn main() {}")
1474+
.build();
1475+
1476+
p.cargo("test -v foo")
1477+
.with_stdout(
1478+
"
1479+
running 1 test
1480+
test tests::foo ... ok
1481+
1482+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
1483+
1484+
",
1485+
)
1486+
.with_stderr("\
1487+
[COMPILING] foo v0.0.1 ([CWD])
1488+
[RUNNING] `rustc --crate-name foo src/lib.rs [..] --test [..]`
1489+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
1490+
[RUNNING] `[CWD]/target/debug/deps/foo-[..] foo`
1491+
",
1492+
)
1493+
.with_stderr_does_not_contain("[RUNNING][..]rustc[..]ex1[..]")
1494+
.run();
1495+
}
1496+
14651497
#[test]
14661498
fn test_no_harness() {
14671499
let p = project()

0 commit comments

Comments
 (0)