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 8, 2024
1 parent ece7d98 commit cbc62cb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 45 deletions.
37 changes: 0 additions & 37 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use std::path::{Path, PathBuf};

pub use bstr;
pub use gimli;
pub use glob;
pub use object;
pub use regex;
pub use wasmparser;
Expand Down Expand Up @@ -224,42 +223,6 @@ 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`.
#[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));
}

#[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()
}

/// 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())
}

/// Return the current working directory.
#[must_use]
pub fn cwd() -> PathBuf {
Expand Down
5 changes: 3 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,10 @@
// 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::{rust_lib_name, rustc};
use std::path::Path;

fn main() {
rustc().input("foo.rs").crate_type("lib").run();
assert_eq!(count_rlibs("foo"), 1);
assert!(Path::new(&rust_lib_name("foo")).exists());
}
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();
}
13 changes: 9 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,17 @@

//@ 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,
};
use std::path::Path;

fn main() {
rustc().input("test.rs").run();
remove_rlibs("test");
remove_dylibs("test");
assert!(Path::new(&dynamic_lib_name("test")).exists());
assert!(Path::new(&rust_lib_name("test")).exists());

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 cbc62cb

Please sign in to comment.