0001 """ 0002 Test cases for the dircache module 0003 Nick Mathewson 0004 """ 0005 0006 import unittest 0007 from test.test_support import run_unittest, TESTFN 0008 import dircache, os, time, sys, tempfile 0009 0010 0011 class DircacheTests(unittest.TestCase): 0012 def setUp(self): 0013 self.tempdir = tempfile.mkdtemp() 0014 0015 def tearDown(self): 0016 for fname in os.listdir(self.tempdir): 0017 self.delTemp(fname) 0018 os.rmdir(self.tempdir) 0019 0020 def writeTemp(self, fname): 0021 f = open(os.path.join(self.tempdir, fname), 'w') 0022 f.close() 0023 0024 def mkdirTemp(self, fname): 0025 os.mkdir(os.path.join(self.tempdir, fname)) 0026 0027 def delTemp(self, fname): 0028 fname = os.path.join(self.tempdir, fname) 0029 if os.path.isdir(fname): 0030 os.rmdir(fname) 0031 else: 0032 os.unlink(fname) 0033 0034 def test_listdir(self): 0035 ## SUCCESSFUL CASES 0036 entries = dircache.listdir(self.tempdir) 0037 self.assertEquals(entries, []) 0038 0039 # Check that cache is actually caching, not just passing through. 0040 self.assert_(dircache.listdir(self.tempdir) is entries) 0041 0042 # Directories aren't "files" on Windows, and directory mtime has 0043 # nothing to do with when files under a directory get created. 0044 # That is, this test can't possibly work under Windows -- dircache 0045 # is only good for capturing a one-shot snapshot there. 0046 0047 if sys.platform[:3] not in ('win', 'os2'): 0048 # Sadly, dircache has the same granularity as stat.mtime, and so 0049 # can't notice any changes that occured within 1 sec of the last 0050 # time it examined a directory. 0051 time.sleep(1) 0052 self.writeTemp("test1") 0053 entries = dircache.listdir(self.tempdir) 0054 self.assertEquals(entries, ['test1']) 0055 self.assert_(dircache.listdir(self.tempdir) is entries) 0056 0057 ## UNSUCCESSFUL CASES 0058 self.assertRaises(OSError, dircache.listdir, self.tempdir+"_nonexistent") 0059 0060 def test_annotate(self): 0061 self.writeTemp("test2") 0062 self.mkdirTemp("A") 0063 lst = ['A', 'test2', 'test_nonexistent'] 0064 dircache.annotate(self.tempdir, lst) 0065 self.assertEquals(lst, ['A/', 'test2', 'test_nonexistent']) 0066 0067 0068 def test_main(): 0069 run_unittest(DircacheTests) 0070 0071 0072 if __name__ == "__main__": 0073 test_main() 0074
Generated by PyXR 0.9.4