Skip to content

Commit 929392d

Browse files
committed
Auto merge of #4931 - Manishearth:testname, r=matklad
Explicitly mention testname argument for cargo test/bench The fact that `cargo test foo` works is totally non obvious. I suspect that 99% of the time folks running `cargo help test` are looking for a way to only run a specific test, and the current help message makes it seem like the way to do that is `cargo test --test foo`, which is incorrect (it runs a specific test target). This PR mentions it explicitly in the help message, first and foremost.
2 parents 941cea5 + c3a7db1 commit 929392d

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/bin/bench.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,18 @@ pub struct Options {
3737
flag_exclude: Vec<String>,
3838
#[serde(rename = "flag_Z")]
3939
flag_z: Vec<String>,
40+
#[serde(rename = "arg_BENCHNAME")]
41+
arg_benchname: Option<String>,
4042
}
4143

4244
pub const USAGE: &'static str = "
4345
Execute all benchmarks of a local package
4446
4547
Usage:
46-
cargo bench [options] [--] [<args>...]
48+
cargo bench [options] [BENCHNAME] [--] [<args>...]
4749
4850
Options:
51+
BENCHNAME If specified, only run benches containing this string in their names
4952
-h, --help Print this message
5053
--lib Benchmark only this package's library
5154
--bin NAME ... Benchmark only the specified binary
@@ -95,7 +98,7 @@ not affect how many jobs are used when running the benchmarks.
9598
Compilation can be customized with the `bench` profile in the manifest.
9699
";
97100

98-
pub fn execute(options: Options, config: &mut Config) -> CliResult {
101+
pub fn execute(mut options: Options, config: &mut Config) -> CliResult {
99102
debug!("executing; cmd=cargo-bench; args={:?}",
100103
env::args().collect::<Vec<_>>());
101104

@@ -139,6 +142,10 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
139142
},
140143
};
141144

145+
if let Some(test) = options.arg_benchname.take() {
146+
options.arg_args.insert(0, test);
147+
}
148+
142149
let err = ops::run_benches(&ws, &ops, &options.arg_args)?;
143150
match err {
144151
None => Ok(()),

src/bin/test.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,18 @@ pub struct Options {
3939
flag_exclude: Vec<String>,
4040
#[serde(rename = "flag_Z")]
4141
flag_z: Vec<String>,
42+
#[serde(rename = "arg_TESTNAME")]
43+
arg_testname: Option<String>,
4244
}
4345

4446
pub const USAGE: &'static str = "
4547
Execute all unit and integration tests of a local package
4648
4749
Usage:
48-
cargo test [options] [--] [<args>...]
50+
cargo test [options] [TESTNAME] [--] [<args>...]
4951
5052
Options:
53+
TESTNAME If specified, only run tests containing this string in their names
5154
-h, --help Print this message
5255
--lib Test only this package's library
5356
--doc Test only this library's documentation
@@ -116,7 +119,7 @@ To get the list of all options available for the test binaries use this:
116119
cargo test -- --help
117120
";
118121

119-
pub fn execute(options: Options, config: &mut Config) -> CliResult {
122+
pub fn execute(mut options: Options, config: &mut Config) -> CliResult {
120123
debug!("executing; cmd=cargo-test; args={:?}",
121124
env::args().collect::<Vec<_>>());
122125

@@ -172,6 +175,12 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
172175
},
173176
};
174177

178+
// TESTNAME is actually an argument of the test binary, but it's
179+
// important so we explicitly mention it and reconfigure
180+
if let Some(test) = options.arg_testname.take() {
181+
options.arg_args.insert(0, test);
182+
}
183+
175184
let err = ops::run_tests(&ws, &ops, &options.arg_args)?;
176185
match err {
177186
None => Ok(()),

0 commit comments

Comments
 (0)