Skip to content

Commit 3a93ad3

Browse files
committed
WIP: unit test
1 parent a610e46 commit 3a93ad3

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

src/env_var.rs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
use std::collections::HashSet;
22
use std::env;
3-
use std::ffi::OsStr;
43
use std::path::PathBuf;
54
use std::process::Command;
65

76
use crate::process;
87

98
pub const RUST_RECURSION_COUNT_MAX: u32 = 20;
109

11-
trait ProcessEnvs {
12-
type Item;
13-
type Iter: Iterator;
14-
fn get_envs(&self) -> Self::Iter<Item=Self::Item>;
15-
fn env<K, V>(&mut self, key: K, val: V) -> &mut Self
16-
where
17-
K: AsRef<OsStr>,
18-
V: AsRef<OsStr>;
19-
}
20-
2110
#[allow(unused)]
2211
pub fn append_path(name: &str, value: Vec<PathBuf>, cmd: &mut Command) {
2312
let old_value = process().var_os(name);
@@ -63,12 +52,11 @@ fn concat_uniq_paths(fst_paths: Vec<PathBuf>, snd_paths: Vec<PathBuf>) -> Vec<Pa
6352
let deduped_snd_paths = dedupe_with_preserved_order(snd_paths);
6453

6554
let vec_fst_paths: Vec<_> = deduped_fst_paths.into_iter().collect();
66-
let vec_snd_paths: Vec<_> = deduped_snd_paths.into_iter().collect();
6755

6856
let mut unified_paths;
6957
unified_paths = vec_fst_paths.clone();
7058
unified_paths.extend(
71-
vec_snd_paths
59+
deduped_snd_paths
7260
.into_iter()
7361
.filter(|v| !vec_fst_paths.contains(v))
7462
.collect::<Vec<_>>(),
@@ -94,6 +82,10 @@ fn dedupe_with_preserved_order(paths: Vec<PathBuf>) -> Vec<PathBuf> {
9482
#[cfg(test)]
9583
mod tests {
9684
use super::*;
85+
use crate::currentprocess;
86+
use crate::test::{with_saved_path, Env};
87+
88+
use std::collections::HashMap;
9789
use std::ffi::OsString;
9890

9991
#[test]
@@ -146,4 +138,46 @@ mod tests {
146138

147139
assert_eq!(concat_uniq_paths(old_paths, new_paths), unified_paths);
148140
}
141+
142+
#[test]
143+
fn prepend_unique_path() {
144+
let mut vars = HashMap::new();
145+
vars.env("PATH", "/home/a/.cargo/bin:/home/b/.cargo/bin");
146+
let tp = Box::new(currentprocess::TestProcess {
147+
vars,
148+
..Default::default()
149+
});
150+
with_saved_path(&|| {
151+
currentprocess::with(tp.clone(), || {
152+
let mut path_entries = vec![];
153+
let mut cmd = Command::new("test");
154+
155+
let a = OsString::from("/home/a/.cargo/bin");
156+
let path_a = PathBuf::from(a);
157+
path_entries.push(path_a);
158+
159+
let _a = OsString::from("/home/a/.cargo/bin");
160+
let _path_a = PathBuf::from(_a);
161+
path_entries.push(_path_a);
162+
163+
let z = OsString::from("/home/z/.cargo/bin");
164+
let path_z = PathBuf::from(z);
165+
path_entries.push(path_z);
166+
167+
prepend_path("PATH", path_entries, &mut cmd);
168+
169+
let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
170+
171+
assert_eq!(
172+
envs,
173+
&[(
174+
OsStr::new("PATH"),
175+
Some(OsStr::new(
176+
"/home/a/.cargo/bin:/home/z/.cargo/bin:/home/b/.cargo/bin"
177+
))
178+
),]
179+
);
180+
});
181+
});
182+
}
149183
}

0 commit comments

Comments
 (0)