Skip to content

Commit

Permalink
wip: port windows tests
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Mar 11, 2024
1 parent eb86fd3 commit a46123c
Show file tree
Hide file tree
Showing 21 changed files with 301 additions and 2 deletions.
1 change: 1 addition & 0 deletions tests/tokio_unix/id_same_as_inner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let child = TokioCommandWrap::with_new("echo", |command| {
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/inner_read_stdout.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("echo", |command| {
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/into_inner_write_stdin.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("cat", |command| {
Expand Down
5 changes: 3 additions & 2 deletions tests/tokio_unix/kill_and_try_wait.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("yes", |command| {
Expand All @@ -18,7 +19,7 @@ async fn nowrap() -> Result<()> {
}

#[tokio::test]
async fn group_process_group() -> Result<()> {
async fn process_group() -> Result<()> {
let mut child = TokioCommandWrap::with_new("yes", |command| {
command.stdout(Stdio::null());
})
Expand All @@ -41,7 +42,7 @@ async fn group_process_group() -> Result<()> {
}

#[tokio::test]
async fn group_process_session() -> Result<()> {
async fn process_session() -> Result<()> {
let mut child = TokioCommandWrap::with_new("yes", |command| {
command.stdout(Stdio::null());
})
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/signals.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("yes", |command| {
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/try_wait_after_die.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("echo", |command| {
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/try_wait_twice_after_sigterm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("yes", |command| {
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/wait_after_die.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("echo", |command| {
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/wait_twice.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("echo", |command| {
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/wait_twice_after_sigterm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("yes", |command| {
Expand Down
1 change: 1 addition & 0 deletions tests/tokio_unix/wait_with_output.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let child = TokioCommandWrap::with_new("echo", |command| {
Expand Down
3 changes: 3 additions & 0 deletions tests/tokio_windows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![cfg(all(windows, feature = "tokio1"))]
#[path = "tokio_windows/mod.rs"]
mod tokio_windows;
26 changes: 26 additions & 0 deletions tests/tokio_windows/id_same_as_inner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::null());
})
.spawn()?;

assert_eq!(child.id(), child.inner().id());

Ok(())
}

#[tokio::test]
async fn process_job_object() -> Result<()> {
let child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::null());
})
.wrap(JobObject)
.spawn()?;

assert_eq!(child.id(), child.inner().id());

Ok(())
}
34 changes: 34 additions & 0 deletions tests/tokio_windows/inner_read_stdout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::piped());
})
.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 process_job_object() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::piped());
})
.wrap(JobObject)
.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(())
}
43 changes: 43 additions & 0 deletions tests/tokio_windows/into_inner_write_stdin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("findstr", |command| {
command.arg("^").stdin(Stdio::piped()).stdout(Stdio::piped());
})
.spawn()?;

if let Some(mut din) = child.stdin().take() {
din.write_all(b"hello").await?;
}

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");
Ok(())
}

#[tokio::test]
async fn job_object() -> Result<()> {
let mut child = TokioCommandWrap::with_new("findstr", |command| {
command.arg("^").stdin(Stdio::piped()).stdout(Stdio::piped());
})
.wrap(JobObject)
.spawn()?
.into_inner();

if let Some(mut din) = child.stdin().take() {
din.write_all(b"hello").await?;
}

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");
Ok(())
}
38 changes: 38 additions & 0 deletions tests/tokio_windows/kill_and_try_wait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("pause").stdout(Stdio::null());
})
.spawn()?;
assert!(child.try_wait()?.is_none(), "pre kill");

Box::into_pin(child.kill()).await?;
sleep(DIE_TIME).await;
assert!(child.try_wait()?.is_some(), "try_wait() one");

sleep(DIE_TIME).await;
assert!(child.try_wait()?.is_some(), "try_wait() two");

Ok(())
}

#[tokio::test]
async fn job_object() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("pause").stdout(Stdio::null());
})
.wrap(JobObject)
.spawn()?;
assert!(child.try_wait()?.is_none(), "pre kill");

Box::into_pin(child.kill()).await?;
sleep(DIE_TIME).await;
assert!(child.try_wait()?.is_some(), "try_wait() one");

sleep(DIE_TIME).await;
assert!(child.try_wait()?.is_some(), "try_wait() two");

Ok(())
}
20 changes: 20 additions & 0 deletions tests/tokio_windows/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mod prelude {
pub use std::{io::Result, process::Stdio, time::Duration};

pub use process_wrap::tokio::*;
pub use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
time::sleep,
};

pub const DIE_TIME: Duration = Duration::from_millis(100);
}

mod id_same_as_inner;
mod inner_read_stdout;
mod into_inner_write_stdin;
mod kill_and_try_wait;
mod try_wait_after_die;
mod wait_after_die;
mod wait_twice;
mod wait_with_output;
30 changes: 30 additions & 0 deletions tests/tokio_windows/try_wait_after_die.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::null());
})
.spawn()?;
sleep(DIE_TIME).await;
let status = child.try_wait()?;
assert!(status.is_some());
assert!(status.unwrap().success());

Ok(())
}

#[tokio::test]
async fn job_object() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::null());
})
.wrap(JobObject)
.spawn()?;
sleep(DIE_TIME).await;
let status = child.try_wait()?;
assert!(status.is_some());
assert!(status.unwrap().success());

Ok(())
}
30 changes: 30 additions & 0 deletions tests/tokio_windows/wait_after_die.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::null());
})
.spawn()?;
sleep(DIE_TIME).await;

let status = Box::into_pin(child.wait()).await?;
assert!(status.success());

Ok(())
}

#[tokio::test]
async fn job_object() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::null());
})
.wrap(JobObject)
.spawn()?;
sleep(DIE_TIME).await;

let status = Box::into_pin(child.wait()).await?;
assert!(status.success());

Ok(())
}
34 changes: 34 additions & 0 deletions tests/tokio_windows/wait_twice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::null());
})
.spawn()?;

let status = Box::into_pin(child.wait()).await?;
assert!(status.success());

let status = Box::into_pin(child.wait()).await?;
assert!(status.success());

Ok(())
}

#[tokio::test]
async fn job_object() -> Result<()> {
let mut child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::null());
})
.wrap(JobObject)
.spawn()?;

let status = Box::into_pin(child.wait()).await?;
assert!(status.success());

let status = Box::into_pin(child.wait()).await?;
assert!(status.success());

Ok(())
}
30 changes: 30 additions & 0 deletions tests/tokio_windows/wait_with_output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use super::prelude::*;

#[tokio::test]
async fn nowrap() -> Result<()> {
let child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::piped());
})
.spawn()?;

let output = Box::into_pin(child.wait_with_output()).await?;
assert!(output.status.success());
assert_eq!(output.stdout, b"hello\r\n".to_vec());
assert_eq!(output.stderr, Vec::new());
Ok(())
}

#[tokio::test]
async fn job_object() -> Result<()> {
let child = TokioCommandWrap::with_new("powershell.exe", |command| {
command.arg("/C").arg("echo hello").stdout(Stdio::piped());
})
.wrap(JobObject)
.spawn()?;

let output = Box::into_pin(child.wait_with_output()).await?;
assert!(output.status.success());
assert_eq!(output.stdout, b"hello\r\n".to_vec());
assert_eq!(output.stderr, Vec::new());
Ok(())
}

0 comments on commit a46123c

Please sign in to comment.