Skip to content

Test runner displays colors even when running in parallel, closes #9388, #782 #11689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/libextra/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use std::io;
use std::io::File;
use std::io::Writer;
use std::io::stdio::StdWriter;
use std::io::LineBufferedWriter;
use std::task;
use std::to_str::ToStr;
use std::f64;
Expand Down Expand Up @@ -339,14 +340,13 @@ pub enum TestResult {
}

enum OutputLocation<T> {
Pretty(term::Terminal<T>),
Raw(T),
Pretty(term::Terminal<LineBufferedWriter<T>>),
Raw(LineBufferedWriter<T>),
}

struct ConsoleTestState<T> {
log_out: Option<File>,
out: OutputLocation<T>,
use_color: bool,
total: uint,
passed: uint,
failed: uint,
Expand All @@ -363,14 +363,13 @@ impl<T: Writer> ConsoleTestState<T> {
Some(ref path) => File::create(path),
None => None
};
let out = match term::Terminal::new(io::stdout()) {
Err(_) => Raw(io::stdout()),
let out = match term::Terminal::new(LineBufferedWriter::new(io::stdout())) {
Err(_) => Raw(LineBufferedWriter::new(io::stdout())),
Ok(t) => Pretty(t)
};
ConsoleTestState {
out: out,
log_out: log_out,
use_color: use_color(),
total: 0u,
passed: 0u,
failed: 0u,
Expand Down Expand Up @@ -423,13 +422,9 @@ impl<T: Writer> ConsoleTestState<T> {
color: term::color::Color) {
match self.out {
Pretty(ref mut term) => {
if self.use_color {
term.fg(color);
}
term.fg(color);
term.write(word.as_bytes());
if self.use_color {
term.reset();
}
term.reset();
}
Raw(ref mut stdout) => stdout.write(word.as_bytes())
}
Expand All @@ -442,6 +437,13 @@ impl<T: Writer> ConsoleTestState<T> {
}
}

pub fn flush(&mut self) {
match self.out {
Pretty(ref mut term) => term.flush(),
Raw(ref mut stdout) => stdout.flush()
}
}

pub fn write_run_start(&mut self, len: uint) {
self.total = len;
let noun = if len != 1 { &"tests" } else { &"test" };
Expand All @@ -451,6 +453,7 @@ impl<T: Writer> ConsoleTestState<T> {
pub fn write_test_start(&mut self, test: &TestDesc, align: NamePadding) {
let name = test.padded_name(self.max_name_len, align);
self.write_plain(format!("test {} ... ", name));
self.flush();
}

pub fn write_result(&mut self, result: &TestResult) {
Expand Down Expand Up @@ -673,7 +676,7 @@ pub fn run_tests_console(opts: &TestOpts,

#[test]
fn should_sort_failures_before_printing_them() {
use std::io::MemWriter;
use std::io::{LineBufferedWriter, MemWriter};
use std::str;

let test_a = TestDesc {
Expand All @@ -690,8 +693,7 @@ fn should_sort_failures_before_printing_them() {

let mut st = ConsoleTestState {
log_out: None,
out: Raw(MemWriter::new()),
use_color: false,
out: Raw(LineBufferedWriter::new(MemWriter::new())),
total: 0u,
passed: 0u,
failed: 0u,
Expand All @@ -704,7 +706,7 @@ fn should_sort_failures_before_printing_them() {

st.write_failures();
let s = match st.out {
Raw(ref m) => str::from_utf8(m.get_ref()),
Raw(ref m) => str::from_utf8(m.get_ref().get_ref()),
Pretty(_) => unreachable!()
};

Expand All @@ -713,8 +715,6 @@ fn should_sort_failures_before_printing_them() {
assert!(apos < bpos);
}

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

#[deriving(Clone)]
enum TestEvent {
TeFiltered(~[TestDesc]),
Expand Down