Debian Codegraph @

testing/mozbase/mozfile/tests/test_copycontents.py

raw
1 #!/usr/bin/env python
2
3 import os
4 import shutil
5 import unittest
6
7 import mozfile
8 import mozunit
9 import stubs
10
11
12 class MozfileCopyContentsTestCase(unittest.TestCase):
13 """Test our ability to copy the contents of directories"""
14
15 def _directory_is_subset(self, set_, subset_):
16 """
17 Confirm that all the contents of 'subset_' are contained in 'set_'
18 """
19 names = os.listdir(subset_)
20 for name in names:
21 full_set_path = os.path.join(set_, name)
22 full_subset_path = os.path.join(subset_, name)
23 if os.path.isdir(full_subset_path):
24 self.assertTrue(os.path.isdir(full_set_path))
25 self._directory_is_subset(full_set_path, full_subset_path)
26 elif os.path.islink(full_subset_path):
27 self.assertTrue(os.path.islink(full_set_path))
28 else:
29 self.assertTrue(os.stat(full_set_path))
30
31 def _directories_are_equal(self, dir1, dir2):
32 """
33 Confirm that the contents of 'dir1' are the same as 'dir2'
34 """
35 names1 = os.listdir(dir1)
36 names2 = os.listdir(dir2)
37 self.assertTrue(len(names1) == len(names2))
38 for name in names1:
39 self.assertTrue(name in names2)
40 dir1_path = os.path.join(dir1, name)
41 dir2_path = os.path.join(dir2, name)
42 if os.path.isdir(dir1_path):
43 self.assertTrue(os.path.isdir(dir2_path))
44 self._directories_are_equal(dir1_path, dir2_path)
45 elif os.path.islink(dir1_path):
46 self.assertTrue(os.path.islink(dir2_path))
47 else:
48 self.assertTrue(os.stat(dir2_path))
49
50 def test_copy_empty_directory(self):
51 tempdir = stubs.create_empty_stub()
52 dstdir = stubs.create_empty_stub()
53 self.assertTrue(os.path.isdir(tempdir))
54
55 mozfile.copy_contents(tempdir, dstdir)
56 self._directories_are_equal(dstdir, tempdir)
57
58 if os.path.isdir(tempdir):
59 shutil.rmtree(tempdir)
60 if os.path.isdir(dstdir):
61 shutil.rmtree(dstdir)
62
63 def test_copy_full_directory(self):
64 tempdir = stubs.create_stub()
65 dstdir = stubs.create_empty_stub()
66 self.assertTrue(os.path.isdir(tempdir))
67
68 mozfile.copy_contents(tempdir, dstdir)
69 self._directories_are_equal(dstdir, tempdir)
70
71 if os.path.isdir(tempdir):
72 shutil.rmtree(tempdir)
73 if os.path.isdir(dstdir):
74 shutil.rmtree(dstdir)
75
76 def test_copy_full_directory_with_existing_file(self):
77 tempdir = stubs.create_stub()
78 dstdir = stubs.create_empty_stub()
79
80 filename = "i_dont_exist_in_tempdir"
81 f = open(os.path.join(dstdir, filename), "w")
82 f.write("Hello World")
83 f.close()
84
85 self.assertTrue(os.path.isdir(tempdir))
86
87 mozfile.copy_contents(tempdir, dstdir)
88 self._directory_is_subset(dstdir, tempdir)
89 self.assertTrue(os.path.exists(os.path.join(dstdir, filename)))
90
91 if os.path.isdir(tempdir):
92 shutil.rmtree(tempdir)
93 if os.path.isdir(dstdir):
94 shutil.rmtree(dstdir)
95
96 def test_copy_full_directory_with_overlapping_file(self):
97 tempdir = stubs.create_stub()
98 dstdir = stubs.create_empty_stub()
99
100 filename = "i_do_exist_in_tempdir"
101 for d in [tempdir, dstdir]:
102 f = open(os.path.join(d, filename), "w")
103 f.write("Hello " + d)
104 f.close()
105
106 self.assertTrue(os.path.isdir(tempdir))
107 self.assertTrue(os.path.exists(os.path.join(tempdir, filename)))
108 self.assertTrue(os.path.exists(os.path.join(dstdir, filename)))
109
110 line = open(os.path.join(dstdir, filename)).readlines()[0]
111 self.assertTrue(line == "Hello " + dstdir)
112
113 mozfile.copy_contents(tempdir, dstdir)
114
115 line = open(os.path.join(dstdir, filename)).readlines()[0]
116 self.assertTrue(line == "Hello " + tempdir)
117 self._directories_are_equal(tempdir, dstdir)
118
119 if os.path.isdir(tempdir):
120 shutil.rmtree(tempdir)
121 if os.path.isdir(dstdir):
122 shutil.rmtree(dstdir)
123
124
125 if __name__ == "__main__":
126 mozunit.main()