-
Notifications
You must be signed in to change notification settings - Fork 178
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
How to test an app that uses rustyline? #652
Comments
Not tested: https://crates.io/crates/pty-process |
I'll try that one out tomorrow. |
You could call functions inside the use std::io::Write;
use rustyline::error::ReadlineError;
use rustyline::Editor;
fn handle_line<W>(mut dest: W, line: String) where W: Write {
write!(&mut dest, "Line: {line}\n").unwrap();
if line == "clear" {
write!(&mut dest, "{esc}[2J{esc}[1;1H", esc = 27 as char).unwrap();
dest.flush().unwrap();
}
}
#[test]
fn line_is_handled() {
let mut dest = Vec::new();
handle_line(&mut dest, "something".to_string());
let actual = String::from_utf8(dest).unwrap();
let expected = "Line: something\n".to_string();
assert_eq!(actual, expected);
}
fn main() {
let mut line_editor = Editor::<()>::new().unwrap();
let mut counter = 0;
let mut dest = std::io::stdout();
loop {
let readline = line_editor.readline("> ");
match readline {
Ok(line) => {
line_editor.add_history_entry(line.clone());
counter += 1;
handle_line(&mut dest, line);
}
Err(ReadlineError::Interrupted) => {
println!("Ctrl+C");
break;
}
Err(ReadlineError::Eof) => {
println!("Ctrl+D | Lines counted: {}", counter);
break;
}
Err(err) => {
println!("Error: {:?}", err);
break;
}
}
}
} This means that the logic inside |
FWIW, here's a working example of end-to-end testing (facilitated via the |
I have been figuring out how to test the following basic CLI app that uses
rustyline
usingstd::process::Command
but I could not figure out how to pull it off right.The following is the code for CLI app:
And code for the test:
When I run the test with
cargo test -- --nocapture
, I only get the following output:I was expecting something like:
I believe #504 may be related to this problem. Nevertheless, how can I achieve the second output?
The text was updated successfully, but these errors were encountered: