Add process::spawn function

This commit is contained in:
Matt Green 2016-11-23 12:59:56 -05:00
parent 98c2698798
commit 54baf4e384
2 changed files with 17 additions and 13 deletions

View File

@ -120,7 +120,11 @@ fn main() {
warn!("Polling for changes every {} ms", args.poll_interval);
}
watcher.watch(cwd).expect("unable to watch cwd");
let result = watcher.watch(cwd);
if let Err(e) = result {
error!("Unable to watch directory/subdirectory: {}", e);
return;
}
// Start child process initially, if necessary
if args.run_initially {
@ -129,7 +133,7 @@ fn main() {
}
let mut guard = child_process.write().unwrap();
*guard = Process::new(&args.cmd, vec![]).ok();
*guard = Some(process::spawn(&args.cmd, vec![]));
}
loop {
@ -160,7 +164,7 @@ fn main() {
{
let mut guard = child_process.write().unwrap();
*guard = Process::new(&args.cmd, paths).ok();
*guard = Some(process::spawn(&args.cmd, paths));
}
}
}

View File

@ -1,6 +1,10 @@
use std::path::PathBuf;
pub use self::imp::*;
pub fn spawn(cmd: &str, updated_paths: Vec<PathBuf>) -> Process {
self::imp::Process::new(cmd, updated_paths).expect("unable to spawn process")
}
pub use self::imp::Process;
#[cfg(target_family = "unix")]
mod imp {
@ -224,7 +228,7 @@ mod tests {
use mktemp::Temp;
use super::imp::Process;
use super::spawn;
use super::get_longest_common_path;
fn file_contents(path: &Path) -> String {
@ -240,17 +244,14 @@ mod tests {
#[test]
fn test_start() {
let process = Process::new("echo hi", vec![]);
assert!(process.is_ok());
let _ = spawn("echo hi", vec![]);
}
#[test]
fn test_wait() {
let file = Temp::new_file().unwrap();
let path = file.to_path_buf();
let process = Process::new(&format!("echo hi > {}", path.to_str().unwrap()), vec![])
.unwrap();
let process = spawn(&format!("echo hi > {}", path.to_str().unwrap()), vec![]);
process.wait();
assert!(file_contents(&path).starts_with("hi"));
@ -261,9 +262,8 @@ mod tests {
let file = Temp::new_file().unwrap();
let path = file.to_path_buf();
let process = Process::new(&format!("sleep 20; echo hi > {}", path.to_str().unwrap()),
vec![])
.unwrap();
let process = spawn(&format!("sleep 20; echo hi > {}", path.to_str().unwrap()),
vec![]);
process.kill();
process.wait();