Skip to content

Commit 1dc17d0

Browse files
author
Stepa
committed
No issue: test_filecmp: Add tests for filecmp.cmp cache behavior and overflow clearing
1 parent 5a22a62 commit 1dc17d0

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

‎Lib/test/test_filecmp.py‎

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,27 @@ def test_different(self):
7070
"Mismatched file to shallow identical file compares as equal")
7171

7272
def test_cache_clear(self):
73-
first_compare = filecmp.cmp(self.name, self.name_same, shallow=False)
74-
second_compare = filecmp.cmp(self.name, self.name_diff, shallow=False)
73+
filecmp.cmp(self.name, self.name_same, shallow=False)
74+
filecmp.cmp(self.name, self.name_diff, shallow=False)
7575
filecmp.clear_cache()
76-
self.assertTrue(len(filecmp._cache) == 0,
77-
"Cache not cleared after calling clear_cache")
76+
self.assertEqual(len(filecmp._cache), 0,
77+
"Cache not cleared after calling clear_cache")
78+
filecmp.clear_cache()
79+
res1 = filecmp.cmp(self.name, self.name_same, shallow=False)
80+
self.assertEqual(len(filecmp._cache), 1)
81+
res2 = filecmp.cmp(self.name, self.name_same, shallow=False)
82+
self.assertTrue(res1)
83+
self.assertEqual(res1, res2)
84+
self.assertEqual(len(filecmp._cache), 1)
85+
86+
def test_cmp_cache_clearing_on_overflow(self):
87+
filecmp.clear_cache()
88+
fake_sig = (33188, 100, 12345)
89+
for i in range(105):
90+
filecmp._cache[(f"fake_f1_{i}", f"fake_f2_{i}", fake_sig, fake_sig)] = True
91+
self.assertGreater(len(filecmp._cache), 100)
92+
filecmp.cmp(self.name, self.name_same_shallow, shallow=False)
93+
self.assertEqual(len(filecmp._cache), 1)
7894

7995
class DirCompareTestCase(unittest.TestCase):
8096
def setUp(self):
@@ -368,4 +384,4 @@ def _assert_report(self, dircmp_report, expected_report_lines):
368384

369385

370386
if __name__ == "__main__":
371-
unittest.main()
387+
unittest.main()

0 commit comments

Comments
 (0)