1
- use std:: collections:: { HashSet , VecDeque } ;
1
+ use std:: collections:: VecDeque ;
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
- /// This can be removed when https://github.com/rust-lang/rust/issues/44434 is
12
- /// stablised.
13
- pub ( crate ) trait ProcessEnvs {
14
- fn env < K , V > ( & mut self , key : K , val : V ) -> & mut Self
15
- where
16
- Self : Sized ,
17
- K : AsRef < OsStr > ,
18
- V : AsRef < OsStr > ;
19
- }
20
-
21
- impl ProcessEnvs for Command {
22
- fn env < K , V > ( & mut self , key : K , val : V ) -> & mut Self
23
- where
24
- Self : Sized ,
25
- K : AsRef < OsStr > ,
26
- V : AsRef < OsStr > ,
27
- {
28
- self . env ( key, val)
29
- }
30
- }
31
-
32
- #[ allow( unused) ]
33
- fn append_path < E : ProcessEnvs > ( name : & str , value : Vec < PathBuf > , cmd : & mut E ) {
34
- let old_value = process ( ) . var_os ( name) ;
35
- let mut parts: Vec < PathBuf > ;
36
- if let Some ( ref v) = old_value {
37
- let old_paths: Vec < PathBuf > = env:: split_paths ( v) . collect :: < Vec < _ > > ( ) ;
38
- parts = concat_uniq_paths ( old_paths, value) ;
39
- } else {
40
- parts = value;
41
- }
42
- if let Ok ( new_value) = env:: join_paths ( parts) {
43
- cmd. env ( name, new_value) ;
44
- }
45
- }
46
-
47
- pub ( crate ) fn prepend_path < E : ProcessEnvs > ( name : & str , prepend : Vec < PathBuf > , cmd : & mut E ) {
10
+ pub ( crate ) fn prepend_path ( name : & str , prepend : Vec < PathBuf > , cmd : & mut Command ) {
48
11
let old_value = process ( ) . var_os ( name) ;
49
12
let parts = if let Some ( ref v) = old_value {
50
13
let mut tail = env:: split_paths ( v) . collect :: < VecDeque < _ > > ( ) ;
@@ -73,117 +36,15 @@ pub(crate) fn inc(name: &str, cmd: &mut Command) {
73
36
cmd. env ( name, ( old_value + 1 ) . to_string ( ) ) ;
74
37
}
75
38
76
- fn concat_uniq_paths ( fst_paths : Vec < PathBuf > , snd_paths : Vec < PathBuf > ) -> Vec < PathBuf > {
77
- let deduped_fst_paths = dedupe_with_preserved_order ( fst_paths) ;
78
- let deduped_snd_paths = dedupe_with_preserved_order ( snd_paths) ;
79
-
80
- let vec_fst_paths: Vec < _ > = deduped_fst_paths. into_iter ( ) . collect ( ) ;
81
-
82
- let mut unified_paths;
83
- unified_paths = vec_fst_paths. clone ( ) ;
84
- unified_paths. extend (
85
- deduped_snd_paths
86
- . into_iter ( )
87
- . filter ( |v| !vec_fst_paths. contains ( v) )
88
- . collect :: < Vec < _ > > ( ) ,
89
- ) ;
90
-
91
- unified_paths
92
- }
93
-
94
- fn dedupe_with_preserved_order ( paths : Vec < PathBuf > ) -> Vec < PathBuf > {
95
- let mut uniq_paths: Vec < PathBuf > = Vec :: new ( ) ;
96
- let mut seen_paths: HashSet < PathBuf > = HashSet :: new ( ) ;
97
-
98
- for path in & paths {
99
- if !seen_paths. contains ( path) {
100
- seen_paths. insert ( path. to_path_buf ( ) ) ;
101
- uniq_paths. push ( path. to_path_buf ( ) ) ;
102
- }
103
- }
104
-
105
- uniq_paths
106
- }
107
-
108
39
#[ cfg( test) ]
109
40
mod tests {
110
41
use super :: * ;
111
42
use crate :: currentprocess;
112
43
use crate :: test:: { with_saved_path, Env } ;
113
44
114
- use super :: ProcessEnvs ;
115
45
use std:: collections:: HashMap ;
116
46
use std:: ffi:: { OsStr , OsString } ;
117
47
118
- #[ derive( Default ) ]
119
- struct TestCommand {
120
- envs : HashMap < OsString , Option < OsString > > ,
121
- }
122
-
123
- impl ProcessEnvs for TestCommand {
124
- fn env < K , V > ( & mut self , key : K , val : V ) -> & mut Self
125
- where
126
- Self : Sized ,
127
- K : AsRef < OsStr > ,
128
- V : AsRef < OsStr > ,
129
- {
130
- self . envs
131
- . insert ( key. as_ref ( ) . to_owned ( ) , Some ( val. as_ref ( ) . to_owned ( ) ) ) ;
132
- self
133
- }
134
- }
135
-
136
- #[ test]
137
- fn deduplicate_and_concat_paths ( ) {
138
- let mut old_paths = vec ! [ ] ;
139
-
140
- let z = OsString :: from ( "/home/z/.cargo/bin" ) ;
141
- let path_z = PathBuf :: from ( z) ;
142
- old_paths. push ( path_z) ;
143
-
144
- let a = OsString :: from ( "/home/a/.cargo/bin" ) ;
145
- let path_a = PathBuf :: from ( a) ;
146
- old_paths. push ( path_a) ;
147
-
148
- let _a = OsString :: from ( "/home/a/.cargo/bin" ) ;
149
- let _path_a = PathBuf :: from ( _a) ;
150
- old_paths. push ( _path_a) ;
151
-
152
- let mut new_paths = vec ! [ ] ;
153
-
154
- let n = OsString :: from ( "/home/n/.cargo/bin" ) ;
155
- let path_n = PathBuf :: from ( n) ;
156
- new_paths. push ( path_n) ;
157
-
158
- let g = OsString :: from ( "/home/g/.cargo/bin" ) ;
159
- let path_g = PathBuf :: from ( g) ;
160
- new_paths. push ( path_g) ;
161
-
162
- let _g = OsString :: from ( "/home/g/.cargo/bin" ) ;
163
- let _path_g = PathBuf :: from ( _g) ;
164
- new_paths. push ( _path_g) ;
165
-
166
- let _z = OsString :: from ( "/home/z/.cargo/bin" ) ;
167
- let path_z = PathBuf :: from ( _z) ;
168
- old_paths. push ( path_z) ;
169
-
170
- let mut unified_paths: Vec < PathBuf > = vec ! [ ] ;
171
- let zpath = OsString :: from ( "/home/z/.cargo/bin" ) ;
172
- let _zpath = PathBuf :: from ( zpath) ;
173
- unified_paths. push ( _zpath) ;
174
- let apath = OsString :: from ( "/home/a/.cargo/bin" ) ;
175
- let _apath = PathBuf :: from ( apath) ;
176
- unified_paths. push ( _apath) ;
177
- let npath = OsString :: from ( "/home/n/.cargo/bin" ) ;
178
- let _npath = PathBuf :: from ( npath) ;
179
- unified_paths. push ( _npath) ;
180
- let gpath = OsString :: from ( "/home/g/.cargo/bin" ) ;
181
- let _gpath = PathBuf :: from ( gpath) ;
182
- unified_paths. push ( _gpath) ;
183
-
184
- assert_eq ! ( concat_uniq_paths( old_paths, new_paths) , unified_paths) ;
185
- }
186
-
187
48
#[ test]
188
49
fn prepend_unique_path ( ) {
189
50
let mut vars = HashMap :: new ( ) ;
@@ -198,7 +59,7 @@ mod tests {
198
59
with_saved_path ( & || {
199
60
currentprocess:: with ( tp. clone ( ) , || {
200
61
let mut path_entries = vec ! [ ] ;
201
- let mut cmd = TestCommand :: default ( ) ;
62
+ let mut cmd = Command :: new ( "test" ) ;
202
63
203
64
let a = OsString :: from ( "/home/a/.cargo/bin" ) ;
204
65
let path_a = PathBuf :: from ( a) ;
@@ -213,13 +74,13 @@ mod tests {
213
74
path_entries. push ( path_z) ;
214
75
215
76
prepend_path ( "PATH" , path_entries, & mut cmd) ;
216
- let envs: Vec < _ > = cmd. envs . iter ( ) . collect ( ) ;
77
+ let envs: Vec < _ > = cmd. get_envs ( ) . collect ( ) ;
217
78
218
79
assert_eq ! (
219
80
envs,
220
81
& [ (
221
- & OsString :: from ( "PATH" ) ,
222
- & Some (
82
+ OsStr :: new ( "PATH" ) ,
83
+ Some (
223
84
env:: join_paths(
224
85
vec![
225
86
"/home/z/.cargo/bin" ,
@@ -229,6 +90,7 @@ mod tests {
229
90
. iter( )
230
91
)
231
92
. unwrap( )
93
+ . as_os_str( )
232
94
)
233
95
) , ]
234
96
) ;
0 commit comments