Skip to content

Commit

Permalink
libstd: Colorify test results when run in parallel
Browse files Browse the repository at this point in the history
Closes #782
  • Loading branch information
lht committed Apr 12, 2012
1 parent 828d067 commit 7b3cb05
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions src/libstd/test.rs
Expand Up @@ -91,7 +91,6 @@ enum test_result { tr_ok, tr_failed, tr_ignored, }
type console_test_state =
@{out: io::writer,
log_out: option<io::writer>,
use_color: bool,
mut total: uint,
mut passed: uint,
mut failed: uint,
Expand All @@ -117,7 +116,6 @@ fn run_tests_console(opts: test_opts,
let st =
@{out: io::stdout(),
log_out: log_out,
use_color: use_color(),
mut total: 0u,
mut passed: 0u,
mut failed: 0u,
Expand Down Expand Up @@ -150,18 +148,18 @@ fn run_tests_console(opts: test_opts,
alt result {
tr_ok {
st.passed += 1u;
write_ok(st.out, st.use_color);
write_ok(st.out);
st.out.write_line("");
}
tr_failed {
st.failed += 1u;
write_failed(st.out, st.use_color);
write_failed(st.out);
st.out.write_line("");
st.failures += [test];
}
tr_ignored {
st.ignored += 1u;
write_ignored(st.out, st.use_color);
write_ignored(st.out);
st.out.write_line("");
}
}
Expand All @@ -180,11 +178,9 @@ fn run_tests_console(opts: test_opts,
}

st.out.write_str(#fmt["\nresult: "]);
if success {
write_ok(st.out, true);
} else { write_failed(st.out, true); }
st.out.write_str(#fmt[". %u passed; %u failed; %u ignored\n\n", st.passed,
st.failed, st.ignored]);
if success { write_ok(st.out); } else { write_failed(st.out); }
st.out.write_str(#fmt[". %u passed; %u failed; %u ignored\n\n",
st.passed, st.failed, st.ignored]);

ret success;

Expand All @@ -197,24 +193,24 @@ fn run_tests_console(opts: test_opts,
}, test.name));
}

fn write_ok(out: io::writer, use_color: bool) {
write_pretty(out, "ok", term::color_green, use_color);
fn write_ok(out: io::writer) {
write_pretty(out, "ok", term::color_green);
}

fn write_failed(out: io::writer, use_color: bool) {
write_pretty(out, "FAILED", term::color_red, use_color);
fn write_failed(out: io::writer) {
write_pretty(out, "FAILED", term::color_red);
}

fn write_ignored(out: io::writer, use_color: bool) {
write_pretty(out, "ignored", term::color_yellow, use_color);
fn write_ignored(out: io::writer) {
write_pretty(out, "ignored", term::color_yellow);
}

fn write_pretty(out: io::writer, word: str, color: u8, use_color: bool) {
if use_color && term::color_supported() {
fn write_pretty(out: io::writer, word: str, color: u8) {
if term::color_supported() {
term::fg(out, color);
}
out.write_str(word);
if use_color && term::color_supported() {
if term::color_supported() {
term::reset(out);
}
}
Expand Down Expand Up @@ -251,7 +247,6 @@ fn should_sort_failures_before_printing_them() {
let st =
@{out: writer,
log_out: option::none,
use_color: false,
mut total: 0u,
mut passed: 0u,
mut failed: 0u,
Expand All @@ -267,8 +262,6 @@ fn should_sort_failures_before_printing_them() {
assert apos < bpos;
}

fn use_color() -> bool { ret get_concurrency() == 1u; }

enum testevent {
te_filtered([test_desc]),
te_wait(test_desc),
Expand Down

1 comment on commit 7b3cb05

@brson
Copy link
Contributor

@brson brson commented on 7b3cb05 Apr 12, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't end up solving the problem, which I probably didn't explain well enough. So the problem with printing colors in parallel was not that the test runner itself is trying to print over itself, but that there are tests which expect to print to the console, and they are running in parallel. So if you, for example, run make check-stage1-std on a multi-core machine, you will probably see a bunch of random colored output.

Please sign in to comment.