Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test runner: cleanup on Ctrl+C/SIGINT #54

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions tests/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ regex = "~1.9"
scraper = "0.17.1"
clap = { version = "4", features = ["derive"] }
tempfile = "3.8.0"
ctrlc = "3.4.1"
wait-timeout = "0.2.0"
69 changes: 60 additions & 9 deletions tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,33 @@ use std::{
io::{self, ErrorKind},
os::unix::fs as ufs,
path::Path,
process::Command,
process::{Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering::Relaxed},
Arc,
},
time::Duration,
};
use wait_timeout::ChildExt;

#[derive(Parser)]
struct Arguments {
#[arg(name = "PATTERN", help = "Only run tests that match <PATTERN>")]
filter: Option<Regex>,
}

fn main() -> io::Result<()> {
fn _main() -> io::Result<()> {
let arguments = Arguments::parse();

let interrupted = Arc::new(AtomicBool::new(false));
let _interrupted = Arc::clone(&interrupted);

ctrlc::set_handler(move || {
_interrupted.store(true, Relaxed);
eprintln!("Received Ctrl+C");
})
.unwrap();

let tempdir = tempfile::tempdir().unwrap();

let case_dir = Path::new("cases");
Expand Down Expand Up @@ -55,17 +70,40 @@ fn main() -> io::Result<()> {

let output = tempdir.path().join(format!("{}.output.html", name));

let status = Command::new("vim")
let mut vim = Command::new("vim")
.args(["--not-a-term", "-S", "convert-to-html.vim"])
.env("CASE", case)
.env("OUTPUT", &output)
.env("HOME", env::current_dir().unwrap())
.output()
.unwrap()
.status;
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.unwrap();

let mut poll_count = 1;
let status = loop {
let poll_interval = Duration::from_millis(if poll_count % 3 == 0 { 333 } else { 100 });
match vim.wait_timeout(poll_interval) {
Ok(Some(status)) => break status,
Ok(None) => {
if interrupted.load(Relaxed) {
vim.kill().unwrap();
return Err(io::Error::new(ErrorKind::Interrupted, "interrupted!"));
}
}
Err(e) => {
return Err(e);
}
}
poll_count += 1;
};

if !status.success() {
panic!("Vim failed with status: {}", status);
return Err(io::Error::new(
ErrorKind::Other,
format!("Vim failed with status: {}", status),
));
}

let html = Html::parse_document(&fs::read_to_string(&output).unwrap());
Expand Down Expand Up @@ -107,6 +145,10 @@ fn main() -> io::Result<()> {
}
};

if interrupted.load(Relaxed) {
return Err(io::Error::new(ErrorKind::Interrupted, "interrupted!"));
}

if diff_output.status.success() {
eprintln!("ok");
passed += 1;
Expand All @@ -121,8 +163,6 @@ fn main() -> io::Result<()> {
}
}

fs::remove_file(".vim").unwrap();

if passed == cases {
Ok(())
} else {
Expand All @@ -132,3 +172,14 @@ fn main() -> io::Result<()> {
))
}
}

fn main() -> io::Result<()> {
let real_main = _main();

// cleanup
if fs::metadata(".vim").is_ok() {
fs::remove_file(".vim").unwrap();
}

real_main
}