Skip to content

Commit bac6020

Browse files
committed
Make test output capture (using unstable set_stdio) optional.
1 parent 24e7c3a commit bac6020

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ license = "MIT OR Apache-2.0"
66

77
[features]
88
asm_black_box = []
9+
capture = []
910

1011
[dependencies]
1112
libc = "0.2"

src/lib.rs

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#![cfg_attr(not(stage0), deny(warnings))]
3434

3535
#![cfg_attr(feature = "asm_black_box", feature(asm))]
36-
#![feature(set_stdio)]
36+
#![cfg_attr(feature = "capture", feature(set_stdio))]
3737

3838
extern crate getopts;
3939
extern crate rustc_serialize;
@@ -329,10 +329,12 @@ fn options() -> getopts::Options {
329329
.optflag("", "bench", "Run benchmarks instead of tests")
330330
.optflag("h", "help", "Display this message (longer with --help)")
331331
.optopt("", "logfile", "Write logs to the specified file instead \
332-
of stdout", "PATH")
333-
.optflag("", "nocapture", "don't capture stdout/stderr of each \
334-
task, allow printing directly")
335-
.optopt("", "color", "Configure coloring of output:
332+
of stdout", "PATH");
333+
if cfg!(feature = "capture") {
334+
opts.optflag("", "nocapture", "don't capture stdout/stderr of each \
335+
task, allow printing directly");
336+
}
337+
opts.optopt("", "color", "Configure coloring of output:
336338
auto = colorize if stdout is a tty and tests are run on serially (default);
337339
always = always colorize output;
338340
never = never colorize output;", "auto|always|never");
@@ -348,11 +350,17 @@ tests whose names contain the filter are run.
348350
349351
By default, all tests are run in parallel. This can be altered with the
350352
RUST_TEST_THREADS environment variable when running tests (set it to 1).
353+
"#, usage = options().usage(&message));
351354

355+
if cfg!(feature = "capture") {
356+
println!(r#"
352357
All tests have their standard output and standard error captured by default.
353358
This can be overridden with the --nocapture flag or the RUST_TEST_NOCAPTURE=1
354359
environment variable. Logging is not captured by default.
360+
"#);
361+
}
355362

363+
println!(r#"
356364
Test Attributes:
357365
358366
#[test] - Indicates a function is a test to be run. This function
@@ -366,8 +374,7 @@ Test Attributes:
366374
#[ignore] - When applied to a function which is already attributed as a
367375
test, then the test runner will ignore these tests during
368376
normal test runs. Running with --ignored will run these
369-
tests."#,
370-
usage = options().usage(&message));
377+
tests."#);
371378
}
372379

373380
// Parses command line arguments into test options
@@ -1067,16 +1074,6 @@ pub fn run_test(opts: &TestOpts,
10671074
monitor_ch: Sender<MonitorMsg>,
10681075
nocapture: bool,
10691076
mut testfn: Box<FnMut() + Send>) {
1070-
struct Sink(Arc<Mutex<Vec<u8>>>);
1071-
impl Write for Sink {
1072-
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
1073-
Write::write(&mut *self.0.lock().unwrap(), data)
1074-
}
1075-
fn flush(&mut self) -> io::Result<()> {
1076-
Ok(())
1077-
}
1078-
}
1079-
10801077
thread::spawn(move || {
10811078
let data = Arc::new(Mutex::new(Vec::new()));
10821079
let data2 = data.clone();
@@ -1085,10 +1082,27 @@ pub fn run_test(opts: &TestOpts,
10851082
StaticTestName(name) => name.to_owned(),
10861083
});
10871084

1085+
#[cfg(feature = "capture")]
1086+
fn capture(data2: Arc<Mutex<Vec<u8>>>) {
1087+
struct Sink(Arc<Mutex<Vec<u8>>>);
1088+
impl Write for Sink {
1089+
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
1090+
Write::write(&mut *self.0.lock().unwrap(), data)
1091+
}
1092+
fn flush(&mut self) -> io::Result<()> {
1093+
Ok(())
1094+
}
1095+
}
1096+
1097+
io::set_print(Box::new(Sink(data2.clone())));
1098+
io::set_panic(Box::new(Sink(data2)));
1099+
}
1100+
#[cfg(not(feature = "capture"))]
1101+
fn capture(_data2: Arc<Mutex<Vec<u8>>>) {}
1102+
10881103
let result_guard = cfg.spawn(move || {
10891104
if !nocapture {
1090-
io::set_print(Box::new(Sink(data2.clone())));
1091-
io::set_panic(Box::new(Sink(data2)));
1105+
capture(data2)
10921106
}
10931107
testfn()
10941108
})

0 commit comments

Comments
 (0)