From f8780ca1e76286688b74d8a6c64d5fadf3cfd2a1 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 28 Dec 2021 03:33:52 -0500 Subject: wip --- tests/basic.rs | 28 +++++++++++++----------- tests/pipe.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/winch.rs | 18 ++++++++-------- 3 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 tests/pipe.rs (limited to 'tests') diff --git a/tests/basic.rs b/tests/basic.rs index 9ff0a33..e164357 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,13 +1,11 @@ -use pty_process::Command as _; - #[cfg(feature = "backend-std")] #[test] fn test_cat_std() { use std::io::{Read as _, Write as _}; - let mut child = std::process::Command::new("cat") - .spawn_pty(Some(&pty_process::Size::new(24, 80))) - .unwrap(); + let pty = pty_process::std::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::std::Command::new("cat").spawn(pty).unwrap(); child.pty().write_all(b"foo\n").unwrap(); // the pty will echo the written bytes back immediately, but the @@ -33,9 +31,10 @@ fn test_cat_smol() { use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; let status = smol::block_on(async { - let mut child = smol::process::Command::new("cat") - .spawn_pty(Some(&pty_process::Size::new(24, 80))) - .unwrap(); + let pty = pty_process::smol::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = + pty_process::smol::Command::new("cat").spawn(pty).unwrap(); child.pty().write_all(b"foo\n").await.unwrap(); // the pty will echo the written bytes back immediately, but the @@ -63,8 +62,10 @@ fn test_cat_async_std() { use async_std::io::ReadExt as _; let status = async_std::task::block_on(async { - let mut child = async_std::process::Command::new("cat") - .spawn_pty(Some(&pty_process::Size::new(24, 80))) + let pty = pty_process::async_std::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::async_std::Command::new("cat") + .spawn(pty) .unwrap(); child.pty().write_all(b"foo\n").await.unwrap(); @@ -92,9 +93,10 @@ fn test_cat_tokio() { use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; async fn async_test_cat_tokio() { - let mut child = tokio::process::Command::new("cat") - .spawn_pty(Some(&pty_process::Size::new(24, 80))) - .unwrap(); + let pty = pty_process::tokio::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = + pty_process::tokio::Command::new("cat").spawn(pty).unwrap(); child.pty_mut().write_all(b"foo\n").await.unwrap(); // the pty will echo the written bytes back immediately, but the diff --git a/tests/pipe.rs b/tests/pipe.rs new file mode 100644 index 0000000..b1e0540 --- /dev/null +++ b/tests/pipe.rs @@ -0,0 +1,68 @@ +#[test] +fn test_pipe_basic() { + use std::os::unix::io::FromRawFd as _; + + let (read_fd, write_fd) = nix::unistd::pipe().unwrap(); + + let mut child_from = std::process::Command::new("seq"); + child_from.args(["1", "10"]); + child_from.stdout(unsafe { std::process::Stdio::from_raw_fd(write_fd) }); + + let mut child_to = std::process::Command::new("tac"); + child_to.stdin(unsafe { std::process::Stdio::from_raw_fd(read_fd) }); + child_to.stdout(std::process::Stdio::piped()); + + assert!(child_from.status().unwrap().success()); + nix::unistd::close(write_fd).unwrap(); + let output = child_to.output().unwrap(); + assert!(output.status.success()); + assert_eq!(output.stdout, b"10\n9\n8\n7\n6\n5\n4\n3\n2\n1\n"); +} + +#[cfg(feature = "backend-async-std")] +// TODO (hangs because i'm still overriding the configured fds) +// #[test] +fn test_pipe_async() { + use async_std::io::ReadExt as _; + use std::os::unix::io::FromRawFd as _; + + let (status_from, status_to) = async_std::task::block_on(async { + let (read_fd, write_fd) = nix::unistd::pipe().unwrap(); + + let pty_from = pty_process::async_std::Pty::new().unwrap(); + pty_from.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut cmd_from = pty_process::async_std::Command::new("seq"); + cmd_from.args(["1", "10"]); + cmd_from + .stdout(unsafe { std::process::Stdio::from_raw_fd(write_fd) }); + let mut child_from = cmd_from.spawn(pty_from).unwrap(); + + let pty_to = pty_process::async_std::Pty::new().unwrap(); + pty_to.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut cmd_to = pty_process::async_std::Command::new("tac"); + cmd_to.stdin(unsafe { std::process::Stdio::from_raw_fd(read_fd) }); + let mut child_to = cmd_to.spawn(pty_to).unwrap(); + + // the pty will echo the written bytes back immediately, but the + // subprocess needs to generate its own output, which takes time, so + // we can't just read immediately (we may just get the echoed bytes). + // because the output generation is happening in the subprocess, we + // also don't have any way to know when (or if!) the subprocess will + // decide to send its output, so sleeping is the best we can do. + async_std::task::sleep(std::time::Duration::from_secs(1)).await; + + let mut buf = [0u8; 1024]; + let bytes = child_to.pty().read(&mut buf).await.unwrap(); + assert_eq!( + &buf[..bytes], + b"10\r\n9\r\n8\r\n7\r\n6\r\n5\r\n4\r\n3\r\n2\r\n1\r\n" + ); + + ( + child_from.status().await.unwrap(), + child_to.status().await.unwrap(), + ) + }); + assert_eq!(status_from.code().unwrap(), 0); + assert_eq!(status_to.code().unwrap(), 0); +} diff --git a/tests/winch.rs b/tests/winch.rs index 234afeb..326efcc 100644 --- a/tests/winch.rs +++ b/tests/winch.rs @@ -1,22 +1,22 @@ -use pty_process::Command as _; use std::io::{Read as _, Write as _}; #[cfg(feature = "backend-std")] #[test] fn test_winch() { - let mut child = std::process::Command::new("perl") - .args(&[ - "-E", - "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>", - ]) - .spawn_pty(Some(&pty_process::Size::new(24, 80))) - .unwrap(); + let pty = pty_process::std::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut cmd = pty_process::std::Command::new("perl"); + cmd.args(&[ + "-E", + "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>", + ]); + let mut child = cmd.spawn(pty).unwrap(); let mut buf = [0u8; 1024]; let bytes = child.pty().read(&mut buf).unwrap(); assert_eq!(&buf[..bytes], b"started\r\n"); - child.resize_pty(&pty_process::Size::new(25, 80)).unwrap(); + child.resize_pty(pty_process::Size::new(25, 80)).unwrap(); let bytes = child.pty().read(&mut buf).unwrap(); assert_eq!(&buf[..bytes], b"WINCH\r\n"); -- cgit v1.2.3-54-g00ecf