diff options
| author | 许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com> | 2024-07-17 12:44:25 +0000 |
|---|---|---|
| committer | 许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com> | 2024-07-17 13:32:29 +0000 |
| commit | 0dfecb3855aabafd14d6156f013b128c12ed7795 (patch) | |
| tree | 2bf0e35f6bfad895d99bc903f96f217eb219725b | |
| parent | 636be91cc0d8e406171ec4aa1b5dea4b77d1a714 (diff) | |
| download | rust-0dfecb3855aabafd14d6156f013b128c12ed7795.tar.gz rust-0dfecb3855aabafd14d6156f013b128c12ed7795.zip | |
run_make_support: move some helpers from assertion to path
| -rw-r--r-- | src/tools/run-make-support/src/assertion_helpers.rs | 50 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/lib.rs | 9 | ||||
| -rw-r--r-- | src/tools/run-make-support/src/path_helpers.rs | 47 |
3 files changed, 53 insertions, 53 deletions
diff --git a/src/tools/run-make-support/src/assertion_helpers.rs b/src/tools/run-make-support/src/assertion_helpers.rs index b57ac475e08..5fb9d0da494 100644 --- a/src/tools/run-make-support/src/assertion_helpers.rs +++ b/src/tools/run-make-support/src/assertion_helpers.rs @@ -1,57 +1,9 @@ //! Collection of assertions and assertion-related helpers. use std::panic; -use std::path::{Path, PathBuf}; +use std::path::Path; use crate::fs as rfs; -use crate::path_helpers::cwd; - -/// Browse the directory `path` non-recursively and return all files which respect the parameters -/// outlined by `closure`. -#[track_caller] -pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>( - path: P, - filter: F, -) -> Vec<PathBuf> { - let mut matching_files = Vec::new(); - for entry in rfs::read_dir(path) { - let entry = entry.expect("failed to read directory entry."); - let path = entry.path(); - - if path.is_file() && filter(&path) { - matching_files.push(path); - } - } - matching_files -} - -/// Returns true if the filename at `path` starts with `prefix`. -pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool { - path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix)) -} - -/// Returns true if the filename at `path` has the extension `extension`. -pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool { - path.as_ref().extension().is_some_and(|ext| ext == extension) -} - -/// Returns true if the filename at `path` does not contain `expected`. -pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool { - !path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected)) -} - -/// Returns true if the filename at `path` is not in `expected`. -pub fn filename_not_in_denylist<P: AsRef<Path>, V: AsRef<[String]>>(path: P, expected: V) -> bool { - let expected = expected.as_ref(); - path.as_ref() - .file_name() - .is_some_and(|name| !expected.contains(&name.to_str().unwrap().to_owned())) -} - -/// Returns true if the filename at `path` ends with `suffix`. -pub fn has_suffix<P: AsRef<Path>>(path: P, suffix: &str) -> bool { - path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().ends_with(suffix)) -} /// 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`. diff --git a/src/tools/run-make-support/src/lib.rs b/src/tools/run-make-support/src/lib.rs index ae34ecd8d97..f97e3dd5d8b 100644 --- a/src/tools/run-make-support/src/lib.rs +++ b/src/tools/run-make-support/src/lib.rs @@ -61,14 +61,15 @@ pub use artifact_names::{ }; /// Path-related helpers. -pub use path_helpers::{cwd, path, source_root}; +pub use path_helpers::{ + cwd, filename_not_in_denylist, has_extension, has_prefix, has_suffix, not_contains, path, + shallow_find_files, source_root, +}; /// Helpers for scoped test execution where certain properties are attempted to be maintained. pub use scoped_run::{run_in_tmpdir, test_while_readonly}; pub use assertion_helpers::{ assert_contains, assert_equals, assert_not_contains, assert_recursive_eq, - count_regex_matches_in_files_with_extension, filename_not_in_denylist, has_extension, - has_prefix, has_suffix, invalid_utf8_contains, invalid_utf8_not_contains, not_contains, - shallow_find_files, + count_regex_matches_in_files_with_extension, invalid_utf8_contains, invalid_utf8_not_contains, }; diff --git a/src/tools/run-make-support/src/path_helpers.rs b/src/tools/run-make-support/src/path_helpers.rs index b29d8727d2b..f37ea8dfef8 100644 --- a/src/tools/run-make-support/src/path_helpers.rs +++ b/src/tools/run-make-support/src/path_helpers.rs @@ -31,3 +31,50 @@ pub fn path<P: AsRef<Path>>(p: P) -> PathBuf { pub fn source_root() -> PathBuf { env_var("SOURCE_ROOT").into() } + +/// Browse the directory `path` non-recursively and return all files which respect the parameters +/// outlined by `closure`. +#[track_caller] +pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>( + path: P, + filter: F, +) -> Vec<PathBuf> { + let mut matching_files = Vec::new(); + for entry in std::fs::read_dir(path).unwrap() { + let entry = entry.expect("failed to read directory entry."); + let path = entry.path(); + + if path.is_file() && filter(&path) { + matching_files.push(path); + } + } + matching_files +} + +/// Returns true if the filename at `path` does not contain `expected`. +pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool { + !path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected)) +} + +/// Returns true if the filename at `path` is not in `expected`. +pub fn filename_not_in_denylist<P: AsRef<Path>, V: AsRef<[String]>>(path: P, expected: V) -> bool { + let expected = expected.as_ref(); + path.as_ref() + .file_name() + .is_some_and(|name| !expected.contains(&name.to_str().unwrap().to_owned())) +} + +/// Returns true if the filename at `path` starts with `prefix`. +pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool { + path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix)) +} + +/// Returns true if the filename at `path` has the extension `extension`. +pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool { + path.as_ref().extension().is_some_and(|ext| ext == extension) +} + +/// Returns true if the filename at `path` ends with `suffix`. +pub fn has_suffix<P: AsRef<Path>>(path: P, suffix: &str) -> bool { + path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().ends_with(suffix)) +} |
