Skip to content

Commit 3485d90

Browse files
committed
auto merge of #13706 : alexcrichton/rust/test-nocapture, r=brson
A new flag to the test runner, --nocapture, can be passed to instruct that the output of tests should not be captured by default. The behavior can also be triggered via a RUST_TEST_NOCAPTURE environment variable being set. Closes #13374
2 parents 22e4e6a + 65f68dc commit 3485d90

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

src/compiletest/compiletest.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,8 @@ pub fn test_opts(config: &config) -> test::TestOpts {
267267
ratchet_metrics: config.ratchet_metrics.clone(),
268268
ratchet_noise_percent: config.ratchet_noise_percent.clone(),
269269
save_metrics: config.save_metrics.clone(),
270-
test_shard: config.test_shard.clone()
270+
test_shard: config.test_shard.clone(),
271+
nocapture: false,
271272
}
272273
}
273274

src/compiletest/runtest.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,7 @@ fn fatal(err: ~str) -> ! { error(err); fail!(); }
955955
fn fatal_ProcRes(err: ~str, proc_res: &ProcRes) -> ! {
956956
print!("\n\
957957
error: {}\n\
958+
status: {}\n\
958959
command: {}\n\
959960
stdout:\n\
960961
------------------------------------------\n\
@@ -965,7 +966,8 @@ stderr:\n\
965966
{}\n\
966967
------------------------------------------\n\
967968
\n",
968-
err, proc_res.cmdline, proc_res.stdout, proc_res.stderr);
969+
err, proc_res.status, proc_res.cmdline, proc_res.stdout,
970+
proc_res.stderr);
969971
fail!();
970972
}
971973

src/libtest/lib.rs

+57-36
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,26 @@ pub struct TestOpts {
257257
pub ratchet_noise_percent: Option<f64>,
258258
pub save_metrics: Option<Path>,
259259
pub test_shard: Option<(uint,uint)>,
260-
pub logfile: Option<Path>
260+
pub logfile: Option<Path>,
261+
pub nocapture: bool,
262+
}
263+
264+
impl TestOpts {
265+
#[cfg(test)]
266+
fn new() -> TestOpts {
267+
TestOpts {
268+
filter: None,
269+
run_ignored: false,
270+
run_tests: false,
271+
run_benchmarks: false,
272+
ratchet_metrics: None,
273+
ratchet_noise_percent: None,
274+
save_metrics: None,
275+
test_shard: None,
276+
logfile: None,
277+
nocapture: false,
278+
}
279+
}
261280
}
262281

263282
/// Result of parsing the options.
@@ -280,7 +299,9 @@ fn optgroups() -> Vec<getopts::OptGroup> {
280299
getopts::optopt("", "logfile", "Write logs to the specified file instead \
281300
of stdout", "PATH"),
282301
getopts::optopt("", "test-shard", "run shard A, of B shards, worth of the testsuite",
283-
"A.B"))
302+
"A.B"),
303+
getopts::optflag("", "nocapture", "don't capture stdout/stderr of each \
304+
task, allow printing directly"))
284305
}
285306

