Add -1 option for integration testing, closes #35

This commit is contained in:
Matt Green 2017-01-27 15:27:44 -05:00
parent 0d5de7c4da
commit b2b0a60ca5
2 changed files with 33 additions and 20 deletions

View File

@ -14,6 +14,7 @@ pub struct Args {
pub debug: bool,
pub run_initially: bool,
pub no_vcs_ignore: bool,
pub once: bool,
pub poll: bool,
pub poll_interval: u32,
}
@ -86,6 +87,9 @@ pub fn get_args() -> Args {
.help("Send SIGKILL to child processes")
.short("k")
.long("kill"))
.arg(Arg::with_name("once")
.short("1")
.hidden(true))
.get_matches();
let cmd = values_t!(args.values_of("command"), String).unwrap().join(" ");
@ -125,6 +129,7 @@ pub fn get_args() -> Args {
debug: args.is_present("debug"),
run_initially: !args.is_present("postpone"),
no_vcs_ignore: args.is_present("no-vcs-ignore"),
once: args.is_present("once"),
poll: args.occurrences_of("poll") > 0,
poll_interval: poll_interval,
}

View File

@ -108,7 +108,7 @@ fn main() {
}
// Start child process initially, if necessary
if args.run_initially {
if args.run_initially && !args.once {
if args.clear_screen {
cli::clear_screen();
}
@ -119,29 +119,13 @@ fn main() {
loop {
debug!("Waiting for filesystem activity");
let paths = wait(&rx, &filter);
let paths = wait_fs(&rx, &filter);
if let Some(path) = paths.get(0) {
debug!("Path updated: {:?}", path);
}
// Wait for current child process to exit
{
let guard = child_process.read().unwrap();
if let Some(ref child) = *guard {
if args.restart {
debug!("Stopping child process");
if kill {
child.kill();
} else {
child.terminate();
}
}
debug!("Waiting for process to exit...");
child.wait();
}
}
wait_process(&child_process, kill, args.restart);
// Launch child process
if args.clear_screen {
@ -153,10 +137,16 @@ fn main() {
let mut guard = child_process.write().unwrap();
*guard = Some(process::spawn(&args.cmd, paths));
}
// Handle once option for integration testing
if args.once {
wait_process(&child_process, kill, false);
break;
}
}
}
fn wait(rx: &Receiver<Event>, filter: &NotificationFilter) -> Vec<PathBuf> {
fn wait_fs(rx: &Receiver<Event>, filter: &NotificationFilter) -> Vec<PathBuf> {
let mut paths = vec![];
let mut cache = HashMap::new();
@ -199,3 +189,21 @@ fn wait(rx: &Receiver<Event>, filter: &NotificationFilter) -> Vec<PathBuf> {
paths
}
fn wait_process(process: &RwLock<Option<Process>>, kill: bool, restart: bool) {
let guard = process.read().unwrap();
if let Some(ref child) = *guard {
if restart {
debug!("Stopping child process");
if kill {
child.kill();
} else {
child.terminate();
}
}
debug!("Waiting for process to exit...");
child.wait();
}
}