1
+ use crate :: process;
1
2
use std:: collections:: HashSet ;
2
3
use std:: env;
3
4
use std:: ffi:: OsStr ;
4
5
use std:: path:: PathBuf ;
5
6
use std:: process:: Command ;
6
-
7
- use crate :: process;
7
+ use std:: vec;
8
8
9
9
pub const RUST_RECURSION_COUNT_MAX : u32 = 20 ;
10
10
@@ -13,14 +13,34 @@ trait ProcessEnvs {
13
13
type Iter : Iterator < Item = Self :: Item > ;
14
14
15
15
fn get_envs ( & self ) -> Self :: Iter ;
16
- fn env < K , V > ( & mut self , key : K , val : V ) -> & mut Self
16
+ fn env < K , V > ( & mut self , key : K , val : V )
17
17
where
18
18
K : AsRef < OsStr > ,
19
19
V : AsRef < OsStr > ;
20
20
}
21
21
22
+ impl ProcessEnvs for Command {
23
+ type Item = Command ;
24
+ type Iter = vec:: IntoIter < Command > ;
25
+
26
+ fn get_envs ( & self ) -> <Command as ProcessEnvs >:: Iter {
27
+ self . get_envs ( )
28
+ }
29
+ fn env < K , V > ( & mut self , key : K , val : V )
30
+ where
31
+ K : AsRef < OsStr > ,
32
+ V : AsRef < OsStr > ,
33
+ {
34
+ self . env ( key, val) ;
35
+ }
36
+ }
37
+
22
38
#[ allow( unused) ]
23
- pub fn append_path ( name : & str , value : Vec < PathBuf > , cmd : & mut Command ) {
39
+ pub fn append_path (
40
+ name : & str ,
41
+ value : Vec < PathBuf > ,
42
+ cmd : & mut dyn ProcessEnvs < Item = Command , Iter = vec:: IntoIter < Command > > ,
43
+ ) {
24
44
let old_value = process ( ) . var_os ( name) ;
25
45
let mut parts: Vec < PathBuf > ;
26
46
if let Some ( ref v) = old_value {
@@ -34,7 +54,11 @@ pub fn append_path(name: &str, value: Vec<PathBuf>, cmd: &mut Command) {
34
54
}
35
55
}
36
56
37
- pub fn prepend_path ( name : & str , value : Vec < PathBuf > , cmd : & mut Command ) {
57
+ pub fn prepend_path (
58
+ name : & str ,
59
+ value : Vec < PathBuf > ,
60
+ cmd : & mut dyn ProcessEnvs < Item = Command , Iter = vec:: IntoIter < Command > > ,
61
+ ) {
38
62
let old_value = process ( ) . var_os ( name) ;
39
63
let parts: Vec < PathBuf > ;
40
64
if let Some ( ref v) = old_value {
@@ -97,9 +121,28 @@ mod tests {
97
121
use crate :: currentprocess;
98
122
use crate :: test:: { with_saved_path, Env } ;
99
123
124
+ use super :: ProcessEnvs ;
100
125
use std:: collections:: HashMap ;
101
126
use std:: ffi:: { OsStr , OsString } ;
102
127
128
+ struct TestCommand ( ) ;
129
+
130
+ impl ProcessEnvs for TestCommand {
131
+ type Item = Command ;
132
+ type Iter = vec:: IntoIter < Command > ;
133
+
134
+ fn get_envs ( & self ) -> <Command as ProcessEnvs >:: Iter {
135
+ self . get_envs ( )
136
+ }
137
+ fn env < K , V > ( & mut self , key : K , val : V ) -> & mut <Command as ProcessEnvs >:: Item
138
+ where
139
+ K : AsRef < OsStr > ,
140
+ V : AsRef < OsStr > ,
141
+ {
142
+ self . env ( key, val)
143
+ }
144
+ }
145
+
103
146
#[ test]
104
147
fn deduplicate_and_concat_paths ( ) {
105
148
let mut old_paths = vec ! [ ] ;
@@ -177,7 +220,6 @@ mod tests {
177
220
path_entries. push ( path_z) ;
178
221
179
222
prepend_path ( "PATH" , path_entries, & mut cmd) ;
180
-
181
223
let envs: Vec < ( & OsStr , Option < & OsStr > ) > = cmd. get_envs ( ) . collect ( ) ;
182
224
183
225
assert_eq ! (
0 commit comments