Skip to content

Commit 57a2f76

Browse files
committed
run_make_support: moved some helpers into string module
1 parent 0dfecb3 commit 57a2f76

File tree

3 files changed

+54
-46
lines changed

3 files changed

+54
-46
lines changed

src/tools/run-make-support/src/assertion_helpers.rs

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,6 @@ use std::path::Path;
55

66
use crate::fs as rfs;
77

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

src/tools/run-make-support/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub mod fs;
1616
pub mod path_helpers;
1717
pub mod run;
1818
pub mod scoped_run;
19+
pub mod string;
1920
pub mod targets;
2021

2122
// Re-exports of third-party library crates.
@@ -71,5 +72,8 @@ pub use scoped_run::{run_in_tmpdir, test_while_readonly};
7172

7273
pub use assertion_helpers::{
7374
assert_contains, assert_equals, assert_not_contains, assert_recursive_eq,
75+
};
76+
77+
pub use string::{
7478
count_regex_matches_in_files_with_extension, invalid_utf8_contains, invalid_utf8_not_contains,
7579
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
use std::path::Path;
2+
3+
use crate::fs as rfs;
4+
use crate::path_helpers::{cwd, has_extension, shallow_find_files};
5+
6+
/// 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`.
8+
pub fn count_regex_matches_in_files_with_extension(re: &regex::Regex, ext: &str) -> usize {
9+
let fetched_files = shallow_find_files(cwd(), |path| has_extension(path, ext));
10+
11+
let mut count = 0;
12+
for file in fetched_files {
13+
let content = rfs::read_to_string(file);
14+
count += content.lines().filter(|line| re.is_match(&line)).count();
15+
}
16+
17+
count
18+
}
19+
20+
/// 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]
24+
pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
25+
let buffer = rfs::read(path.as_ref());
26+
let expected = expected.as_ref();
27+
if !String::from_utf8_lossy(&buffer).contains(expected) {
28+
eprintln!("=== FILE CONTENTS (LOSSY) ===");
29+
eprintln!("{}", String::from_utf8_lossy(&buffer));
30+
eprintln!("=== SPECIFIED TEXT ===");
31+
eprintln!("{}", expected);
32+
panic!("specified text was not found in file");
33+
}
34+
}
35+
36+
/// 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]
40+
pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) {
41+
let buffer = rfs::read(path.as_ref());
42+
let expected = expected.as_ref();
43+
if String::from_utf8_lossy(&buffer).contains(expected) {
44+
eprintln!("=== FILE CONTENTS (LOSSY) ===");
45+
eprintln!("{}", String::from_utf8_lossy(&buffer));
46+
eprintln!("=== SPECIFIED TEXT ===");
47+
eprintln!("{}", expected);
48+
panic!("specified text was unexpectedly found in file");
49+
}
50+
}

0 commit comments

Comments
 (0)