Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5b6bbcce authored by Martin Geisler's avatar Martin Geisler
Browse files

PDL: let ‘find_binary’ helper return a ‘Result’

This allows us to show what we searched for and where. It makes the
error messages much more useful when you run ‘cargo test’ without
having the necessary binaries in your ‘$PATH’.

Test: ‘cargo test’ without having run ‘m pdl’
Change-Id: I98b3cc3c9d10fa930bc449faddc5c6186583ef54
parent 2ea11bdb
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -8,18 +8,23 @@ use tempfile::NamedTempFile;

/// Search for a binary in `$PATH` or as a sibling to the current
/// executable (typically the test binary).
fn find_binary(name: &str) -> Option<std::path::PathBuf> {
fn find_binary(name: &str) -> Result<std::path::PathBuf, String> {
    let mut current_exe = std::env::current_exe().unwrap();
    current_exe.pop();
    let paths = std::env::var_os("PATH").unwrap();
    for mut path in std::iter::once(current_exe).chain(std::env::split_paths(&paths)) {
    for mut path in std::iter::once(current_exe.clone()).chain(std::env::split_paths(&paths)) {
        path.push(name);
        if path.exists() {
            return Some(path);
            return Ok(path);
        }
    }

    None
    Err(format!(
        "could not find '{}' in the directory of the binary ({}) or in $PATH ({})",
        name,
        current_exe.to_string_lossy(),
        paths.to_string_lossy(),
    ))
}

/// Parse a string fragment as a PDL file.