Skip to content

Add cargo-test job #67

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

Merged
merged 7 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ jobs:
command: check


test:
name: test
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.60.0
- stable
- beta
# - nightly
steps:
- name: Checkout sources
uses: actions/checkout@v3
- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
- uses: swatinem/rust-cache@v1
- name: cargo-test
uses: actions-rs/cargo@v1
with:
command: test
args: --all --all-features


deny:
name: deny
runs-on: ubuntu-latest
Expand Down Expand Up @@ -97,6 +123,7 @@ jobs:
- deny
- fmt
- clippy
- test
runs-on: ubuntu-latest
steps:
- name: CI succeeded
Expand Down
16 changes: 9 additions & 7 deletions src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize
ReadUntil::NBytes(n) => {
if *n <= buffer.len() {
Some((0, *n))
} else if eof && buffer.is_empty() {
} else if eof && !buffer.is_empty() {
// reached almost end of buffer, return string, even though it will be
// smaller than the wished n bytes
Some((0, buffer.len()))
Expand Down Expand Up @@ -171,11 +171,11 @@ impl NBReader {
// this is just from experience, e.g. "sleep 5" returns the other error which
// most probably means that there is no stdout stream at all -> send EOF
// this only happens on Linux, not on OSX
Err(PipeError::IO(ref err)) if err.kind() == io::ErrorKind::Other => {
self.eof = true
Err(PipeError::IO(ref err)) => {
// For an explanation of why we use `raw_os_error` see:
// https://github.com/zhiburt/ptyprocess/commit/df003c8e3ff326f7d17bc723bc7c27c50495bb62
self.eof = err.raw_os_error() == Some(5)
}
// discard other errors
Err(_) => {}
}
}
Ok(())
Expand Down Expand Up @@ -274,7 +274,7 @@ impl NBReader {
pub fn try_read(&mut self) -> Option<char> {
// discard eventual errors, EOF will be handled in read_until correctly
let _ = self.read_into_buffer();
if self.buffer.is_empty() {
if !self.buffer.is_empty() {
self.buffer.drain(..1).last()
} else {
None
Expand Down Expand Up @@ -390,7 +390,9 @@ mod tests {
fn test_try_read() {
let f = io::Cursor::new("lorem");
let mut r = NBReader::new(f, None);
r.read_until(&ReadUntil::NBytes(4)).expect("4 bytes");
let bytes = r.read_until(&ReadUntil::NBytes(4)).unwrap();
assert!(bytes.0.is_empty());
assert_eq!(bytes.1, "lore");
assert_eq!(Some('m'), r.try_read());
assert_eq!(None, r.try_read());
assert_eq!(None, r.try_read());
Expand Down
28 changes: 9 additions & 19 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ impl<W: Write> StreamSession<W> {
///
/// ```
/// use rexpect::{spawn, ReadUntil};
/// # use rexpect::errors::*;
/// # use rexpect::error::Error;
///
/// # fn main() {
/// # || -> Result<()> {
/// # || -> Result<(), Error> {
/// let mut s = spawn("cat", Some(1000))?;
/// s.send_line("hello, polly!")?;
/// s.exp_any(vec![ReadUntil::String("hello".into()),
Expand All @@ -155,7 +155,6 @@ impl<W: Write> StreamSession<W> {
pub struct PtySession {
pub process: PtyProcess,
pub stream: StreamSession<File>,
pub commandname: String, // only for debugging purposes now
}

// make StreamSession's methods available directly
Expand All @@ -179,10 +178,10 @@ impl DerefMut for PtySession {
/// ```
///
/// use rexpect::spawn;
/// # use rexpect::errors::*;
/// # use rexpect::error::Error;
///
/// # fn main() {
/// # || -> Result<()> {
/// # || -> Result<(), Error> {
/// let mut s = spawn("cat", Some(1000))?;
/// s.send_line("hello, polly!")?;
/// let line = s.read_line()?;
Expand All @@ -192,19 +191,11 @@ impl DerefMut for PtySession {
/// # }
/// ```
impl PtySession {
fn new(
process: PtyProcess,
timeout_ms: Option<u64>,
commandname: String,
) -> Result<Self, Error> {
fn new(process: PtyProcess, timeout_ms: Option<u64>) -> Result<Self, Error> {
let f = process.get_file_handle();
let reader = f.try_clone()?;
let stream = StreamSession::new(reader, f, timeout_ms);
Ok(Self {
process,
stream,
commandname,
})
Ok(Self { process, stream })
}
}

Expand Down Expand Up @@ -241,11 +232,10 @@ pub fn spawn(program: &str, timeout_ms: Option<u64>) -> Result<PtySession, Error

/// See `spawn`
pub fn spawn_command(command: Command, timeout_ms: Option<u64>) -> Result<PtySession, Error> {
let commandname = format!("{:?}", &command);
let mut process = PtyProcess::new(command)?;
process.set_kill_timeout(timeout_ms);

PtySession::new(process, timeout_ms, commandname)
PtySession::new(process, timeout_ms)
}

/// A repl session: e.g. bash or the python shell:
Expand Down Expand Up @@ -293,10 +283,10 @@ impl PtyReplSession {
///
/// ```
/// use rexpect::spawn_bash;
/// # use rexpect::errors::*;
/// # use rexpect::error::Error;
///
/// # fn main() {
/// # || -> Result<()> {
/// # || -> Result<(), Error> {
/// let mut p = spawn_bash(Some(1000))?;
/// p.execute("cat <(echo ready) -", "ready")?;
/// p.send_line("hans")?;
Expand Down