2 Test cases for the dircache module
7 from test
.test_support
import run_unittest
, import_module
8 dircache
= import_module('dircache', deprecated
=True)
9 import os
, time
, sys
, tempfile
12 class DircacheTests(unittest
.TestCase
):
14 self
.tempdir
= tempfile
.mkdtemp()
17 for fname
in os
.listdir(self
.tempdir
):
19 os
.rmdir(self
.tempdir
)
21 def writeTemp(self
, fname
):
22 f
= open(os
.path
.join(self
.tempdir
, fname
), 'w')
25 def mkdirTemp(self
, fname
):
26 os
.mkdir(os
.path
.join(self
.tempdir
, fname
))
28 def delTemp(self
, fname
):
29 fname
= os
.path
.join(self
.tempdir
, fname
)
30 if os
.path
.isdir(fname
):
35 def test_listdir(self
):
37 entries
= dircache
.listdir(self
.tempdir
)
38 self
.assertEquals(entries
, [])
40 # Check that cache is actually caching, not just passing through.
41 self
.assertTrue(dircache
.listdir(self
.tempdir
) is entries
)
43 # Directories aren't "files" on Windows, and directory mtime has
44 # nothing to do with when files under a directory get created.
45 # That is, this test can't possibly work under Windows -- dircache
46 # is only good for capturing a one-shot snapshot there.
48 if sys
.platform
[:3] not in ('win', 'os2'):
49 # Sadly, dircache has the same granularity as stat.mtime, and so
50 # can't notice any changes that occurred within 1 sec of the last
51 # time it examined a directory.
53 self
.writeTemp("test1")
54 entries
= dircache
.listdir(self
.tempdir
)
55 self
.assertEquals(entries
, ['test1'])
56 self
.assertTrue(dircache
.listdir(self
.tempdir
) is entries
)
59 self
.assertRaises(OSError, dircache
.listdir
, self
.tempdir
+"_nonexistent")
61 def test_annotate(self
):
62 self
.writeTemp("test2")
64 lst
= ['A', 'test2', 'test_nonexistent']
65 dircache
.annotate(self
.tempdir
, lst
)
66 self
.assertEquals(lst
, ['A/', 'test2', 'test_nonexistent'])
71 run_unittest(DircacheTests
)
76 if __name__
== "__main__":