286307
fn usage(binary: &str, helpstr: &str) {
@@ -295,6 +316,10 @@ have a substring match, only those tests are run.
295316
By default, all tests are run in parallel. This can be altered with the
296317
RUST_TEST_TASKS environment variable when running tests (set it to 1).
297318
319+
All tests have their standard output and standard error captured by default.
320+
This can be overridden with the --nocapture flag or the RUST_TEST_NOCAPTURE=1
321+
environment variable. Logging is not captured by default.
322+
298323
Test Attributes:
299324
300325
#[test] - Indicates a function is a test to be run. This function
@@ -351,6 +376,11 @@ pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
351376
let test_shard = matches.opt_str("test-shard");
352377
let test_shard = opt_shard(test_shard);
353378

379+
let mut nocapture = matches.opt_present("nocapture");
380+
if !nocapture {
381+
nocapture = os::getenv("RUST_TEST_NOCAPTURE").is_some();
382+
}
383+
354384
let test_opts = TestOpts {
355385
filter: filter,
356386
run_ignored: run_ignored,
@@ -360,7 +390,8 @@ pub fn parse_opts(args: &[~str]) -> Option<OptRes> {
360390
ratchet_noise_percent: ratchet_noise_percent,
361391
save_metrics: save_metrics,
362392
test_shard: test_shard,
363-
logfile: logfile
393+
logfile: logfile,
394+
nocapture: nocapture,
364395
};
365396

366397
Some(Ok(test_opts))
@@ -843,7 +874,7 @@ fn run_tests(opts: &TestOpts,
843874
// that hang forever.
844875
try!(callback(TeWait(test.desc.clone(), test.testfn.padding())));
845876
}
846-
run_test(!opts.run_tests, test, tx.clone());
877+
run_test(opts, !opts.run_tests, test, tx.clone());
847878
pending += 1;
848879
}
849880

@@ -859,7 +890,7 @@ fn run_tests(opts: &TestOpts,
859890
// (this includes metric fns)
860891
for b in filtered_benchs_and_metrics.move_iter() {
861892
try!(callback(TeWait(b.desc.clone(), b.testfn.padding())));
862-
run_test(!opts.run_benchmarks, b, tx.clone());
893+
run_test(opts, !opts.run_benchmarks, b, tx.clone());
863894
let (test, result, stdout) = rx.recv();
864895
try!(callback(TeResult(test, result, stdout)));
865896
}
@@ -941,7 +972,8 @@ pub fn filter_tests(
941972
}
942973
}
943974

944-
pub fn run_test(force_ignore: bool,
975+
pub fn run_test(opts: &TestOpts,
976+
force_ignore: bool,
945977
test: TestDescAndFn,
946978
monitor_ch: Sender<MonitorMsg>) {
947979

@@ -955,6 +987,7 @@ pub fn run_test(force_ignore: bool,
955987
#[allow(deprecated_owned_vector)]
956988
fn run_test_inner(desc: TestDesc,
957989
monitor_ch: Sender<MonitorMsg>,
990+
nocapture: bool,
958991
testfn: proc():Send) {
959992
spawn(proc() {
960993
let (tx, rx) = channel();
@@ -965,8 +998,12 @@ pub fn run_test(force_ignore: bool,
965998
DynTestName(ref name) => name.clone().into_maybe_owned(),
966999
StaticTestName(name) => name.into_maybe_owned(),
9671000
});
968-
task.opts.stdout = Some(~stdout as ~Writer:Send);
969-
task.opts.stderr = Some(~stderr as ~Writer:Send);
1001+
if nocapture {
1002+
drop((stdout, stderr));
1003+
} else {
1004+
task.opts.stdout = Some(~stdout as ~Writer:Send);
1005+
task.opts.stderr = Some(~stderr as ~Writer:Send);
1006+
}
9701007
let result_future = task.future_result();
9711008
task.spawn(testfn);
9721009

@@ -1000,8 +1037,9 @@ pub fn run_test(force_ignore: bool,
10001037
monitor_ch.send((desc, TrMetrics(mm), Vec::new()));
10011038
return;
10021039
}
1003-
DynTestFn(f) => run_test_inner(desc, monitor_ch, f),
1004-
StaticTestFn(f) => run_test_inner(desc, monitor_ch, proc() f())
1040+
DynTestFn(f) => run_test_inner(desc, monitor_ch, opts.nocapture, f),
1041+
StaticTestFn(f) => run_test_inner(desc, monitor_ch, opts.nocapture,
1042+
proc() f())
10051043
}
10061044
}
10071045

@@ -1320,7 +1358,7 @@ mod tests {
13201358
testfn: DynTestFn(proc() f()),
13211359
};
13221360
let (tx, rx) = channel();
1323-
run_test(false, desc, tx);
1361+
run_test(&TestOpts::new(), false, desc, tx);
13241362
let (_, res, _) = rx.recv();
13251363
assert!(res != TrOk);
13261364
}
@@ -1337,7 +1375,7 @@ mod tests {
13371375
testfn: DynTestFn(proc() f()),
13381376
};
13391377
let (tx, rx) = channel();
1340-
run_test(false, desc, tx);
1378+
run_test(&TestOpts::new(), false, desc, tx);
13411379
let (_, res, _) = rx.recv();
13421380
assert!(res == TrIgnored);
13431381
}
@@ -1354,7 +1392,7 @@ mod tests {
13541392
testfn: DynTestFn(proc() f()),
13551393
};
13561394
let (tx, rx) = channel();
1357-
run_test(false, desc, tx);
1395+
run_test(&TestOpts::new(), false, desc, tx);
13581396
let (_, res, _) = rx.recv();
13591397
assert!(res == TrOk);
13601398
}
@@ -1371,7 +1409,7 @@ mod tests {
13711409
testfn: DynTestFn(proc() f()),
13721410
};
13731411
let (tx, rx) = channel();
1374-
run_test(false, desc, tx);
1412+
run_test(&TestOpts::new(), false, desc, tx);
13751413
let (_, res, _) = rx.recv();
13761414
assert!(res == TrFailed);
13771415
}
@@ -1401,17 +1439,9 @@ mod tests {
14011439
// When we run ignored tests the test filter should filter out all the
14021440
// unignored tests and flip the ignore flag on the rest to false
14031441

1404-
let opts = TestOpts {
1405-
filter: None,
1406-
run_ignored: true,
1407-
logfile: None,
1408-
run_tests: true,
1409-
run_benchmarks: false,
1410-
ratchet_noise_percent: None,
1411-
ratchet_metrics: None,
1412-
save_metrics: None,
1413-
test_shard: None
1414-
};
1442+
let mut opts = TestOpts::new();
1443+
opts.run_tests = true;
1444+
opts.run_ignored = true;
14151445

14161446
let tests = vec!(
14171447
TestDescAndFn {
@@ -1439,17 +1469,8 @@ mod tests {
14391469

14401470
#[test]
14411471
pub fn sort_tests() {
1442-
let opts = TestOpts {
1443-
filter: None,
1444-
run_ignored: false,
1445-
logfile: None,
1446-
run_tests: true,
1447-
run_benchmarks: false,
1448-
ratchet_noise_percent: None,
1449-
ratchet_metrics: None,
1450-
save_metrics: None,
1451-
test_shard: None
1452-
};
1472+
let mut opts = TestOpts::new();
1473+
opts.run_tests = true;
14531474

14541475
let names =
14551476
vec!("sha1::test".to_owned(), "int::test_to_str".to_owned(), "int::test_pow".to_owned(),

0 commit comments

Comments
 (0)