run_make_support/
path_helpers.rs

1//! Collection of path-related helpers.
2
3use std::path::{Path, PathBuf};
4
5use crate::env::env_var;
6use crate::rfs;
7
8/// Return the current working directory.
9///
10/// This forwards to [`std::env::current_dir`], please see its docs regarding platform-specific
11/// behavior.
12#[must_use]
13pub fn cwd() -> PathBuf {
14    std::env::current_dir().unwrap()
15}
16
17/// Construct a `PathBuf` relative to the current working directory by joining `cwd()` with the
18/// relative path. This is mostly a convenience helper so the test writer does not need to write
19/// `PathBuf::from(path_like_string)`.
20///
21/// # Example
22///
23/// ```rust
24/// # use run_make_support::path;
25/// let p = path("support_file.txt");
26/// ```
27pub fn path<P: AsRef<Path>>(p: P) -> PathBuf {
28    cwd().join(p.as_ref())
29}
30
31/// Path to the root `rust-lang/rust` source checkout.
32#[must_use]
33pub fn source_root() -> PathBuf {
34    env_var("SOURCE_ROOT").into()
35}
36
37/// Path to the build directory root.
38#[must_use]
39pub fn build_root() -> PathBuf {
40    env_var("BUILD_ROOT").into()
41}
42
43/// Browse the directory `path` non-recursively and return all files which respect the parameters
44/// outlined by `closure`.
45#[track_caller]
46pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
47    path: P,
48    filter: F,
49) -> Vec<PathBuf> {
50    let mut matching_files = Vec::new();
51    for entry in rfs::read_dir(path) {
52        let entry = entry.expect("failed to read directory entry.");
53        let path = entry.path();
54
55        if path.is_file() && filter(&path) {
56            matching_files.push(path);
57        }
58    }
59    matching_files
60}
61
62/// Browse the directory `path` non-recursively and return all directories which respect the
63/// parameters outlined by `closure`.
64#[track_caller]
65pub fn shallow_find_directories<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
66    path: P,
67    filter: F,
68) -> Vec<PathBuf> {
69    let mut matching_files = Vec::new();
70    for entry in rfs::read_dir(path) {
71        let entry = entry.expect("failed to read directory entry.");
72        let path = entry.path();
73
74        if path.is_dir() && filter(&path) {
75            matching_files.push(path);
76        }
77    }
78    matching_files
79}
80
81/// Returns true if the filename at `path` does not contain `expected`.
82pub fn not_contains<P: AsRef<Path>>(path: P, expected: &str) -> bool {
83    !path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(expected))
84}
85
86/// Returns true if the filename at `path` is not in `expected`.
87pub fn filename_not_in_denylist<P: AsRef<Path>, V: AsRef<[String]>>(path: P, expected: V) -> bool {
88    let expected = expected.as_ref();
89    path.as_ref()
90        .file_name()
91        .is_some_and(|name| !expected.contains(&name.to_str().unwrap().to_owned()))
92}
93
94/// Returns true if the filename at `path` starts with `prefix`.
95pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
96    path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
97}
98
99/// Returns true if the filename at `path` has the extension `extension`.
100pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
101    path.as_ref().extension().is_some_and(|ext| ext == extension)
102}
103
104/// Returns true if the filename at `path` ends with `suffix`.
105pub fn has_suffix<P: AsRef<Path>>(path: P, suffix: &str) -> bool {
106    path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().ends_with(suffix))
107}
108
109/// Returns true if the filename at `path` contains `needle`.
110pub fn filename_contains<P: AsRef<Path>>(path: P, needle: &str) -> bool {
111    path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().contains(needle))
112}
113
114/// Helper for reading entries in a given directory and its children.
115pub fn read_dir_entries_recursive<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, mut callback: F) {
116    fn read_dir_entries_recursive_inner<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, callback: &mut F) {
117        for entry in rfs::read_dir(dir) {
118            let path = entry.unwrap().path();
119            callback(&path);
120            if path.is_dir() {
121                read_dir_entries_recursive_inner(path, callback);
122            }
123        }
124    }
125
126    read_dir_entries_recursive_inner(dir, &mut callback);
127}