"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/tests/pysize_tests.py" (11 Mar 2007, 2952 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 #!/usr/bin/env python
    2 #
    3 # This program is free software; you can redistribute it and/or modify
    4 # it under the terms of the GNU General Public License as published by
    5 # the Free Software Foundation; either version 2 of the License, or
    6 # (at your option) any later version.
    7 #
    8 # This program is distributed in the hope that it will be useful,
    9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   11 # GNU Library General Public License for more details.
   12 #
   13 # You should have received a copy of the GNU General Public License
   14 # along with this program; if not, write to the Free Software
   15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   16 #
   17 # See the COPYING file for license information.
   18 #
   19 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
   20 
   21 import os
   22 import tarfile
   23 import unittest
   24 import shutil
   25 import sys
   26 import optparse
   27 
   28 import coverage
   29 
   30 PYSIZE_EXAMPLE_PATH = '/tmp/pysize_example_dir'
   31 ALL_TESTS = unittest.TestSuite()
   32 
   33 def setup():
   34     if not os.path.exists('/tmp/pysize_example_dir'):
   35         tar = tarfile.open('pysize_example_dir.tar.bz2', 'r:bz2')
   36         print 'Extracting pysize_example_dir.tar.bz2'
   37         for tarinfo in tar:
   38             tar.extract(tarinfo, '/tmp')
   39         tar.close()
   40 
   41 def cleanup():
   42     print 'Removing', PYSIZE_EXAMPLE_PATH
   43     shutil.rmtree(PYSIZE_EXAMPLE_PATH)
   44 
   45 def import_tests():
   46     py_files = [p for p in os.listdir('tests') if p.endswith('.py')]
   47     tests = list(set([p[:p.index('.')] for p in py_files]))
   48     for name in tests:
   49         module = getattr(__import__('tests.' + name), name)
   50         for test in module.TESTS:
   51             suite = unittest.defaultTestLoader.loadTestsFromTestCase(test)
   52             ALL_TESTS.addTest(suite)
   53 
   54 def parse_cmdline():
   55     parser = optparse.OptionParser()
   56     parser.add_option('--keep', '-k', action='store_true', dest='keep',
   57                       default=False, help='keep /tmp/pysize_example_dir')
   58     parser.add_option('--coverage', '-c', action='store_true', dest='coverage',
   59                       default=False, help='include coverage tests')
   60     options, args = parser.parse_args()
   61     if args:
   62         parser.error()
   63     return options
   64 
   65 def end_coverage():
   66     coverage.stop()
   67     modules = []
   68     for name, module in sys.modules.iteritems():
   69         if name.startswith('pysize.') and module:
   70             path = '../' + name.replace('.', '/') + '.py'
   71             if os.path.exists(path):
   72                 modules.append(module)
   73     coverage.report(modules)
   74 
   75 def main():
   76     options = parse_cmdline()
   77     sys.path.insert(0, '..')
   78     if options.coverage:
   79         coverage.start()
   80     import_tests()
   81     try:
   82         setup()
   83         unittest.main(argv=[sys.argv[0], '-v'], defaultTest='ALL_TESTS')
   84     finally:
   85         if options.coverage:
   86             end_coverage()
   87         if not options.keep:
   88             cleanup()
   89 
   90 if __name__ == '__main__':
   91     main()