Skip to content

Commit 53e5568

Browse files
bors[bot]matthiasbeyergz
authored
Merge #67
67: Add cargo-test job r=matthiasbeyer a=matthiasbeyer Adds a cargo-test job to CI. Co-authored-by: Matthias Beyer <mail@beyermatthias.de> Co-authored-by: Gerd Zellweger <mail@gerdzellweger.com>
2 parents f7d3e18 + dcbe31e commit 53e5568

File tree

3 files changed

+45
-26
lines changed

3 files changed

+45
-26
lines changed

.github/workflows/ci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ jobs:
3434
command: check
3535

3636

37+
test:
38+
name: test
39+
runs-on: ubuntu-latest
40+
strategy:
41+
matrix:
42+
rust:
43+
- 1.60.0
44+
- stable
45+
- beta
46+
# - nightly
47+
steps:
48+
- name: Checkout sources
49+
uses: actions/checkout@v3
50+
- name: Install toolchain
51+
uses: actions-rs/toolchain@v1
52+
with:
53+
toolchain: ${{ matrix.rust }}
54+
override: true
55+
- uses: swatinem/rust-cache@v1
56+
- name: cargo-test
57+
uses: actions-rs/cargo@v1
58+
with:
59+
command: test
60+
args: --all --all-features
61+
62+
3763
deny:
3864
name: deny
3965
runs-on: ubuntu-latest
@@ -97,6 +123,7 @@ jobs:
97123
- deny
98124
- fmt
99125
- clippy
126+
- test
100127
runs-on: ubuntu-latest
101128
steps:
102129
- name: CI succeeded

src/reader.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn find(needle: &ReadUntil, buffer: &str, eof: bool) -> Option<(usize, usize
7575
ReadUntil::NBytes(n) => {
7676
if *n <= buffer.len() {
7777
Some((0, *n))
78-
} else if eof && buffer.is_empty() {
78+
} else if eof && !buffer.is_empty() {
7979
// reached almost end of buffer, return string, even though it will be
8080
// smaller than the wished n bytes
8181
Some((0, buffer.len()))
@@ -171,11 +171,11 @@ impl NBReader {
171171
// this is just from experience, e.g. "sleep 5" returns the other error which
172172
// most probably means that there is no stdout stream at all -> send EOF
173173
// this only happens on Linux, not on OSX
174-
Err(PipeError::IO(ref err)) if err.kind() == io::ErrorKind::Other => {
175-
self.eof = true
174+
Err(PipeError::IO(ref err)) => {
175+
// For an explanation of why we use `raw_os_error` see:
176+
// https://github.com/zhiburt/ptyprocess/commit/df003c8e3ff326f7d17bc723bc7c27c50495bb62
177+
self.eof = err.raw_os_error() == Some(5)
176178
}
177-
// discard other errors
178-
Err(_) => {}
179179
}
180180
}
181181
Ok(())
@@ -274,7 +274,7 @@ impl NBReader {
274274
pub fn try_read(&mut self) -> Option<char> {
275275
// discard eventual errors, EOF will be handled in read_until correctly
276276
let _ = self.read_into_buffer();
277-
if self.buffer.is_empty() {
277+
if !self.buffer.is_empty() {
278278
self.buffer.drain(..1).last()
279279
} else {
280280
None
@@ -390,7 +390,9 @@ mod tests {
390390
fn test_try_read() {
391391
let f = io::Cursor::new("lorem");
392392
let mut r = NBReader::new(f, None);
393-
r.read_until(&ReadUntil::NBytes(4)).expect("4 bytes");
393+
let bytes = r.read_until(&ReadUntil::NBytes(4)).unwrap();
394+
assert!(bytes.0.is_empty());
395+
assert_eq!(bytes.1, "lore");
394396
assert_eq!(Some('m'), r.try_read());
395397
assert_eq!(None, r.try_read());
396398
assert_eq!(None, r.try_read());

src/session.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ impl<W: Write> StreamSession<W> {
134134
///
135135
/// ```
136136
/// use rexpect::{spawn, ReadUntil};
137-
/// # use rexpect::errors::*;
137+
/// # use rexpect::error::Error;
138138
///
139139
/// # fn main() {
140-
/// # || -> Result<()> {
140+
/// # || -> Result<(), Error> {
141141
/// let mut s = spawn("cat", Some(1000))?;
142142
/// s.send_line("hello, polly!")?;
143143
/// s.exp_any(vec![ReadUntil::String("hello".into()),
@@ -155,7 +155,6 @@ impl<W: Write> StreamSession<W> {
155155
pub struct PtySession {
156156
pub process: PtyProcess,
157157
pub stream: StreamSession<File>,
158-
pub commandname: String, // only for debugging purposes now
159158
}
160159

161160
// make StreamSession's methods available directly
@@ -179,10 +178,10 @@ impl DerefMut for PtySession {
179178
/// ```
180179
///
181180
/// use rexpect::spawn;
182-
/// # use rexpect::errors::*;
181+
/// # use rexpect::error::Error;
183182
///
184183
/// # fn main() {
185-
/// # || -> Result<()> {
184+
/// # || -> Result<(), Error> {
186185
/// let mut s = spawn("cat", Some(1000))?;
187186
/// s.send_line("hello, polly!")?;
188187
/// let line = s.read_line()?;
@@ -192,19 +191,11 @@ impl DerefMut for PtySession {
192191
/// # }
193192
/// ```
194193
impl PtySession {
195-
fn new(
196-
process: PtyProcess,
197-
timeout_ms: Option<u64>,
198-
commandname: String,
199-
) -> Result<Self, Error> {
194+
fn new(process: PtyProcess, timeout_ms: Option<u64>) -> Result<Self, Error> {
200195
let f = process.get_file_handle();
201196
let reader = f.try_clone()?;
202197
let stream = StreamSession::new(reader, f, timeout_ms);
203-
Ok(Self {
204-
process,
205-
stream,
206-
commandname,
207-
})
198+
Ok(Self { process, stream })
208199
}
209200
}
210201

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

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

248-
PtySession::new(process, timeout_ms, commandname)
238+
PtySession::new(process, timeout_ms)
249239
}
250240

251241
/// A repl session: e.g. bash or the python shell:
@@ -293,10 +283,10 @@ impl PtyReplSession {
293283
///
294284
/// ```
295285
/// use rexpect::spawn_bash;
296-
/// # use rexpect::errors::*;
286+
/// # use rexpect::error::Error;
297287
///
298288
/// # fn main() {
299-
/// # || -> Result<()> {
289+
/// # || -> Result<(), Error> {
300290
/// let mut p = spawn_bash(Some(1000))?;
301291
/// p.execute("cat <(echo ready) -", "ready")?;
302292
/// p.send_line("hans")?;

0 commit comments

Comments
 (0)