Skip to content

Commit 979195b

Browse files
Merge #52
52: cargo-fmt in CI and format code r=matthiasbeyer a=matthiasbeyer This PR adds cargo-fmt in the actions workflows and formats the code, so the lint succeeds. cargo-fmt configuration is set to default. Co-authored-by: Matthias Beyer <[email protected]>
2 parents 089150f + 0d2812f commit 979195b

File tree

12 files changed

+163
-111
lines changed

12 files changed

+163
-111
lines changed

.github/workflows/ci.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ jobs:
3333
with:
3434
command: check
3535

36+
37+
fmt:
38+
name: format
39+
runs-on: ubuntu-latest
40+
41+
steps:
42+
- uses: actions/checkout@v3
43+
- uses: actions-rs/toolchain@v1
44+
with:
45+
toolchain: 1.60.0
46+
- run: rustup component add rustfmt
47+
- name: cargo-fmt
48+
uses: actions-rs/cargo@v1
49+
with:
50+
command: fmt
51+
args: -- --check
52+
53+
3654
# We need some "accummulation" job here because bors fails (timeouts) to
3755
# listen on matrix builds.
3856
# Hence, we have some kind of dummy here that bors can listen on
@@ -41,6 +59,7 @@ jobs:
4159
if: ${{ success() }}
4260
needs:
4361
- check
62+
- fmt
4463
runs-on: ubuntu-latest
4564
steps:
4665
- name: CI succeeded

examples/bash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate rexpect;
2-
use rexpect::spawn_bash;
32
use rexpect::errors::*;
3+
use rexpect::spawn_bash;
44

