fix: file name check

This commit is contained in:
DaniPopes 2022-12-30 08:43:55 +01:00
parent 8a67e5c54d
commit 33080ec5c0
No known key found for this signature in database
GPG Key ID: 0F09640DDB7AC692
1 changed files with 10 additions and 11 deletions

View File

@ -229,7 +229,7 @@ impl EthersCrate {
/// [ref]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates /// [ref]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates
#[inline] #[inline]
fn is_crate_root(manifest_dir: impl AsRef<Path>) -> bool { fn is_crate_root(manifest_dir: impl AsRef<Path>) -> bool {
env::var_os("CARGO_TARGET_TMPDIR").is_some() || is_crate_name_in_dirs(manifest_dir) let manifest_dir = manifest_dir.as_ref();
} }
/// Returns whether `CARGO_CRATE_NAME` is the name of a file or directory in the first level of /// Returns whether `CARGO_CRATE_NAME` is the name of a file or directory in the first level of
@ -265,7 +265,7 @@ fn is_crate_root(manifest_dir: impl AsRef<Path>) -> bool {
/// The resulting `CARGO_CRATE_NAME` values will be: /// The resulting `CARGO_CRATE_NAME` values will be:
/// ///
/// | Path | Value | /// | Path | Value |
/// |:---------------------------------------|-----------------------:| /// |:-------------------------------------- | ----------------------:|
/// | benches/large-input.rs | large-input | /// | benches/large-input.rs | large-input |
/// | benches/multi-file-bench/\*\*/\*.rs | multi-file-bench | /// | benches/multi-file-bench/\*\*/\*.rs | multi-file-bench |
/// | examples/simple.rs | simple | /// | examples/simple.rs | simple |
@ -273,31 +273,30 @@ fn is_crate_root(manifest_dir: impl AsRef<Path>) -> bool {
/// | tests/some-integration-tests.rs | some-integration-tests | /// | tests/some-integration-tests.rs | some-integration-tests |
/// | tests/multi-file-test/\*\*/\*.rs | multi-file-test | /// | tests/multi-file-test/\*\*/\*.rs | multi-file-test |
#[inline] #[inline]
fn is_crate_name_in_dirs(manifest_dir: impl AsRef<Path>) -> bool { fn is_crate_name_in_dirs(manifest_dir: &Path) -> bool {
let crate_name = match env::var("CARGO_CRATE_NAME") { let crate_name = match env::var("CARGO_CRATE_NAME") {
Ok(name) => name, Ok(name) => name,
Err(_) => return false, Err(_) => return false,
}; };
let manifest_dir = manifest_dir.as_ref();
let dirs = let dirs =
[manifest_dir.join("tests"), manifest_dir.join("examples"), manifest_dir.join("benches")]; [manifest_dir.join("tests"), manifest_dir.join("examples"), manifest_dir.join("benches")];
dirs.iter().any(|dir| { dirs.iter().any(|dir| {
fs::read_dir(dir) fs::read_dir(dir)
.ok() .ok()
.and_then(|entries| { .and_then(|entries| {
entries entries.filter_map(Result::ok).find(|entry| file_stem_eq(entry.path(), &crate_name))
.filter_map(Result::ok)
.find(|entry| file_name_contains(entry.path(), &crate_name))
}) })
.is_some() .is_some()
}) })
} }
#[inline] #[inline]
fn file_name_contains<T: AsRef<Path>, U: AsRef<str>>(path: T, s: U) -> bool { fn file_stem_eq<T: AsRef<Path>, U: AsRef<str>>(path: T, s: U) -> bool {
match path.as_ref().file_name() { if let Some(stem) = path.as_ref().file_stem() {
Some(name) => name.to_string_lossy().contains(s.as_ref()), if let Some(stem) = stem.to_str() {
None => false, return stem == s.as_ref()
} }
}
false
} }