Radish alpha
h
rad:z3gqcJUoA1n9HaHKufZs5FCSGazv5
Radicle Heartwood Protocol & Stack
Radicle
Git
cli-test: Refactor Path Handling
Merged lorenz opened 2 months ago

In the previous refactoring in commit 4894657b, the order of entries in $PATH was changed unintentionally. Revisit the order and use nicer APIs to handle paths.

1 file changed +43 -46 d88ef3fa 119445ce
modified crates/radicle-cli-test/src/lib.rs
@@ -4,16 +4,15 @@ 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 = ':';
+
const CARGO_TARGET_DIR_DIRNAME: &str = "target";
+

+
const CARGO_PROFILE: &str = "debug";

/// Used to ensure the build task is only run once.
static BUILD: sync::Once = sync::Once::new();
@@ -200,9 +199,13 @@ impl TestFormula {
            for (package, binary) in binaries {
                log::debug!(target: "test", "Building binaries for package `{package}`..");

+
                let cargo_manifest_dir = cargo_manifest_dir();
+

                let results = escargot::CargoBuild::new()
                    .package(package)
                    .bin(binary)
+
                    .manifest_path(cargo_manifest_dir.clone().join("Cargo.toml"))
+
                    .target_dir(cargo_manifest_dir.join(CARGO_TARGET_DIR_DIRNAME))
                    .exec()
                    .unwrap();

@@ -407,13 +410,6 @@ impl TestFormula {

            // For each command.
            for (i, assertion) in test.assertions.iter().enumerate() {
-
                // Expand environment variables.
-
                let mut args = assertion.args.clone();
-
                for arg in &mut args {
-
                    for (k, v) in run.envs() {
-
                        *arg = arg.replace(format!("${k}").as_str(), &v);
-
                    }
-
                }
                let location = assertion
                    .path
                    .file_name()
@@ -422,9 +418,7 @@ impl TestFormula {
                    .map(|f| f + ":" + assertion.line.to_string().as_str())
                    .unwrap_or(String::from("<none>"));

-
                let (cmd, cmd_display) = if assertion.command == "rad" {
-
                    (snapbox::cmd::cargo_bin("rad"), "rad".to_string())
-
                } else if assertion.command == "cd" {
+
                if assertion.command == "cd" {
                    let arg = assertion.args.first().unwrap();
                    let dir: PathBuf = arg.into();
                    let dir = run.path().join(dir);
@@ -443,9 +437,15 @@ impl TestFormula {
                    run.cd(dir);

                    continue;
-
                } else {
-
                    (PathBuf::from(&assertion.command), assertion.command.clone())
-
                };
+
                }
+

+
                // Expand environment variables.
+
                let mut args = assertion.args.clone();
+
                for arg in &mut args {
+
                    for (k, v) in run.envs() {
+
                        *arg = arg.replace(format!("${k}").as_str(), &v);
+
                    }
+
                }

                if !run.path().exists() {
                    log::warn!(target: "test", "{location}: Directory {} does not exist. Creating..", run.path().display());
@@ -462,13 +462,9 @@ 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())
+
                let command = Command::new(assertion.command.clone())
                    .env_clear()
                    .env("PATH", &bins)
                    .env("RUST_BACKTRACE", "1")
@@ -478,7 +474,7 @@ impl TestFormula {
                    .args(args.clone())
                    .with_assert(assert.clone());

-
                log::debug!(target: "test", "{location}: `{} {}` @ {}", cmd_display, args.join(" "), run.path().display());
+
                log::debug!(target: "test", "{location}: `{} {}` @ {}", assertion.command, args.join(" "), run.path().display());
                log::trace!(target: "test", "{location}: {}", run.envs().map(|(k, v)| format!("{}={}", k, v)).collect::<Vec<_>>().join(", "));
                log::logger().flush();

@@ -516,11 +512,11 @@ impl TestFormula {
                    }
                    Err(err) => {
                        if err.kind() == io::ErrorKind::NotFound {
-
                            log::error!(target: "test", "{location}: Command `{}` does not exist..", cmd.display());
+
                            log::error!(target: "test", "{location}: Command `{}` does not exist..", assertion.command);
                        }
                        return Err(io::Error::new(
                            err.kind(),
-
                            format!("{location}: {err}: `{}`", cmd.display()),
+
                            format!("{location}: {err}: `{}`", assertion.command),
                        ));
                    }
                }
@@ -531,24 +527,28 @@ impl TestFormula {
    }
}

-
/// Get the list of binary paths to use as `PATH` for the tests,
+
fn cargo_manifest_dir() -> PathBuf {
+
    env::var("CARGO_MANIFEST_DIR").map(PathBuf::from).unwrap()
+
}
+

+
/// 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();
-

-
    if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
-
        let profile = cfg!(debug_assertions)
-
            .then_some("debug")
-
            .unwrap_or("release");
-
        let target_dir = env::var("CARGO_TARGET_DIR").unwrap_or("target".to_string());
-

-
        bins.push(
-
            PathBuf::from(manifest_dir.as_str())
-
                .join(&target_dir)
-
                .join(profile),
-
        )
+
    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);
+

+
    bins.push(
+
        cargo_manifest_dir()
+
            .join(CARGO_TARGET_DIR_DIRNAME)
+
            .join(CARGO_PROFILE),
+
    );
+

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

    #[cfg(windows)]
@@ -562,9 +562,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
}