Strange that we could get away without that.
[gsh.git] / tests / gsh_tests.py
blobfdcddb782430afb74efa4f225404893538bd6857
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) 2007 Guillaume Chazarain <guichaz@yahoo.fr>
21 import os
22 import unittest
23 import sys
24 import optparse
25 import pexpect
27 import coverage
29 ALL_TESTS = unittest.TestSuite()
31 def import_tests():
32 py_files = [p for p in os.listdir('tests') if p.endswith('.py')]
33 tests = list(set([p[:p.index('.')] for p in py_files]))
34 for name in tests:
35 module = getattr(__import__('tests.' + name), name)
36 for test in module.TESTS:
37 suite = unittest.defaultTestLoader.loadTestsFromTestCase(test)
38 ALL_TESTS.addTest(suite)
40 def parse_cmdline():
41 parser = optparse.OptionParser()
42 parser.add_option('--coverage', '-c', action='store_true', dest='coverage',
43 default=False, help='include coverage tests')
44 options, args = parser.parse_args()
45 if args:
46 parser.error()
47 return options
49 def remove_coverage_files():
50 for filename in os.listdir('.'):
51 if filename.startswith('.coverage'):
52 os.remove(filename)
54 def end_coverage():
55 coverage.the_coverage.start()
56 coverage.the_coverage.collect()
57 coverage.the_coverage.stop()
58 modules = [p[:-3] for p in os.listdir('../gsh') if p.endswith('.py')]
59 coverage.report(['../gsh/%s.py' % (m) for m in modules])
60 remove_coverage_files()
61 # Prevent the atexit.register(the_coverage.save) from recreating the files
62 coverage.the_coverage.usecache = coverage.the_coverage.cache = None
64 def main():
65 options = parse_cmdline()
66 if options.coverage:
67 remove_coverage_files()
68 import_tests()
69 try:
70 unittest.main(argv=[sys.argv[0], '-v'], defaultTest='ALL_TESTS')
71 finally:
72 if options.coverage:
73 end_coverage()
75 def launch_gsh(args):
76 args = ['../gsh.py'] + args
77 if parse_cmdline().coverage:
78 args = ['./coverage.py', '-x', '-p'] + args
79 return pexpect.spawn(args[0], args=args[1:], timeout=5)
81 if __name__ == '__main__':
82 main()