|
1 | 1 | //! Collection of assertions and assertion-related helpers. |
2 | 2 |
|
3 | 3 | use std::panic; |
4 | | -use std::path::{Path, PathBuf}; |
| 4 | +use std::path::Path; |
5 | 5 |
|
6 | 6 | 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 | | -} |
55 | 7 |
|
56 | 8 | /// Gathers all files in the current working directory that have the extension `ext`, and counts |
57 | 9 | /// the number of lines within that contain a match with the regex pattern `re`. |
|
0 commit comments