Skip to content

Commit

Permalink
wip: start adding back tests
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Mar 9, 2024
1 parent 2d4c4e3 commit 3c1b517
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/tokio/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl TokioCommandWrap {
}
}

pub fn wrap<W: TokioCommandWrapper + 'static>(&mut self, wrapper: W) {
pub fn wrap<W: TokioCommandWrapper + 'static>(&mut self, wrapper: W) -> &mut Self {
let typeid = TypeId::of::<W>();
let mut wrapper = Some(Box::new(wrapper));
let extant = self
Expand All @@ -37,6 +37,8 @@ impl TokioCommandWrap {
if let Some(wrapper) = wrapper {
extant.extend(wrapper);
}

self
}

pub fn spawn(mut self) -> Result<Box<dyn TokioChildWrapper>> {
Expand Down
46 changes: 46 additions & 0 deletions tests/tokio_unix.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 3c1b517

Please sign in to comment.