From 3b550f5d3dad77a56455352579fae3071b42e86d Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 29 Dec 2021 02:55:24 -0500 Subject: wip another complete refactor --- tests/basic.rs | 58 +++++++++++++-------------- tests/pipe.rs | 2 +- tests/winch.rs | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 139 insertions(+), 43 deletions(-) (limited to 'tests') diff --git a/tests/basic.rs b/tests/basic.rs index e164357..a623f13 100644 --- a/tests/basic.rs +++ b/tests/basic.rs @@ -1,11 +1,12 @@ -#[cfg(feature = "backend-std")] #[test] -fn test_cat_std() { +fn test_cat_blocking() { use std::io::{Read as _, Write as _}; - let pty = pty_process::std::Pty::new().unwrap(); + let pty = pty_process::blocking::Pty::new().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut child = pty_process::std::Command::new("cat").spawn(pty).unwrap(); + let mut child = pty_process::blocking::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 @@ -25,16 +26,16 @@ fn test_cat_std() { assert_eq!(status.code().unwrap(), 0); } -#[cfg(feature = "backend-smol")] +#[cfg(feature = "async")] #[test] -fn test_cat_smol() { - use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; +fn test_cat_async_std() { + use async_std::io::prelude::WriteExt as _; + use async_std::io::ReadExt as _; - let status = smol::block_on(async { - let pty = pty_process::smol::Pty::new().unwrap(); + let status = async_std::task::block_on(async { + let pty = pty_process::Pty::new().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut child = - pty_process::smol::Command::new("cat").spawn(pty).unwrap(); + let mut child = pty_process::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 @@ -43,7 +44,7 @@ fn test_cat_smol() { // 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. - smol::Timer::after(std::time::Duration::from_secs(1)).await; + async_std::task::sleep(std::time::Duration::from_secs(1)).await; let mut buf = [0u8; 1024]; let bytes = child.pty().read(&mut buf).await.unwrap(); @@ -55,18 +56,15 @@ fn test_cat_smol() { assert_eq!(status.code().unwrap(), 0); } -#[cfg(feature = "backend-async-std")] +#[cfg(feature = "async")] #[test] -fn test_cat_async_std() { - use async_std::io::prelude::WriteExt as _; - use async_std::io::ReadExt as _; +fn test_cat_smol() { + use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; - let status = async_std::task::block_on(async { - let pty = pty_process::async_std::Pty::new().unwrap(); + let status = smol::block_on(async { + let pty = pty_process::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(); + let mut child = pty_process::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 @@ -75,7 +73,7 @@ fn test_cat_async_std() { // 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; + smol::Timer::after(std::time::Duration::from_secs(1)).await; let mut buf = [0u8; 1024]; let bytes = child.pty().read(&mut buf).await.unwrap(); @@ -87,18 +85,18 @@ fn test_cat_async_std() { assert_eq!(status.code().unwrap(), 0); } -#[cfg(feature = "backend-tokio")] +#[cfg(feature = "async")] #[test] fn test_cat_tokio() { use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; + use tokio_util::compat::FuturesAsyncReadCompatExt as _; async fn async_test_cat_tokio() { - let pty = pty_process::tokio::Pty::new().unwrap(); + let pty = pty_process::Pty::new().unwrap(); pty.resize(pty_process::Size::new(24, 80)).unwrap(); - let mut child = - pty_process::tokio::Command::new("cat").spawn(pty).unwrap(); + let mut child = pty_process::Command::new("cat").spawn(pty).unwrap(); - child.pty_mut().write_all(b"foo\n").await.unwrap(); + child.pty_mut().compat().write_all(b"foo\n").await.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). @@ -108,12 +106,12 @@ fn test_cat_tokio() { tokio::time::sleep(std::time::Duration::from_secs(1)).await; let mut buf = [0u8; 1024]; - let bytes = child.pty_mut().read(&mut buf).await.unwrap(); + let bytes = child.pty_mut().compat().read(&mut buf).await.unwrap(); assert_eq!(&buf[..bytes], b"foo\r\nfoo\r\n"); - child.pty_mut().write_all(&[4u8]).await.unwrap(); + child.pty_mut().compat().write_all(&[4u8]).await.unwrap(); - let status = child.wait().await.unwrap(); + let status = child.status().await.unwrap(); assert_eq!(status.code().unwrap(), 0); } tokio::runtime::Runtime::new().unwrap().block_on(async { diff --git a/tests/pipe.rs b/tests/pipe.rs index b1e0540..1bf49f2 100644 --- a/tests/pipe.rs +++ b/tests/pipe.rs @@ -19,7 +19,7 @@ fn test_pipe_basic() { assert_eq!(output.stdout, b"10\n9\n8\n7\n6\n5\n4\n3\n2\n1\n"); } -#[cfg(feature = "backend-async-std")] +#[cfg(feature = "todo")] // TODO (hangs because i'm still overriding the configured fds) // #[test] fn test_pipe_async() { diff --git a/tests/winch.rs b/tests/winch.rs index 326efcc..9d26751 100644 --- a/tests/winch.rs +++ b/tests/winch.rs @@ -1,22 +1,22 @@ -use std::io::{Read as _, Write as _}; - -#[cfg(feature = "backend-std")] #[test] -fn test_winch() { - let pty = pty_process::std::Pty::new().unwrap(); +fn test_winch_std() { + use std::io::{Read as _, Write as _}; + + let pty = pty_process::blocking::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 child = pty_process::blocking::Command::new("perl") + .args(&[ + "-E", + "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>", + ]) + .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.pty().resize(pty_process::Size::new(25, 80)).unwrap(); let bytes = child.pty().read(&mut buf).unwrap(); assert_eq!(&buf[..bytes], b"WINCH\r\n"); @@ -25,3 +25,101 @@ fn test_winch() { let status = child.wait().unwrap(); assert_eq!(status.code().unwrap(), 0); } + +#[cfg(feature = "async")] +#[test] +fn test_winch_async_std() { + use async_std::io::prelude::WriteExt as _; + use async_std::io::ReadExt as _; + + let status = async_std::task::block_on(async { + let pty = pty_process::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::Command::new("perl") + .args(&[ + "-E", + "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>", + ]) + .spawn(pty) + .unwrap(); + + let mut buf = [0u8; 1024]; + let bytes = child.pty().read(&mut buf).await.unwrap(); + assert_eq!(&buf[..bytes], b"started\r\n"); + + child.pty().resize(pty_process::Size::new(25, 80)).unwrap(); + + let bytes = child.pty().read(&mut buf).await.unwrap(); + assert_eq!(&buf[..bytes], b"WINCH\r\n"); + + child.pty().write_all(b"\n").await.unwrap(); + child.status().await.unwrap() + }); + assert_eq!(status.code().unwrap(), 0); +} + +#[cfg(feature = "async")] +#[test] +fn test_winch_smol() { + use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; + + let status = smol::block_on(async { + let pty = pty_process::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::Command::new("perl") + .args(&[ + "-E", + "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>", + ]) + .spawn(pty) + .unwrap(); + + let mut buf = [0u8; 1024]; + let bytes = child.pty().read(&mut buf).await.unwrap(); + assert_eq!(&buf[..bytes], b"started\r\n"); + + child.pty().resize(pty_process::Size::new(25, 80)).unwrap(); + + let bytes = child.pty().read(&mut buf).await.unwrap(); + assert_eq!(&buf[..bytes], b"WINCH\r\n"); + + child.pty().write_all(b"\n").await.unwrap(); + child.status().await.unwrap() + }); + assert_eq!(status.code().unwrap(), 0); +} + +#[cfg(feature = "async")] +#[test] +fn test_winch_tokio() { + use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; + use tokio_util::compat::FuturesAsyncReadCompatExt as _; + + async fn async_test_cat_tokio() -> std::process::ExitStatus { + let pty = pty_process::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut child = pty_process::Command::new("perl") + .args(&[ + "-E", + "$|++; $SIG{WINCH} = sub { say 'WINCH' }; say 'started'; <>", + ]) + .spawn(pty) + .unwrap(); + + let mut buf = [0u8; 1024]; + let bytes = child.pty().compat().read(&mut buf).await.unwrap(); + assert_eq!(&buf[..bytes], b"started\r\n"); + + child.pty().resize(pty_process::Size::new(25, 80)).unwrap(); + + let bytes = child.pty().compat().read(&mut buf).await.unwrap(); + assert_eq!(&buf[..bytes], b"WINCH\r\n"); + + child.pty().compat().write_all(b"\n").await.unwrap(); + child.status().await.unwrap() + } + tokio::runtime::Runtime::new().unwrap().block_on(async { + let status = async_test_cat_tokio().await; + assert_eq!(status.code().unwrap(), 0); + }); +} -- cgit v1.2.3-54-g00ecf