@@ -84,13 +84,53 @@ fn remove_tempdirs() -> Result<(), io::Error> {
84
84
// NOTE: hardcodes that `tempfile::tempdir()` uses `std::env::temp_dir`.
85
85
for entry in std:: fs:: read_dir ( std:: env:: temp_dir ( ) ) ? {
86
86
let entry = entry?;
87
- if !entry. path ( ) . starts_with ( TEMPDIR_PREFIX ) {
87
+ if !entry. metadata ( ) ? . is_dir ( ) {
88
88
continue ;
89
89
}
90
- if entry. metadata ( ) ?. is_dir ( ) {
91
- fs:: remove_dir_all ( entry. path ( ) ) ?;
90
+
91
+ if let Some ( dir_name) = entry. path ( ) . file_name ( ) {
92
+ if dir_name. to_string_lossy ( ) . starts_with ( TEMPDIR_PREFIX ) {
93
+ fs:: remove_dir_all ( entry. path ( ) ) ?;
94
+ }
92
95
}
93
96
}
94
97
95
98
Ok ( ( ) )
96
99
}
100
+
101
+ #[ cfg( test) ]
102
+ mod tests {
103
+ use super :: * ;
104
+
105
+ #[ test]
106
+ fn remove_existing_tempdirs ( ) {
107
+ let file_with_prefix = tempfile:: Builder :: new ( )
108
+ . prefix ( TEMPDIR_PREFIX )
109
+ . tempfile ( )
110
+ . unwrap ( ) ;
111
+
112
+ let dir_with_prefix = tempfile:: Builder :: new ( )
113
+ . prefix ( TEMPDIR_PREFIX )
114
+ . tempdir ( )
115
+ . unwrap ( ) ;
116
+
117
+ let file_inside = dir_with_prefix. path ( ) . join ( "some_file_name" ) ;
118
+ fs:: File :: create ( & file_inside) . unwrap ( ) ;
119
+
120
+ let other_file = tempfile:: Builder :: new ( ) . tempfile ( ) . unwrap ( ) ;
121
+
122
+ let other_dir = tempfile:: Builder :: new ( ) . tempdir ( ) . unwrap ( ) ;
123
+
124
+ assert ! ( dir_with_prefix. path( ) . exists( ) ) ;
125
+
126
+ remove_tempdirs ( ) . unwrap ( ) ;
127
+
128
+ assert ! ( !dir_with_prefix. path( ) . exists( ) ) ;
129
+ assert ! ( !file_inside. exists( ) ) ;
130
+
131
+ // all these still exist
132
+ assert ! ( file_with_prefix. path( ) . exists( ) ) ;
133
+ assert ! ( other_file. path( ) . exists( ) ) ;
134
+ assert ! ( other_dir. path( ) . exists( ) ) ;
135
+ }
136
+ }
0 commit comments