Fred Drake | 84073bf | 2001-07-19 22:59:09 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test cases for the dircache module |
| 3 | Nick Mathewson |
| 4 | """ |
| 5 | |
| 6 | import unittest |
Brett Cannon | 0aa6e1b | 2008-05-10 21:12:57 +0000 | [diff] [blame] | 7 | from test.test_support import run_unittest, TESTFN, import_module |
| 8 | dircache = import_module('dircache', deprecated=True) |
| 9 | import os, time, sys, tempfile |
Fred Drake | 84073bf | 2001-07-19 22:59:09 +0000 | [diff] [blame] | 10 | |
| 11 | |
| 12 | class DircacheTests(unittest.TestCase): |
| 13 | def setUp(self): |
Michael W. Hudson | fe27ff8 | 2004-08-03 11:08:32 +0000 | [diff] [blame] | 14 | self.tempdir = tempfile.mkdtemp() |
Fred Drake | 84073bf | 2001-07-19 22:59:09 +0000 | [diff] [blame] | 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) |
Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 34 | |
Fred Drake | 84073bf | 2001-07-19 22:59:09 +0000 | [diff] [blame] | 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. |
Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 41 | self.assert_(dircache.listdir(self.tempdir) is entries) |
| 42 | |
Tim Peters | 1377594 | 2001-07-21 02:22:14 +0000 | [diff] [blame] | 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. |
| 47 | |
| 48 | if sys.platform[:3] not in ('win', 'os2'): |
| 49 | # Sadly, dircache has the same granularity as stat.mtime, and so |
Fred Drake | db390c1 | 2005-10-28 14:39:47 +0000 | [diff] [blame] | 50 | # can't notice any changes that occurred within 1 sec of the last |
Tim Peters | 1377594 | 2001-07-21 02:22:14 +0000 | [diff] [blame] | 51 | # time it examined a directory. |
| 52 | time.sleep(1) |
| 53 | self.writeTemp("test1") |
| 54 | entries = dircache.listdir(self.tempdir) |
| 55 | self.assertEquals(entries, ['test1']) |
| 56 | self.assert_(dircache.listdir(self.tempdir) is entries) |
Tim Peters | 87cc0c3 | 2001-07-21 01:41:30 +0000 | [diff] [blame] | 57 | |
Fred Drake | 84073bf | 2001-07-19 22:59:09 +0000 | [diff] [blame] | 58 | ## UNSUCCESSFUL CASES |
Martin v. Löwis | c6bb6c0 | 2003-09-20 15:52:21 +0000 | [diff] [blame] | 59 | self.assertRaises(OSError, dircache.listdir, self.tempdir+"_nonexistent") |
Fred Drake | 84073bf | 2001-07-19 22:59:09 +0000 | [diff] [blame] | 60 | |
| 61 | def test_annotate(self): |
| 62 | self.writeTemp("test2") |
| 63 | self.mkdirTemp("A") |
| 64 | lst = ['A', 'test2', 'test_nonexistent'] |
| 65 | dircache.annotate(self.tempdir, lst) |
| 66 | self.assertEquals(lst, ['A/', 'test2', 'test_nonexistent']) |
| 67 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 68 | |
| 69 | def test_main(): |
Brett Cannon | 0aa6e1b | 2008-05-10 21:12:57 +0000 | [diff] [blame] | 70 | try: |
| 71 | run_unittest(DircacheTests) |
| 72 | finally: |
| 73 | dircache.reset() |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 74 | |
| 75 | |
| 76 | if __name__ == "__main__": |
| 77 | test_main() |