aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid LeGare <legare@google.com>2022-03-02 21:48:34 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2022-03-02 21:48:34 +0000
commit90fdb407fc7b3314cccbf6638cbd48adb9da84d7 (patch)
tree0ee35a94c440d4b17b59f4adbc625f2cee3fb7f3
parent4fa35c7f3e0b6c375dc2b10148435ff9a247ff0c (diff)
parent8bc650c94f38e3354d5baca30e4ca47c82351bec (diff)
downloadwhich-android13-qpr3-c-s5-release.tar.gz
Update which to 4.2.4 am: 413469211c am: d4a8e3fb8d am: 8bc650c94ft_frc_odp_330442040t_frc_odp_330442000t_frc_ase_330444010android-13.0.0_r83android-13.0.0_r82android-13.0.0_r81android-13.0.0_r80android-13.0.0_r79android-13.0.0_r78android-13.0.0_r77android-13.0.0_r76android-13.0.0_r75android-13.0.0_r74android-13.0.0_r73android-13.0.0_r72android-13.0.0_r71android-13.0.0_r70android-13.0.0_r69android-13.0.0_r68android-13.0.0_r67android-13.0.0_r66android-13.0.0_r65android-13.0.0_r64android-13.0.0_r63android-13.0.0_r62android-13.0.0_r61android-13.0.0_r60android-13.0.0_r59android-13.0.0_r58android-13.0.0_r57android-13.0.0_r56android-13.0.0_r55android-13.0.0_r54android-13.0.0_r53android-13.0.0_r52android-13.0.0_r51android-13.0.0_r50android-13.0.0_r49android-13.0.0_r48android-13.0.0_r47android-13.0.0_r46android-13.0.0_r45android-13.0.0_r44android-13.0.0_r43android-13.0.0_r42android-13.0.0_r41android-13.0.0_r40android-13.0.0_r39android-13.0.0_r38android-13.0.0_r37android-13.0.0_r36android-13.0.0_r35android-13.0.0_r34android-13.0.0_r33android-13.0.0_r32android-13.0.0_r30android-13.0.0_r29android-13.0.0_r28android-13.0.0_r27android-13.0.0_r24android-13.0.0_r23android-13.0.0_r22android-13.0.0_r21android-13.0.0_r20android-13.0.0_r19android-13.0.0_r18android-13.0.0_r17android-13.0.0_r16aml_go_odp_330912000aml_go_ads_330915100aml_go_ads_330915000aml_go_ads_330913000android13-qpr3-s9-releaseandroid13-qpr3-s8-releaseandroid13-qpr3-s7-releaseandroid13-qpr3-s6-releaseandroid13-qpr3-s5-releaseandroid13-qpr3-s4-releaseandroid13-qpr3-s3-releaseandroid13-qpr3-s2-releaseandroid13-qpr3-s14-releaseandroid13-qpr3-s13-releaseandroid13-qpr3-s12-releaseandroid13-qpr3-s11-releaseandroid13-qpr3-s10-releaseandroid13-qpr3-s1-releaseandroid13-qpr3-releaseandroid13-qpr3-c-s8-releaseandroid13-qpr3-c-s7-releaseandroid13-qpr3-c-s6-releaseandroid13-qpr3-c-s5-releaseandroid13-qpr3-c-s4-releaseandroid13-qpr3-c-s3-releaseandroid13-qpr3-c-s2-releaseandroid13-qpr3-c-s12-releaseandroid13-qpr3-c-s11-releaseandroid13-qpr3-c-s10-releaseandroid13-qpr3-c-s1-releaseandroid13-qpr2-s9-releaseandroid13-qpr2-s8-releaseandroid13-qpr2-s7-releaseandroid13-qpr2-s6-releaseandroid13-qpr2-s5-releaseandroid13-qpr2-s3-releaseandroid13-qpr2-s2-releaseandroid13-qpr2-s12-releaseandroid13-qpr2-s11-releaseandroid13-qpr2-s10-releaseandroid13-qpr2-s1-releaseandroid13-qpr2-releaseandroid13-qpr2-b-s1-releaseandroid13-qpr1-s8-releaseandroid13-qpr1-s7-releaseandroid13-qpr1-s6-releaseandroid13-qpr1-s5-releaseandroid13-qpr1-s4-releaseandroid13-qpr1-s3-releaseandroid13-qpr1-s2-releaseandroid13-qpr1-s1-releaseandroid13-qpr1-releaseandroid13-mainline-go-adservices-releaseandroid13-frc-odp-releaseandroid13-devandroid13-d4-s2-releaseandroid13-d4-s1-releaseandroid13-d4-releaseandroid13-d3-s1-releaseandroid13-d2-release
Original change: https://android-review.googlesource.com/c/platform/external/rust/crates/which/+/2004691 Change-Id: I151c8d199c873888cae814773d473c90fc5e0875
-rw-r--r--.cargo_vcs_info.json7
-rw-r--r--.github/workflows/rust.yml98
-rw-r--r--Android.bp2
-rw-r--r--Cargo.toml13
-rw-r--r--Cargo.toml.orig5
-rw-r--r--METADATA10
-rw-r--r--README.md23
-rw-r--r--src/finder.rs31
-rw-r--r--src/lib.rs51
-rw-r--r--tests/basic.rs16
10 files changed, 192 insertions, 64 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json
index 26fe28d..692ca11 100644
--- a/.cargo_vcs_info.json
+++ b/.cargo_vcs_info.json
@@ -1,5 +1,6 @@
{
"git": {
- "sha1": "bdd46e52931db0b693e30bc08c4edfa499392903"
- }
-}
+ "sha1": "8fbe34239c16af9cd253e36e9c2d3384f9b55f83"
+ },
+ "path_in_vcs": ""
+} \ No newline at end of file
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
index 5ac490b..dd380d3 100644
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -1,23 +1,93 @@
-name: Rust
+name: Main workflow
+on:
+ push:
+ pull_request:
-on: [push, pull_request]
+jobs:
+ # Run the `rustfmt` code formatter
+ rustfmt:
+ name: Rustfmt [Formatter]
+ runs-on: ubuntu-latest
+ steps:
+ - name: Setup | Checkout
+ uses: actions/checkout@v2
-env:
- CARGO_TERM_COLOR: always
+ - name: Setup | Rust
+ uses: actions-rs/toolchain@v1
+ with:
+ toolchain: stable
+ override: true
+ profile: minimal
+ components: rustfmt
-jobs:
+ - name: Build | Format
+ run: cargo fmt --all -- --check
+
+ # Run the `clippy` linting tool
+ clippy:
+ name: Clippy [Linter]
+ strategy:
+ matrix:
+ os: [ubuntu-latest]
+ runs-on: ${{ matrix.os }}
+ steps:
+ - name: Setup | Checkout
+ uses: actions/checkout@v2
+
+ - name: Setup | Rust
+ uses: actions-rs/toolchain@v1
+ with:
+ toolchain: stable
+ override: true
+ profile: minimal
+ components: clippy
+
+ - name: Build | Lint
+ uses: actions-rs/cargo@v1
+ with:
+ command: clippy
+ args: --workspace --all-targets --all-features
+
+ # Ensure that the project could be successfully compiled
+ cargo_check:
+ name: Compile
+ runs-on: ubuntu-latest
+ steps:
+ - name: Setup | Checkout
+ uses: actions/checkout@v2
+
+ - name: Setup | Rust
+ uses: actions-rs/toolchain@v1
+ with:
+ toolchain: stable
+ profile: minimal
+ override: true
+
+ - name: Build | Check
+ run: cargo check --workspace
+
+ # Run tests on Linux, macOS, and Windows
+ # On both Rust stable and Rust nightly
test:
- name: Build and test
+ name: Test Suite
runs-on: ${{ matrix.os }}
+ needs: cargo_check # First check then run expansive tests
strategy:
fail-fast: false
matrix:
- os:
- - ubuntu-latest
- - windows-latest
- - macos-latest
-
+ os: [ubuntu-latest, windows-latest]
+ rust: [stable, nightly]
steps:
- - uses: actions/checkout@v2
- - run: cargo build --verbose
- - run: cargo test --verbose
+ - name: Setup | Checkout
+ uses: actions/checkout@v2
+
+ - name: Setup | Rust
+ uses: actions-rs/toolchain@v1
+ with:
+ toolchain: ${{ matrix.rust }}
+ profile: minimal
+ override: true
+
+ # Run the ignored tests that expect the above setup
+ - name: Build | Test
+ run: cargo test --workspace --all-features -- -Z unstable-options --include-ignored
diff --git a/Android.bp b/Android.bp
index 9ab98c9..3d469f9 100644
--- a/Android.bp
+++ b/Android.bp
@@ -23,7 +23,7 @@ rust_library {
host_supported: true,
crate_name: "which",
cargo_env_compat: true,
- cargo_pkg_version: "4.2.2",
+ cargo_pkg_version: "4.2.4",
srcs: ["src/lib.rs"],
edition: "2018",
rustlibs: [
diff --git a/Cargo.toml b/Cargo.toml
index 7814b7b..850a29c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,17 +3,16 @@
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
-# to registry (e.g., crates.io) dependencies
+# to registry (e.g., crates.io) dependencies.
#
-# If you believe there's an error in this file please file an
-# issue against the rust-lang/cargo repository. If you're
-# editing this file be aware that the upstream Cargo.toml
-# will likely look very different (and much more reasonable)
+# If you are reading this file be aware that the original Cargo.toml
+# will likely look very different (and much more reasonable).
+# See Cargo.toml.orig for the original contents.
[package]
edition = "2018"
name = "which"
-version = "4.2.2"
+version = "4.2.4"
authors = ["Harry Fei <tiziyuanfang@gmail.com>"]
description = "A Rust equivalent of Unix command \"which\". Locate installed executable in cross platforms."
documentation = "https://docs.rs/which/"
@@ -22,6 +21,8 @@ keywords = ["which", "which-rs", "unix", "command"]
categories = ["os", "filesystem"]
license = "MIT"
repository = "https://github.com/harryfei/which-rs.git"
+[package.metadata.docs.rs]
+all-features = true
[dependencies.either]
version = "1.6"
diff --git a/Cargo.toml.orig b/Cargo.toml.orig
index 12b29d3..ec31c55 100644
--- a/Cargo.toml.orig
+++ b/Cargo.toml.orig
@@ -1,6 +1,6 @@
[package]
name = "which"
-version = "4.2.2"
+version = "4.2.4"
edition = "2018"
authors = ["Harry Fei <tiziyuanfang@gmail.com>"]
repository = "https://github.com/harryfei/which-rs.git"
@@ -21,3 +21,6 @@ lazy_static = "1"
[dev-dependencies]
tempdir = "0.3.7"
+
+[package.metadata.docs.rs]
+all-features = true
diff --git a/METADATA b/METADATA
index ea19f37..6a034c4 100644
--- a/METADATA
+++ b/METADATA
@@ -7,13 +7,13 @@ third_party {
}
url {
type: ARCHIVE
- value: "https://static.crates.io/crates/which/which-4.2.2.crate"
+ value: "https://static.crates.io/crates/which/which-4.2.4.crate"
}
- version: "4.2.2"
+ version: "4.2.4"
license_type: NOTICE
last_upgrade_date {
- year: 2021
- month: 8
- day: 9
+ year: 2022
+ month: 3
+ day: 1
}
}
diff --git a/README.md b/README.md
index f20f929..5615c36 100644
--- a/README.md
+++ b/README.md
@@ -10,16 +10,25 @@ A Rust equivalent of Unix command "which". Locate installed executable in cross
* Windows
* macOS
-## Example
+## Examples
-To find which rustc exectable binary is using.
+1) To find which rustc executable binary is using.
-``` rust
-use which::which;
+ ``` rust
+ use which::which;
-let result = which::which("rustc").unwrap();
-assert_eq!(result, PathBuf::from("/usr/bin/rustc"));
-```
+ let result = which("rustc").unwrap();
+ assert_eq!(result, PathBuf::from("/usr/bin/rustc"));
+ ```
+
+2. After enabling the `regex` feature, find all cargo subcommand executables on the path:
+
+ ``` rust
+ use which::which_re;
+
+ which_re(Regex::new("^cargo-.*").unwrap()).unwrap()
+ .for_each(|pth| println!("{}", pth.to_string_lossy()));
+ ```
## Documentation
diff --git a/src/finder.rs b/src/finder.rs
index 416ee3c..43b659d 100644
--- a/src/finder.rs
+++ b/src/finder.rs
@@ -5,6 +5,8 @@ use crate::helper::has_executable_extension;
use either::Either;
#[cfg(feature = "regex")]
use regex::Regex;
+#[cfg(feature = "regex")]
+use std::borrow::Borrow;
use std::env;
use std::ffi::OsStr;
#[cfg(feature = "regex")]
@@ -54,7 +56,7 @@ impl Finder {
&self,
binary_name: T,
paths: Option<U>,
- cwd: V,
+ cwd: Option<V>,
binary_checker: CompositeChecker,
) -> Result<impl Iterator<Item = PathBuf>>
where
@@ -64,15 +66,18 @@ impl Finder {
{
let path = PathBuf::from(&binary_name);
- let binary_path_candidates = if path.has_separator() {
- // Search binary in cwd if the path have a path separator.
- Either::Left(Self::cwd_search_candidates(path, cwd).into_iter())
- } else {
- // Search binary in PATHs(defined in environment variable).
- let p = paths.ok_or(Error::CannotFindBinaryPath)?;
- let paths: Vec<_> = env::split_paths(&p).collect();
-
- Either::Right(Self::path_search_candidates(path, paths).into_iter())
+ let binary_path_candidates = match cwd {
+ Some(cwd) if path.has_separator() => {
+ // Search binary in cwd if the path have a path separator.
+ Either::Left(Self::cwd_search_candidates(path, cwd).into_iter())
+ }
+ _ => {
+ // Search binary in PATHs(defined in environment variable).
+ let p = paths.ok_or(Error::CannotFindBinaryPath)?;
+ let paths: Vec<_> = env::split_paths(&p).collect();
+
+ Either::Right(Self::path_search_candidates(path, paths).into_iter())
+ }
};
Ok(binary_path_candidates.filter(move |p| binary_checker.is_valid(p)))
@@ -81,7 +86,7 @@ impl Finder {
#[cfg(feature = "regex")]
pub fn find_re<T>(
&self,
- binary_regex: Regex,
+ binary_regex: impl Borrow<Regex>,
paths: Option<T>,
binary_checker: CompositeChecker,
) -> Result<impl Iterator<Item = PathBuf>>
@@ -99,7 +104,7 @@ impl Finder {
.map(|e| e.path())
.filter(move |p| {
if let Some(unicode_file_name) = p.file_name().unwrap().to_str() {
- binary_regex.is_match(unicode_file_name)
+ binary_regex.borrow().is_match(unicode_file_name)
} else {
false
}
@@ -153,7 +158,7 @@ impl Finder {
.map(|pathext| {
pathext.split(';')
.filter_map(|s| {
- if s.as_bytes()[0] == b'.' {
+ if s.as_bytes().first() == Some(&b'.') {
Some(s.to_owned())
} else {
// Invalid segment; just ignore it.
diff --git a/src/lib.rs b/src/lib.rs
index d9377f5..2b27094 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -9,7 +9,7 @@
//! use which::which;
//! use std::path::PathBuf;
//!
-//! let result = which::which("rustc").unwrap();
+//! let result = which("rustc").unwrap();
//! assert_eq!(result, PathBuf::from("/usr/bin/rustc"));
//!
//! ```
@@ -26,6 +26,8 @@ mod helper;
#[cfg(feature = "regex")]
use regex::Regex;
+#[cfg(feature = "regex")]
+use std::borrow::Borrow;
use std::env;
use std::fmt;
use std::path;
@@ -63,19 +65,27 @@ pub fn which<T: AsRef<OsStr>>(binary_name: T) -> Result<path::PathBuf> {
/// Find all binaries with `binary_name` in the path list `paths`, using `cwd` to resolve relative paths.
pub fn which_all<T: AsRef<OsStr>>(binary_name: T) -> Result<impl Iterator<Item = path::PathBuf>> {
- let cwd = env::current_dir().map_err(|_| Error::CannotGetCurrentDir)?;
+ let cwd = env::current_dir().ok();
+
+ let binary_checker = build_binary_checker();
- which_in_all(binary_name, env::var_os("PATH"), cwd)
+ let finder = Finder::new();
+
+ finder.find(binary_name, env::var_os("PATH"), cwd, binary_checker)
}
/// Find all binaries matching a regular expression in a the system PATH.
///
+/// Only available when feature `regex` is enabled.
+///
/// # Arguments
///
/// * `regex` - A regular expression to match binaries with
///
/// # Examples
///
+/// Find Python executables:
+///
/// ```no_run
/// use regex::Regex;
/// use which::which;
@@ -86,8 +96,18 @@ pub fn which_all<T: AsRef<OsStr>>(binary_name: T) -> Result<impl Iterator<Item =
/// let python_paths = vec![PathBuf::from("/usr/bin/python2"), PathBuf::from("/usr/bin/python3")];
/// assert_eq!(binaries, python_paths);
/// ```
+///
+/// Find all cargo subcommand executables on the path:
+///
+/// ```
+/// use which::which_re;
+/// use regex::Regex;
+///
+/// which_re(Regex::new("^cargo-.*").unwrap()).unwrap()
+/// .for_each(|pth| println!("{}", pth.to_string_lossy()));
+/// ```
#[cfg(feature = "regex")]
-pub fn which_re(regex: Regex) -> Result<impl Iterator<Item = path::PathBuf>> {
+pub fn which_re(regex: impl Borrow<Regex>) -> Result<impl Iterator<Item = path::PathBuf>> {
which_re_in(regex, env::var_os("PATH"))
}
@@ -104,6 +124,8 @@ where
/// Find all binaries matching a regular expression in a list of paths.
///
+/// Only available when feature `regex` is enabled.
+///
/// # Arguments
///
/// * `regex` - A regular expression to match binaries with
@@ -124,13 +146,14 @@ where
/// assert_eq!(binaries, python_paths);
/// ```
#[cfg(feature = "regex")]
-pub fn which_re_in<T>(regex: Regex, paths: Option<T>) -> Result<impl Iterator<Item = path::PathBuf>>
+pub fn which_re_in<T>(
+ regex: impl Borrow<Regex>,
+ paths: Option<T>,
+) -> Result<impl Iterator<Item = path::PathBuf>>
where
T: AsRef<OsStr>,
{
- let binary_checker = CompositeChecker::new()
- .add_checker(Box::new(ExistedChecker::new()))
- .add_checker(Box::new(ExecutableChecker::new()));
+ let binary_checker = build_binary_checker();
let finder = Finder::new();
@@ -148,13 +171,17 @@ where
U: AsRef<OsStr>,
V: AsRef<path::Path>,
{
- let binary_checker = CompositeChecker::new()
- .add_checker(Box::new(ExistedChecker::new()))
- .add_checker(Box::new(ExecutableChecker::new()));
+ let binary_checker = build_binary_checker();
let finder = Finder::new();
- finder.find(binary_name, paths, cwd, binary_checker)
+ finder.find(binary_name, paths, Some(cwd), binary_checker)
+}
+
+fn build_binary_checker() -> CompositeChecker {
+ CompositeChecker::new()
+ .add_checker(Box::new(ExistedChecker::new()))
+ .add_checker(Box::new(ExecutableChecker::new()))
}
/// An owned, immutable wrapper around a `PathBuf` containing the path of an executable.
diff --git a/tests/basic.rs b/tests/basic.rs
index e3bc73c..897e912 100644
--- a/tests/basic.rs
+++ b/tests/basic.rs
@@ -141,8 +141,11 @@ fn test_which_re_in_with_matches() {
.collect();
let temp = f.tempdir;
-
- assert_eq!(result, vec![temp.path().join("a/bin_0"), temp.path().join("b/bin_1")])
+
+ assert_eq!(
+ result,
+ vec![temp.path().join("a/bin_0"), temp.path().join("b/bin_1")]
+ )
}
#[test]
@@ -160,6 +163,15 @@ fn test_which_re_in_without_matches() {
}
#[test]
+#[cfg(all(unix, feature = "regex"))]
+fn test_which_re_accepts_owned_and_borrow() {
+ which::which_re(Regex::new(r".").unwrap());
+ which::which_re(&Regex::new(r".").unwrap());
+ which::which_re_in(Regex::new(r".").unwrap(), Some("pth"));
+ which::which_re_in(&Regex::new(r".").unwrap(), Some("pth"));
+}
+
+#[test]
#[cfg(unix)]
fn test_which_extension() {
let f = TestFixture::new();