diff options
| author | 许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com> | 2024-07-17 12:52:57 +0000 |
|---|---|---|
| committer | 许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com> | 2024-07-17 13:32:29 +0000 |
| commit | 57a2f76557c3b4ea4ad0c40f1c9dc9c11ca9d289 (patch) | |
| tree | 31ffa9d9ac2cb59ef46a51b97f05558a0507345f | |
| parent | 0dfecb3855aabafd14d6156f013b128c12ed7795 (diff) | |
| download | rust-57a2f76557c3b4ea4ad0c40f1c9dc9c11ca9d289.tar.gz rust-57a2f76557c3b4ea4ad0c40f1c9dc9c11ca9d289.zip | |
run_make_support: moved some helpers into `string` module
| -rw-r--r-- | src/tools/run-make-support/src/assertion_helpers.rs | 46 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/lib.rs | 4 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/string.rs | 50 |
3 files changed, 54 insertions, 46 deletions
diff --git a/src/tools/run-make-support/src/assertion_helpers.rs b/src/tools/run-make-support/src/assertion_helpers.rs index 5fb9d0da494..844c2d477aa 100644 --- a/src/tools/run-make-support/src/assertion_helpers.rs +++ b/src/tools/run-make-support/src/assertion_helpers.rs @@ -5,52 +5,6 @@ use std::path::Path; use crate::fs as rfs; -/// Gathers all files in the current working directory that have the extension `ext`, and counts -/// the number of lines within that contain a match with the regex pattern `re`. -pub fn count_regex_matches_in_files_with_extension(re: ®ex::Regex, ext: &str) -> usize { - let fetched_files = shallow_find_files(cwd(), |path| has_extension(path, ext)); - - let mut count = 0; - for file in fetched_files { - let content = rfs::read_to_string(file); - count += content.lines().filter(|line| re.is_match(&line)).count(); - } - - count -} - -/// Read the contents of a file that cannot simply be read by -/// [`read_to_string`][crate::fs::read_to_string], due to invalid UTF-8 data, then assert -/// that it contains `expected`. -#[track_caller] -pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) { - let buffer = rfs::read(path.as_ref()); - let expected = expected.as_ref(); - if !String::from_utf8_lossy(&buffer).contains(expected) { - eprintln!("=== FILE CONTENTS (LOSSY) ==="); - eprintln!("{}", String::from_utf8_lossy(&buffer)); - eprintln!("=== SPECIFIED TEXT ==="); - eprintln!("{}", expected); - panic!("specified text was not found in file"); - } -} - -/// Read the contents of a file that cannot simply be read by -/// [`read_to_string`][crate::fs::read_to_string], due to invalid UTF-8 data, then assert -/// that it does not contain `expected`. -#[track_caller] -pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) { - let buffer = rfs::read(path.as_ref()); - let expected = expected.as_ref(); - if String::from_utf8_lossy(&buffer).contains(expected) { - eprintln!("=== FILE CONTENTS (LOSSY) ==="); - eprintln!("{}", String::from_utf8_lossy(&buffer)); - eprintln!("=== SPECIFIED TEXT ==="); - eprintln!("{}", expected); - panic!("specified text was unexpectedly found in file"); - } -} - /// Assert that `actual` is equal to `expected`. #[track_caller] pub fn assert_equals<A: AsRef<str>, E: AsRef<str>>(actual: A, expected: E) { diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index f97e3dd5d8b..3dbc2d201ff 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -16,6 +16,7 @@ pub mod fs; pub mod path_helpers; pub mod run; pub mod scoped_run; +pub mod string; pub mod targets; // Re-exports of third-party library crates. @@ -71,5 +72,8 @@ pub use scoped_run::{run_in_tmpdir, test_while_readonly}; pub use assertion_helpers::{ assert_contains, assert_equals, assert_not_contains, assert_recursive_eq, +}; + +pub use string::{ count_regex_matches_in_files_with_extension, invalid_utf8_contains, invalid_utf8_not_contains, }; diff --git a/src/tools/run-make-support/src/string.rs b/src/tools/run-make-support/src/string.rs new file mode 100644 index 00000000000..f0b1e2334d8 --- /dev/null +++ b/src/tools/run-make-support/src/string.rs @@ -0,0 +1,50 @@ +use std::path::Path; + +use crate::fs as rfs; +use crate::path_helpers::{cwd, has_extension, shallow_find_files}; + +/// Gathers all files in the current working directory that have the extension `ext`, and counts +/// the number of lines within that contain a match with the regex pattern `re`. +pub fn count_regex_matches_in_files_with_extension(re: ®ex::Regex, ext: &str) -> usize { + let fetched_files = shallow_find_files(cwd(), |path| has_extension(path, ext)); + + let mut count = 0; + for file in fetched_files { + let content = rfs::read_to_string(file); + count += content.lines().filter(|line| re.is_match(&line)).count(); + } + + count +} + +/// Read the contents of a file that cannot simply be read by +/// [`read_to_string`][crate::fs::read_to_string], due to invalid UTF-8 data, then assert +/// that it contains `expected`. +#[track_caller] +pub fn invalid_utf8_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) { + let buffer = rfs::read(path.as_ref()); + let expected = expected.as_ref(); + if !String::from_utf8_lossy(&buffer).contains(expected) { + eprintln!("=== FILE CONTENTS (LOSSY) ==="); + eprintln!("{}", String::from_utf8_lossy(&buffer)); + eprintln!("=== SPECIFIED TEXT ==="); + eprintln!("{}", expected); + panic!("specified text was not found in file"); + } +} + +/// Read the contents of a file that cannot simply be read by +/// [`read_to_string`][crate::fs::read_to_string], due to invalid UTF-8 data, then assert +/// that it does not contain `expected`. +#[track_caller] +pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expected: S) { + let buffer = rfs::read(path.as_ref()); + let expected = expected.as_ref(); + if String::from_utf8_lossy(&buffer).contains(expected) { + eprintln!("=== FILE CONTENTS (LOSSY) ==="); + eprintln!("{}", String::from_utf8_lossy(&buffer)); + eprintln!("=== SPECIFIED TEXT ==="); + eprintln!("{}", expected); + panic!("specified text was unexpectedly found in file"); + } +} |
