[][src]Struct rexpect::session::PtyReplSession

pub struct PtyReplSession {
    pub prompt: String,
    pub pty_session: PtySession,
    pub quit_command: Option<String>,
    pub echo_on: bool,
}

A repl session: e.g. bash or the python shell: You have a prompt where a user inputs commands and the shell executes it and writes some output

Fields

prompt: String

the prompt, used for wait_for_prompt, e.g. ">>> " for python

pty_session: PtySession

the pty_session you prepared before (initiating the shell, maybe set a custom prompt, etc.) see spawn_bash for an example

quit_command: Option<String>

if set, then the quit_command is called when this object is dropped you need to provide this if the shell you're testing is not killed by just sending SIGTERM

echo_on: bool

set this to true if the repl has echo on (i.e. sends user input to stdout) although echo is set off at pty fork (see PtyProcess::new) a few repls still seem to be able to send output. You may need to try with true first, and if tests fail set this to false.

Implementations

impl PtyReplSession[src]

pub fn wait_for_prompt(&mut self) -> Result<String>[src]

pub fn execute(&mut self, cmd: &str, ready_regex: &str) -> Result<()>[src]

Send cmd to repl and:

  1. wait for the cmd to be echoed (if echo_on == true)
  2. wait for the ready string being present

Q: Why can't I just do send_line and immediately continue? A: Executing a command in e.g. bash causes a fork. If the Unix kernel chooses the parent process (bash) to go first and the bash process sends e.g. Ctrl-C then the Ctrl-C goes to nirvana. The only way to prevent this situation is to wait for a ready string being present in the output.

Another safe way to tackle this problem is to use send_line() and wait_for_prompt()

Example:

use rexpect::spawn_bash;

let mut p = spawn_bash(Some(1000))?;
p.execute("cat <(echo ready) -", "ready")?;
p.send_line("hans")?;
p.exp_string("hans")?;

pub fn send_line(&mut self, line: &str) -> Result<usize>[src]

send line to repl (and flush output) and then, if echo_on=true wait for the input to appear. Return: number of bytes written

Trait Implementations

impl Deref for PtyReplSession[src]

type Target = PtySession

The resulting type after dereferencing.

impl DerefMut for PtyReplSession[src]

impl Drop for PtyReplSession[src]

fn drop(&mut self)[src]

for e.g. bash we need to run quit at the end. if we leave that out, PtyProcess would try to kill the bash which would not work, as a SIGTERM is not enough to kill bash

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,