From 9680ecc109e13b0afa3e7e51168d02912441f7aa Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 18 Dec 2016 22:48:18 -0800 Subject: [PATCH] process: Share init in tests --- tests/smoke.rs | 39 +++------------------------------------ tests/stdio.rs | 17 ++++++----------- tests/support/mod.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 47 deletions(-) create mode 100644 tests/support/mod.rs diff --git a/tests/smoke.rs b/tests/smoke.rs index 142e1a444..a914e6c7f 100644 --- a/tests/smoke.rs +++ b/tests/smoke.rs @@ -1,50 +1,17 @@ -extern crate futures; extern crate tokio_core; extern crate tokio_process; -use std::env; -use std::sync::mpsc::channel; -use std::sync::{Once, ONCE_INIT}; -use std::thread; -use std::process::Command; - use tokio_core::reactor::Core; use tokio_process::CommandExt; -static INIT: Once = ONCE_INIT; - -fn init() { - INIT.call_once(|| { - let (tx, rx) = channel(); - thread::spawn(move || { - let mut lp = Core::new().unwrap(); - let mut cmd = exit(); - let mut child = cmd.spawn_async(&lp.handle()).unwrap(); - drop(child.kill()); - lp.run(child).unwrap(); - tx.send(()).unwrap(); - drop(lp.run(futures::empty::<(), ()>())); - }); - rx.recv().unwrap(); - }); -} - -fn exit() -> Command { - let mut me = env::current_exe().unwrap(); - me.pop(); - if me.ends_with("deps") { - me.pop(); - } - me.push("exit"); - Command::new(me) -} +mod support; #[test] fn simple() { - init(); + support::init(); let mut lp = Core::new().unwrap(); - let mut cmd = exit(); + let mut cmd = support::cmd("exit"); cmd.arg("2"); let mut child = cmd.spawn_async(&lp.handle()).unwrap(); let id = child.id(); diff --git a/tests/stdio.rs b/tests/stdio.rs index c95fcb4fb..8109af19c 100644 --- a/tests/stdio.rs +++ b/tests/stdio.rs @@ -6,7 +6,6 @@ extern crate tokio_process; extern crate log; extern crate env_logger; -use std::env; use std::io; use std::process::{Stdio, ExitStatus, Command}; @@ -16,14 +15,10 @@ use tokio_core::io::{read_until, write_all, read_to_end}; use tokio_core::reactor::Core; use tokio_process::{CommandExt, Child}; +mod support; + fn cat() -> Command { - let mut path = env::current_exe().unwrap(); - path.pop(); - if path.ends_with("deps") { - path.pop(); - } - path.push("cat"); - let mut cmd = Command::new(path); + let mut cmd = support::cmd("cat"); cmd.stdin(Stdio::piped()) .stdout(Stdio::piped()); cmd @@ -88,7 +83,7 @@ fn feed_cat(mut cat: Child, n: usize) -> BoxFuture { /// /// - The child does produce EOF on stdout after the last line. fn feed_a_lot() { - let _ = ::env_logger::init(); + support::init(); let mut lp = Core::new().unwrap(); let child = cat().spawn_async(&lp.handle()).unwrap(); @@ -98,7 +93,7 @@ fn feed_a_lot() { #[test] fn drop_kills() { - let _ = ::env_logger::init(); + support::init(); let mut lp = Core::new().unwrap(); let mut child = cat().spawn_async(&lp.handle()).unwrap(); @@ -114,7 +109,7 @@ fn drop_kills() { #[test] fn wait_with_output_captures() { - let _ = ::env_logger::init(); + support::init(); let mut core = Core::new().unwrap(); diff --git a/tests/support/mod.rs b/tests/support/mod.rs new file mode 100644 index 000000000..79f213d1c --- /dev/null +++ b/tests/support/mod.rs @@ -0,0 +1,43 @@ +extern crate env_logger; +extern crate futures; +extern crate tokio_core; +extern crate tokio_process; + +use std::env; +use std::process::Command; +use std::sync::{Once, ONCE_INIT}; +use std::thread; +use std::sync::mpsc::channel; + +use self::tokio_core::reactor::Core; +use self::futures::future; +use self::tokio_process::CommandExt; + +pub fn init() { + static INIT: Once = ONCE_INIT; + + INIT.call_once(|| { + drop(env_logger::init()); + let (tx, rx) = channel(); + thread::spawn(move || { + let mut lp = Core::new().unwrap(); + let mut cmd = cmd("exit"); + let mut child = cmd.spawn_async(&lp.handle()).unwrap(); + drop(child.kill()); + lp.run(child).unwrap(); + tx.send(()).unwrap(); + drop(lp.run(future::empty::<(), ()>())); + }); + rx.recv().unwrap(); + }); +} + +pub fn cmd(s: &str) -> Command { + let mut me = env::current_exe().unwrap(); + me.pop(); + if me.ends_with("deps") { + me.pop(); + } + me.push(s); + Command::new(me) +}