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>
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):
29 def log_error(self
, text
, e
):
41 class TestComputeSize(unittest
.TestCase
):
43 os
.mkdir('/tmp/pysize_example_dir/unreadable_dir')
44 os
.chmod('/tmp/pysize_example_dir/unreadable_dir', 000)
47 os
.rmdir('/tmp/pysize_example_dir/unreadable_dir')
49 def testSizeDir(self
):
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/" +
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
):
66 size
= compute_size
.slow('/tmp/pysize_example_dir/unreadable_file',
68 self
.assertEqual(size
, 12288)
70 def testSizeNoSuchPath(self
):
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
):
81 size_observable
.add_observer(inc_nr_calls
)
84 self
.assertEqual(compute_size
.slow('/tmp/pysize_example_dir',
87 self
.assertEqual(self
.nr_calls
, 10132)
88 self
.assertEqual(compute_size
.slow('/tmp/pysize_example_dir',
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',
96 self
.assertEqual(self
.nr_calls
, 10133)
98 TESTS
= (TestComputeSize
,)