Skip to content

Commit 1d034f3

Browse files
committed
Add unit test
1 parent 0c6afc3 commit 1d034f3

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

src/env_var.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,52 @@ pub fn inc(name: &str, cmd: &mut Command) {
4747

4848
cmd.env(name, (old_value + 1).to_string());
4949
}
50+
51+
#[cfg(test)]
52+
mod tests {
53+
use super::*;
54+
use crate::currentprocess;
55+
use crate::test::{with_saved_path, Env};
56+
57+
use std::collections::HashMap;
58+
use std::ffi::{OsStr, OsString};
59+
60+
#[test]
61+
fn prepent_unique_path() {
62+
let mut vars = HashMap::new();
63+
vars.env("PATH", "/home/a/.cargo/bin:/home/b/.cargo/bin");
64+
let tp = Box::new(currentprocess::TestProcess {
65+
vars,
66+
..Default::default()
67+
});
68+
with_saved_path(&|| {
69+
currentprocess::with(tp.clone(), || {
70+
let mut path_entries = vec![];
71+
let mut cmd = Command::new("test");
72+
73+
// duplicated PATH.
74+
let a = OsString::from("/home/a/.cargo/bin");
75+
let path_a = PathBuf::from(a);
76+
path_entries.push(path_a);
77+
// unique PATH.
78+
let z = OsString::from("/home/z/.cargo/bin");
79+
let path_z = PathBuf::from(z);
80+
path_entries.push(path_z);
81+
82+
prepend_path("PATH", path_entries, &mut cmd);
83+
84+
let envs: Vec<(&OsStr, Option<&OsStr>)> = cmd.get_envs().collect();
85+
86+
assert_eq!(
87+
envs,
88+
&[(
89+
OsStr::new("PATH"),
90+
Some(OsStr::new(
91+
"/home/a/.cargo/bin:/home/b/.cargo/bin:/home/z/.cargo/bin"
92+
))
93+
),]
94+
);
95+
});
96+
});
97+
}
98+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(command_access)]
12
#![deny(rust_2018_idioms)]
23
#![allow(
34
clippy::too_many_arguments,

0 commit comments

Comments
 (0)