1+ import os
2+ import tempfile
3+ from glob import glob
14from unittest import TestCase
25
36from mdps_ds_lib .lib .utils .file_utils import FileUtils
@@ -14,3 +17,35 @@ def test_is_relative_path(self):
1417 self .assertTrue (FileUtils .is_relative_path ('./test' ))
1518 self .assertTrue (FileUtils .is_relative_path ('../test' ))
1619 return
20+
21+ def test_copy_dir (self ):
22+ with tempfile .TemporaryDirectory () as tmp_dir_name :
23+ src_dir = os .path .join (tmp_dir_name , 'src_dir' )
24+ dest_dir1 = os .path .join (tmp_dir_name , 'dest_dir1' )
25+ dest_dir2 = os .path .join (tmp_dir_name , 'dest_dir2' )
26+
27+ src_dir1 = os .path .join (tmp_dir_name , 'src_dir' , 'child1' )
28+ src_dir2 = os .path .join (tmp_dir_name , 'src_dir' , 'child2' , 'g-child1' )
29+ dest_dir22 = os .path .join (tmp_dir_name , 'dest_dir2' , 'child2' , 'g-child1' )
30+ FileUtils .mk_dir_p (src_dir1 )
31+ FileUtils .mk_dir_p (src_dir2 )
32+ FileUtils .mk_dir_p (dest_dir22 )
33+
34+ FileUtils .write_json (os .path .join (src_dir1 , 'test1.json' ), {'test' : 1 }, True , False , True )
35+ FileUtils .write_json (os .path .join (src_dir2 , 'test2.json' ), {'test' : 1 }, True , False , True )
36+ FileUtils .write_json (os .path .join (src_dir2 , 'test3.json' ), {'test' : 1 }, True , False , True )
37+ FileUtils .write_json (os .path .join (dest_dir22 , 'test3.json' ), {'test' : 100 }, True , False , True )
38+ FileUtils .write_json (os .path .join (dest_dir22 , 'test4.json' ), {'test' : 88 }, True , False , True )
39+
40+ FileUtils .copy_dir (src_dir , dest_dir1 )
41+ FileUtils .copy_dir (src_dir , dest_dir2 , True )
42+
43+ result = [k for k in glob (os .path .join (dest_dir1 , '**/*' ), recursive = True )]
44+ print (len (result ), 6 , f'wrong length: { result } ' )
45+ result = [k for k in glob (os .path .join (dest_dir2 , '**/*' ), recursive = True )]
46+ print (len (result ), 7 , f'wrong length: { result } ' )
47+
48+ with self .assertRaises (FileExistsError ) as cm :
49+ FileUtils .copy_dir (src_dir , dest_dir2 )
50+
51+ return
0 commit comments