Skip to content

Commit

Permalink
shallow_find_files function for run_make_support
Browse files Browse the repository at this point in the history
  • Loading branch information
Oneirical committed Jul 2, 2024
1 parent 66491f6 commit 9324b98
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/doc/reference
Submodule reference updated 1 files
+3 −1 src/items/unions.md
2 changes: 1 addition & 1 deletion src/doc/rust-by-example
2 changes: 1 addition & 1 deletion src/tools/cargo
Submodule cargo updated 155 files
52 changes: 22 additions & 30 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use std::panic;
use std::path::{Path, PathBuf};

pub use gimli;
pub use glob;
pub use object;
pub use regex;
pub use wasmparser;
Expand Down Expand Up @@ -223,40 +222,33 @@ pub fn bin_name(name: &str) -> String {
if is_windows() { format!("{name}.exe") } else { name.to_string() }
}

/// Remove all dynamic libraries possessing a name starting with `paths`.
/// Browse the directory `path` non-recursively and return all files which respect the parameters
/// outlined by `closure`.
#[track_caller]
pub fn remove_dylibs(paths: &str) {
let paths = format!(r"{paths}*");
remove_glob(dynamic_lib_name(&paths).as_str());
}

/// Remove all rust libraries possessing a name starting with `paths`.
#[track_caller]
pub fn remove_rlibs(paths: &str) {
let paths = format!(r"{paths}*");
remove_glob(rust_lib_name(&paths).as_str());
}

#[track_caller]
fn remove_glob(paths: &str) {
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
paths
.filter_map(|entry| entry.ok())
.filter(|entry| entry.as_path().is_file())
.for_each(|file| fs_wrapper::remove_file(&file));
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
path: P,
closure: F,
) -> Vec<PathBuf> {
let mut matching_files = Vec::new();
for entry in fs_wrapper::read_dir(path) {
let entry = entry.expect("failed to read directory entry.");
let path = entry.path();

if path.is_file() && closure(&path) {
matching_files.push(path);
}
}
matching_files
}

#[track_caller]
fn count_glob(paths: &str) -> usize {
let paths = glob::glob(paths).expect(format!("Glob expression {paths} is not valid.").as_str());
paths.filter_map(|entry| entry.ok()).filter(|entry| entry.as_path().is_file()).count()
/// 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))
}

/// Count the number of rust libraries possessing a name starting with `paths`.
#[track_caller]
pub fn count_rlibs(paths: &str) -> usize {
let paths = format!(r"{paths}*");
count_glob(rust_lib_name(&paths).as_str())
/// 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)
}

/// Return the current working directory.
Expand Down
4 changes: 2 additions & 2 deletions tests/run-make/lib-trait-for-trait-no-ice/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// the lib crate-type flag was actually followed.
// See https://github.com/rust-lang/rust/issues/18943

use run_make_support::{count_rlibs, rustc};
use run_make_support::{fs_wrapper, rust_lib_name, rustc};

fn main() {
rustc().input("foo.rs").crate_type("lib").run();
assert_eq!(count_rlibs("foo"), 1);
fs_wrapper::remove_file(rust_lib_name("foo"));
}
4 changes: 2 additions & 2 deletions tests/run-make/mixing-libs/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

//@ ignore-cross-compile

use run_make_support::{remove_dylibs, rustc};
use run_make_support::{dynamic_lib_name, fs_wrapper, rustc};

fn main() {
rustc().input("rlib.rs").crate_type("rlib").crate_type("dylib").run();
Expand All @@ -16,6 +16,6 @@ fn main() {

// librlib's dynamic version needs to be removed here to prevent prog.rs from fetching
// the wrong one.
remove_dylibs("rlib");
fs_wrapper::remove_file(dynamic_lib_name("rlib"));
rustc().input("prog.rs").run_fail();
}
10 changes: 6 additions & 4 deletions tests/run-make/obey-crate-type-flag/rmake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

//@ ignore-cross-compile

use run_make_support::{count_rlibs, remove_dylibs, remove_rlibs, rustc};
use run_make_support::{
cwd, dynamic_lib_name, fs_wrapper, has_extension, rust_lib_name, rustc, shallow_find_files,
};

fn main() {
rustc().input("test.rs").run();
remove_rlibs("test");
remove_dylibs("test");
fs_wrapper::remove_file(dynamic_lib_name("test"));
fs_wrapper::remove_file(rust_lib_name("test"));
rustc().crate_type("dylib").input("test.rs").run();
assert_eq!(count_rlibs("test"), 0);
assert!(shallow_find_files(cwd(), |path| { has_extension(path, "rlib") }).is_empty());
}

0 comments on commit 9324b98

Please sign in to comment.