Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(32437)

Delta Between Two Patch Sets: Lib/test/test_filecmp.py

Issue 16510: Using appropriate checks in tests
Left Patch Set: Created 6 years, 10 months ago
Right Patch Set: Created 5 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_faulthandler.py ('k') | Lib/test/test_fnmatch.py » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1
2 import os, filecmp, shutil, tempfile 1 import os, filecmp, shutil, tempfile
3 import unittest 2 import unittest
4 from test import support 3 from test import support
5 4
6 class FileCompareTestCase(unittest.TestCase): 5 class FileCompareTestCase(unittest.TestCase):
7 def setUp(self): 6 def setUp(self):
8 self.name = support.TESTFN 7 self.name = support.TESTFN
9 self.name_same = support.TESTFN + '-same' 8 self.name_same = support.TESTFN + '-same'
10 self.name_diff = support.TESTFN + '-diff' 9 self.name_diff = support.TESTFN + '-diff'
11 data = 'Contents of file go here.\n' 10 data = 'Contents of file go here.\n'
(...skipping 21 matching lines...) Expand all
33 "Comparing file to identical file fails") 32 "Comparing file to identical file fails")
34 self.assertTrue(filecmp.cmp(self.name, self.name), 33 self.assertTrue(filecmp.cmp(self.name, self.name),
35 "Comparing file to identical file fails") 34 "Comparing file to identical file fails")
36 35
37 def test_different(self): 36 def test_different(self):
38 self.assertFalse(filecmp.cmp(self.name, self.name_diff), 37 self.assertFalse(filecmp.cmp(self.name, self.name_diff),
39 "Mismatched files compare as equal") 38 "Mismatched files compare as equal")
40 self.assertFalse(filecmp.cmp(self.name, self.dir), 39 self.assertFalse(filecmp.cmp(self.name, self.dir),
41 "File and directory compare as equal") 40 "File and directory compare as equal")
42 41
42 def test_cache_clear(self):
43 first_compare = filecmp.cmp(self.name, self.name_same, shallow=False)
44 second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False)
45 filecmp.clear_cache()
46 self.assertTrue(len(filecmp._cache) == 0,
47 "Cache not cleared after calling clear_cache")
48
43 class DirCompareTestCase(unittest.TestCase): 49 class DirCompareTestCase(unittest.TestCase):
44 def setUp(self): 50 def setUp(self):
45 tmpdir = tempfile.gettempdir() 51 tmpdir = tempfile.gettempdir()
46 self.dir = os.path.join(tmpdir, 'dir') 52 self.dir = os.path.join(tmpdir, 'dir')
47 self.dir_same = os.path.join(tmpdir, 'dir-same') 53 self.dir_same = os.path.join(tmpdir, 'dir-same')
48 self.dir_diff = os.path.join(tmpdir, 'dir-diff') 54 self.dir_diff = os.path.join(tmpdir, 'dir-diff')
55
56 # Another dir is created under dir_same, but it has a name from the
57 # ignored list so it should not affect testing results.
58 self.dir_ignored = os.path.join(self.dir_same, '.hg')
59
49 self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a') 60 self.caseinsensitive = os.path.normcase('A') == os.path.normcase('a')
50 data = 'Contents of file go here.\n' 61 data = 'Contents of file go here.\n'
51 for dir in [self.dir, self.dir_same, self.dir_diff]: 62 for dir in (self.dir, self.dir_same, self.dir_diff, self.dir_ignored):
52 shutil.rmtree(dir, True) 63 shutil.rmtree(dir, True)
53 os.mkdir(dir) 64 os.mkdir(dir)
54 if self.caseinsensitive and dir is self.dir_same: 65 if self.caseinsensitive and dir is self.dir_same:
55 fn = 'FiLe' # Verify case-insensitive comparison 66 fn = 'FiLe' # Verify case-insensitive comparison
56 else: 67 else:
57 fn = 'file' 68 fn = 'file'
58 output = open(os.path.join(dir, fn), 'w') 69 output = open(os.path.join(dir, fn), 'w')
59 output.write(data) 70 output.write(data)
60 output.close() 71 output.close()
61 72
62 output = open(os.path.join(self.dir_diff, 'file2'), 'w') 73 output = open(os.path.join(self.dir_diff, 'file2'), 'w')
63 output.write('An extra file.\n') 74 output.write('An extra file.\n')
64 output.close() 75 output.close()
65 76
66 def tearDown(self): 77 def tearDown(self):
67 shutil.rmtree(self.dir) 78 for dir in (self.dir, self.dir_same, self.dir_diff):
68 shutil.rmtree(self.dir_same) 79 shutil.rmtree(dir)
69 shutil.rmtree(self.dir_diff) 80
81 def test_default_ignores(self):
82 self.assertIn('.hg', filecmp.DEFAULT_IGNORES)
70 83
71 def test_cmpfiles(self): 84 def test_cmpfiles(self):
72 self.assertEqual(filecmp.cmpfiles(self.dir, self.dir, ['file']), 85 self.assertEqual(filecmp.cmpfiles(self.dir, self.dir, ['file']),
73 (['file'], [], []), 86 (['file'], [], []),
74 "Comparing directory to itself fails") 87 "Comparing directory to itself fails")
75 self.assertEqual(filecmp.cmpfiles(self.dir, self.dir_same, ['file']), 88 self.assertEqual(filecmp.cmpfiles(self.dir, self.dir_same, ['file']),
76 (['file'], [], []), 89 (['file'], [], []),
77 "Comparing directory to same fails") 90 "Comparing directory to same fails")
78 91
79 # Try it with shallow=False 92 # Try it with shallow=False
(...skipping 20 matching lines...) Expand all
100 # Check attributes for comparison of two identical directories 113 # Check attributes for comparison of two identical directories
101 left_dir, right_dir = self.dir, self.dir_same 114 left_dir, right_dir = self.dir, self.dir_same
102 d = filecmp.dircmp(left_dir, right_dir) 115 d = filecmp.dircmp(left_dir, right_dir)
103 self.assertEqual(d.left, left_dir) 116 self.assertEqual(d.left, left_dir)
104 self.assertEqual(d.right, right_dir) 117 self.assertEqual(d.right, right_dir)
105 if self.caseinsensitive: 118 if self.caseinsensitive:
106 self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']]) 119 self.assertEqual([d.left_list, d.right_list],[['file'], ['FiLe']])
107 else: 120 else:
108 self.assertEqual([d.left_list, d.right_list],[['file'], ['file']]) 121 self.assertEqual([d.left_list, d.right_list],[['file'], ['file']])
109 self.assertEqual(d.common, ['file']) 122 self.assertEqual(d.common, ['file'])
110 self.assertTrue(d.left_only == d.right_only == []) 123 self.assertEqual(d.left_only, [])
124 self.assertEqual(d.left_only, [])
111 self.assertEqual(d.same_files, ['file']) 125 self.assertEqual(d.same_files, ['file'])
112 self.assertEqual(d.diff_files, []) 126 self.assertEqual(d.diff_files, [])
113 127
114 # Check attributes for comparison of two different directories 128 # Check attributes for comparison of two different directories
115 left_dir, right_dir = self.dir, self.dir_diff 129 left_dir, right_dir = self.dir, self.dir_diff
116 d = filecmp.dircmp(left_dir, right_dir) 130 d = filecmp.dircmp(left_dir, right_dir)
117 self.assertEqual(d.left, left_dir) 131 self.assertEqual(d.left, left_dir)
118 self.assertEqual(d.right, right_dir) 132 self.assertEqual(d.right, right_dir)
119 self.assertEqual(d.left_list, ['file']) 133 self.assertEqual(d.left_list, ['file'])
120 self.assertTrue(d.right_list == ['file', 'file2']) 134 self.assertTrue(d.right_list == ['file', 'file2'])
(...skipping 10 matching lines...) Expand all
131 d = filecmp.dircmp(self.dir, self.dir_diff) 145 d = filecmp.dircmp(self.dir, self.dir_diff)
132 self.assertEqual(d.same_files, ['file']) 146 self.assertEqual(d.same_files, ['file'])
133 self.assertEqual(d.diff_files, ['file2']) 147 self.assertEqual(d.diff_files, ['file2'])
134 148
135 149
136 def test_main(): 150 def test_main():
137 support.run_unittest(FileCompareTestCase, DirCompareTestCase) 151 support.run_unittest(FileCompareTestCase, DirCompareTestCase)
138 152
139 if __name__ == "__main__": 153 if __name__ == "__main__":
140 test_main() 154 test_main()
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+