Skip to content
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

Exp and windows: rethinking any matching #27

Closed
wants to merge 11 commits into from
69 changes: 44 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ readme = "README.md"


[dependencies]
nix = "0.14"
regex = "1"
error-chain = "0.12"
tempfile = "3"
ptyproc = { version = "0.1", git = "https://github.com/TyPR124/ptyproc" }


[badges]
travis-ci = { repository = "philippkeller/rexpect" }
37 changes: 22 additions & 15 deletions examples/exit_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,40 @@ extern crate rexpect;

use rexpect::spawn;
use rexpect::errors::*;
use rexpect::process::wait;


/// The following code emits:
/// cat exited with code 0, all good!
/// cat exited with code 1
/// Output (stdout and stderr): cat: /this/does/not/exist: No such file or directory
fn exit_code_fun() -> Result<()> {

let p = spawn("cat /etc/passwd", Some(2000))?;
let mut p = spawn("cat /etc/passwd", Some(2000))?;
match p.process.wait() {
Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat exited with code 0, all good!"),
_ => println!("cat exited with code >0, or it was killed"),
Ok(status) if status.success() => println!("cat exited with code 0, all good!"),
Ok(status) => {
if let Some(code) = status.code() {
println!("Cat failed with exit code {}", code);
println!("Output (stdout and stderr): {}", p.exp_eof()?);
} else {
println!("cat was probably killed")
}
},
Err(err) => println!("failed to wait on process {:?}", err),
}

let mut p = spawn("cat /this/does/not/exist", Some(2000))?;
match p.process.wait() {
Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat succeeded"),
Ok(wait::WaitStatus::Exited(_, c)) => {
println!("Cat failed with exit code {}", c);
println!("Output (stdout and stderr): {}", p.exp_eof()?);
Ok(status) if status.success() => println!("cat exited with code 0, all good!"),
Ok(status) => {
if let Some(code) = status.code() {
println!("Cat failed with exit code {}", code);
println!("Output (stdout and stderr): {}", p.exp_eof()?);
} else {
println!("cat was probably killed")
}
},
// for other possible return types of wait()
// see here: https://tailhook.github.io/rotor/nix/sys/wait/enum.WaitStatus.html
_ => println!("cat was probably killed"),
Err(err) => println!("failed to wait on process {:?}", err),
}

Ok(())
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@
//!
//! ```

pub mod process;
pub mod session;
pub mod reader;

pub use session::{spawn, spawn_bash, spawn_python, spawn_stream};
pub use reader::ReadUntil;

pub use ptyproc::{Command, PtyProcess, PtyReader, PtyWriter};

pub mod errors {
use std::time;
Expand Down
Loading