1
1
mirror of https://github.com/casey/just.git synced 2024-11-23 20:15:05 +03:00
just/tests/interrupts.rs
Casey Rodarmor aefdcea7d0
Gargantuan refactor (#522)
- Instead of changing the current directory with `env::set_current_dir`
  to be implicitly inherited by subprocesses, we now use
  `Command::current_dir` to set it explicitly. This feels much better,
  since we aren't dependent on the implicit state of the process's
  current directory.

- Subcommand execution is much improved.

- Added a ton of tests for config parsing, config execution, working
  dir, and search dir.

- Error messages are improved. Many more will be colored.

- The Config is now onwed, instead of borrowing from the arguments and
  the `clap::ArgMatches` object. This is a huge ergonomic improvement,
  especially in tests, and I don't think anyone will notice.

- `--edit` now uses `$VISUAL`, `$EDITOR`, or `vim`, in that order,
  matching git, which I think is what most people will expect.

- Added a cute `tmptree!{}` macro, for creating temporary directories
  populated with directories and files for tests.

- Admitted that grammer is LL(k) and I don't know what `k` is.
2019-11-09 21:43:20 -08:00

85 lines
1.4 KiB
Rust

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