Add --version
[pysize.git] / tests / pysize_tests.py
blob157fed3dc4e4bb301df25e66b86db405130af7cb
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 Guillaume Chazarain <guichaz@yahoo.fr>
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 start_coverage():
66 coverage.erase()
67 coverage.start()
69 def end_coverage():
70 coverage.stop()
71 modules = []
72 for name, module in sys.modules.iteritems():
73 if name.startswith('pysize.') and module:
74 path = '../' + name.replace('.', '/') + '.py'
75 if os.path.exists(path):
76 modules.append(module)
77 coverage.report(modules)
78 coverage.erase()
80 def main():
81 options = parse_cmdline()
82 sys.path.insert(0, '..')
83 if options.coverage:
84 start_coverage()
85 import_tests()
86 try:
87 setup()
88 unittest.main(argv=[sys.argv[0], '-v'], defaultTest='ALL_TESTS')
89 finally:
90 if options.coverage:
91 end_coverage()
92 if not options.keep:
93 cleanup()
95 if __name__ == '__main__':
96 main()