Skip to content

Commit

Permalink
test runner (ftdetect): remove anti-patterns; other code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
laniakea64 committed Jan 16, 2024
1 parent 222f821 commit 60fb16e
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions tests/src/test-ftdetect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use rand::{
rngs::ThreadRng,
Rng,
};
use regex::{Captures, Regex};
use serde::Deserialize;
use std::{
collections::HashMap,
Expand Down Expand Up @@ -37,9 +36,10 @@ fn random_alnum(rng: &mut ThreadRng, minlen: u8, maxlen: u8) -> String {
}

fn fuzz_filename(rng: &mut ThreadRng, filename: String) -> String {
let rx = Regex::new(r"\*").unwrap();
rx.replace_all(filename.as_str(), |_: &Captures| random_alnum(rng, 3, 8))
.into_owned()
filename
.split_inclusive('*')
.map(|part| part.replace('*', random_alnum(rng, 3, 8).as_str()))
.collect()
}

fn _main() -> io::Result<()> {
Expand All @@ -57,7 +57,7 @@ fn _main() -> io::Result<()> {
let total = cases.len();
let mut passed = 0;

let mut file2case: HashMap<String, FtdetectCase> = HashMap::with_capacity(total);
let mut file2case = HashMap::<String, FtdetectCase>::with_capacity(total);
for case in cases {
let fname = match &case.filename {
Some(n) => fuzz_filename(&mut rng, n.to_string()),
Expand All @@ -67,11 +67,10 @@ fn _main() -> io::Result<()> {
fs::write(
&actual_file,
match &case.content {
Some(t) => t.clone(),
None => "".to_string(),
Some(t) => t,
None => "",
},
)
.unwrap();
)?;
file2case.insert(actual_file.into_os_string().into_string().unwrap(), case);
}

Expand Down Expand Up @@ -111,33 +110,22 @@ fn _main() -> io::Result<()> {
));
}

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

let filetype_rx = Regex::new(r"^\s*filetype=(.*)$").unwrap();
let mut current_key = "";
for line in ftdetections.lines() {
if line.is_empty() {
continue;
} else if !current_key.is_empty() {
let detected_filetype_match = match filetype_rx.captures(line) {
Some(o) => o,
None => {
return Err(io::Error::new(
ErrorKind::Other,
format!("filetype regex failed to match line: {}", line),
))
}
};
let detected_filetype_match = match detected_filetype_match.get(1) {
Some(o) => o,
let filetype = match line.split_once("filetype=") {
Some((_, ft)) => ft,
None => {
return Err(io::Error::new(
ErrorKind::Other,
format!("filetype regex missing capture in match on line: {}", line),
format!("expected to find \"filetype=\" in line: {:?}", line),
))
}
};
let filetype = detected_filetype_match.as_str();
let case = file2case.get(current_key).unwrap();
if (filetype == "just" && !case.not_justfile) || (case.not_justfile && filetype != "just") {
passed += 1;
Expand Down

0 comments on commit 60fb16e

Please sign in to comment.