Skip to content

Commit ee2a888

Browse files
committed
extra: Capture stdout/stderr of tests by default
When tests fail, their stdout and stderr is printed as part of the summary, but this helps suppress failure messages from #[should_fail] tests and generally clean up the output of the test runner.
1 parent 665555d commit ee2a888

File tree

6 files changed

+78
-34
lines changed

6 files changed

+78
-34
lines changed

src/compiletest/header.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub struct TestProps {
3030
check_lines: ~[~str],
3131
// Flag to force a crate to be built with the host architecture
3232
force_host: bool,
33+
// Check stdout for error-pattern output as well as stderr
34+
check_stdout: bool,
3335
}
3436

3537
// Load any test directives embedded in the file
@@ -42,6 +44,7 @@ pub fn load_props(testfile: &Path) -> TestProps {
4244
let mut debugger_cmds = ~[];
4345
let mut check_lines = ~[];
4446
let mut force_host = false;
47+
let mut check_stdout = false;
4548
iter_header(testfile, |ln| {
4649
match parse_error_pattern(ln) {
4750
Some(ep) => error_patterns.push(ep),
@@ -60,6 +63,10 @@ pub fn load_props(testfile: &Path) -> TestProps {
6063
force_host = parse_force_host(ln);
6164
}
6265

66+
if !check_stdout {
67+
check_stdout = parse_check_stdout(ln);
68+
}
69+
6370
match parse_aux_build(ln) {
6471
Some(ab) => { aux_builds.push(ab); }
6572
None => {}
@@ -91,6 +98,7 @@ pub fn load_props(testfile: &Path) -> TestProps {
9198
debugger_cmds: debugger_cmds,
9299
check_lines: check_lines,
93100
force_host: force_host,
101+
check_stdout: check_stdout,
94102
};
95103
}
96104

@@ -155,6 +163,10 @@ fn parse_force_host(line: &str) -> bool {
155163
parse_name_directive(line, "force-host")
156164
}
157165

166+
fn parse_check_stdout(line: &str) -> bool {
167+
parse_name_directive(line, "check-stdout")
168+
}
169+
158170
fn parse_exec_env(line: &str) -> Option<(~str, ~str)> {
159171
parse_name_value_directive(line, ~"exec-env").map(|nv| {
160172
// nv is either FOO or FOO=BAR

src/compiletest/runtest.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,12 @@ fn check_error_patterns(props: &TestProps,
452452
let mut next_err_idx = 0u;
453453
let mut next_err_pat = &props.error_patterns[next_err_idx];
454454
let mut done = false;
455-
for line in ProcRes.stderr.lines() {
455+
let output_to_check = if props.check_stdout {
456+
ProcRes.stdout + ProcRes.stderr
457+
} else {
458+
ProcRes.stderr.clone()
459+
};
460+
for line in output_to_check.lines() {
456461
if line.contains(*next_err_pat) {
457462
debug!("found error pattern {}", *next_err_pat);
458463
next_err_idx += 1u;

src/libextra/test.rs

Lines changed: 52 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@ use stats;
2626
use time::precise_time_ns;
2727
use collections::TreeMap;
2828

29-
use std::clone::Clone;
3029
use std::cmp;
3130
use std::io;
32-
use std::io::File;
33-
use std::io::Writer;
31+
use std::io::{File, PortReader, ChanWriter};
3432
use std::io::stdio::StdWriter;
33+
use std::str;
3534
use std::task;
3635
use std::to_str::ToStr;
3736
use std::f64;
@@ -358,7 +357,7 @@ struct ConsoleTestState<T> {
358357
ignored: uint,
359358
measured: uint,
360359
metrics: MetricMap,
361-
failures: ~[TestDesc],
360+
failures: ~[(TestDesc, ~[u8])],
362361
max_name_len: uint, // number of columns to fill when aligning names
363362
}
364363

@@ -498,9 +497,23 @@ impl<T: Writer> ConsoleTestState<T> {
498497
pub fn write_failures(&mut self) -> io::IoResult<()> {
499498
if_ok!(self.write_plain("\nfailures:\n"));
500499
let mut failures = ~[];
501-
for f in self.failures.iter() {
500+
let mut fail_out = ~"";
501+
for &(ref f, ref stdout) in self.failures.iter() {
502502
failures.push(f.name.to_str());
503+
if stdout.len() > 0 {
504+
fail_out.push_str(format!("---- {} stdout ----\n\t",
505+
f.name.to_str()));
506+
let output = str::from_utf8_lossy(*stdout);
507+
fail_out.push_str(output.as_slice().replace("\n", "\n\t"));
508+
fail_out.push_str("\n");
509+
}
510+
}
511+
if fail_out.len() > 0 {
512+
if_ok!(self.write_plain("\n"));
513+
if_ok!(self.write_plain(fail_out));
503514
}
515+
516+
if_ok!(self.write_plain("\nfailures:\n"));
504517
failures.sort();
505518
for name in failures.iter() {
506519
if_ok!(self.write_plain(format!(" {}\n", name.to_str())));
@@ -632,7 +645,7 @@ pub fn run_tests_console(opts: &TestOpts,
632645
match (*event).clone() {
633646
TeFiltered(ref filtered_tests) => st.write_run_start(filtered_tests.len()),
634647
TeWait(ref test, padding) => st.write_test_start(test, padding),
635-
TeResult(test, result) => {
648+
TeResult(test, result, stdout) => {
636649
if_ok!(st.write_log(&test, &result));
637650
if_ok!(st.write_result(&result));
638651
match result {
@@ -655,7 +668,7 @@ pub fn run_tests_console(opts: &TestOpts,
655668
}
656669
TrFailed => {
657670
st.failed += 1;
658-
st.failures.push(test);
671+
st.failures.push((test, stdout));
659672
}
660673
}
661674
Ok(())
@@ -717,17 +730,17 @@ fn should_sort_failures_before_printing_them() {
717730
measured: 0u,
718731
max_name_len: 10u,
719732
metrics: MetricMap::new(),
720-
failures: ~[test_b, test_a]
733+
failures: ~[(test_b, ~[]), (test_a, ~[])]
721734
};
722735

723736
st.write_failures().unwrap();
724737
let s = match st.out {
725-
Raw(ref m) => str::from_utf8(m.get_ref()).unwrap(),
738+
Raw(ref m) => str::from_utf8_lossy(m.get_ref()),
726739
Pretty(_) => unreachable!()
727740
};
728741

729-
let apos = s.find_str("a").unwrap();
730-
let bpos = s.find_str("b").unwrap();
742+
let apos = s.as_slice().find_str("a").unwrap();
743+
let bpos = s.as_slice().find_str("b").unwrap();
731744
assert!(apos < bpos);
732745
}
733746

@@ -737,11 +750,10 @@ fn use_color() -> bool { return get_concurrency() == 1; }
737750
enum TestEvent {
738751
TeFiltered(~[TestDesc]),
739752
TeWait(TestDesc, NamePadding),
740-
TeResult(TestDesc, TestResult),
753+
TeResult(TestDesc, TestResult, ~[u8] /* stdout */),
741754
}
742755

743-
/// The message sent to the test monitor from the individual runners.
744-
pub type MonitorMsg = (TestDesc, TestResult);
756+
pub type MonitorMsg = (TestDesc, TestResult, ~[u8] /* stdout */);
745757

746758
fn run_tests(opts: &TestOpts,
747759
tests: ~[TestDescAndFn],
@@ -783,11 +795,11 @@ fn run_tests(opts: &TestOpts,
783795
pending += 1;
784796
}
785797

786-
let (desc, result) = p.recv();
798+
let (desc, result, stdout) = p.recv();
787799
if concurrency != 1 {
788800
if_ok!(callback(TeWait(desc.clone(), PadNone)));
789801
}
790-
if_ok!(callback(TeResult(desc, result)));
802+
if_ok!(callback(TeResult(desc, result, stdout)));
791803
pending -= 1;
792804
}
793805

@@ -796,8 +808,8 @@ fn run_tests(opts: &TestOpts,
796808
for b in filtered_benchs_and_metrics.move_iter() {
797809
if_ok!(callback(TeWait(b.desc.clone(), b.testfn.padding())));
798810
run_test(!opts.run_benchmarks, b, ch.clone());
799-
let (test, result) = p.recv();
800-
if_ok!(callback(TeResult(test, result)));
811+
let (test, result, stdout) = p.recv();
812+
if_ok!(callback(TeResult(test, result, stdout)));
801813
}
802814
Ok(())
803815
}
@@ -884,7 +896,7 @@ pub fn run_test(force_ignore: bool,
884896
let TestDescAndFn {desc, testfn} = test;
885897

886898
if force_ignore || desc.ignore {
887-
monitor_ch.send((desc, TrIgnored));
899+
monitor_ch.send((desc, TrIgnored, ~[]));
888900
return;
889901
}
890902

@@ -893,40 +905,47 @@ pub fn run_test(force_ignore: bool,
893905
testfn: proc()) {
894906
spawn(proc() {
895907
let mut task = task::task();
896-
task.name(match desc.name {
897-
DynTestName(ref name) => name.to_owned().into_maybe_owned(),
898-
StaticTestName(name) => name.into_maybe_owned()
899-
});
908+
let (p, c) = Chan::new();
909+
let mut reader = PortReader::new(p);
910+
let stdout = ChanWriter::new(c.clone());
911+
let stderr = ChanWriter::new(c);
912+
match desc.name {
913+
DynTestName(ref name) => task.name(name.clone()),
914+
StaticTestName(name) => task.name(name),
915+
}
916+
task.opts.stdout = Some(~stdout as ~Writer);
917+
task.opts.stderr = Some(~stderr as ~Writer);
900918
let result_future = task.future_result();
901919
task.spawn(testfn);
902920

921+
let stdout = reader.read_to_end().unwrap();
903922
let task_result = result_future.recv();
904923
let test_result = calc_result(&desc, task_result.is_ok());
905-
monitor_ch.send((desc.clone(), test_result));
906-
});
924+
monitor_ch.send((desc.clone(), test_result, stdout));
925+
})
907926
}
908927

909928
match testfn {
910929
DynBenchFn(bencher) => {
911930
let bs = ::test::bench::benchmark(|harness| bencher.run(harness));
912-
monitor_ch.send((desc, TrBench(bs)));
931+
monitor_ch.send((desc, TrBench(bs), ~[]));
913932
return;
914933
}
915934
StaticBenchFn(benchfn) => {
916935
let bs = ::test::bench::benchmark(|harness| benchfn(harness));
917-
monitor_ch.send((desc, TrBench(bs)));
936+
monitor_ch.send((desc, TrBench(bs), ~[]));
918937
return;
919938
}
920939
DynMetricFn(f) => {
921940
let mut mm = MetricMap::new();
922941
f(&mut mm);
923-
monitor_ch.send((desc, TrMetrics(mm)));
942+
monitor_ch.send((desc, TrMetrics(mm), ~[]));
924943
return;
925944
}
926945
StaticMetricFn(f) => {
927946
let mut mm = MetricMap::new();
928947
f(&mut mm);
929-
monitor_ch.send((desc, TrMetrics(mm)));
948+
monitor_ch.send((desc, TrMetrics(mm), ~[]));
930949
return;
931950
}
932951
DynTestFn(f) => run_test_inner(desc, monitor_ch, f),
@@ -1264,7 +1283,7 @@ mod tests {
12641283
};
12651284
let (p, ch) = Chan::new();
12661285
run_test(false, desc, ch);
1267-
let (_, res) = p.recv();
1286+
let (_, res, _) = p.recv();
12681287
assert!(res != TrOk);
12691288
}
12701289
@@ -1281,7 +1300,7 @@ mod tests {
12811300
};
12821301
let (p, ch) = Chan::new();
12831302
run_test(false, desc, ch);
1284-
let (_, res) = p.recv();
1303+
let (_, res, _) = p.recv();
12851304
assert_eq!(res, TrIgnored);
12861305
}
12871306
@@ -1298,7 +1317,7 @@ mod tests {
12981317
};
12991318
let (p, ch) = Chan::new();
13001319
run_test(false, desc, ch);
1301-
let (_, res) = p.recv();
1320+
let (_, res, _) = p.recv();
13021321
assert_eq!(res, TrOk);
13031322
}
13041323
@@ -1315,7 +1334,7 @@ mod tests {
13151334
};
13161335
let (p, ch) = Chan::new();
13171336
run_test(false, desc, ch);
1318-
let (_, res) = p.recv();
1337+
let (_, res, _) = p.recv();
13191338
assert_eq!(res, TrFailed);
13201339
}
13211340

src/libstd/io/comm_adapters.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ impl ChanWriter {
9696
}
9797
}
9898

99+
impl Clone for ChanWriter {
100+
fn clone(&self) -> ChanWriter {
101+
ChanWriter { chan: self.chan.clone() }
102+
}
103+
}
104+
99105
impl Writer for ChanWriter {
100106
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
101107
if !self.chan.try_send(buf.to_owned()) {

src/test/run-fail/run-unexported-tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// error-pattern:runned an unexported test
1212
// compile-flags:--test
13+
// check-stdout
1314

1415
extern mod extra;
1516

src/test/run-fail/test-fail.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// check-stdout
1112
// error-pattern:task 'test_foo' failed at
1213
// compile-flags: --test
1314

0 commit comments

Comments
 (0)