rust-cli / rexpect

.github/workflows/ci.yml
https://docs.rs/rexpect
MIT License
328 stars 56 forks source link

replwrap style run function #69

Open jcaplan opened 2 years ago

jcaplan commented 2 years ago

It would also be nice to borrow from replwrap and have a method to fold these three lines into one:

    p.send_line("hostname")?;
    let hostname = p.read_line()?;
    p.wait_for_prompt()?; // go sure `hostname` is really done

becomes something like

    let output = p.run("hostname")?;

other things that would help missing from pexpect that I've had to add for run(): take multiple commands to pass a script that you read from the host filesystem either as a vec or by splitting on newlines, sanitizing carriage returns, stripping out colour

jcaplan commented 2 years ago

another thing for a clean repl that's really helpful in driving embedded testing is nice color coded logging on the host. being able to assign each repl a name and getting something back like:

dut> ifconfig
dut<  insert_ifconfig_output_here

where dut< and dut> are coloured differently. setup something like:

    let mut p = spawn_ssh(ip_addr, username, password)?
                .set_name('dut')
                .repl_log();
    let output = p.run("ifconfig")?;

works great if you're doing tests with multiple targets and/or also running stuff in a terminal on the host machine.

jcaplan commented 2 years ago

without colour/other control character stripping I get a test failure because my bash prompt pollutes the terminal. This is a problem people likely face with pexpect all the time (I know I have) and would be nice if there was a built-in way to deal with it.

test session::tests::test_bash ... FAILED                                              
test session::tests::test_kill_timeout ... ok                                          
test session::tests::test_bash_control_chars ... ok                                    

failures:                                                                              

---- session::tests::test_bash stdout ----                                             
thread 'session::tests::test_bash' panicked at 'assertion failed: `(left == right)`    
  left: `"/tmp\r\n"`,                                                                  
 right: `"\u{1b}[?2004l\r/tmp\r\n\u{1b}[?2004h"`', src/session.rs:540:13               
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace