1
1
mirror of https://github.com/casey/just.git synced 2024-11-23 11:04:09 +03:00
just/tests/interrupts.rs

85 lines
1.4 KiB
Rust
Raw Normal View History

#[cfg(unix)]
2019-04-12 10:46:29 +03:00
mod unix {
use executable_path::executable_path;
use std::{
fs,
2019-04-12 10:46:29 +03:00
process::Command,
time::{Duration, Instant},
};
use test_utilities::tempdir;
2019-04-12 10:46:29 +03:00
fn kill(process_id: u32) {
unsafe {
libc::kill(process_id as i32, libc::SIGINT);
}
}
2019-04-12 10:46:29 +03:00
fn interrupt_test(justfile: &str) {
let tmp = tempdir();
2019-04-12 10:46:29 +03:00
let mut justfile_path = tmp.path().to_path_buf();
justfile_path.push("justfile");
fs::write(justfile_path, justfile).unwrap();
2019-04-12 10:46:29 +03:00
let start = Instant::now();
2019-04-12 10:46:29 +03:00
let mut child = Command::new(&executable_path("just"))
.current_dir(&tmp)
.spawn()
.expect("just invocation failed");
2019-04-12 10:46:29 +03:00
while start.elapsed() < Duration::from_millis(500) {}
2019-04-12 10:46:29 +03:00
kill(child.id());
2019-04-12 10:46:29 +03:00
let status = child.wait().unwrap();
2019-04-12 10:46:29 +03:00
let elapsed = start.elapsed();
2019-04-12 10:46:29 +03:00
if elapsed > Duration::from_secs(2) {
panic!("process returned too late: {:?}", elapsed);
}
2019-04-12 10:46:29 +03:00
if elapsed < Duration::from_millis(100) {
panic!("process returned too early : {:?}", elapsed);
}
2019-04-12 10:46:29 +03:00
assert_eq!(status.code(), Some(130));
}
2019-04-12 10:46:29 +03:00
#[test]
#[ignore]
2019-04-12 10:46:29 +03:00
fn interrupt_shebang() {
interrupt_test(
"
default:
#!/usr/bin/env sh
sleep 1
",
2019-04-12 10:46:29 +03:00
);
}
2019-04-12 10:46:29 +03:00
#[test]
#[ignore]
2019-04-12 10:46:29 +03:00
fn interrupt_line() {
interrupt_test(
"
default:
@sleep 1
",
2019-04-12 10:46:29 +03:00
);
}
2019-04-12 10:46:29 +03:00
#[test]
#[ignore]
fn interrupt_backtick() {
interrupt_test(
"
foo := `sleep 1`
default:
2019-04-12 10:46:29 +03:00
@echo {{foo}}
",
2019-04-12 10:46:29 +03:00
);
}
}