Skip to content

Commit

Permalink
Test --startup-script
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Dec 1, 2023
1 parent aac7bc3 commit 3966f5d
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
v0.3.0 (in development)
-----------------------
- Increased MSRV to 1.70
- Added `--startup-script` and `--startup-interval-ms` options
- Added `--startup-script` and `--startup-wait-ms` options

v0.2.0 (2023-06-03)
-------------------
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ Options
- `--servername <DOMAIN>` — (with `--tls`) Use the given domain name for SNI
and certificate hostname validation; defaults to the remote host name

- `--startup-interval-ms <INT>` — Specify the time to wait in milliseconds
between sending lines of the startup script
- `--startup-wait-ms <INT>` — Specify the time to wait in milliseconds
before sending each line of the startup script [default value: 500]

- `-S <FILE>`, `--startup-script <FILE>` — On startup, send lines read from the
given file to the server before requesting user input
Expand Down
7 changes: 4 additions & 3 deletions doc/confab.1
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ The default value is 65535.
Use the given domain name for SNI and certificate hostname validation;
defaults to the remote host name
.TP
\fB\-\-startup\-interval\-ms \fIint\fR
Specify the time to wait in milliseconds between sending lines of the startup
script
\fB\-\-startup\-wait\-ms \fIint\fR
Specify the time to wait in milliseconds before sending each line of the
startup script.
The default value is 500.
.TP
\fB\-S\fR \fIfile\fR, \fB\-\-startup\-script\fR \fIfile\fR
On startup, send lines read from the given file to the server before requesting
Expand Down
24 changes: 14 additions & 10 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pin_project! {
lines: Lines<BufReader<TokioFile>>,
#[pin]
nap: Option<Sleep>,
next_line: Option<Input>,
delay: Duration,
}
}
Expand All @@ -32,7 +33,8 @@ impl StartupScript {
pub(crate) fn new(reader: BufReader<TokioFile>, delay: Duration) -> StartupScript {
StartupScript {
lines: reader.lines(),
nap: None,
nap: Some(sleep(delay)),
next_line: None,
delay,
}
}
Expand All @@ -43,19 +45,21 @@ impl Stream for StartupScript {

fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = self.project();
if this.next_line.is_none() {
match ready!(this.lines.as_mut().poll_next_line(cx)) {
Ok(Some(line)) => {
*this.next_line = Some(Input::Line(line));
this.nap.set(Some(sleep(*this.delay)));
}
Ok(None) => return None.into(),
Err(e) => return Some(Err(InterfaceError::ReadScript(e))).into(),
}
}
if let Some(nap) = this.nap.as_mut().as_pin_mut() {
ready!(nap.poll(cx));
this.nap.set(None);
}
let r = match ready!(this.lines.as_mut().poll_next_line(cx)) {
Ok(Some(line)) => Some(Ok(Input::Line(line))),
Ok(None) => None,
Err(e) => Some(Err(InterfaceError::ReadScript(e))),
};
// TODO: Should the stream be forcibly fused when we're about to return
// None?
this.nap.set(Some(sleep(*this.delay)));
r.into()
this.next_line.take().map(Ok).into()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ struct Arguments {
#[arg(long, value_name = "DOMAIN")]
servername: Option<String>,

/// Time to wait in milliseconds between sending lines of the startup
/// Time to wait in milliseconds before sending each line of the startup
/// script
#[arg(long, default_value_t = 500, value_name = "INT")]
startup_interval_ms: u64,
startup_wait_ms: u64,

/// On startup, send lines read from the given file to the server before
/// requesting user input
Expand Down Expand Up @@ -117,7 +117,7 @@ impl Arguments {
);
Some(StartupScript::new(
fp,
Duration::from_millis(self.startup_interval_ms),
Duration::from_millis(self.startup_wait_ms),
))
} else {
None
Expand Down
65 changes: 63 additions & 2 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use serde::Deserialize;
use serde_jsonlines::json_lines;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::io::{Seek, Write};
use std::net::{IpAddr, SocketAddr};
use std::path::PathBuf;
use std::process::Command;
use std::time::Duration;
use tempfile::{tempdir, TempDir};
use tempfile::{tempdir, NamedTempFile, TempDir};
use time::OffsetDateTime;
use tokio::io::AsyncWriteExt;
use tokio::net::TcpListener;
Expand Down Expand Up @@ -86,7 +87,6 @@ impl Tester {
};
runner.connect().await;
runner.get("Welcome to the confab Test Server!").await;
runner.p.expect("confab> ").await.unwrap();
runner
}
}
Expand Down Expand Up @@ -133,11 +133,18 @@ impl Runner {

async fn enter<S: Into<Sent> + Send>(&mut self, entry: S) {
let entry = entry.into();
self.p.expect("confab> ").await.unwrap();
self.p.send(entry.typed()).await.unwrap();
self.expect(entry.printed()).await;
self.transcribe(entry.transcription());
}

async fn script_enter<S: Into<Sent> + Send>(&mut self, entry: S) {
let entry = entry.into();
self.expect(entry.printed()).await;
self.transcribe(entry.transcription());
}

async fn get<R: Into<Recv> + Send>(&mut self, r: R) {
let r = r.into();
self.expect(r.printed()).await;
Expand Down Expand Up @@ -600,3 +607,57 @@ async fn test_no_crlf_recv_crlf() {
.await;
r.quit().await;
}

#[tokio::test]
async fn startup_script() {
let mut scriptfile = NamedTempFile::new().unwrap();
writeln!(scriptfile, "Hello!").unwrap();
writeln!(scriptfile, "This is from a startup script.").unwrap();
scriptfile.flush().unwrap();
scriptfile.rewind().unwrap();

let mut r = Tester::new()
.arg("--startup-script")
.arg(scriptfile.path())
.transcript()
.build()
.await;

sleep(Duration::from_millis(500)).await;
r.script_enter("Hello!").await;
r.get(r#"You sent: "Hello!""#).await;
sleep(Duration::from_millis(500)).await;
r.script_enter("This is from a startup script.").await;
r.get(r#"You sent: "This is from a startup script.""#).await;

r.enter("Hello again!").await;
r.get(r#"You sent: "Hello again!""#).await;
r.enter("This is from the prompt.").await;
r.get(r#"You sent: "This is from the prompt.""#).await;

r.quit().await;
}

#[tokio::test]
async fn quit_from_startup_script() {
let mut scriptfile = NamedTempFile::new().unwrap();
writeln!(scriptfile, "Hello!").unwrap();
writeln!(scriptfile, "quit").unwrap();
writeln!(scriptfile, "wait no-").unwrap();
scriptfile.flush().unwrap();
scriptfile.rewind().unwrap();
let mut r = Tester::new()
.arg("--startup-script")
.arg(scriptfile.path())
.transcript()
.build()
.await;
sleep(Duration::from_millis(500)).await;
r.script_enter("Hello!").await;
r.get(r#"You sent: "Hello!""#).await;
sleep(Duration::from_millis(500)).await;
r.script_enter("quit").await;
r.get(r#"You sent: "quit""#).await;
r.get("Goodbye.").await;
r.finish().await;
}

0 comments on commit 3966f5d

Please sign in to comment.