]>
Commit | Line | Data |
---|---|---|
4710c53d | 1 | """\r |
2 | Test cases for the dircache module\r | |
3 | Nick Mathewson\r | |
4 | """\r | |
5 | \r | |
6 | import unittest\r | |
7 | from test.test_support import run_unittest, import_module\r | |
8 | dircache = import_module('dircache', deprecated=True)\r | |
9 | import os, time, sys, tempfile\r | |
10 | \r | |
11 | \r | |
12 | class DircacheTests(unittest.TestCase):\r | |
13 | def setUp(self):\r | |
14 | self.tempdir = tempfile.mkdtemp()\r | |
15 | \r | |
16 | def tearDown(self):\r | |
17 | for fname in os.listdir(self.tempdir):\r | |
18 | self.delTemp(fname)\r | |
19 | os.rmdir(self.tempdir)\r | |
20 | \r | |
21 | def writeTemp(self, fname):\r | |
22 | f = open(os.path.join(self.tempdir, fname), 'w')\r | |
23 | f.close()\r | |
24 | \r | |
25 | def mkdirTemp(self, fname):\r | |
26 | os.mkdir(os.path.join(self.tempdir, fname))\r | |
27 | \r | |
28 | def delTemp(self, fname):\r | |
29 | fname = os.path.join(self.tempdir, fname)\r | |
30 | if os.path.isdir(fname):\r | |
31 | os.rmdir(fname)\r | |
32 | else:\r | |
33 | os.unlink(fname)\r | |
34 | \r | |
35 | def test_listdir(self):\r | |
36 | ## SUCCESSFUL CASES\r | |
37 | entries = dircache.listdir(self.tempdir)\r | |
38 | self.assertEqual(entries, [])\r | |
39 | \r | |
40 | # Check that cache is actually caching, not just passing through.\r | |
41 | self.assertTrue(dircache.listdir(self.tempdir) is entries)\r | |
42 | \r | |
43 | # Directories aren't "files" on Windows, and directory mtime has\r | |
44 | # nothing to do with when files under a directory get created.\r | |
45 | # That is, this test can't possibly work under Windows -- dircache\r | |
46 | # is only good for capturing a one-shot snapshot there.\r | |
47 | \r | |
48 | if sys.platform[:3] not in ('win', 'os2'):\r | |
49 | # Sadly, dircache has the same granularity as stat.mtime, and so\r | |
50 | # can't notice any changes that occurred within 1 sec of the last\r | |
51 | # time it examined a directory.\r | |
52 | time.sleep(1)\r | |
53 | self.writeTemp("test1")\r | |
54 | entries = dircache.listdir(self.tempdir)\r | |
55 | self.assertEqual(entries, ['test1'])\r | |
56 | self.assertTrue(dircache.listdir(self.tempdir) is entries)\r | |
57 | \r | |
58 | ## UNSUCCESSFUL CASES\r | |
59 | self.assertRaises(OSError, dircache.listdir, self.tempdir+"_nonexistent")\r | |
60 | \r | |
61 | def test_annotate(self):\r | |
62 | self.writeTemp("test2")\r | |
63 | self.mkdirTemp("A")\r | |
64 | lst = ['A', 'test2', 'test_nonexistent']\r | |
65 | dircache.annotate(self.tempdir, lst)\r | |
66 | self.assertEqual(lst, ['A/', 'test2', 'test_nonexistent'])\r | |
67 | \r | |
68 | \r | |
69 | def test_main():\r | |
70 | try:\r | |
71 | run_unittest(DircacheTests)\r | |
72 | finally:\r | |
73 | dircache.reset()\r | |
74 | \r | |
75 | \r | |
76 | if __name__ == "__main__":\r | |
77 | test_main()\r |