Update copyright years
[pysize.git] / tests / tests / compute_size.py
blobd9f3916abca9b0e28a208844903830a6775a3b85
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006, 2007, 2008 Guillaume Chazarain <guichaz@yahoo.fr>
19 import os
20 import unittest
21 from pysize.core import compute_size
22 from pysize.core.compute_size import size_observable
23 from pysize.core.pysize_global_fs_cache import drop_caches
25 class ErrorLogger(object):
26 def __init__(self):
27 self.log = ''
29 def log_error(self, text, e):
30 self.log += text
31 self.log += ' '
32 self.log += str(e)
34 def silent(text, e):
35 pass
37 def fatal(text, e):
38 print text
39 raise e
41 class TestComputeSize(unittest.TestCase):
42 def setUp(self):
43 os.mkdir('/tmp/pysize_example_dir/unreadable_dir')
44 os.chmod('/tmp/pysize_example_dir/unreadable_dir', 000)
46 def tearDown(self):
47 os.rmdir('/tmp/pysize_example_dir/unreadable_dir')
49 def testSizeDir(self):
50 drop_caches()
51 logger = ErrorLogger()
52 size = compute_size.slow('/tmp/pysize_example_dir',
53 error_cb=logger.log_error)
54 self.assertEqual(size, 4608000)
55 self.assertEqual(logger.log, "size(/tmp/pysize_example_dir/" +
56 "unreadable_dir) [Errno 13] Permission " +
57 "denied: '/tmp/pysize_example_dir/" +
58 "unreadable_dir'")
60 # Retry using cached results
61 size = compute_size.slow('/tmp/pysize_example_dir', error_cb=fatal)
62 self.assertEqual(size, 4608000)
64 def testSizeFile(self):
65 drop_caches()
66 size = compute_size.slow('/tmp/pysize_example_dir/unreadable_file',
67 error_cb=fatal)
68 self.assertEqual(size, 12288)
70 def testSizeNoSuchPath(self):
71 drop_caches()
72 logger = ErrorLogger()
73 size = compute_size.slow('/no such/ path', error_cb=logger.log_error)
74 self.assertEqual(size, 0)
75 self.assertEqual(logger.log, "size(/no such/ path) [Errno 2] No such " +
76 "file or directory: '/no such/ path'")
78 def testSizeCache(self):
79 def inc_nr_calls():
80 self.nr_calls += 1
81 size_observable.add_observer(inc_nr_calls)
82 drop_caches()
83 self.nr_calls = 0
84 self.assertEqual(compute_size.slow('/tmp/pysize_example_dir',
85 error_cb=silent),
86 4608000)
87 self.assertEqual(self.nr_calls, 10132)
88 self.assertEqual(compute_size.slow('/tmp/pysize_example_dir',
89 error_cb=fatal),
90 4608000)
91 self.assertEqual(self.nr_calls, 10133)
92 size_observable.del_observer(inc_nr_calls)
93 self.assertEqual(compute_size.slow('/tmp/pysize_example_dir',
94 error_cb=fatal),
95 4608000)
96 self.assertEqual(self.nr_calls, 10133)
98 TESTS = (TestComputeSize,)