Skip to content

Commit b82eb28

Browse files
committed
convert benches to tests in subprocess if we're not benchmarking
1 parent 2f7fd2e commit b82eb28

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

library/test/src/lib.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ use time::TestExecTime;
9292
const ERROR_EXIT_CODE: i32 = 101;
9393

9494
const SECONDARY_TEST_INVOKER_VAR: &str = "__RUST_TEST_INVOKE";
95+
const SECONDARY_TEST_BENCH_BENCHMARKS_VAR: &str = "__RUST_TEST_BENCH_BENCHMARKS";
9596

9697
// The default console test runner. It accepts the command line
9798
// arguments and a vector of test_descs.
@@ -171,10 +172,18 @@ pub fn test_main_static_abort(tests: &[&TestDescAndFn]) {
171172
// will then exit the process.
172173
if let Ok(name) = env::var(SECONDARY_TEST_INVOKER_VAR) {
173174
env::remove_var(SECONDARY_TEST_INVOKER_VAR);
175+
176+
// Convert benchmarks to tests if we're not benchmarking.
177+
let mut tests = tests.iter().map(make_owned_test).collect::<Vec<_>>();
178+
if env::var(SECONDARY_TEST_BENCH_BENCHMARKS_VAR).is_ok() {
179+
env::remove_var(SECONDARY_TEST_BENCH_BENCHMARKS_VAR);
180+
} else {
181+
tests = convert_benchmarks_to_tests(tests);
182+
};
183+
174184
let test = tests
175-
.iter()
185+
.into_iter()
176186
.filter(|test| test.desc.name.as_slice() == name)
177-
.map(make_owned_test)
178187
.next()
179188
.unwrap_or_else(|| panic!("couldn't find a test with the provided name '{name}'"));
180189
let TestDescAndFn { desc, testfn } = test;
@@ -557,6 +566,7 @@ pub fn run_test(
557566
let name = desc.name.clone();
558567
let nocapture = opts.nocapture;
559568
let time_options = opts.time_options;
569+
let bench_benchmarks = opts.bench_benchmarks;
560570

561571
let runtest = move || match strategy {
562572
RunStrategy::InProcess => run_test_in_process(
@@ -575,6 +585,7 @@ pub fn run_test(
575585
time_options.is_some(),
576586
monitor_ch,
577587
time_options,
588+
bench_benchmarks,
578589
),
579590
};
580591

@@ -672,13 +683,17 @@ fn spawn_test_subprocess(
672683
report_time: bool,
673684
monitor_ch: Sender<CompletedTest>,
674685
time_opts: Option<time::TestTimeOptions>,
686+
bench_benchmarks: bool,
675687
) {
676688
let (result, test_output, exec_time) = (|| {
677689
let args = env::args().collect::<Vec<_>>();
678690
let current_exe = &args[0];
679691

680692
let mut command = Command::new(current_exe);
681693
command.env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice());
694+
if bench_benchmarks {
695+
command.env(SECONDARY_TEST_BENCH_BENCHMARKS_VAR, "1");
696+
}
682697
if nocapture {
683698
command.stdout(process::Stdio::inherit());
684699
command.stderr(process::Stdio::inherit());

0 commit comments

Comments
 (0)