File size: 1,083 Bytes
96fe658
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import os
import shutil
import tempfile
import unittest

from swift.utils import copy_files_by_pattern


class TestFileUtils(unittest.TestCase):

    def setUp(self):
        self._tmp_dir = tempfile.TemporaryDirectory()
        self.tmp_dir = self._tmp_dir.name

    def tearDown(self):
        shutil.rmtree(self.tmp_dir)

    def test_copy_files(self):
        os.makedirs(os.path.join(self.tmp_dir, 'source'))
        os.makedirs(os.path.join(self.tmp_dir, 'source', 'subfolder'))
        with open(os.path.join(self.tmp_dir, 'source', '1.txt'), 'w') as f:
            f.write('')
        with open(os.path.join(self.tmp_dir, 'source', 'subfolder', '2.txt'), 'w') as f:
            f.write('')
        copy_files_by_pattern(
            os.path.join(self.tmp_dir, 'source'), os.path.join(self.tmp_dir, 'target'), ['*.txt', 'subfolder/*.txt'])
        self.assertTrue(os.path.exists(os.path.join(self.tmp_dir, 'target', '1.txt')))
        self.assertTrue(os.path.exists(os.path.join(self.tmp_dir, 'target', 'subfolder', '2.txt')))


if __name__ == '__main__':
    unittest.main()