1
1
use std:: collections:: HashSet ;
2
2
use std:: env;
3
- use std:: ffi:: OsStr ;
4
3
use std:: path:: PathBuf ;
5
4
use std:: process:: Command ;
6
5
7
6
use crate :: process;
8
7
9
8
pub const RUST_RECURSION_COUNT_MAX : u32 = 20 ;
10
9
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
-
21
10
#[ allow( unused) ]
22
11
pub fn append_path ( name : & str , value : Vec < PathBuf > , cmd : & mut Command ) {
23
12
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
63
52
let deduped_snd_paths = dedupe_with_preserved_order ( snd_paths) ;
64
53
65
54
let vec_fst_paths: Vec < _ > = deduped_fst_paths. into_iter ( ) . collect ( ) ;
66
- let vec_snd_paths: Vec < _ > = deduped_snd_paths. into_iter ( ) . collect ( ) ;
67
55
68
56
let mut unified_paths;
69
57
unified_paths = vec_fst_paths. clone ( ) ;
70
58
unified_paths. extend (
71
- vec_snd_paths
59
+ deduped_snd_paths
72
60
. into_iter ( )
73
61
. filter ( |v| !vec_fst_paths. contains ( v) )
74
62
. collect :: < Vec < _ > > ( ) ,
@@ -94,6 +82,10 @@ fn dedupe_with_preserved_order(paths: Vec<PathBuf>) -> Vec<PathBuf> {
94
82
#[ cfg( test) ]
95
83
mod tests {
96
84
use super :: * ;
85
+ use crate :: currentprocess;
86
+ use crate :: test:: { with_saved_path, Env } ;
87
+
88
+ use std:: collections:: HashMap ;
97
89
use std:: ffi:: OsString ;
98
90
99
91
#[ test]
@@ -146,4 +138,46 @@ mod tests {
146
138
147
139
assert_eq ! ( concat_uniq_paths( old_paths, new_paths) , unified_paths) ;
148
140
}
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
+ }
149
183
}
0 commit comments