cargo/tests/test_shell.rs

60 lines
2 KiB
Rust
Raw Normal View History

2014-05-28 01:53:30 +00:00
use support::{ResultTest,Tap,shell_writes};
use hamcrest::{assert_that};
2014-06-22 01:53:07 +00:00
use std::io::{MemWriter, BufWriter, IoResult};
2014-05-28 01:53:30 +00:00
use cargo::core::shell::{Shell,ShellConfig};
use term::{Terminal,TerminfoTerminal,color};
fn setup() {
}
2014-06-22 01:53:07 +00:00
fn writer(buf: &mut [u8]) -> Box<Writer> {
box BufWriter::new(buf) as Box<Writer>
}
2014-05-28 01:53:30 +00:00
test!(non_tty {
let config = ShellConfig { color: true, verbose: true, tty: false };
2014-06-22 01:53:07 +00:00
let mut buf: Vec<u8> = Vec::from_elem(9, 0 as u8);
Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| {
2014-05-28 01:53:30 +00:00
shell.say("Hey Alex", color::RED).assert();
2014-06-22 01:53:07 +00:00
assert_that(buf.as_slice(), shell_writes("Hey Alex\n"));
2014-05-28 01:53:30 +00:00
});
})
test!(color_explicitly_disabled {
let config = ShellConfig { color: false, verbose: true, tty: true };
2014-06-22 01:53:07 +00:00
let mut buf: Vec<u8> = Vec::from_elem(9, 0 as u8);
Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| {
2014-05-28 01:53:30 +00:00
shell.say("Hey Alex", color::RED).assert();
2014-06-22 01:53:07 +00:00
assert_that(buf.as_slice(), shell_writes("Hey Alex\n"));
2014-05-28 01:53:30 +00:00
});
})
test!(colored_shell {
let term: Option<TerminfoTerminal<MemWriter>> =
Terminal::new(MemWriter::new());
if term.is_none() { return }
let config = ShellConfig { color: true, verbose: true, tty: true };
2014-06-23 16:44:00 +00:00
let mut buf: Vec<u8> = Vec::from_elem(100, 0 as u8);
2014-06-22 01:53:07 +00:00
Shell::create(writer(buf.as_mut_slice()), config).tap(|shell| {
2014-05-28 01:53:30 +00:00
shell.say("Hey Alex", color::RED).assert();
2014-06-23 16:44:00 +00:00
let buf = buf.as_slice().slice_to(buf.iter().position(|a| *a == 0).unwrap());
assert_that(buf, shell_writes(colored_output("Hey Alex\n",
color::RED).assert()));
2014-05-28 01:53:30 +00:00
});
})
fn colored_output<S: Str>(string: S, color: color::Color) -> IoResult<String> {
let mut term: TerminfoTerminal<MemWriter> =
Terminal::new(MemWriter::new()).assert();
2014-05-28 01:53:30 +00:00
try!(term.reset());
try!(term.fg(color));
try!(term.write_str(string.as_slice()));
try!(term.reset());
try!(term.flush());
Ok(String::from_utf8_lossy(term.get_ref().get_ref()).to_string())
2014-05-28 01:53:30 +00:00
}