Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli-test: Refactor Path Handling
Lorenz Leutgeb committed 2 months ago
commit 5aca9bf16aff8323009b11137c4e998315128d1e
parent d88ef3f
1 file changed +13 -18
modified crates/radicle-cli-test/src/lib.rs
@@ -4,17 +4,12 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync;
-
use std::{env, ffi, fs, io, mem};
+
use std::{env, fs, io, mem};

use snapbox::cmd::{Command, OutputAssert};
use snapbox::{Assert, Substitutions};
use thiserror::Error;

-
#[cfg(windows)]
-
const PATH_SEPARATOR: char = ';';
-
#[cfg(not(windows))]
-
const PATH_SEPARATOR: char = ':';
-

/// Used to ensure the build task is only run once.
static BUILD: sync::Once = sync::Once::new();

@@ -462,11 +457,7 @@ impl TestFormula {
                    vec![]
                };

-
                let bins = bins(self.cwd.clone())
-
                    .iter()
-
                    .map(|p| p.as_os_str())
-
                    .collect::<Vec<_>>()
-
                    .join(ffi::OsStr::new(&PATH_SEPARATOR.to_string()));
+
                let bins = std::env::join_paths(bins(self.cwd.clone())).unwrap();

                let command = Command::new(cmd.clone())
                    .env_clear()
@@ -531,12 +522,14 @@ impl TestFormula {
    }
}

-
/// Get the list of binary paths to use as `PATH` for the tests,
+
/// Get the list of binary paths to use as `$PATH` for the tests,
/// starting with the current working directory.
fn bins(cwd: PathBuf) -> Vec<PathBuf> {
-
    let mut bins: Vec<PathBuf> = env::var("PATH")
-
        .map(|env_path| env_path.split(PATH_SEPARATOR).map(PathBuf::from).collect())
-
        .unwrap_or_default();
+
    let mut bins: Vec<PathBuf> = Vec::new();
+

+
    // Add current working directory to `$PATH`,
+
    // this makes it more convenient to execute scripts during testing.
+
    bins.push(cwd);

    if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
        let profile = cfg!(debug_assertions)
@@ -551,6 +544,11 @@ fn bins(cwd: PathBuf) -> Vec<PathBuf> {
        )
    }

+
    // Add the "real" `$PATH`.
+
    if let Ok(path) = env::var("PATH") {
+
        bins.extend(env::split_paths(&path));
+
    }
+

    #[cfg(windows)]
    {
        // Radicle CLI tests rely on various Unix coreutils
@@ -562,9 +560,6 @@ fn bins(cwd: PathBuf) -> Vec<PathBuf> {
        bins.push(PathBuf::from(r#"C:\Program Files\Git\usr\bin"#));
    }

-
    // Add current working directory to `$PATH`,
-
    // this makes it more convenient to execute scripts during testing.
-
    bins.push(cwd);
    bins
}