2022-12-27 20:16:18 -08:00
|
|
|
use {
|
|
|
|
super::*,
|
|
|
|
std::time::{Duration, Instant},
|
|
|
|
};
|
2019-04-12 00:46:29 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
fn kill(process_id: u32) {
|
|
|
|
unsafe {
|
|
|
|
libc::kill(process_id as i32, libc::SIGINT);
|
2018-08-27 16:03:52 -07:00
|
|
|
}
|
2022-06-18 21:56:31 -07:00
|
|
|
}
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
fn interrupt_test(arguments: &[&str], justfile: &str) {
|
|
|
|
let tmp = tempdir();
|
|
|
|
let mut justfile_path = tmp.path().to_path_buf();
|
|
|
|
justfile_path.push("justfile");
|
|
|
|
fs::write(justfile_path, unindent(justfile)).unwrap();
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
let start = Instant::now();
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-12-15 16:53:21 -08:00
|
|
|
let mut child = Command::new(executable_path("just"))
|
2023-01-12 23:53:14 -08:00
|
|
|
.current_dir(&tmp)
|
2022-06-18 21:56:31 -07:00
|
|
|
.args(arguments)
|
|
|
|
.spawn()
|
|
|
|
.expect("just invocation failed");
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
while start.elapsed() < Duration::from_millis(500) {}
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
kill(child.id());
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
let status = child.wait().unwrap();
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
let elapsed = start.elapsed();
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
if elapsed > Duration::from_secs(2) {
|
2023-01-26 18:49:03 -08:00
|
|
|
panic!("process returned too late: {elapsed:?}");
|
2022-06-18 21:56:31 -07:00
|
|
|
}
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
if elapsed < Duration::from_millis(100) {
|
2023-01-26 18:49:03 -08:00
|
|
|
panic!("process returned too early : {elapsed:?}");
|
2019-04-12 00:46:29 -07:00
|
|
|
}
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
assert_eq!(status.code(), Some(130));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn interrupt_shebang() {
|
|
|
|
interrupt_test(
|
|
|
|
&[],
|
|
|
|
"
|
2021-05-09 20:35:35 -07:00
|
|
|
default:
|
|
|
|
#!/usr/bin/env sh
|
|
|
|
sleep 1
|
|
|
|
",
|
2022-06-18 21:56:31 -07:00
|
|
|
);
|
|
|
|
}
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn interrupt_line() {
|
|
|
|
interrupt_test(
|
|
|
|
&[],
|
|
|
|
"
|
2021-05-09 20:35:35 -07:00
|
|
|
default:
|
|
|
|
@sleep 1
|
|
|
|
",
|
2022-06-18 21:56:31 -07:00
|
|
|
);
|
|
|
|
}
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn interrupt_backtick() {
|
|
|
|
interrupt_test(
|
|
|
|
&[],
|
|
|
|
"
|
2021-05-09 20:35:35 -07:00
|
|
|
foo := `sleep 1`
|
2018-08-27 16:03:52 -07:00
|
|
|
|
2021-05-09 20:35:35 -07:00
|
|
|
default:
|
|
|
|
@echo {{foo}}
|
|
|
|
",
|
2022-06-18 21:56:31 -07:00
|
|
|
);
|
|
|
|
}
|
2021-05-09 20:35:35 -07:00
|
|
|
|
2022-06-18 21:56:31 -07:00
|
|
|
#[test]
|
|
|
|
#[ignore]
|
|
|
|
fn interrupt_command() {
|
|
|
|
interrupt_test(&["--command", "sleep", "1"], "");
|
2018-08-27 16:03:52 -07:00
|
|
|
}
|