From 8409fbf1b5b3e3a1b99dd7c5d0b0b4271b23d8b5 Mon Sep 17 00:00:00 2001 From: laniakea64 Date: Wed, 24 Jan 2024 21:34:18 -0500 Subject: [PATCH] tests: unify code for running Vim --- tests/convert-to-html.vim | 3 --- tests/src/common.rs | 47 +++++++++++++++++++++++++++++++++++++- tests/src/main.rs | 46 ++++++------------------------------- tests/src/test-ftdetect.rs | 42 ++++------------------------------ 4 files changed, 57 insertions(+), 81 deletions(-) diff --git a/tests/convert-to-html.vim b/tests/convert-to-html.vim index b03b56b..577a02c 100644 --- a/tests/convert-to-html.vim +++ b/tests/convert-to-html.vim @@ -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 diff --git a/tests/src/common.rs b/tests/src/common.rs index b4bfb74..6aedf14 100644 --- a/tests/src/common.rs +++ b/tests/src/common.rs @@ -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() { @@ -30,3 +35,43 @@ pub fn setup_ctrlc_handler() -> Arc { interrupted } + +pub fn run_vim(args: Vec<&str>, output: &PathBuf, interrupted: &Arc) -> 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), + )) + } +} diff --git a/tests/src/main.rs b/tests/src/main.rs index cefdc66..fb9ed41 100644 --- a/tests/src/main.rs +++ b/tests/src/main.rs @@ -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 { @@ -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); diff --git a/tests/src/test-ftdetect.rs b/tests/src/test-ftdetect.rs index ea4c7d8..5937cac 100644 --- a/tests/src/test-ftdetect.rs +++ b/tests/src/test-ftdetect.rs @@ -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 { @@ -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)?;