1use std::path::Path;
23use crate::fs;
4use crate::path_helpers::{cwd, has_extension, shallow_find_files};
56/// Gathers all files in the current working directory that have the extension `ext`, and counts
7/// the number of lines within that contain a match with the regex pattern `re`.
8pub fn count_regex_matches_in_files_with_extension(re: ®ex::Regex, ext: &str) -> usize {
9let fetched_files = shallow_find_files(cwd(), |path| has_extension(path, ext));
1011let mut count = 0;
12for file in fetched_files {
13let content = fs::read_to_string(file);
14 count += content.lines().filter(|line| re.is_match(&line)).count();
15 }
1617 count
18}
1920/// Read the contents of a file that cannot simply be read by
21/// [`read_to_string`][crate::fs::read_to_string], due to invalid UTF-8 data, then assert
22/// that it contains `expected`.
23#[track_caller]
24pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
25let buffer = fs::read(path.as_ref());
26let expected = expected.as_ref();
27if !String::from_utf8_lossy(&buffer).contains(expected) {
28eprintln!("=== FILE CONTENTS (LOSSY) ===");
29eprintln!("{}", String::from_utf8_lossy(&buffer));
30eprintln!("=== SPECIFIED TEXT ===");
31eprintln!("{}", expected);
32panic!("specified text was not found in file");
33 }
34}
3536/// Read the contents of a file that cannot simply be read by
37/// [`read_to_string`][crate::fs::read_to_string], due to invalid UTF-8 data, then assert
38/// that it does not contain `expected`.
39#[track_caller]
40pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
41let buffer = fs::read(path.as_ref());
42let expected = expected.as_ref();
43if String::from_utf8_lossy(&buffer).contains(expected) {
44eprintln!("=== FILE CONTENTS (LOSSY) ===");
45eprintln!("{}", String::from_utf8_lossy(&buffer));
46eprintln!("=== SPECIFIED TEXT ===");
47eprintln!("{}", expected);
48panic!("specified text was unexpectedly found in file");
49 }
50}