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

add argument --counts to run repeated tests, benchmarks #10174

Closed
wants to merge 2 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
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn cli() -> App {
"no-fail-fast",
"Run all benchmarks regardless of failure",
))
.arg_counts()
.arg_unit_graph()
.after_help("Run `cargo help bench` for more detailed information.\n")
}
Expand All @@ -65,6 +66,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ops = TestOptions {
no_run: args.is_present("no-run"),
no_fail_fast: args.is_present("no-fail-fast"),
counts: args.value_of_u32("counts")?,
compile_opts,
};

Expand Down
2 changes: 2 additions & 0 deletions src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn cli() -> App {
.arg(opt("doc", "Test only this library's documentation"))
.arg(opt("no-run", "Compile, but don't run tests"))
.arg(opt("no-fail-fast", "Run all tests regardless of failure"))
.arg_counts()
.arg_package_spec(
"Package to run tests for",
"Test all packages in the workspace",
Expand Down Expand Up @@ -117,6 +118,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let ops = ops::TestOptions {
no_run,
no_fail_fast: args.is_present("no-fail-fast"),
counts: args.value_of_u32("counts")?,
compile_opts,
};

Expand Down
25 changes: 14 additions & 11 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct TestOptions {
pub compile_opts: ops::CompileOptions,
pub no_run: bool,
pub no_fail_fast: bool,
pub counts: Option<u32>,
}

pub fn run_tests(
Expand Down Expand Up @@ -113,18 +114,20 @@ fn run_unit_tests(
.shell()
.verbose(|shell| shell.status("Running", &cmd))?;

let result = cmd.exec();
for _ in 0..options.counts.unwrap_or(1) {
let result = cmd.exec();

if let Err(e) = result {
let e = e.downcast::<ProcessError>()?;
errors.push((
unit.target.kind().clone(),
unit.target.name().to_string(),
unit.pkg.name().to_string(),
e,
));
if !options.no_fail_fast {
break;
if let Err(e) = result {
let e = e.downcast::<ProcessError>()?;
errors.push((
unit.target.kind().clone(),
unit.target.name().to_string(),
unit.pkg.name().to_string(),
e,
));
if !options.no_fail_fast {
break;
}
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/cargo/util/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ pub trait AppExt: Sized {
)
}

fn arg_counts(self) -> Self {
self._arg(
opt("counts", "Number of repeated runs, default to 1")
.short("c")
.value_name("N"),
)
}

fn arg_jobs(self) -> Self {
self._arg(
opt("jobs", "Number of parallel jobs, defaults to # of CPUs")
Expand Down
31 changes: 31 additions & 0 deletions tests/testsuite/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1756,3 +1756,34 @@ fn json_artifact_includes_executable_for_benchmark() {
)
.run();
}

#[cargo_test]
fn cargo_bench_counts() {
if !is_nightly() {
return;
}

let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
#![feature(test)]
#[cfg(test)]
extern crate test;
fn main() {}
#[bench] fn bench_hello(_b: &mut test::Bencher) {}
"#,
)
.build();

p.cargo("bench --counts 4")
.with_stderr(
"\
[COMPILING] foo v0.5.0 ([CWD])
[FINISHED] bench [optimized] target(s) in [..]
[RUNNING] [..] (target/release/deps/foo-[..][EXE])",
)
.with_stdout_contains_n("test bench_hello ... bench: [..]", 4)
.run();
}
25 changes: 25 additions & 0 deletions tests/testsuite/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4455,3 +4455,28 @@ fn test_workspaces_cwd() {
.with_stdout_contains("test test_integration_deep_cwd ... ok")
.run();
}

#[cargo_test]
fn cargo_test_counts() {
let p = project()
.file("Cargo.toml", &basic_bin_manifest("foo"))
.file(
"src/main.rs",
r#"
fn main() {}
#[test] fn test_hello() {}
"#,
)
.build();

p.cargo("test -c 4")
.with_stderr(
"\
[COMPILING] foo v0.5.0 ([CWD])
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])
",
)
.with_stdout_contains_n("test test_hello ... ok", 4)
.run();
}