Skip to content

Commit 0dfecb3

Browse files
committed
run_make_support: move some helpers from assertion to path
1 parent 636be91 commit 0dfecb3

File tree

3 files changed

+53
-53
lines changed

3 files changed

+53
-53
lines changed

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

Lines changed: 1 addition & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,9 @@
11
//! Collection of assertions and assertion-related helpers.
22
33
use std::panic;
4-
use std::path::{Path, PathBuf};
4+
use std::path::Path;
55

66
use crate::fs as rfs;
7-
use crate::path_helpers::cwd;
8-
9-
/// Browse the directory `path` non-recursively and return all files which respect the parameters
10-
/// outlined by `closure`.
11-
#[track_caller]
12-
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
13-
path: P,
14-
filter: F,
15-
) -> Vec<PathBuf> {
16-
let mut matching_files = Vec::new();
17-
for entry in rfs::read_dir(path) {
18-
let entry = entry.expect("failed to read directory entry.");
19-
let path = entry.path();
20-
21-
if path.is_file() && filter(&path) {
22-
matching_files.push(path);
23-
}
24-
}
25-
matching_files
26-
}
27-
28-
/// Returns true if the filename at `path` starts with `prefix`.
29-
pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
30-
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
31-
}
32-
33-
/// Returns true if the filename at `path` has the extension `extension`.
34-
pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
35-
path.as_ref().extension().is_some_and(|ext| ext == extension)
36-
}
37-
38-
/// Returns true if the filename at `path` does not contain `expected`.
39-
pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool {
40-
!path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected))
41-
}
42-
43-
/// Returns true if the filename at `path` is not in `expected`.
44-
pub fn filename_not_in_denylist<P: AsRef<Path>, V: AsRef<[String]>>(path: P, expected: V) -> bool {
45-
let expected = expected.as_ref();
46-
path.as_ref()
47-
.file_name()
48-
.is_some_and(|name| !expected.contains(&name.to_str().unwrap().to_owned()))
49-
}
50-
51-
/// Returns true if the filename at `path` ends with `suffix`.
52-
pub fn has_suffix<P: AsRef<Path>>(path: P, suffix: &str) -> bool {
53-
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().ends_with(suffix))
54-
}
557

568
/// Gathers all files in the current working directory that have the extension `ext`, and counts
579
/// the number of lines within that contain a match with the regex pattern `re`.

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ pub use artifact_names::{
6161
};
6262

6363
/// Path-related helpers.
64-
pub use path_helpers::{cwd, path, source_root};
64+
pub use path_helpers::{
65+
cwd, filename_not_in_denylist, has_extension, has_prefix, has_suffix, not_contains, path,
66+
shallow_find_files, source_root,
67+
};
6568

6669
/// Helpers for scoped test execution where certain properties are attempted to be maintained.
6770
pub use scoped_run::{run_in_tmpdir, test_while_readonly};
6871

6972
pub use assertion_helpers::{
7073
assert_contains, assert_equals, assert_not_contains, assert_recursive_eq,
71-
count_regex_matches_in_files_with_extension, filename_not_in_denylist, has_extension,
72-
has_prefix, has_suffix, invalid_utf8_contains, invalid_utf8_not_contains, not_contains,
73-
shallow_find_files,
74+
count_regex_matches_in_files_with_extension, invalid_utf8_contains, invalid_utf8_not_contains,
7475
};

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,50 @@ pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
3131
pub fn source_root() -> PathBuf {
3232
env_var("SOURCE_ROOT").into()
3333
}
34+
35+
/// Browse the directory `path` non-recursively and return all files which respect the parameters
36+
/// outlined by `closure`.
37+
#[track_caller]
38+
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
39+
path: P,
40+
filter: F,
41+
) -> Vec<PathBuf> {
42+
let mut matching_files = Vec::new();
43+
for entry in std::fs::read_dir(path).unwrap() {
44+
let entry = entry.expect("failed to read directory entry.");
45+
let path = entry.path();
46+
47+
if path.is_file() && filter(&path) {
48+
matching_files.push(path);
49+
}
50+
}
51+
matching_files
52+
}
53+
54+
/// Returns true if the filename at `path` does not contain `expected`.
55+
pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool {
56+
!path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected))
57+
}
58+
59+
/// Returns true if the filename at `path` is not in `expected`.
60+
pub fn filename_not_in_denylist<P: AsRef<Path>, V: AsRef<[String]>>(path: P, expected: V) -> bool {
61+
let expected = expected.as_ref();
62+
path.as_ref()
63+
.file_name()
64+
.is_some_and(|name| !expected.contains(&name.to_str().unwrap().to_owned()))
65+
}
66+
67+
/// Returns true if the filename at `path` starts with `prefix`.
68+
pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
69+
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
70+
}
71+
72+
/// Returns true if the filename at `path` has the extension `extension`.
73+
pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
74+
path.as_ref().extension().is_some_and(|ext| ext == extension)
75+
}
76+
77+
/// Returns true if the filename at `path` ends with `suffix`.
78+
pub fn has_suffix<P: AsRef<Path>>(path: P, suffix: &str) -> bool {
79+
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().ends_with(suffix))
80+
}

0 commit comments

Comments
 (0)