Skip to content

Commit 1a46ce8

Browse files
committed
refactor(test): Track 'capture' rather than 'nocapture'
Rather than fixing the wording to match the new flag name, I switched to positive logic under the assumption people have a harder time following negative logic.
1 parent 2205455 commit 1a46ce8

File tree

6 files changed

+25
-25
lines changed

6 files changed

+25
-25
lines changed

Diff for: library/test/src/bench.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub fn benchmark<F>(
193193
id: TestId,
194194
desc: TestDesc,
195195
monitor_ch: Sender<CompletedTest>,
196-
nocapture: bool,
196+
capture: bool,
197197
f: F,
198198
) where
199199
F: FnMut(&mut Bencher) -> Result<(), String>,
@@ -202,7 +202,7 @@ pub fn benchmark<F>(
202202

203203
let data = Arc::new(Mutex::new(Vec::new()));
204204

205-
if !nocapture {
205+
if capture {
206206
io::set_output_capture(Some(data.clone()));
207207
}
208208

Diff for: library/test/src/cli.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub struct TestOpts {
1818
pub run_tests: bool,
1919
pub bench_benchmarks: bool,
2020
pub logfile: Option<PathBuf>,
21-
pub nocapture: bool,
21+
pub capture: bool,
2222
pub color: ColorConfig,
2323
pub format: OutputFormat,
2424
pub shuffle: bool,
@@ -36,7 +36,7 @@ pub struct TestOpts {
3636
impl TestOpts {
3737
pub fn use_color(&self) -> bool {
3838
match self.color {
39-
ColorConfig::AutoColor => !self.nocapture && io::stdout().is_terminal(),
39+
ColorConfig::AutoColor => self.capture && io::stdout().is_terminal(),
4040
ColorConfig::AlwaysColor => true,
4141
ColorConfig::NeverColor => false,
4242
}
@@ -274,7 +274,7 @@ fn parse_opts_impl(matches: getopts::Matches) -> OptRes {
274274
let logfile = get_log_file(&matches)?;
275275
let run_ignored = get_run_ignored(&matches, include_ignored)?;
276276
let filters = matches.free.clone();
277-
let nocapture = get_nocapture(&matches)?;
277+
let capture = get_capture(&matches)?;
278278
let test_threads = get_test_threads(&matches)?;
279279
let color = get_color_config(&matches)?;
280280
let format = get_format(&matches, quiet, allow_unstable)?;
@@ -295,7 +295,7 @@ fn parse_opts_impl(matches: getopts::Matches) -> OptRes {
295295
run_tests,
296296
bench_benchmarks,
297297
logfile,
298-
nocapture,
298+
capture,
299299
color,
300300
format,
301301
shuffle,
@@ -446,16 +446,16 @@ fn get_color_config(matches: &getopts::Matches) -> OptPartRes<ColorConfig> {
446446
Ok(color)
447447
}
448448

449-
fn get_nocapture(matches: &getopts::Matches) -> OptPartRes<bool> {
450-
let mut nocapture = matches.opt_present("nocapture");
451-
if !nocapture {
452-
nocapture = match env::var("RUST_TEST_NOCAPTURE") {
449+
fn get_capture(matches: &getopts::Matches) -> OptPartRes<bool> {
450+
let mut capture = !matches.opt_present("nocapture");
451+
if capture {
452+
capture = !match env::var("RUST_TEST_NOCAPTURE") {
453453
Ok(val) => &val != "0",
454454
Err(_) => false,
455455
};
456456
}
457457

458-
Ok(nocapture)
458+
Ok(capture)
459459
}
460460

461461
fn get_run_ignored(matches: &getopts::Matches, include_ignored: bool) -> OptPartRes<RunIgnored> {

Diff for: library/test/src/lib.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub fn test_main(args: &[String], tests: Vec<TestDescAndFn>, options: Option<Opt
115115
process::exit(ERROR_EXIT_CODE);
116116
}
117117
} else {
118-
if !opts.nocapture {
118+
if opts.capture {
119119
// If we encounter a non-unwinding panic, flush any captured output from the current test,
120120
// and stop capturing output to ensure that the non-unwinding panic message is visible.
121121
// We also acquire the locks for both output streams to prevent output from other threads
@@ -574,15 +574,15 @@ pub fn run_test(
574574
}
575575

576576
let name = desc.name.clone();
577-
let nocapture = opts.nocapture;
577+
let capture = opts.capture;
578578
let time_options = opts.time_options;
579579
let bench_benchmarks = opts.bench_benchmarks;
580580

581581
let runtest = move || match strategy {
582582
RunStrategy::InProcess => run_test_in_process(
583583
id,
584584
desc,
585-
nocapture,
585+
capture,
586586
time_options.is_some(),
587587
runnable_test,
588588
monitor_ch,
@@ -591,7 +591,7 @@ pub fn run_test(
591591
RunStrategy::SpawnPrimary => spawn_test_subprocess(
592592
id,
593593
desc,
594-
nocapture,
594+
capture,
595595
time_options.is_some(),
596596
monitor_ch,
597597
time_options,
@@ -626,7 +626,7 @@ pub fn run_test(
626626
}
627627
Runnable::Bench(runnable_bench) => {
628628
// Benchmarks aren't expected to panic, so we run them all in-process.
629-
runnable_bench.run(id, &desc, &monitor_ch, opts.nocapture);
629+
runnable_bench.run(id, &desc, &monitor_ch, opts.capture);
630630
None
631631
}
632632
}
@@ -644,7 +644,7 @@ fn __rust_begin_short_backtrace<T, F: FnOnce() -> T>(f: F) -> T {
644644
fn run_test_in_process(
645645
id: TestId,
646646
desc: TestDesc,
647-
nocapture: bool,
647+
capture: bool,
648648
report_time: bool,
649649
runnable_test: RunnableTest,
650650
monitor_ch: Sender<CompletedTest>,
@@ -653,7 +653,7 @@ fn run_test_in_process(
653653
// Buffer for capturing standard I/O
654654
let data = Arc::new(Mutex::new(Vec::new()));
655655

656-
if !nocapture {
656+
if capture {
657657
io::set_output_capture(Some(data.clone()));
658658
}
659659

@@ -691,7 +691,7 @@ where
691691
fn spawn_test_subprocess(
692692
id: TestId,
693693
desc: TestDesc,
694-
nocapture: bool,
694+
capture: bool,
695695
report_time: bool,
696696
monitor_ch: Sender<CompletedTest>,
697697
time_opts: Option<time::TestTimeOptions>,
@@ -706,7 +706,7 @@ fn spawn_test_subprocess(
706706
if bench_benchmarks {
707707
command.env(SECONDARY_TEST_BENCH_BENCHMARKS_VAR, "1");
708708
}
709-
if nocapture {
709+
if !capture {
710710
command.stdout(process::Stdio::inherit());
711711
command.stderr(process::Stdio::inherit());
712712
}

Diff for: library/test/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl TestOpts {
2424
run_tests: false,
2525
bench_benchmarks: false,
2626
logfile: None,
27-
nocapture: false,
27+
capture: true,
2828
color: AutoColor,
2929
format: OutputFormat::Pretty,
3030
shuffle: false,

Diff for: library/test/src/types.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@ impl RunnableBench {
174174
id: TestId,
175175
desc: &TestDesc,
176176
monitor_ch: &Sender<CompletedTest>,
177-
nocapture: bool,
177+
capture: bool,
178178
) {
179179
match self {
180180
RunnableBench::Static(f) => {
181-
crate::bench::benchmark(id, desc.clone(), monitor_ch.clone(), nocapture, f)
181+
crate::bench::benchmark(id, desc.clone(), monitor_ch.clone(), capture, f)
182182
}
183183
RunnableBench::Dynamic(f) => {
184-
crate::bench::benchmark(id, desc.clone(), monitor_ch.clone(), nocapture, f)
184+
crate::bench::benchmark(id, desc.clone(), monitor_ch.clone(), capture, f)
185185
}
186186
}
187187
}

Diff for: src/tools/compiletest/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ pub fn parse_config(args: Vec<String>) -> Config {
432432
target_cfgs: OnceLock::new(),
433433
builtin_cfg_names: OnceLock::new(),
434434

435-
nocapture: matches.opt_present("no-capture"),
435+
capture: !matches.opt_present("no-capture"),
436436

437437
git_repository: matches.opt_str("git-repository").unwrap(),
438438
nightly_branch: matches.opt_str("nightly-branch").unwrap(),

0 commit comments

Comments
 (0)