Skip to content

Commit

Permalink
wip: port std_unix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
passcod committed Mar 11, 2024
1 parent 2f4bba2 commit 491931d
Show file tree
Hide file tree
Showing 13 changed files with 606 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/std_unix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#![cfg(all(unix, feature = "std"))]
#[path = "std_unix/mod.rs"]
mod std_unix;
39 changes: 39 additions & 0 deletions tests/std_unix/id_same_as_inner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use super::prelude::*;

#[test]
fn nowrap() -> Result<()> {
let child = StdCommandWrap::with_new("echo", |command| {
command.stdout(Stdio::null());
})
.spawn()?;

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

Ok(())
}

#[test]
fn process_group() -> Result<()> {
let child = StdCommandWrap::with_new("echo", |command| {
command.stdout(Stdio::null());
})
.wrap(ProcessGroup::leader())
.spawn()?;

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

Ok(())
}

#[test]
fn process_session() -> Result<()> {
let child = StdCommandWrap::with_new("echo", |command| {
command.stdout(Stdio::null());
})
.wrap(ProcessSession)
.spawn()?;

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

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

#[test]
fn nowrap() -> Result<()> {
let mut child = StdCommandWrap::with_new("echo", |command| {
command.arg("hello").stdout(Stdio::piped());
})
.spawn()?;

let mut output = String::new();
if let Some(mut out) = child.stdout().take() {
out.read_to_string(&mut output)?;
}

assert_eq!(output.as_str(), "hello\n");
Ok(())
}

#[test]
fn process_group() -> Result<()> {
let mut child = StdCommandWrap::with_new("echo", |command| {
command.arg("hello").stdout(Stdio::piped());
})
.wrap(ProcessGroup::leader())
.spawn()?;

let mut output = String::new();
if let Some(mut out) = child.stdout().take() {
out.read_to_string(&mut output)?;
}

assert_eq!(output.as_str(), "hello\n");
Ok(())
}

#[test]
fn process_session() -> Result<()> {
let mut child = StdCommandWrap::with_new("echo", |command| {
command.arg("hello").stdout(Stdio::piped());
})
.wrap(ProcessSession)
.spawn()?;

let mut output = String::new();
if let Some(mut out) = child.stdout().take() {
out.read_to_string(&mut output)?;
}

assert_eq!(output.as_str(), "hello\n");
Ok(())
}
66 changes: 66 additions & 0 deletions tests/std_unix/into_inner_write_stdin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use super::prelude::*;

#[test]
fn nowrap() -> Result<()> {
let mut child = StdCommandWrap::with_new("cat", |command| {
command.stdin(Stdio::piped()).stdout(Stdio::piped());
})
.spawn()?
.into_inner();

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

let mut output = String::new();
if let Some(mut out) = child.stdout.take() {
out.read_to_string(&mut output)?;
}

assert_eq!(output.as_str(), "hello");
Ok(())
}

#[test]
fn process_group() -> Result<()> {
let mut child = StdCommandWrap::with_new("cat", |command| {
command.stdin(Stdio::piped()).stdout(Stdio::piped());
})
.wrap(ProcessGroup::leader())
.spawn()?
.into_inner();

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

let mut output = String::new();
if let Some(mut out) = child.stdout.take() {
out.read_to_string(&mut output)?;
}

assert_eq!(output.as_str(), "hello");
Ok(())
}

#[test]
fn process_session() -> Result<()> {
let mut child = StdCommandWrap::with_new("cat", |command| {
command.stdin(Stdio::piped()).stdout(Stdio::piped());
})
.wrap(ProcessSession)
.spawn()?
.into_inner();

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

let mut output = String::new();
if let Some(mut out) = child.stdout.take() {
out.read_to_string(&mut output)?;
}

assert_eq!(output.as_str(), "hello");
Ok(())
}
61 changes: 61 additions & 0 deletions tests/std_unix/kill_and_try_wait.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use super::prelude::*;

#[test]
fn nowrap() -> Result<()> {
let mut child = StdCommandWrap::with_new("yes", |command| {
command.stdout(Stdio::null());
})
.spawn()?;
assert!(child.try_wait()?.is_none(), "pre kill");

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

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

Ok(())
}

#[test]
fn process_group() -> Result<()> {
let mut child = StdCommandWrap::with_new("yes", |command| {
command.stdout(Stdio::null());
})
.wrap(ProcessGroup::leader())
.spawn()?;
assert!(child.try_wait()?.is_none(), "pre kill");

child.kill()?;

let status = (child.wait())?;
assert!(!status.success());

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

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

Ok(())
}

#[test]
fn process_session() -> Result<()> {
let mut child = StdCommandWrap::with_new("yes", |command| {
command.stdout(Stdio::null());
})
.wrap(ProcessSession)
.spawn()?;
assert!(child.try_wait()?.is_none(), "pre kill");

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

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

Ok(())
}
26 changes: 26 additions & 0 deletions tests/std_unix/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
mod prelude {
pub use std::{
io::{Read, Result, Write},
os::unix::process::ExitStatusExt,
process::Stdio,
thread::sleep,
time::Duration,
};

pub use nix::sys::signal::Signal;
pub use process_wrap::std::*;

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 signals;
mod try_wait_after_die;
mod try_wait_twice_after_sigterm;
mod wait_after_die;
mod wait_twice;
mod wait_twice_after_sigterm;
mod wait_with_output;
57 changes: 57 additions & 0 deletions tests/std_unix/signals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use super::prelude::*;

#[test]
fn nowrap() -> Result<()> {
let mut child = StdCommandWrap::with_new("yes", |command| {
command.stdout(Stdio::null());
})
.spawn()?;

child.signal(Signal::SIGCONT)?;
sleep(DIE_TIME);
assert!(child.try_wait()?.is_none(), "not exited with sigcont");

child.signal(Signal::SIGTERM)?;
sleep(DIE_TIME);
assert!(child.try_wait()?.is_some(), "exited with sigterm");

Ok(())
}

#[test]
fn process_group() -> Result<()> {
let mut child = StdCommandWrap::with_new("yes", |command| {
command.stdout(Stdio::null());
})
.wrap(ProcessGroup::leader())
.spawn()?;

child.signal(Signal::SIGCONT)?;
sleep(DIE_TIME);
assert!(child.try_wait()?.is_none(), "not exited with sigcont");

child.signal(Signal::SIGTERM)?;
sleep(DIE_TIME);
assert!(child.try_wait()?.is_some(), "exited with sigterm");

Ok(())
}

#[test]
fn process_session() -> Result<()> {
let mut child = StdCommandWrap::with_new("yes", |command| {
command.stdout(Stdio::null());
})
.wrap(ProcessSession)
.spawn()?;

child.signal(Signal::SIGCONT)?;
sleep(DIE_TIME);
assert!(child.try_wait()?.is_none(), "not exited with sigcont");

child.signal(Signal::SIGTERM)?;
sleep(DIE_TIME);
assert!(child.try_wait()?.is_some(), "exited with sigterm");

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

#[test]
fn nowrap() -> Result<()> {
let mut child = StdCommandWrap::with_new("echo", |command| {
command.stdout(Stdio::null());
})
.spawn()?;
sleep(DIE_TIME);
let status = child.try_wait()?;
assert!(status.is_some());
assert!(status.unwrap().success());

Ok(())
}

#[test]
fn process_group() -> Result<()> {
let mut child = StdCommandWrap::with_new("echo", |command| {
command.stdout(Stdio::null());
})
.wrap(ProcessGroup::leader())
.spawn()?;
sleep(DIE_TIME);
let status = child.try_wait()?;
assert!(status.is_some());
assert!(status.unwrap().success());

Ok(())
}

#[test]
fn process_session() -> Result<()> {
let mut child = StdCommandWrap::with_new("echo", |command| {
command.stdout(Stdio::null());
})
.wrap(ProcessSession)
.spawn()?;
sleep(DIE_TIME);
let status = child.try_wait()?;
assert!(status.is_some());
assert!(status.unwrap().success());

Ok(())
}
Loading

0 comments on commit 491931d

Please sign in to comment.