Skip to content

Commit

Permalink
WIP: add impl
Browse files Browse the repository at this point in the history
  • Loading branch information
chansuke committed Nov 4, 2021
1 parent c0cf508 commit afe83e0
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions src/env_var.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::process;
use std::collections::HashSet;
use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::process::Command;

use crate::process;
use std::vec;

pub const RUST_RECURSION_COUNT_MAX: u32 = 20;

Expand All @@ -13,14 +13,34 @@ trait ProcessEnvs {
type Iter: Iterator<Item = Self::Item>;

fn get_envs(&self) -> Self::Iter;
fn env<K, V>(&mut self, key: K, val: V) -> &mut Self
fn env<K, V>(&mut self, key: K, val: V)
where
K: AsRef<OsStr>,
V: AsRef<OsStr>;
}

impl ProcessEnvs for Command {
type Item = Command;
type Iter = vec::IntoIter<Command>;

fn get_envs(&self) -> <Command as ProcessEnvs>::Iter {
self.get_envs()
}
fn env<K, V>(&mut self, key: K, val: V)
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.env(key, val);
}
}

#[allow(unused)]
pub fn append_path(name: &str, value: Vec<PathBuf>, cmd: &mut Command) {
pub fn append_path(
name: &str,
value: Vec<PathBuf>,
cmd: &mut dyn ProcessEnvs<Item = Command, Iter = vec::IntoIter<Command>>,
) {
let old_value = process().var_os(name);
let mut parts: Vec<PathBuf>;
if let Some(ref v) = old_value {
Expand All @@ -34,7 +54,11 @@ pub fn append_path(name: &str, value: Vec<PathBuf>, cmd: &mut Command) {
}
}

pub fn prepend_path(name: &str, value: Vec<PathBuf>, cmd: &mut Command) {
pub fn prepend_path(
name: &str,
value: Vec<PathBuf>,
cmd: &mut dyn ProcessEnvs<Item = Command, Iter = vec::IntoIter<Command>>,
) {
let old_value = process().var_os(name);
let parts: Vec<PathBuf>;
if let Some(ref v) = old_value {
Expand Down Expand Up @@ -97,9 +121,28 @@ mod tests {
use crate::currentprocess;
use crate::test::{with_saved_path, Env};

use super::ProcessEnvs;
use std::collections::HashMap;
use std::ffi::{OsStr, OsString};

struct TestCommand();

impl ProcessEnvs for TestCommand {
type Item = Command;
type Iter = vec::IntoIter<Command>;

fn get_envs(&self) -> <Command as ProcessEnvs>::Iter {
self.get_envs()
}
fn env<K, V>(&mut self, key: K, val: V) -> &mut <Command as ProcessEnvs>::Item
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.env(key, val)
}
}

#[test]
fn deduplicate_and_concat_paths() {
let mut old_paths = vec![];
Expand Down Expand Up @@ -177,7 +220,6 @@ mod tests {
path_entries.push(path_z);

prepend_path("PATH", path_entries, &mut cmd);

let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();

assert_eq!(
Expand Down

0 comments on commit afe83e0

Please sign in to comment.