@@ -238,6 +238,7 @@ fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()> {
238238#[ cfg( test) ]
239239mod tests {
240240 use super :: * ;
241+ use ignore:: gitignore:: GitignoreBuilder ;
241242 use std:: io:: Result ;
242243 use std:: path:: Path ;
243244
@@ -298,4 +299,56 @@ mod tests {
298299 panic ! ( "output/symlink.png should exist" )
299300 }
300301 }
302+
303+ #[ test]
304+ fn copy_files_except_ignored_test ( ) {
305+ let tmp = match tempfile:: TempDir :: new ( ) {
306+ Ok ( t) => t,
307+ Err ( e) => panic ! ( "Could not create a temp dir: {e}" ) ,
308+ } ;
309+
310+ // Create files and directories
311+ write ( tmp. path ( ) . join ( "file.txt" ) , "" ) . unwrap ( ) ;
312+ write ( tmp. path ( ) . join ( "file.json" ) , "" ) . unwrap ( ) ;
313+ write ( tmp. path ( ) . join ( "ignored_dir/nested.txt" ) , "" ) . unwrap ( ) ;
314+ write ( tmp. path ( ) . join ( "included_dir/file.json" ) , "" ) . unwrap ( ) ;
315+ write ( tmp. path ( ) . join ( "included_dir/file.txt" ) , "" ) . unwrap ( ) ;
316+
317+ // Create a gitignore that ignores *.txt and ignored_dir/
318+ let mut builder = GitignoreBuilder :: new ( tmp. path ( ) ) ;
319+ builder. add_line ( None , "*.txt" ) . unwrap ( ) ;
320+ builder. add_line ( None , "ignored_dir/" ) . unwrap ( ) ;
321+ let ignore = builder. build ( ) . unwrap ( ) ;
322+
323+ // Create output dir
324+ create_dir_all ( tmp. path ( ) . join ( "output" ) ) . unwrap ( ) ;
325+
326+ copy_files_except_ignored (
327+ tmp. path ( ) ,
328+ & tmp. path ( ) . join ( "output" ) ,
329+ true ,
330+ None ,
331+ Some ( & ignore) ,
332+ )
333+ . unwrap ( ) ;
334+
335+ // Check that .txt files are ignored
336+ if tmp. path ( ) . join ( "output/file.txt" ) . exists ( ) {
337+ panic ! ( "output/file.txt should not exist" )
338+ }
339+ if tmp. path ( ) . join ( "output/included_dir/file.txt" ) . exists ( ) {
340+ panic ! ( "output/included_dir/file.txt should not exist" )
341+ }
342+ // Check that ignored_dir is not copied
343+ if tmp. path ( ) . join ( "output/ignored_dir" ) . exists ( ) {
344+ panic ! ( "output/ignored_dir should not exist" )
345+ }
346+ // Check that non-ignored files are copied
347+ if !tmp. path ( ) . join ( "output/file.json" ) . exists ( ) {
348+ panic ! ( "output/file.json should exist" )
349+ }
350+ if !tmp. path ( ) . join ( "output/included_dir/file.json" ) . exists ( ) {
351+ panic ! ( "output/included_dir/file.json should exist" )
352+ }
353+ }
301354}
0 commit comments