@@ -109,15 +109,6 @@ pub fn copy_files_except_ext(
109
109
return Ok ( ( ) ) ;
110
110
}
111
111
112
- // Is the destination inside of the source?
113
- if to. canonicalize ( ) ?. starts_with ( from. canonicalize ( ) ?) {
114
- return Err ( Error :: from ( format ! (
115
- "Destination directory cannot be contained in source directory: '{}' is in '{}'" ,
116
- to. display( ) ,
117
- from. display( )
118
- ) ) ) ;
119
- }
120
-
121
112
for entry in fs:: read_dir ( from) ? {
122
113
let entry = entry?;
123
114
let metadata = entry. metadata ( ) ?;
@@ -185,82 +176,64 @@ mod tests {
185
176
use super :: copy_files_except_ext;
186
177
use std:: fs;
187
178
188
- #[ test]
189
- fn it_fails_when_destination_is_in_source ( ) {
190
- let tmp = match tempfile:: TempDir :: new ( ) {
191
- Ok ( t) => t,
192
- Err ( e) => panic ! ( "Could not create a temp dir: {}" , e) ,
193
- } ;
194
- let dst = tmp. path ( ) . join ( "destination" ) ;
195
- fs:: create_dir ( & dst) . unwrap ( ) ;
196
- assert ! (
197
- format!( "{:?}" , copy_files_except_ext( tmp. path( ) , & dst, true , & [ ] ) )
198
- . contains( "Destination directory cannot be contained in source directory: " )
199
- ) ;
200
- }
201
-
202
179
#[ test]
203
180
fn copy_files_except_ext_test ( ) {
204
- let src = match tempfile:: TempDir :: new ( ) {
181
+ let tmp = match tempfile:: TempDir :: new ( ) {
205
182
Ok ( t) => t,
206
183
Err ( e) => panic ! ( "Could not create a temp dir: {}" , e) ,
207
184
} ;
208
185
209
186
// Create a couple of files
210
- if let Err ( err) = fs:: File :: create ( & src . path ( ) . join ( "file.txt" ) ) {
187
+ if let Err ( err) = fs:: File :: create ( & tmp . path ( ) . join ( "file.txt" ) ) {
211
188
panic ! ( "Could not create file.txt: {}" , err) ;
212
189
}
213
- if let Err ( err) = fs:: File :: create ( & src . path ( ) . join ( "file.md" ) ) {
190
+ if let Err ( err) = fs:: File :: create ( & tmp . path ( ) . join ( "file.md" ) ) {
214
191
panic ! ( "Could not create file.md: {}" , err) ;
215
192
}
216
- if let Err ( err) = fs:: File :: create ( & src . path ( ) . join ( "file.png" ) ) {
193
+ if let Err ( err) = fs:: File :: create ( & tmp . path ( ) . join ( "file.png" ) ) {
217
194
panic ! ( "Could not create file.png: {}" , err) ;
218
195
}
219
- if let Err ( err) = fs:: create_dir ( & src . path ( ) . join ( "sub_dir" ) ) {
196
+ if let Err ( err) = fs:: create_dir ( & tmp . path ( ) . join ( "sub_dir" ) ) {
220
197
panic ! ( "Could not create sub_dir: {}" , err) ;
221
198
}
222
- if let Err ( err) = fs:: File :: create ( & src . path ( ) . join ( "sub_dir/file.png" ) ) {
199
+ if let Err ( err) = fs:: File :: create ( & tmp . path ( ) . join ( "sub_dir/file.png" ) ) {
223
200
panic ! ( "Could not create sub_dir/file.png: {}" , err) ;
224
201
}
225
- if let Err ( err) = fs:: create_dir ( & src . path ( ) . join ( "sub_dir_exists" ) ) {
202
+ if let Err ( err) = fs:: create_dir ( & tmp . path ( ) . join ( "sub_dir_exists" ) ) {
226
203
panic ! ( "Could not create sub_dir_exists: {}" , err) ;
227
204
}
228
- if let Err ( err) = fs:: File :: create ( & src . path ( ) . join ( "sub_dir_exists/file.txt" ) ) {
205
+ if let Err ( err) = fs:: File :: create ( & tmp . path ( ) . join ( "sub_dir_exists/file.txt" ) ) {
229
206
panic ! ( "Could not create sub_dir_exists/file.txt: {}" , err) ;
230
207
}
231
208
232
209
// Create output dir
233
- let dst = match tempfile:: TempDir :: new ( ) {
234
- Ok ( t) => t,
235
- Err ( e) => panic ! ( "Could not create a temp dir: {}" , e) ,
236
- } ;
237
- if let Err ( err) = fs:: create_dir ( & dst. path ( ) . join ( "output" ) ) {
210
+ if let Err ( err) = fs:: create_dir ( & tmp. path ( ) . join ( "output" ) ) {
238
211
panic ! ( "Could not create output: {}" , err) ;
239
212
}
240
- if let Err ( err) = fs:: create_dir ( & dst . path ( ) . join ( "output/sub_dir_exists" ) ) {
213
+ if let Err ( err) = fs:: create_dir ( & tmp . path ( ) . join ( "output/sub_dir_exists" ) ) {
241
214
panic ! ( "Could not create output/sub_dir_exists: {}" , err) ;
242
215
}
243
216
244
217
if let Err ( e) =
245
- copy_files_except_ext ( & src . path ( ) , & dst . path ( ) . join ( "output" ) , true , & [ "md" ] )
218
+ copy_files_except_ext ( & tmp . path ( ) , & tmp . path ( ) . join ( "output" ) , true , & [ "md" ] )
246
219
{
247
220
panic ! ( "Error while executing the function:\n {:?}" , e) ;
248
221
}
249
222
250
223
// Check if the correct files where created
251
- if !( & dst . path ( ) . join ( "output/file.txt" ) ) . exists ( ) {
224
+ if !( & tmp . path ( ) . join ( "output/file.txt" ) ) . exists ( ) {
252
225
panic ! ( "output/file.txt should exist" )
253
226
}
254
- if ( & dst . path ( ) . join ( "output/file.md" ) ) . exists ( ) {
227
+ if ( & tmp . path ( ) . join ( "output/file.md" ) ) . exists ( ) {
255
228
panic ! ( "output/file.md should not exist" )
256
229
}
257
- if !( & dst . path ( ) . join ( "output/file.png" ) ) . exists ( ) {
230
+ if !( & tmp . path ( ) . join ( "output/file.png" ) ) . exists ( ) {
258
231
panic ! ( "output/file.png should exist" )
259
232
}
260
- if !( & dst . path ( ) . join ( "output/sub_dir/file.png" ) ) . exists ( ) {
233
+ if !( & tmp . path ( ) . join ( "output/sub_dir/file.png" ) ) . exists ( ) {
261
234
panic ! ( "output/sub_dir/file.png should exist" )
262
235
}
263
- if !( & dst . path ( ) . join ( "output/sub_dir_exists/file.txt" ) ) . exists ( ) {
236
+ if !( & tmp . path ( ) . join ( "output/sub_dir_exists/file.txt" ) ) . exists ( ) {
264
237
panic ! ( "output/sub_dir/file.png should exist" )
265
238
}
266
239
}
0 commit comments