ยปCore Development>Code coverage>Lib/test/test_filecmp.py

Python code coverage for Lib/test/test_filecmp.py

#countcontent
1n/aimport filecmp
2n/aimport os
3n/aimport shutil
4n/aimport tempfile
5n/aimport unittest
6n/a
7n/afrom test import support
8n/a
9n/a
10n/aclass FileCompareTestCase(unittest.TestCase):
11n/a def setUp(self):
12n/a self.name = support.TESTFN
13n/a self.name_same = support.TESTFN + '-same'
14n/a self.name_diff = support.TESTFN + '-diff'
15n/a data = 'Contents of file go here.\n'
16n/a for name in [self.name, self.name_same, self.name_diff]:
17n/a with open(name, 'w') as output:
18n/a output.write(data)
19n/a
20n/a with open(self.name_diff, 'a+') as output:
21n/a output.write('An extra line.\n')
22n/a self.dir = tempfile.gettempdir()
23n/a
24n/a def tearDown(self):
25n/a os.unlink(self.name)
26n/a os.unlink(self.name_same)
27n/a os.unlink(self.name_diff)
28n/a
29n/a def test_matching(self):
30n/a self.assertTrue(filecmp.cmp(self.name, self.name),
31n/a "Comparing file to itself fails")
32n/a self.assertTrue(filecmp.cmp(self.name, self.name, shallow=False),
33n/a "Comparing file to itself fails")
34n/a self.assertTrue(filecmp.cmp(self.name, self.name_same),
35n/a "Comparing file to identical file fails")
36n/a self.assertTrue(filecmp.cmp(self.name, self.name_same, shallow=False),
37n/a "Comparing file to identical file fails")
38n/a
39n/a def test_different(self):
40n/a self.assertFalse(filecmp.cmp(self.name, self.name_diff),
41n/a "Mismatched files compare as equal")
42n/a self.assertFalse(filecmp.cmp(self.name, self.dir),
43n/a "File and directory compare as equal")
44n/a
45n/a def test_cache_clear(self):
46n/a first_compare = filecmp.cmp(self.name, self.name_same, shallow=False)
47n/a second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False)
48n/a filecmp.clear_cache()
49n/a self.assertTrue(len(filecmp._cache) == 0,
50n/a "Cache not cleared after calling clear_cache")
51n/a
52n/aclass DirCompareTestCase(unittest.TestCase):
53n/a def setUp(self):
54n/a tmpdir = tempfile.gettempdir()
55n/a self.dir = os.path.join(tmpdir, 'dir')
56n/a self.dir_same = os.path.join(tmpdir, 'dir-same')
57n/a self.dir_diff = os.path.join(tmpdir, 'dir-diff')
58n/a
59n/a # Another dir is created under dir_same, but it has a name from the
60n/a # ignored list so it should not affect testing results.
61n/a self.dir_ignored = os.path.join(self.dir_same, '.hg')
62n/a
63n/a self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
64n/a data = 'Contents of file go here.\n'
65n/a for dir in (self.dir, self.dir_same, self.dir_diff, self.dir_ignored):
66n/a shutil.rmtree(dir, True)
67n/a os.mkdir(dir)
68n/a if self.caseinsensitive and dir is self.dir_same:
69n/a fn = 'FiLe' # Verify case-insensitive comparison
70n/a else:
71n/a fn = 'file'
72n/a with open(os.path.join(dir, fn), 'w') as output:
73n/a output.write(data)
74n/a
75n/a with open(os.path.join(self.dir_diff, 'file2'), 'w') as output:
76n/a output.write('An extra file.\n')
77n/a
78n/a def tearDown(self):
79n/a for dir in (self.dir, self.dir_same, self.dir_diff):
80n/a shutil.rmtree(dir)
81n/a
82n/a def test_default_ignores(self):
83n/a self.assertIn('.hg', filecmp.DEFAULT_IGNORES)
84n/a
85n/a def test_cmpfiles(self):
86n/a self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file']) ==
87n/a (['file'], [], []),
88n/a "Comparing directory to itself fails")
89n/a self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file']) ==
90n/a (['file'], [], []),
91n/a "Comparing directory to same fails")
92n/a
93n/a # Try it with shallow=False
94n/a self.assertTrue(filecmp.cmpfiles(self.dir, self.dir, ['file'],
95n/a shallow=False) ==
96n/a (['file'], [], []),
97n/a "Comparing directory to itself fails")
98n/a self.assertTrue(filecmp.cmpfiles(self.dir, self.dir_same, ['file'],
99n/a shallow=False),
100n/a "Comparing directory to same fails")
101n/a
102n/a # Add different file2
103n/a with open(os.path.join(self.dir, 'file2'), 'w') as output:
104n/a output.write('Different contents.\n')
105n/a
106n/a self.assertFalse(filecmp.cmpfiles(self.dir, self.dir_same,
107n/a ['file', 'file2']) ==
108n/a (['file'], ['file2'], []),
109n/a "Comparing mismatched directories fails")
110n/a
111n/a
112n/a def test_dircmp(self):
113n/a # Check attributes for comparison of two identical directories
114n/a left_dir, right_dir = self.dir, self.dir_same
115n/a d = filecmp.dircmp(left_dir, right_dir)
116n/a self.assertEqual(d.left, left_dir)
117n/a self.assertEqual(d.right, right_dir)
118n/a if self.caseinsensitive:
119n/a self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']])
120n/a else:
121n/a self.assertEqual([d.left_list, d.right_list],[['file'], ['file']])
122n/a self.assertEqual(d.common, ['file'])
123n/a self.assertEqual(d.left_only, [])
124n/a self.assertEqual(d.right_only, [])
125n/a self.assertEqual(d.same_files, ['file'])
126n/a self.assertEqual(d.diff_files, [])
127n/a expected_report = [
128n/a "diff {} {}".format(self.dir, self.dir_same),
129n/a "Identical files : ['file']",
130n/a ]
131n/a self._assert_report(d.report, expected_report)
132n/a
133n/a # Check attributes for comparison of two different directories (right)
134n/a left_dir, right_dir = self.dir, self.dir_diff
135n/a d = filecmp.dircmp(left_dir, right_dir)
136n/a self.assertEqual(d.left, left_dir)
137n/a self.assertEqual(d.right, right_dir)
138n/a self.assertEqual(d.left_list, ['file'])
139n/a self.assertEqual(d.right_list, ['file', 'file2'])
140n/a self.assertEqual(d.common, ['file'])
141n/a self.assertEqual(d.left_only, [])
142n/a self.assertEqual(d.right_only, ['file2'])
143n/a self.assertEqual(d.same_files, ['file'])
144n/a self.assertEqual(d.diff_files, [])
145n/a expected_report = [
146n/a "diff {} {}".format(self.dir, self.dir_diff),
147n/a "Only in {} : ['file2']".format(self.dir_diff),
148n/a "Identical files : ['file']",
149n/a ]
150n/a self._assert_report(d.report, expected_report)
151n/a
152n/a # Check attributes for comparison of two different directories (left)
153n/a left_dir, right_dir = self.dir, self.dir_diff
154n/a shutil.move(
155n/a os.path.join(self.dir_diff, 'file2'),
156n/a os.path.join(self.dir, 'file2')
157n/a )
158n/a d = filecmp.dircmp(left_dir, right_dir)
159n/a self.assertEqual(d.left, left_dir)
160n/a self.assertEqual(d.right, right_dir)
161n/a self.assertEqual(d.left_list, ['file', 'file2'])
162n/a self.assertEqual(d.right_list, ['file'])
163n/a self.assertEqual(d.common, ['file'])
164n/a self.assertEqual(d.left_only, ['file2'])
165n/a self.assertEqual(d.right_only, [])
166n/a self.assertEqual(d.same_files, ['file'])
167n/a self.assertEqual(d.diff_files, [])
168n/a expected_report = [
169n/a "diff {} {}".format(self.dir, self.dir_diff),
170n/a "Only in {} : ['file2']".format(self.dir),
171n/a "Identical files : ['file']",
172n/a ]
173n/a self._assert_report(d.report, expected_report)
174n/a
175n/a # Add different file2
176n/a with open(os.path.join(self.dir_diff, 'file2'), 'w') as output:
177n/a output.write('Different contents.\n')
178n/a d = filecmp.dircmp(self.dir, self.dir_diff)
179n/a self.assertEqual(d.same_files, ['file'])
180n/a self.assertEqual(d.diff_files, ['file2'])
181n/a expected_report = [
182n/a "diff {} {}".format(self.dir, self.dir_diff),
183n/a "Identical files : ['file']",
184n/a "Differing files : ['file2']",
185n/a ]
186n/a self._assert_report(d.report, expected_report)
187n/a
188n/a def test_report_partial_closure(self):
189n/a left_dir, right_dir = self.dir, self.dir_same
190n/a d = filecmp.dircmp(left_dir, right_dir)
191n/a expected_report = [
192n/a "diff {} {}".format(self.dir, self.dir_same),
193n/a "Identical files : ['file']",
194n/a ]
195n/a self._assert_report(d.report_partial_closure, expected_report)
196n/a
197n/a def test_report_full_closure(self):
198n/a left_dir, right_dir = self.dir, self.dir_same
199n/a d = filecmp.dircmp(left_dir, right_dir)
200n/a expected_report = [
201n/a "diff {} {}".format(self.dir, self.dir_same),
202n/a "Identical files : ['file']",
203n/a ]
204n/a self._assert_report(d.report_full_closure, expected_report)
205n/a
206n/a def _assert_report(self, dircmp_report, expected_report_lines):
207n/a with support.captured_stdout() as stdout:
208n/a dircmp_report()
209n/a report_lines = stdout.getvalue().strip().split('\n')
210n/a self.assertEqual(report_lines, expected_report_lines)
211n/a
212n/a
213n/adef test_main():
214n/a support.run_unittest(FileCompareTestCase, DirCompareTestCase)
215n/a
216n/aif __name__ == "__main__":
217n/a test_main()