Skip to content

Commit

Permalink
tests: unify code for running Vim
Browse files Browse the repository at this point in the history
  • Loading branch information
laniakea64 committed Jan 25, 2024
1 parent 58afdbe commit 8409fbf
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 81 deletions.
3 changes: 0 additions & 3 deletions tests/convert-to-html.vim
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ hi Type ctermfg=7*
hi Typedef ctermfg=7*
hi Underlined ctermfg=7*

" edit the input justfile
edit $CASE

" convert justfile to HTML
TOhtml

Expand Down
47 changes: 46 additions & 1 deletion tests/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use std::{
fs,
env, fs,
io::{self, ErrorKind},
os::unix::fs as ufs,
path::PathBuf,
process::{Command, Stdio},
sync::{
atomic::{AtomicBool, Ordering::Relaxed},
Arc,
},
time::Duration,
};
use wait_timeout::ChildExt;

pub fn clean_dotvim_symlink() {
if fs::metadata(".vim").is_ok() {
Expand All @@ -30,3 +35,43 @@ pub fn setup_ctrlc_handler() -> Arc<AtomicBool> {

interrupted
}

pub fn run_vim(args: Vec<&str>, output: &PathBuf, interrupted: &Arc<AtomicBool>) -> io::Result<()> {
let mut vim = Command::new("vim")
.arg("--not-a-term")
.args(args)
.env("OUTPUT", output)
.env("HOME", env::current_dir().unwrap())
.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() {
Ok(())
} else {
Err(io::Error::new(
ErrorKind::Other,
format!("Vim failed with status: {}", status),
))
}
}
46 changes: 7 additions & 39 deletions tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@ use fancy_regex::Regex;
use rayon::prelude::*;
use scraper::{Html, Selector};
use std::{
env,
ffi::OsStr,
fs,
io::{self, ErrorKind},
path::{Path, PathBuf},
process::{Command, Stdio},
process::Command,
sync::{
atomic::{AtomicU64, Ordering::Relaxed},
Arc,
},
time::{Duration, Instant},
time::Instant,
};
use wait_timeout::ChildExt;

#[derive(Parser)]
struct Arguments {
Expand Down Expand Up @@ -74,41 +72,11 @@ fn _main() -> io::Result<()> {

let ts = Instant::now();

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())
.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() {
return Err(io::Error::new(
ErrorKind::Other,
format!("Vim failed with status: {}", status),
));
}
run_vim(
vec!["-S", "convert-to-html.vim", case.to_str().unwrap()],
&output,
&interrupted,
)?;

let vim_time = ts.elapsed().as_millis() as u64;
total_vim_time.fetch_add(vim_time, Relaxed);
Expand Down
42 changes: 4 additions & 38 deletions tests/src/test-ftdetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ use rand::{
use serde::Deserialize;
use std::{
collections::HashMap,
env, fs,
fs,
io::{self, ErrorKind},
process::{Command, Stdio},
sync::atomic::Ordering::Relaxed,
time::Duration,
};
use wait_timeout::ChildExt;

#[derive(Debug, Default, Deserialize)]
struct FtdetectCase {
Expand Down Expand Up @@ -76,39 +72,9 @@ fn _main() -> io::Result<()> {

let ftdetect_results = tempdir.path().join("ftdetect_results.txt");

let mut vim = Command::new("vim")
.args(["--not-a-term", "-R", "-S", "batch_ftdetect_res.vim"])
.args(file2case.keys())
.env("OUTPUT", &ftdetect_results)
.env("HOME", env::current_dir().unwrap())
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.unwrap();

let poll_interval = Duration::from_millis(100);
let status = loop {
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);
}
}
};

if !status.success() {
return Err(io::Error::new(
ErrorKind::Other,
format!("Vim failed with status: {}", status),
));
}
let mut args = vec!["-R", "-S", "batch_ftdetect_res.vim"];
args.extend(file2case.keys().map(|s| s.as_str()));
run_vim(args, &ftdetect_results, &interrupted)?;

let ftdetections = fs::read_to_string(ftdetect_results)?;

Expand Down

0 comments on commit 8409fbf

Please sign in to comment.