"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/tests/tests/compute_size.py" (11 Mar 2007, 3596 Bytes) of package /linux/privat/old/pysize-0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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.
    5 #
    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.
   10 #
   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.
   14 #
   15 # See the COPYING file for license information.
   16 #
   17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
   18 
   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
   24 
   25 class ErrorLogger(object):
   26     def __init__(self):
   27         self.log = ''
   28 
   29     def log_error(self, text, e):
   30         self.log += text
   31         self.log += ' '
   32         self.log += str(e)
   33 
   34 def silent(text, e):
   35     pass
   36 
   37 def fatal(text, e):
   38     print text
   39     raise e
   40 
   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)
   45 
   46     def tearDown(self):
   47         os.rmdir('/tmp/pysize_example_dir/unreadable_dir')
   48 
   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, "(/tmp/pysize_example_dir) " +
   56                          "size.fast(unreadable_dir) [Errno 13] " +
   57                          "Permission denied: 'unreadable_dir'")
   58 
   59         # Retry using cached results
   60         size = compute_size.slow('/tmp/pysize_example_dir', error_cb=fatal)
   61         self.assertEqual(size, 4608000)
   62 
   63     def testSizeFile(self):
   64         drop_caches()
   65         size = compute_size.slow('/tmp/pysize_example_dir/unreadable_file',
   66                                  error_cb=fatal)
   67         self.assertEqual(size, 12288)
   68 
   69     def testSizeNoSuchPath(self):
   70         drop_caches()
   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'")
   76 
   77     def testSizeCache(self):
   78         def inc_nr_calls():
   79             self.nr_calls += 1
   80         size_observable.add_observer(inc_nr_calls)
   81         drop_caches()
   82         self.nr_calls = 0
   83         self.assertEqual(compute_size.slow('/tmp/pysize_example_dir',
   84                                            error_cb=silent),
   85                          4608000)
   86         self.assertEqual(self.nr_calls, 10132)
   87         self.assertEqual(compute_size.slow('/tmp/pysize_example_dir',
   88                                            error_cb=fatal),
   89                          4608000)
   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',
   93                                            error_cb=fatal),
   94                          4608000)
   95         self.assertEqual(self.nr_calls, 10133)
   96 
   97 TESTS = (TestComputeSize,)