Fix double spacing
[pysize.git] / tests / pysize_tests.py
blob8043de2a213515be7a056ebdd1a49eff446b14b1
1 #!/usr/bin/env python
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.
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.
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.
17 # See the COPYING file for license information.
19 # Copyright (c) 2006, 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
21 import os
22 import tarfile
23 import unittest
24 import shutil
25 import sys
26 import optparse
28 import coverage
30 PYSIZE_EXAMPLE_PATH = '/tmp/pysize_example_dir'
31 ALL_TESTS = unittest.TestSuite()
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()
41 def cleanup():
42 print 'Removing', PYSIZE_EXAMPLE_PATH
43 shutil.rmtree(PYSIZE_EXAMPLE_PATH)
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)
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
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)
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()
90 if __name__ == '__main__':
91 main()