Improve shell and add tests

This commit is contained in:
Yehuda Katz 2014-05-27 18:53:30 -07:00
parent 574829e9f7
commit d5a7bc33d2
2 changed files with 41 additions and 2 deletions

View File

@ -3,7 +3,6 @@ use term::{Terminal,color};
use term::color::Color;
use term::attr::Attr;
use std::io::IoResult;
use std::io::stdio::StdWriter;
pub struct ShellConfig {
pub color: bool,
@ -23,7 +22,7 @@ pub struct Shell<T> {
impl<T: Writer + Send> Shell<T> {
pub fn create(out: T, config: ShellConfig) -> Option<Shell<T>> {
if config.tty {
if config.tty && config.color {
let term: Option<term::TerminfoTerminal<T>> = Terminal::new(out);
term.map(|t| Shell { terminal: Color(box t as Box<Terminal<T>>), config: config })
} else {

40
tests/test_shell.rs Normal file
View File

@ -0,0 +1,40 @@
use support::{ResultTest,Tap,shell_writes};
use hamcrest::{assert_that};
use std::io::{MemWriter,IoResult};
use std::str::from_utf8_lossy;
use cargo::core::shell::{Shell,ShellConfig};
use term::{Terminal,TerminfoTerminal,color};
fn setup() {
}
test!(non_tty {
Shell::create(MemWriter::new(), ShellConfig { color: true, verbose: true, tty: false }).assert().tap(|shell| {
shell.say("Hey Alex", color::RED).assert();
assert_that(shell, shell_writes("Hey Alex\n"));
});
})
test!(color_explicitly_disabled {
Shell::create(MemWriter::new(), ShellConfig { color: false, verbose: true, tty: true }).assert().tap(|shell| {
shell.say("Hey Alex", color::RED).assert();
assert_that(shell, shell_writes("Hey Alex\n"));
});
})
test!(colored_shell {
Shell::create(MemWriter::new(), ShellConfig { color: true, verbose: true, tty: true }).assert().tap(|shell| {
shell.say("Hey Alex", color::RED).assert();
assert_that(shell, shell_writes(colored_output("Hey Alex\n", color::RED).assert()));
});
})
fn colored_output<S: Str>(string: S, color: color::Color) -> IoResult<String> {
let mut term: TerminfoTerminal<MemWriter> = Terminal::new(MemWriter::new()).assert();
try!(term.reset());
try!(term.fg(color));
try!(term.write_str(string.as_slice()));
try!(term.reset());
try!(term.flush());
Ok(from_utf8_lossy(term.get_ref().get_ref()).to_str())
}