|
| 1 | +""" |
| 2 | + Test cases for the dircache module |
| 3 | + Nick Mathewson |
| 4 | +""" |
| 5 | + |
| 6 | +import unittest |
| 7 | +from test_support import run_unittest, TESTFN |
| 8 | +import dircache, os, time |
| 9 | + |
| 10 | + |
| 11 | +class DircacheTests(unittest.TestCase): |
| 12 | + def setUp(self): |
| 13 | + self.tempdir = TESTFN+"_dir" |
| 14 | + os.mkdir(self.tempdir) |
| 15 | + |
| 16 | + def tearDown(self): |
| 17 | + for fname in os.listdir(self.tempdir): |
| 18 | + self.delTemp(fname) |
| 19 | + os.rmdir(self.tempdir) |
| 20 | + |
| 21 | + def writeTemp(self, fname): |
| 22 | + f = open(os.path.join(self.tempdir, fname), 'w') |
| 23 | + f.close() |
| 24 | + |
| 25 | + def mkdirTemp(self, fname): |
| 26 | + os.mkdir(os.path.join(self.tempdir, fname)) |
| 27 | + |
| 28 | + def delTemp(self, fname): |
| 29 | + fname = os.path.join(self.tempdir, fname) |
| 30 | + if os.path.isdir(fname): |
| 31 | + os.rmdir(fname) |
| 32 | + else: |
| 33 | + os.unlink(fname) |
| 34 | + |
| 35 | + def test_listdir(self): |
| 36 | + ## SUCCESSFUL CASES |
| 37 | + entries = dircache.listdir(self.tempdir) |
| 38 | + self.assertEquals(entries, []) |
| 39 | + |
| 40 | + # Check that cache is actually caching, not just passing through. |
| 41 | + self.assert_(dircache.listdir(self.tempdir) is entries) |
| 42 | + |
| 43 | + # Sadly, dircache has the same granularity as stat.mtime, and so |
| 44 | + # can't notice any changes that occured within 1 sec of the last |
| 45 | + # time it examined a directory. |
| 46 | + time.sleep(1) |
| 47 | + self.writeTemp("test1") |
| 48 | + entries = dircache.listdir(self.tempdir) |
| 49 | + self.assertEquals(entries, ['test1']) |
| 50 | + self.assert_(dircache.listdir(self.tempdir) is entries) |
| 51 | + |
| 52 | + ## UNSUCCESSFUL CASES |
| 53 | + self.assertEquals(dircache.listdir(self.tempdir+"_nonexistent"), []) |
| 54 | + |
| 55 | + def test_annotate(self): |
| 56 | + self.writeTemp("test2") |
| 57 | + self.mkdirTemp("A") |
| 58 | + lst = ['A', 'test2', 'test_nonexistent'] |
| 59 | + dircache.annotate(self.tempdir, lst) |
| 60 | + self.assertEquals(lst, ['A/', 'test2', 'test_nonexistent']) |
| 61 | + |
| 62 | +run_unittest(DircacheTests) |
0 commit comments