Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bring multiple test filtering to test/bench #6697

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 deletions src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ pub fn cli() -> App {
.about("Execute all benchmarks of a local package")
.arg(
Arg::with_name("BENCHNAME")
.help("If specified, only run benches containing this string in their names"),
.help("If specified, only run benches containing this string in their names")
.multiple(true),
)
.arg(
Arg::with_name("args")
Expand Down Expand Up @@ -82,19 +83,10 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
compile_opts,
};

let mut bench_args = vec![];
bench_args.extend(
args.value_of("BENCHNAME")
.into_iter()
.map(|s| s.to_string()),
);
bench_args.extend(
args.values_of("args")
.unwrap_or_default()
.map(|s| s.to_string()),
);
let bench_names = args.values_of("BENCHNAME").unwrap_or_default().collect::<Vec<_>>();
let bench_args = args.values_of("args").unwrap_or_default().collect::<Vec<_>>();

let err = ops::run_benches(&ws, &ops, &bench_args)?;
let err = ops::run_benches(&ws, &ops, &bench_names, &bench_args)?;
match err {
None => Ok(()),
Some(err) => Err(match err.exit.as_ref().and_then(|e| e.code()) {
Expand Down
17 changes: 6 additions & 11 deletions src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ pub fn cli() -> App {
.about("Execute all unit and integration tests and build examples of a local package")
.arg(
Arg::with_name("TESTNAME")
.help("If specified, only run tests containing this string in their names"),
.help("If specified, only run tests containing this string in their names")
.multiple(true),
)
.arg(
Arg::with_name("args")
Expand Down Expand Up @@ -96,14 +97,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {

// `TESTNAME` is actually an argument of the test binary, but it's
// important, so we explicitly mention it and reconfigure.
let test_name: Option<&str> = args.value_of("TESTNAME");
let mut test_args = vec![];
test_args.extend(test_name.into_iter().map(|s| s.to_string()));
test_args.extend(
args.values_of("args")
.unwrap_or_default()
.map(|s| s.to_string()),
);
let test_names = args.values_of("TESTNAME").unwrap_or_default().collect::<Vec<_>>();
let test_args = args.values_of("args").unwrap_or_default().collect::<Vec<_>>();

let no_run = args.is_present("no-run");
let doc = args.is_present("doc");
Expand All @@ -128,7 +123,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
FilterRule::none(),
FilterRule::none(),
);
} else if test_name.is_some() {
} else if !test_names.is_empty() {
if let CompileFilter::Default { .. } = compile_opts.filter {
compile_opts.filter = ops::CompileFilter::new(
LibRule::Default, // compile the library, so the unit tests can be run filtered
Expand All @@ -146,7 +141,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
compile_opts,
};

let err = ops::run_tests(&ws, &ops, &test_args)?;
let err = ops::run_tests(&ws, &ops, &test_names, &test_args)?;
match err {
None => Ok(()),
Some(err) => Err(match err.exit.as_ref().and_then(|e| e.code()) {
Expand Down
80 changes: 66 additions & 14 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,55 @@ pub struct TestOptions<'a> {
pub fn run_tests(
ws: &Workspace<'_>,
options: &TestOptions<'_>,
test_args: &[String],
test_names: &[&str],
test_args: &[&str],
) -> CargoResult<Option<CargoTestError>> {
let compilation = compile_tests(ws, options)?;

if options.no_run {
return Ok(None);
}
let (test, mut errors) = run_unit_tests(options, test_args, &compilation)?;

// If we have an error and want to fail fast, then return.
if !errors.is_empty() && !options.no_fail_fast {
return Ok(Some(CargoTestError::new(test, errors)));
let test_names = if test_names.is_empty() {
vec![None]
} else {
test_names.iter().map(|test_name| Some(test_name)).collect()
};

let mut test = Test::Multiple;
let mut errors = Vec::new();

for test_name in &test_names {
let mut test_args = test_args.to_vec();
if let Some(test_name) = test_name {
test_args.insert(0, test_name)
}

let (test1, errors1) = run_unit_tests(options, &test_args, &compilation)?;
test = if errors.is_empty() { test1 } else { Test::Multiple };
errors.extend(errors1);

// If we have an error and want to fail fast, then return.
if !errors.is_empty() && !options.no_fail_fast {
return Ok(Some(CargoTestError::new(test, errors)));
}
}

for test_name in &test_names {
let mut test_args = test_args.to_vec();
if let Some(test_name) = test_name {
test_args.insert(0, test_name)
}

let (doctest, docerrors) = run_doc_tests(options, &test_args, &compilation)?;
test = if docerrors.is_empty() { test } else { doctest };
errors.extend(docerrors);

if !errors.is_empty() && !options.no_fail_fast {
return Ok(Some(CargoTestError::new(test, errors)))
}
}

let (doctest, docerrors) = run_doc_tests(options, test_args, &compilation)?;
let test = if docerrors.is_empty() { test } else { doctest };
errors.extend(docerrors);
if errors.is_empty() {
Ok(None)
} else {
Expand All @@ -42,16 +74,36 @@ pub fn run_tests(
pub fn run_benches(
ws: &Workspace<'_>,
options: &TestOptions<'_>,
args: &[String],
bench_names: &[&str],
args: &[&str],
) -> CargoResult<Option<CargoTestError>> {
let mut args = args.to_vec();
args.push("--bench".to_string());
let compilation = compile_tests(ws, options)?;

if options.no_run {
return Ok(None);
}
let (test, errors) = run_unit_tests(options, &args, &compilation)?;

let bench_names = if bench_names.is_empty() {
vec![None]
} else {
bench_names.iter().map(|bench_name| Some(bench_name)).collect()
};

let mut test = Test::Multiple;
let mut errors = Vec::new();

for bench_name in bench_names {
let mut args = args.to_vec();
if let Some(bench_name) = bench_name {
args.insert(0, bench_name)
}
args.push("--bench");

let (test1, errors1) = run_unit_tests(options, &args, &compilation)?;
test = if errors.is_empty() { test1 } else { Test::Multiple };
errors.extend(errors1);
}

match errors.len() {
0 => Ok(None),
_ => Ok(Some(CargoTestError::new(test, errors))),
Expand All @@ -72,7 +124,7 @@ fn compile_tests<'a>(
/// Runs the unit and integration tests of a package.
fn run_unit_tests(
options: &TestOptions<'_>,
test_args: &[String],
test_args: &[&str],
compilation: &Compilation<'_>,
) -> CargoResult<(Test, Vec<ProcessError>)> {
let config = options.compile_opts.config;
Expand Down Expand Up @@ -125,7 +177,7 @@ fn run_unit_tests(

fn run_doc_tests(
options: &TestOptions<'_>,
test_args: &[String],
test_args: &[&str],
compilation: &Compilation<'_>,
) -> CargoResult<(Test, Vec<ProcessError>)> {
let mut errors = Vec::new();
Expand Down
5 changes: 1 addition & 4 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,10 +481,7 @@ impl<'a> ArgMatchesExt for ArgMatches<'a> {
}

pub fn values(args: &ArgMatches<'_>, name: &str) -> Vec<String> {
args.values_of(name)
.unwrap_or_default()
.map(|s| s.to_string())
.collect()
args._values_of(name)
}

#[derive(PartialEq, PartialOrd, Eq, Ord)]
Expand Down
2 changes: 1 addition & 1 deletion src/doc/man/cargo-bench.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cargo-bench - Execute benchmarks of a package

== SYNOPSIS

`cargo bench [_OPTIONS_] [BENCHNAME] [-- _BENCH-OPTIONS_]`
`cargo bench [_OPTIONS_] [BENCHNAME]... [-- _BENCH-OPTIONS_]`

== DESCRIPTION

Expand Down
2 changes: 1 addition & 1 deletion src/doc/man/cargo-test.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ cargo-test - Execute unit and integration tests of a package

== SYNOPSIS

`cargo test [_OPTIONS_] [TESTNAME] [-- _TEST-OPTIONS_]`
`cargo test [_OPTIONS_] [TESTNAME]... [-- _TEST-OPTIONS_]`

== DESCRIPTION

Expand Down
2 changes: 1 addition & 1 deletion src/doc/man/generated/cargo-bench.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h2 id="cargo_bench_name">NAME</h2>
<h2 id="cargo_bench_synopsis">SYNOPSIS</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>cargo bench [<em>OPTIONS</em>] [BENCHNAME] [-- <em>BENCH-OPTIONS</em>]</code></p>
<p><code>cargo bench [<em>OPTIONS</em>] [BENCHNAME]&#8230;&#8203; [-- <em>BENCH-OPTIONS</em>]</code></p>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/doc/man/generated/cargo-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h2 id="cargo_test_name">NAME</h2>
<h2 id="cargo_test_synopsis">SYNOPSIS</h2>
<div class="sectionbody">
<div class="paragraph">
<p><code>cargo test [<em>OPTIONS</em>] [TESTNAME] [-- <em>TEST-OPTIONS</em>]</code></p>
<p><code>cargo test [<em>OPTIONS</em>] [TESTNAME]&#8230;&#8203; [-- <em>TEST-OPTIONS</em>]</code></p>
</div>
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/etc/man/cargo-bench.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.\" Title: cargo-bench
.\" Author: [see the "AUTHOR(S)" section]
.\" Generator: Asciidoctor 1.5.8
.\" Date: 2018-12-23
.\" Date: 2019-02-24
.\" Manual: \ \&
.\" Source: \ \&
.\" Language: English
.\"
.TH "CARGO\-BENCH" "1" "2018-12-23" "\ \&" "\ \&"
.TH "CARGO\-BENCH" "1" "2019-02-24" "\ \&" "\ \&"
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.ss \n[.ss] 0
Expand All @@ -31,7 +31,7 @@
cargo\-bench \- Execute benchmarks of a package
.SH "SYNOPSIS"
.sp
\fBcargo bench [\fIOPTIONS\fP] [BENCHNAME] [\-\- \fIBENCH\-OPTIONS\fP]\fP
\fBcargo bench [\fIOPTIONS\fP] [BENCHNAME]... [\-\- \fIBENCH\-OPTIONS\fP]\fP
.SH "DESCRIPTION"
.sp
Compile and execute benchmarks.
Expand Down
6 changes: 3 additions & 3 deletions src/etc/man/cargo-test.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
.\" Title: cargo-test
.\" Author: [see the "AUTHOR(S)" section]
.\" Generator: Asciidoctor 1.5.8
.\" Date: 2018-12-23
.\" Date: 2019-02-24
.\" Manual: \ \&
.\" Source: \ \&
.\" Language: English
.\"
.TH "CARGO\-TEST" "1" "2018-12-23" "\ \&" "\ \&"
.TH "CARGO\-TEST" "1" "2019-02-24" "\ \&" "\ \&"
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.ss \n[.ss] 0
Expand All @@ -31,7 +31,7 @@
cargo\-test \- Execute unit and integration tests of a package
.SH "SYNOPSIS"
.sp
\fBcargo test [\fIOPTIONS\fP] [TESTNAME] [\-\- \fITEST\-OPTIONS\fP]\fP
\fBcargo test [\fIOPTIONS\fP] [TESTNAME]... [\-\- \fITEST\-OPTIONS\fP]\fP
.SH "DESCRIPTION"
.sp
Compile and execute unit and integration tests.
Expand Down
37 changes: 35 additions & 2 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,43 @@ fn pass_through_command_line() {
.run();
}

// Regression test for running cargo-test twice with
// tests in an rlib
#[test]
fn specify_multiple_tests() {
let p = project()
.file("src/lib.rs", "#[test] fn foo() {} #[test] fn bar() {}")
.build();

p.cargo("test foo bar")
.with_stderr(
"\
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] target/debug/deps/foo-[..][EXE]
[RUNNING] target/debug/deps/foo-[..][EXE]
",
)
.with_stdout(
"
running 1 test
test foo ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out


running 1 test
test bar ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 1 filtered out

",
)
.run();
}

#[test]
fn cargo_test_twice() {
// Regression test for running cargo-test twice with
// tests in an rlib
let p = project()
.file("Cargo.toml", &basic_lib_manifest("foo"))
.file(
Expand Down