blob: 3f10872caf52fb89a89c3c0ce55527faf08589a2 [file] [log] [blame]
Fred Drake84073bf2001-07-19 22:59:09 +00001"""
2 Test cases for the dircache module
3 Nick Mathewson
4"""
5
6import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00007from test.test_support import run_unittest, TESTFN
Michael W. Hudsonfe27ff82004-08-03 11:08:32 +00008import dircache, os, time, sys, tempfile
Fred Drake84073bf2001-07-19 22:59:09 +00009
10
11class DircacheTests(unittest.TestCase):
12 def setUp(self):
Michael W. Hudsonfe27ff82004-08-03 11:08:32 +000013 self.tempdir = tempfile.mkdtemp()
Fred Drake84073bf2001-07-19 22:59:09 +000014
15 def tearDown(self):
16 for fname in os.listdir(self.tempdir):
17 self.delTemp(fname)
18 os.rmdir(self.tempdir)
19
20 def writeTemp(self, fname):
21 f = open(os.path.join(self.tempdir, fname), 'w')
22 f.close()
23
24 def mkdirTemp(self, fname):
25 os.mkdir(os.path.join(self.tempdir, fname))
26
27 def delTemp(self, fname):
28 fname = os.path.join(self.tempdir, fname)
29 if os.path.isdir(fname):
30 os.rmdir(fname)
31 else:
32 os.unlink(fname)
Tim Peters87cc0c32001-07-21 01:41:30 +000033
Fred Drake84073bf2001-07-19 22:59:09 +000034 def test_listdir(self):
35 ## SUCCESSFUL CASES
36 entries = dircache.listdir(self.tempdir)
37 self.assertEquals(entries, [])
38
39 # Check that cache is actually caching, not just passing through.
Tim Peters87cc0c32001-07-21 01:41:30 +000040 self.assert_(dircache.listdir(self.tempdir) is entries)
41
Tim Peters13775942001-07-21 02:22:14 +000042 # Directories aren't "files" on Windows, and directory mtime has
43 # nothing to do with when files under a directory get created.
44 # That is, this test can't possibly work under Windows -- dircache
45 # is only good for capturing a one-shot snapshot there.
46
47 if sys.platform[:3] not in ('win', 'os2'):
48 # Sadly, dircache has the same granularity as stat.mtime, and so
49 # can't notice any changes that occured within 1 sec of the last
50 # time it examined a directory.
51 time.sleep(1)
52 self.writeTemp("test1")
53 entries = dircache.listdir(self.tempdir)
54 self.assertEquals(entries, ['test1'])
55 self.assert_(dircache.listdir(self.tempdir) is entries)
Tim Peters87cc0c32001-07-21 01:41:30 +000056
Fred Drake84073bf2001-07-19 22:59:09 +000057 ## UNSUCCESSFUL CASES
Martin v. Löwisc6bb6c02003-09-20 15:52:21 +000058 self.assertRaises(OSError, dircache.listdir, self.tempdir+"_nonexistent")
Fred Drake84073bf2001-07-19 22:59:09 +000059
60 def test_annotate(self):
61 self.writeTemp("test2")
62 self.mkdirTemp("A")
63 lst = ['A', 'test2', 'test_nonexistent']
64 dircache.annotate(self.tempdir, lst)
65 self.assertEquals(lst, ['A/', 'test2', 'test_nonexistent'])
66
Fred Drake2e2be372001-09-20 21:33:42 +000067
68def test_main():
69 run_unittest(DircacheTests)
70
71
72if __name__ == "__main__":
73 test_main()