55
fn run() -> Result<()> {
66
let mut p = spawn_bash(Some(1000))?;

examples/bash_read.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
extern crate rexpect;
2-
use rexpect::spawn_bash;
32
use rexpect::errors::*;
4-
3+
use rexpect::spawn_bash;
54

65
fn run() -> Result<()> {
76
let mut p = spawn_bash(Some(2000))?;
@@ -19,7 +18,10 @@ fn run() -> Result<()> {
1918
let (_, words) = p.exp_regex("[0-9]+")?;
2019
let (_, bytes) = p.exp_regex("[0-9]+")?;
2120
p.wait_for_prompt()?; // go sure `wc` is really done
22-
println!("/etc/passwd has {} lines, {} words, {} chars", lines, words, bytes);
21+
println!(
22+
"/etc/passwd has {} lines, {} words, {} chars",
23+
lines, words, bytes
24+
);
2325

2426
// case 3: read while program is still executing
2527
p.execute("ping 8.8.8.8", "bytes of data")?; // returns when it sees "bytes of data" in output

examples/exit_code.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
extern crate rexpect;
22

3-
use rexpect::spawn;
43
use rexpect::errors::*;
54
use rexpect::process::wait;
6-
5+
use rexpect::spawn;
76

87
/// The following code emits:
98
/// cat exited with code 0, all good!
109
/// cat exited with code 1
1110
/// Output (stdout and stderr): cat: /this/does/not/exist: No such file or directory
1211
fn exit_code_fun() -> Result<()> {
13-
1412
let p = spawn("cat /etc/passwd", Some(2000))?;
1513
match p.process.wait() {
1614
Ok(wait::WaitStatus::Exited(_, 0)) => println!("cat exited with code 0, all good!"),
@@ -23,7 +21,7 @@ fn exit_code_fun() -> Result<()> {
2321
Ok(wait::WaitStatus::Exited(_, c)) => {
2422
println!("Cat failed with exit code {}", c);
2523
println!("Output (stdout and stderr): {}", p.exp_eof()?);
26-
},
24+
}
2725
// for other possible return types of wait()
2826
// see here: https://tailhook.github.io/rotor/nix/sys/wait/enum.WaitStatus.html
2927
_ => println!("cat was probably killed"),
@@ -32,7 +30,6 @@ fn exit_code_fun() -> Result<()> {
3230
Ok(())
3331
}
3432

35-
3633
fn main() {
3734
exit_code_fun().unwrap_or_else(|e| panic!("cat function failed with {}", e));
3835
}

examples/ftp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
extern crate rexpect;
22

3-
use rexpect::spawn;
43
use rexpect::errors::*;
4+
use rexpect::spawn;
55

66
fn do_ftp() -> Result<()> {
77
let mut p = spawn("ftp speedtest.tele2.net", Some(2000))?;
@@ -19,7 +19,6 @@ fn do_ftp() -> Result<()> {
1919
Ok(())
2020
}
2121

22-
2322
fn main() {
2423
do_ftp().unwrap_or_else(|e| panic!("ftp job failed with {}", e));
2524
}

examples/repl.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
33
extern crate rexpect;
44

5-
use rexpect::spawn;
6-
use rexpect::session::PtyReplSession;
75
use rexpect::errors::*;
6+
use rexpect::session::PtyReplSession;
7+
use rexpect::spawn;
88

99
fn ed_session() -> Result<PtyReplSession> {
1010
let mut ed = PtyReplSession {
@@ -40,4 +40,4 @@ fn do_ed_repl() -> Result<()> {
4040

4141
fn main() {
4242
do_ed_repl().unwrap_or_else(|e| panic!("ed session failed with {}", e));
43-
}
43+
}

examples/tcp.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use rexpect::spawn_stream;
2-
use std::net::TcpStream;
32
use std::error::Error;
3+
use std::net::TcpStream;
44

5-
fn main() -> Result<(), Box<dyn Error>>
6-
{
5+
fn main() -> Result<(), Box<dyn Error>> {
76
let tcp = TcpStream::connect("www.google.com:80")?;
87
let tcp_w = tcp.try_clone()?;
98
let mut session = spawn_stream(tcp, tcp_w, Some(2000));
@@ -13,4 +12,4 @@ fn main() -> Result<(), Box<dyn Error>>
1312
session.send_line("")?;
1413
session.exp_string("HTTP/1.1 200 OK")?;
1514
Ok(())
16-
}
15+
}

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# default

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@
7979
//! ```
8080
8181
pub mod process;
82-
pub mod session;
8382
pub mod reader;
83+
pub mod session;
8484

85-
pub use session::{spawn, spawn_bash, spawn_python, spawn_stream};
8685
pub use reader::ReadUntil;
86+
pub use session::{spawn, spawn_bash, spawn_python, spawn_stream};
8787

8888
pub mod errors {
8989
use std::time;
9090
// Create the Error, ErrorKind, ResultExt, and Result types
91-
error_chain::error_chain!{
91+
error_chain::error_chain! {
9292
errors {
9393
EOF(expected:String, got:String, exit_code:Option<String>) {
9494
description("End of filestream (usually stdout) occurred, most probably\

src/process.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
//! Start a process via pty
22
3+
use crate::errors::*;
4+
use nix;
5+
use nix::fcntl::{open, OFlag};
6+
use nix::libc::{STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO};
7+
use nix::pty::{grantpt, posix_openpt, unlockpt, PtyMaster};
8+
pub use nix::sys::{signal, wait};
9+
use nix::sys::{stat, termios};
10+
use nix::unistd::{dup, dup2, fork, setsid, ForkResult, Pid};
311
use std;
412
use std::fs::File;
5-
use std::process::Command;
13+
use std::os::unix::io::{AsRawFd, FromRawFd};
614
use std::os::unix::process::CommandExt;
7-
use std::os::unix::io::{FromRawFd, AsRawFd};
8-
use std::{thread, time};
9-
use nix::pty::{posix_openpt, grantpt, unlockpt, PtyMaster};
10-
use nix::fcntl::{OFlag, open};
11-
use nix;
12-
use nix::sys::{stat, termios};
13-
use nix::unistd::{fork, ForkResult, setsid, dup, dup2, Pid};
14-
use nix::libc::{STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO};
15-
pub use nix::sys::{wait, signal};
16-
use crate::errors::*; // load error-chain
15+
use std::process::Command;
16+
use std::{thread, time}; // load error-chain
1717

1818
/// Start a process in a forked tty so you can interact with it the same as you would
1919
/// within a terminal
@@ -60,7 +60,6 @@ pub struct PtyProcess {
6060
kill_timeout: Option<time::Duration>,
6161
}
6262

63-
6463
#[cfg(target_os = "linux")]
6564
use nix::pty::ptsname_r;
6665

@@ -69,8 +68,8 @@ use nix::pty::ptsname_r;
6968
/// instead of using a static mutex this calls ioctl with TIOCPTYGNAME directly
7069
/// based on https://blog.tarq.io/ptsname-on-osx-with-rust/
7170
fn ptsname_r(fd: &PtyMaster) -> nix::Result<String> {
72-
use std::ffi::CStr;
7371
use nix::libc::{ioctl, TIOCPTYGNAME};
72+
use std::ffi::CStr;
7473

7574
// the buffer size on OSX is 128, defined by sys/ttycom.h
7675
let mut buf: [i8; 128] = [0; 128];
@@ -103,9 +102,11 @@ impl PtyProcess {
103102
match fork()? {
104103
ForkResult::Child => {
105104
setsid()?; // create new session with child as session leader
106-
let slave_fd = open(std::path::Path::new(&slave_name),
107-
OFlag::O_RDWR,
108-
stat::Mode::empty())?;
105+
let slave_fd = open(
106+
std::path::Path::new(&slave_name),
107+
OFlag::O_RDWR,
108+
stat::Mode::empty(),
109+
)?;
109110

110111
// assign stdin, stdout, stderr to the tty, just like a terminal does
111112
dup2(slave_fd, STDIN_FILENO)?;
@@ -120,16 +121,14 @@ impl PtyProcess {
120121
command.exec();
121122
Err(nix::Error::last())
122123
}
123-
ForkResult::Parent { child: child_pid } => {
124-
Ok(PtyProcess {
125-
pty: master_fd,
126-
child_pid: child_pid,
127-
kill_timeout: None,
128-
})
129-
}
124+
ForkResult::Parent { child: child_pid } => Ok(PtyProcess {
125+
pty: master_fd,
126+
child_pid: child_pid,
127+
kill_timeout: None,
128+
}),
130129
}
131130
}()
132-
.chain_err(|| format!("could not execute {:?}", command))
131+
.chain_err(|| format!("could not execute {:?}", command))
133132
}
134133

135134
/// Get handle to pty fork for reading/writing
@@ -189,8 +188,7 @@ impl PtyProcess {
189188

190189
/// Non-blocking variant of `kill()` (doesn't wait for process to be killed)
191190
pub fn signal(&mut self, sig: signal::Signal) -> Result<()> {
192-
signal::kill(self.child_pid, sig)
193-
.chain_err(|| "failed to send signal to process")?;
191+
signal::kill(self.child_pid, sig).chain_err(|| "failed to send signal to process")?;
194192
Ok(())
195193
}
196194

@@ -214,7 +212,6 @@ impl PtyProcess {
214212
Err(e) => return Err(format!("kill resulted in error: {:?}", e).into()),
215213
}
216214

217-
218215
match self.status() {
219216
Some(status) if status != wait::WaitStatus::StillAlive => return Ok(status),
220217
Some(_) | None => thread::sleep(time::Duration::from_millis(100)),
@@ -243,9 +240,9 @@ impl Drop for PtyProcess {
243240
#[cfg(test)]
244241
mod tests {
245242
use super::*;
246-
use std::io::{BufReader, LineWriter};
247-
use nix::sys::{wait, signal};
243+
use nix::sys::{signal, wait};
248244
use std::io::prelude::*;
245+
use std::io::{BufReader, LineWriter};
249246

250247
#[test]
251248
/// Open cat, write string, read back string twice, send Ctrl^C and check that cat exited
@@ -271,6 +268,6 @@ mod tests {
271268
assert_eq!(should, wait::waitpid(process.child_pid, None).unwrap());
272269
Ok(())
273270
}()
274-
.unwrap_or_else(|e| panic!("test_cat failed: {}", e));
271+
.unwrap_or_else(|e| panic!("test_cat failed: {}", e));
275272
}
276273
}

0 commit comments

Comments
 (0)