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 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
, "(/tmp/pysize_example_dir) " +
56 "size.fast(unreadable_dir) [Errno 13] " +
57 "Permission denied: 'unreadable_dir'")
59 # Retry using cached results
60 size
= compute_size
.slow('/tmp/pysize_example_dir', error_cb
=fatal
)
61 self
.assertEqual(size
, 4608000)
63 def testSizeFile(self
):
65 size
= compute_size
.slow('/tmp/pysize_example_dir/unreadable_file',
67 self
.assertEqual(size
, 12288)
69 def testSizeNoSuchPath(self
):
71 logger
= ErrorLogger()
72 size
= compute_size
.slow('/no such/ path', error_cb
=logger
.log_error
)
73 self
.assertEqual(size
, 0)
74 self
.assertEqual(logger
.log
, "size.slow(/no such/ path) " +
75 "[Errno 2] No such file or directory: '/no such/ path'")
77 def testSizeCache(self
):
80 size_observable
.add_observer(inc_nr_calls
)
83 self
.assertEqual(compute_size
.slow('/tmp/pysize_example_dir',
86 self
.assertEqual(self
.nr_calls
, 10132)
87 self
.assertEqual(compute_size
.slow('/tmp/pysize_example_dir',
90 self
.assertEqual(self
.nr_calls
, 10133)
91 size_observable
.del_observer(inc_nr_calls
)
92 self
.assertEqual(compute_size
.slow('/tmp/pysize_example_dir',
95 self
.assertEqual(self
.nr_calls
, 10133)
97 TESTS
= (TestComputeSize
,)