annotate Lib/test/test_dircache.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 614c84670c63
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
18757
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
1 """
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
2 Test cases for the dircache module
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
3 Nick Mathewson
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
4 """
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
5
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
6 import unittest
58817
70e115c34f97 Remove unused imports in test modules.
Georg Brandl <georg@python.org>
parents: 55002
diff changeset
7 from test.test_support import run_unittest, import_module
46838
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 35806
diff changeset
8 dircache = import_module('dircache', deprecated=True)
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 35806
diff changeset
9 import os, time, sys, tempfile
18757
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
10
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
11
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
12 class DircacheTests(unittest.TestCase):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
13 def setUp(self):
32920
bda98304deb5 make this test work when run repeatedly.
Michael W. Hudson <mwh@python.net>
parents: 30470
diff changeset
14 self.tempdir = tempfile.mkdtemp()
18757
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
15
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
16 def tearDown(self):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
17 for fname in os.listdir(self.tempdir):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
18 self.delTemp(fname)
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
19 os.rmdir(self.tempdir)
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
20
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
21 def writeTemp(self, fname):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
22 f = open(os.path.join(self.tempdir, fname), 'w')
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
23 f.close()
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
24
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
25 def mkdirTemp(self, fname):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
26 os.mkdir(os.path.join(self.tempdir, fname))
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
27
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
28 def delTemp(self, fname):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
29 fname = os.path.join(self.tempdir, fname)
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
30 if os.path.isdir(fname):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
31 os.rmdir(fname)
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
32 else:
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
33 os.unlink(fname)
18787
f999d3609f74 Whitespace normalization, plus:
Tim Peters <tim.peters@gmail.com>
parents: 18757
diff changeset
34
18757
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
35 def test_listdir(self):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
36 ## SUCCESSFUL CASES
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
37 entries = dircache.listdir(self.tempdir)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58817
diff changeset
38 self.assertEqual(entries, [])
18757
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
39
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
40 # Check that cache is actually caching, not just passing through.
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 46838
diff changeset
41 self.assertTrue(dircache.listdir(self.tempdir) is entries)
18787
f999d3609f74 Whitespace normalization, plus:
Tim Peters <tim.peters@gmail.com>
parents: 18757
diff changeset
42
18788
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
43 # Directories aren't "files" on Windows, and directory mtime has
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
44 # nothing to do with when files under a directory get created.
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
45 # That is, this test can't possibly work under Windows -- dircache
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
46 # is only good for capturing a one-shot snapshot there.
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
47
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
48 if sys.platform[:3] not in ('win', 'os2'):
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
49 # Sadly, dircache has the same granularity as stat.mtime, and so
35806
44fe157255bc fix typos, mostly in comments
Fred Drake <fdrake@acm.org>
parents: 32920
diff changeset
50 # can't notice any changes that occurred within 1 sec of the last
18788
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
51 # time it examined a directory.
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
52 time.sleep(1)
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
53 self.writeTemp("test1")
2402070ee024 On WIndows, skip the part of test_dircache that can't work on Windows.
Tim Peters <tim.peters@gmail.com>
parents: 18787
diff changeset
54 entries = dircache.listdir(self.tempdir)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58817
diff changeset
55 self.assertEqual(entries, ['test1'])
55002
6768b2350c57 convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents: 46838
diff changeset
56 self.assertTrue(dircache.listdir(self.tempdir) is entries)
18787
f999d3609f74 Whitespace normalization, plus:
Tim Peters <tim.peters@gmail.com>
parents: 18757
diff changeset
57
18757
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
58 ## UNSUCCESSFUL CASES
30470
1e1e685d69ef Patch #707167: Pass dircache exceptions to the caller. Fixes #682813.
Martin v. Löwis <martin@v.loewis.de>
parents: 24364
diff changeset
59 self.assertRaises(OSError, dircache.listdir, self.tempdir+"_nonexistent")
18757
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
60
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
61 def test_annotate(self):
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
62 self.writeTemp("test2")
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
63 self.mkdirTemp("A")
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
64 lst = ['A', 'test2', 'test_nonexistent']
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
65 dircache.annotate(self.tempdir, lst)
66348
614c84670c63 Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58817
diff changeset
66 self.assertEqual(lst, ['A/', 'test2', 'test_nonexistent'])
18757
4d46a1b33c62 Regression test for the dircache module, contributed by Nick Mathewson.
Fred Drake <fdrake@acm.org>
parents:
diff changeset
67
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18788
diff changeset
68
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18788
diff changeset
69 def test_main():
46838
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 35806
diff changeset
70 try:
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 35806
diff changeset
71 run_unittest(DircacheTests)
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 35806
diff changeset
72 finally:
33d4e03b23ba Deprecate the dircache module for 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 35806
diff changeset
73 dircache.reset()
20013
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18788
diff changeset
74
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18788
diff changeset
75
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18788
diff changeset
76 if __name__ == "__main__":
e3184e46303b Change the PyUnit-based tests to use the test_main() approach. This
Fred Drake <fdrake@acm.org>
parents: 18788
diff changeset
77 test_main()