Skip to content

Commit e9dd971

Browse files
committed
Capture stderr when running compile tests. Closes #755
1 parent 4527652 commit e9dd971

File tree

1 file changed

+74
-42
lines changed

1 file changed

+74
-42
lines changed

src/test/compiletest/compiletest.rs

Lines changed: 74 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -299,33 +299,6 @@ fn output_base_name(config: &config, testfile: &str) -> str {
299299
#fmt("%s%s.%s", base, filename, config.stage_id)
300300
}
301301

302-
#[cfg(target_os = "win32")]
303-
#[cfg(target_os = "linux")]
304-
fn dump_output(config: &config, testfile: &str, out: &str) {
305-
let outfile = make_out_name(config, testfile);
306-
let writer = io::file_writer(outfile, [io::create, io::truncate]);
307-
writer.write_str(out);
308-
maybe_dump_to_stdout(config, out);
309-
}
310-
311-
// FIXME (726): Can't use file_writer on mac
312-
#[cfg(target_os = "macos")]
313-
fn dump_output(config: &config, testfile: &str, out: &str) {
314-
maybe_dump_to_stdout(config, out);
315-
}
316-
317-
fn maybe_dump_to_stdout(config: &config, out: &str) {
318-
if config.verbose {
319-
io::stdout().write_line("------------------------------------------");
320-
io::stdout().write_line(out);
321-
io::stdout().write_line("------------------------------------------");
322-
}
323-
}
324-
325-
fn make_out_name(config: &config, testfile: &str) -> str {
326-
output_base_name(config, testfile) + ".out"
327-
}
328-
329302
fn logv(config: &config, s: &str) {
330303
log s;
331304
if config.verbose { io::stdout().write_line(s); }
@@ -472,7 +445,7 @@ mod runtest {
472445

473446
let next_err_idx = 0u;
474447
let next_err_pat = props.error_patterns.(next_err_idx);
475-
for line: str in str::split(procres.out, '\n' as u8) {
448+
for line: str in str::split(procres.stdout, '\n' as u8) {
476449
if str::find(line, next_err_pat) > 0 {
477450
log #fmt("found error pattern %s", next_err_pat);
478451
next_err_idx += 1u;
@@ -500,7 +473,7 @@ mod runtest {
500473

501474
type procargs = {prog: str, args: vec[str]};
502475

503-
type procres = {status: int, out: str, cmdline: str};
476+
type procres = {status: int, stdout: str, stderr: str, cmdline: str};
504477

505478
fn compile_test(cx: &cx, props: &test_props, testfile: &str) -> procres {
506479
compose_and_run(cx, testfile, bind make_compile_args(_, props, _),
@@ -554,8 +527,52 @@ mod runtest {
554527
cmdline
555528
};
556529
let res = procsrv::run(cx.procsrv, lib_path, prog, args);
557-
dump_output(cx.config, testfile, res.out);
558-
ret {status: res.status, out: res.out, cmdline: cmdline};
530+
dump_output(cx.config, testfile, res.out, res.err);
531+
ret {status: res.status, stdout: res.out,
532+
stderr: res.err, cmdline: cmdline};
533+
}
534+
535+
fn dump_output(config: &config, testfile: &str,
536+
out: &str, err: &str) {
537+
dump_output_file(config, testfile, out, "out");
538+
dump_output_file(config, testfile, err, "err");
539+
maybe_dump_to_stdout(config, out, err);
540+
}
541+
542+
#[cfg(target_os = "win32")]
543+
#[cfg(target_os = "linux")]
544+
fn dump_output_file(config: &config, testfile: &str,
545+
out: &str, extension: &str) {
546+
let outfile = make_out_name(config, testfile, extension);
547+
let writer = io::file_writer(outfile, [io::create, io::truncate]);
548+
writer.write_str(out);
549+
}
550+
551+
// FIXME (726): Can't use file_writer on mac
552+
#[cfg(target_os = "macos")]
553+
fn dump_output_file(config: &config, testfile: &str,
554+
out: &str, extension: &str) {
555+
}
556+
557+
fn make_out_name(config: &config, testfile: &str,
558+
extension: &str) -> str {
559+
output_base_name(config, testfile) + "." + extension
560+
}
561+
562+
fn maybe_dump_to_stdout(config: &config,
563+
out: &str, err: &str) {
564+
if config.verbose {
565+
let sep1 = #fmt("-%s-----------------------------------",
566+
"stdout");
567+
let sep2 = #fmt("-%s-----------------------------------",
568+
"stderr");
569+
let sep3 = "------------------------------------------";
570+
io::stdout().write_line(sep1);
571+
io::stdout().write_line(out);
572+
io::stdout().write_line(sep2);
573+
io::stdout().write_line(err);
574+
io::stdout().write_line(sep3);
575+
}
559576
}
560577

561578
fn error(err: &str) { io::stdout().write_line(#fmt("\nerror: %s", err)); }
@@ -567,12 +584,16 @@ mod runtest {
567584
#fmt("\n\
568585
error: %s\n\
569586
command: %s\n\
570-
output:\n\
587+
stdout:\n\
588+
------------------------------------------\n\
589+
%s\n\
590+
------------------------------------------\n\
591+
stderr:\n\
571592
------------------------------------------\n\
572593
%s\n\
573594
------------------------------------------\n\
574595
\n",
575-
err, procres.cmdline, procres.out);
596+
err, procres.cmdline, procres.stdout, procres.stderr);
576597
io::stdout().write_str(msg);
577598
fail;
578599
}
@@ -599,7 +620,7 @@ mod procsrv {
599620

600621
tag request { exec(str, str, vec[str], chan[response]); stop; }
601622

602-
type response = {pid: int, outfd: int};
623+
type response = {pid: int, outfd: int, errfd: int};
603624

604625
fn mk() -> handle {
605626
let setupport = port();
@@ -629,22 +650,28 @@ mod procsrv {
629650
}
630651

631652
fn run(handle: &handle, lib_path: &str, prog: &str, args: &vec[str]) ->
632-
{status: int, out: str} {
653+
{status: int, out: str, err: str} {
633654
let p = port[response]();
634655
let ch = chan(p);
635656
task::send(handle.chan, exec(lib_path, prog, args, ch));
636-
637657
let resp = task::recv(p);
658+
let output = readclose(resp.outfd);
659+
let errput = readclose(resp.errfd);
660+
let status = os::waitpid(resp.pid);
661+
ret {status: status, out: output, err: errput};
662+
}
663+
664+
fn readclose(fd: int) -> str {
638665
// Copied from run::program_output
639-
let outfile = os::fd_FILE(resp.outfd);
640-
let reader = io::new_reader(io::FILE_buf_reader(outfile, false));
666+
let file = os::fd_FILE(fd);
667+
let reader = io::new_reader(io::FILE_buf_reader(file, false));
641668
let buf = "";
642669
while !reader.eof() {
643670
let bytes = reader.read_bytes(4096u);
644671
buf += str::unsafe_from_bytes(bytes);
645672
}
646-
os::libc::fclose(outfile);
647-
ret {status: os::waitpid(resp.pid), out: buf};
673+
os::libc::fclose(file);
674+
ret buf;
648675
}
649676

650677
fn worker(p: port[request]) {
@@ -654,15 +681,20 @@ mod procsrv {
654681
// This is copied from run::start_program
655682
let pipe_in = os::pipe();
656683
let pipe_out = os::pipe();
684+
let pipe_err = os::pipe();
657685
let spawnproc =
658686
bind run::spawn_process(prog, args, pipe_in.in,
659-
pipe_out.out, 0);
687+
pipe_out.out, pipe_err.out);
660688
let pid = with_lib_path(lib_path, spawnproc);
661689
if pid == -1 { fail; }
662690
os::libc::close(pipe_in.in);
663691
os::libc::close(pipe_in.out);
664692
os::libc::close(pipe_out.out);
665-
task::send(respchan, {pid: pid, outfd: pipe_out.in});
693+
os::libc::close(pipe_err.out);
694+
task::send(respchan,
695+
{pid: pid,
696+
outfd: pipe_out.in,
697+
errfd: pipe_err.in});
666698
}
667699
stop. { ret; }
668700
}

0 commit comments

Comments
 (0)