From 3c1b517b1b7676089f0d4c0cacfaaa2992d047b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fe=CC=81lix=20Saparelli?= Date: Sat, 9 Mar 2024 18:22:32 +1300 Subject: [PATCH] wip: start adding back tests --- src/tokio/core.rs | 4 +++- tests/tokio_unix.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 tests/tokio_unix.rs diff --git a/src/tokio/core.rs b/src/tokio/core.rs index 1cb121f..b437022 100644 --- a/src/tokio/core.rs +++ b/src/tokio/core.rs @@ -27,7 +27,7 @@ impl TokioCommandWrap { } } - pub fn wrap(&mut self, wrapper: W) { + pub fn wrap(&mut self, wrapper: W) -> &mut Self { let typeid = TypeId::of::(); let mut wrapper = Some(Box::new(wrapper)); let extant = self @@ -37,6 +37,8 @@ impl TokioCommandWrap { if let Some(wrapper) = wrapper { extant.extend(wrapper); } + + self } pub fn spawn(mut self) -> Result> { diff --git a/tests/tokio_unix.rs b/tests/tokio_unix.rs new file mode 100644 index 0000000..ecc8fe1 --- /dev/null +++ b/tests/tokio_unix.rs @@ -0,0 +1,46 @@ +#![cfg(all(unix, feature = "tokio1"))] + +use std::{ + io::Result, + process::Stdio, +}; + +use process_wrap::tokio::*; +use tokio::{ + io::AsyncReadExt, + process::Command, +}; + +// each test has a _nowrap variant that uses the process-wrap API but doesn't apply any Wrappers for comparison/debugging. + +#[tokio::test] +async fn inner_read_stdout_nowrap() -> Result<()> { + let mut command = Command::new("echo"); + command.arg("hello").stdout(Stdio::piped()); + let mut child = TokioCommandWrap::new(command).spawn()?; + + let mut output = String::new(); + if let Some(mut out) = child.stdout().take() { + out.read_to_string(&mut output).await?; + } + + assert_eq!(output.as_str(), "hello\n"); + Ok(()) +} + +#[tokio::test] +async fn inner_read_stdout_process_group() -> Result<()> { + let mut command = Command::new("echo"); + command.arg("hello").stdout(Stdio::piped()); + let mut command = TokioCommandWrap::new(command); + command.wrap(ProcessGroup::leader()); + + let mut child = command.spawn()?; + let mut output = String::new(); + if let Some(mut out) = child.stdout().take() { + out.read_to_string(&mut output).await?; + } + + assert_eq!(output.as_str(), "hello\n"); + Ok(()) +}