Repository: sharkdp/bat
File: tests/utils/mocked_pagers.rs (Rust)

Code before:
#![allow(unused)] // Because indirectly included by e.g. system_wide_config.rs, but not used

use assert_cmd::Command;
use predicates::prelude::predicate;
use std::env;
use std::path::{Path, PathBuf};

/// For some tests we want mocked versions of some pagers
/// This fn returns the absolute path to the directory with these mocked pagers
fn get_mocked_pagers_dir() -> PathBuf {
    let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR");
    Path::new(&cargo_manifest_dir)
        .join("tests")
        .join("mocked-pagers")
}

/// On Unix: 'most' -> 'most'
/// On Windows: 'most' -> 'most.bat'
pub fn from(base: &str) -> String {
    let mut cmd_and_args = base.split(" ");
    let suffix = if cfg!(windows) { ".bat" } else { "" };
    match cmd_and_args.next() {
        Some(s) => {
            let mut out_cmd = format!("{}{}", s, suffix);
            let remainder: Vec<&str> = cmd_and_args.collect();
            if (remainder.len() > 0) {
                out_cmd.push_str(" ");
                out_cmd.push_str(remainder.join(" ").as_str());
            }

            out_cmd
        }
        None => String::from(base),
    }
}

/// Prepends a directory to the PATH environment variable
/// Returns the original value for later restoration
fn prepend_dir_to_path_env_var(dir: PathBuf) -> String {
    // Get current PATH
    let original_path = env::var("PATH").expect("No PATH?!");

    // Add the new dir first
    let mut split_paths = env::split_paths(&original_path).collect::<Vec<_>>();
    split_paths.insert(0, dir);


Review comment (suggestion):
Since we already depend on `shell_words::split` I think this code would end up simpler if we used it here too? If it ends up being worse, we can keep this as it is.

Code after:
#![allow(unused)] // Because indirectly included by e.g. system_wide_config.rs, but not used

use assert_cmd::Command;
use predicates::prelude::predicate;
use std::env;
use std::path::{Path, PathBuf};

/// For some tests we want mocked versions of some pagers
/// This fn returns the absolute path to the directory with these mocked pagers
fn get_mocked_pagers_dir() -> PathBuf {
    let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Missing CARGO_MANIFEST_DIR");
    Path::new(&cargo_manifest_dir)
        .join("tests")
        .join("mocked-pagers")
}

/// On Unix: 'most' -> 'most'
/// On Windows: 'most' -> 'most.bat'
pub fn from(base: &str) -> String {
    let mut cmd_and_args = shell_words::split(base).unwrap();
    let suffix = if cfg!(windows) { ".bat" } else { "" };
    let mut out_cmd = format!("{}{}", cmd_and_args.first().unwrap(), suffix);

    if (cmd_and_args.len() > 1) {
        out_cmd.push(' ');
        out_cmd.push_str(cmd_and_args[1..].to_vec().join(" ").as_str());
    }

    out_cmd
}

/// Prepends a directory to the PATH environment variable
/// Returns the original value for later restoration
fn prepend_dir_to_path_env_var(dir: PathBuf) -> String {
    // Get current PATH
    let original_path = env::var("PATH").expect("No PATH?!");

    // Add the new dir first
    let mut split_paths = env::split_paths(&original_path).collect::<Vec<_>>();
    split_paths.insert(0, dir);

    // Set PATH with the new dir
    let new_path = env::join_paths(split_paths).expect("Failed to join paths");
    env::set_var("PATH", new_path);

    // Return the original value for later restoration of it