@@ -47,3 +47,52 @@ pub fn inc(name: &str, cmd: &mut Command) {
47
47
48
48
cmd. env ( name, ( old_value + 1 ) . to_string ( ) ) ;
49
49
}
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
+ }
0 commit comments